Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Load sentiment analysis model
|
| 8 |
+
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 9 |
+
|
| 10 |
+
class SentimentRequest(BaseModel):
|
| 11 |
+
text: str
|
| 12 |
+
|
| 13 |
+
class SentimentResponse(BaseModel):
|
| 14 |
+
label: str
|
| 15 |
+
score: float
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def home():
|
| 19 |
+
return {"message": "Sentiment Analysis API is running!"}
|
| 20 |
+
|
| 21 |
+
@app.post("/predict/", response_model=SentimentResponse)
|
| 22 |
+
def predict(request: SentimentRequest):
|
| 23 |
+
result = sentiment_pipeline(request.text)
|
| 24 |
+
return SentimentResponse(label=result[0]['label'], score=result[0]['score'])
|