just-api / app.py
Voffchik's picture
Update app.py
220c6b7 verified
raw
history blame
708 Bytes
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline
import os
import asyncio
from contextlib import asynccontextmanager
@asynccontextmanager
async def lifespan(app: FastAPI):
global classifier
classifier = pipeline("sentiment-analysis")
yield
app = FastAPI(lifespan=lifespan)
class TextRequest(BaseModel):
text: str
@app.post("/analyze")
async def analyze(request: TextRequest):
try:
result = classifier(request.text)[0]
return {
"text": request.text,
"sentiment": result['label'],
"confidence": round(result['score'], 4)
}
except Exception as e:
return {"error": str(e)}