Spaces:
Runtime error
Runtime error
Commit ·
5f16f9a
1
Parent(s): e72395f
Updates
Browse files- app.py +9 -3
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -1,8 +1,13 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI(title="Sentimind API")
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
class InputText(BaseModel):
|
| 7 |
text: str
|
| 8 |
|
|
@@ -12,9 +17,10 @@ def root():
|
|
| 12 |
|
| 13 |
@app.post("/predict")
|
| 14 |
def predict(input_data: InputText):
|
| 15 |
-
#
|
|
|
|
| 16 |
return {
|
| 17 |
"input_text": input_data.text,
|
| 18 |
-
"sentiment":
|
| 19 |
-
"confidence":
|
| 20 |
}
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
from pydantic import BaseModel
|
| 3 |
+
from transformers import pipeline
|
| 4 |
|
| 5 |
app = FastAPI(title="Sentimind API")
|
| 6 |
|
| 7 |
+
# Load sentiment analysis model
|
| 8 |
+
sentiment_analyzer = pipeline("sentiment-analysis",
|
| 9 |
+
model="distilbert-base-uncased-finetuned-sst-2-english")
|
| 10 |
+
|
| 11 |
class InputText(BaseModel):
|
| 12 |
text: str
|
| 13 |
|
|
|
|
| 17 |
|
| 18 |
@app.post("/predict")
|
| 19 |
def predict(input_data: InputText):
|
| 20 |
+
# Run model inference
|
| 21 |
+
result = sentiment_analyzer(input_data.text)[0] # returns {'label': 'POSITIVE', 'score': 0.99}
|
| 22 |
return {
|
| 23 |
"input_text": input_data.text,
|
| 24 |
+
"sentiment": result['label'].lower(),
|
| 25 |
+
"confidence": float(result['score'])
|
| 26 |
}
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
fastapi
|
| 2 |
-
uvicorn[standard]
|
|
|
|
|
|
|
|
|
| 1 |
fastapi
|
| 2 |
+
uvicorn[standard]
|
| 3 |
+
transformers
|
| 4 |
+
torch
|