EmotionSense / main.py
faizan20's picture
Upload 8 files
c46e765 verified
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.routes import router as emotion_router
import os
app = FastAPI(
title="NLP Emotion Analyzer",
description="Emotion & Sentiment Analyzer using HuggingFace models",
version="1.0.0"
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(emotion_router, tags=["Emotion Analyzer"])
@app.get("/")
def root():
return {"message": "Welcome to NLP Emotion Analyzer API", "status": "running", "endpoints": ["/api/predict", "/api/explain"]}
# Warm up models on startup to avoid long first request
@app.on_event("startup")
def startup_event():
try:
from app.model_loader import model_registry
model_registry.initialize()
try:
from app.predict import text_predictor
_ = text_predictor.predict("warm up", task="sentiment")
except Exception:
pass
print("Models initialized.")
except Exception as e:
print("Models not initialized:", e)
if __name__ == "__main__":
import uvicorn
uvicorn.run("app.main:app", host="127.0.0.1", port=8000, reload=True)