Spaces:
Running
Running
File size: 846 Bytes
95fc527 20712aa 95fc527 20712aa 95fc527 20712aa |
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 |
import os
from fastapi import FastAPI
from pydantic import BaseModel
from transformers import pipeline
# Set custom cache directory to avoid permission issues
os.environ["TRANSFORMERS_CACHE"] = "/app/cache"
app = FastAPI()
# Explicitly specify a model (avoid default selection)
MODEL_NAME = "distilbert-base-uncased-finetuned-sst-2-english"
sentiment_pipeline = pipeline("sentiment-analysis", model=MODEL_NAME)
class SentimentRequest(BaseModel):
text: str
class SentimentResponse(BaseModel):
label: str
score: float
@app.get("/")
def home():
return {"message": "Sentiment Analysis API is running!"}
@app.post("/predict/", response_model=SentimentResponse)
def predict(request: SentimentRequest):
result = sentiment_pipeline(request.text)
return SentimentResponse(label=result[0]['label'], score=result[0]['score'])
|