Download app.py from jakaria6284/englis_model_api_endpoint: direct link, hf CLI and curl.
- Browser
- Download file 1.03 kB
-
https://huggingface.co/spaces/jakaria6284/englis_model_api_endpoint/resolve/main/app.py
- Command line
-
hf download hf://spaces/jakaria6284/englis_model_api_endpoint/app.py
-
curl -L -o app.py https://huggingface.co/spaces/jakaria6284/englis_model_api_endpoint/resolve/main/app.py
1.03 kB
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| import joblib | |
| app = FastAPI() | |
| class PredictRequest(BaseModel): | |
| text: str | |
| # Load model and vectorizer | |
| model = joblib.load("english_model.pkl") | |
| vectorizer = joblib.load("english_tfid.pkl") | |
| label_encoder = joblib.load("english_label_encoder.pkl") | |
| def root(): | |
| return {"message": "Welcome to the English model API endpoint"} | |
| def predict(request: PredictRequest): | |
| vect = vectorizer.transform([request.text]) | |
| pred = model.predict(vect) # returns encoded int | |
| label = label_encoder.inverse_transform(pred) # convert back to string | |
| # Map label strings to friendly messages | |
| mapping = { | |
| "age": "Hate regarding age", | |
| "ethnicity": "Hate regarding ethnicity", | |
| "gender": "Hate regarding gender", | |
| "not_cyberbullying": "Not Hate", | |
| "other_cyberbullying": "Other hate", | |
| "religion": "Hate regarding religion" | |
| } | |
| return {"prediction": mapping[label[0]]} | |