File size: 1,295 Bytes
c46e765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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)