Fast API — Fastest of All

Celestial
4 min readApr 28, 2024

--

Creating the API for our Trained model Using Fast API and then Creating the Extension (Spoiler Detection)

For Better reach (Googly image is used)

Let’s write something new this time with hope of new and more reach. (I know you read the sub title)

Last week, something which almost took all my hair while making the extension for spoiler detection. I never made extensions before for no bad reasons. But now I have some bad reasons to make them which paid me for my efforts, lol. I won’t take your time but too much time about how I made the extension using a fast Api with your model.

Disclaimer no model training is done here , you can train your model using my snippet code available on -https://github.com/SuperCelestial/IMBDsentimentAnalysis/blob/main/imbd_SVM.ipynb

, the only thing you must have is a model ready in pickle form- i know you all can do it ! it’s easy pizzy.

The most popular thing for any learner is to know the art of copying, So this article is all about that , how professionally you can learn and copy. Few things which you need is a laptop and ofc model trained in pickle format. Then this pickle file must be shifted to the folder of your desire. Now the fastApi plays the role of hero or even heroine because it makes your code easy pizzy!

Before entering to the code side let me introduce how actually our car will work. So let’s learn how engine is made:

1. I hope you know about the extension and how laymen use it, same goes here.. The background functionality is that (pay attention) — it will take the data from any website user will land on to and then what ? those data will be passed on to our functions which will be in Javascript (don’t worry , I’m here to explain and confuse u all, so chill)

2. Coming to the next , the Javascript function will first pass those 10000 data to our model through the api ofc to ‘fast api’

3. Our api file will contain code where we made a function which will take data in str format and chunk it into 300 and pass it on to our model to provide the result that whether it is spoiler or not, simple as that, easy pizzy!

Lets code the fast api python file first, then will do the extension. Period.

So let me show you how many files u all will require -

IMP (highlight them)

Fast API file can be outside this folder or even in the same folder.

So as u know in python everything starts with a library — so import the library because you cant own one!

import uvicorn 
from fastapi import FastAPI
import joblib -- for model.pkl loading
from pydantic import BaseModel -- for data type
from typing import List
from fastapi.middleware.cors import CORSMiddleware
# Here Assigning the variable for FastApi
app = FastAPI()
# Allow requests from all origins
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["*"],
)
# Loading fine tuned bert model for imdb spoiler reviews detection
model = joblib.load('model.pkl') -- this is the model trained pickle file you gotta do by yourself

class UserInput(BaseModel): -- For data to be in string format
text: str

def chunk_data(input_data: str) -> List[str]:
"""Chunk the input data into 300-character chunks.
Arguments:
input_data (str): The input data to be chunked.
Returns:
List[str]: A list of 300-character chunks.
"""
chunk_size = 300
chunks = [input_data[i:i+chunk_size] for i in range(0, len(input_data), chunk_size)]
return chunks

-- Function to check the spoiler
@app.post('/check_spoiler')
def operate(input: UserInput):
"""
Operates on the input text to check for spoilers.
Arguments:
input (UserInput): The user input containing the text to be checked.
Returns:
dict: A dictionary containing the chunks of text marked as spoilers.
"""
input_text = input.text

# Chunk the input text into 300-character chunks
text_chunks = chunk_data(input_text)
spoiler_chunks = []
# Iterate over each chunk and predict if it's a spoiler
for chunk in text_chunks:
prediction = model.predict([[chunk]])
if prediction[0] == "spoiler":
spoiler_chunks.append(chunk)
# Return only the chunks that are marked as spoilers
return {'spoiler_chunks': spoiler_chunks}

The last step for the day is -

if __name__ == '__main__':
uvicorn.run(app, host='127.0.0.1', port=8000)
# for running the code I provided

This is the small code used for making api files , when u run the code using - uvicorn fast_api:app you will get the localhost url helps you in extension usage ..
Now without wasting time of yours , i want to tell you all — to know more read my another blog on how to make the extension — {it will come soon}

--

--

Celestial
Celestial

Written by Celestial

Uncovering Patterns , Empowering Strategies.

No responses yet