harassment-api / model.py
shobika04's picture
Upload model.py
1eafd3e verified
Raw
History Blame Contribute Delete
2.44 kB
import threading
import os
import time
from contextlib import asynccontextmanager
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
# Global variables
tokenizer = None
model = None
is_ready = False
model_error = None
def load_model():
global tokenizer, model, is_ready, model_error
try:
# πŸ”₯ Move heavy imports inside the thread to prevent blocking startup
print("πŸ”„ Loading heavy AI libraries in background...")
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
print("πŸ”„ Downloading model from Hugging Face...")
model_path = "shobika04/harassment-nlp-model"
hf_token = os.getenv("HF_TOKEN")
tokenizer = AutoTokenizer.from_pretrained(model_path, token=hf_token)
model = AutoModelForSequenceClassification.from_pretrained(model_path, token=hf_token)
model.eval()
is_ready = True
print("βœ… Model loaded successfully!")
except Exception as e:
print(f"❌ Error loading model: {e}")
model_error = str(e)
@asynccontextmanager
async def lifespan(app: FastAPI):
thread = threading.Thread(target=load_model)
thread.start()
yield
app = FastAPI(lifespan=lifespan)
class MessageRequest(BaseModel):
text: str
@app.get("/")
def health():
if is_ready:
return {"status": "NLP service running", "ready": True}
return {"status": "Loading model in background...", "ready": False}
@app.post("/predict")
def predict(req: MessageRequest):
if not is_ready:
raise HTTPException(status_code=503, detail="Model is still loading...")
import torch # Local import for speed
inputs = tokenizer(req.text, return_tensors="pt", truncation=True, padding=True)
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.softmax(outputs.logits, dim=1)
predicted_class = torch.argmax(probabilities, dim=1).item()
confidence = probabilities[0][predicted_class].item()
prediction = "NonPredator" if predicted_class == 0 else "Predator"
return {"prediction": prediction, "confidence": round(confidence, 4)}
if __name__ == "__main__":
import uvicorn
# πŸ”₯ REMOVED reload=True for stable production deployment
uvicorn.run("model:app", host="0.0.0.0", port=7860)