semantic-bot / app.py
shoaibrza9999's picture
Upload app.py with huggingface_hub
0ddae70 verified
Raw
History Blame Contribute Delete
1.13 kB
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from pydantic import BaseModel
import joblib
import re
app = FastAPI(title="Hinglish Semantic Reaction Bot")
clf = None
class PredictRequest(BaseModel):
text: str
class PredictResponse(BaseModel):
emoji: str
def preprocess(text):
text = text.lower()
text = re.sub(r'[^a-z\s]', '', text)
return text
@app.on_event("startup")
async def load_classifier():
global clf
print("Loading trained TF-IDF classifier...")
clf = joblib.load("classifier.joblib")
print("Startup complete. Classifier is ready.")
@app.get("/", response_class=HTMLResponse)
async def serve_ui():
with open("index.html", "r") as f:
html_content = f.read()
return HTMLResponse(content=html_content)
@app.post("/api/predict", response_model=PredictResponse)
async def predict_emoji(request: PredictRequest):
if not request.text.strip():
return PredictResponse(emoji="🤔")
cleaned_text = preprocess(request.text)
predicted_emoji = clf.predict([cleaned_text])[0]
return PredictResponse(emoji=predicted_emoji)