Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Load YOUR trained model
|
| 8 |
+
classifier = pipeline(
|
| 9 |
+
"sentiment-analysis",
|
| 10 |
+
model="sameerdorjee07/nepali-sentiment-xlmr",
|
| 11 |
+
device=-1
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
class TextInput(BaseModel):
|
| 15 |
+
text: str
|
| 16 |
+
|
| 17 |
+
@app.get("/")
|
| 18 |
+
def home():
|
| 19 |
+
return {"message": "Nepali Sentiment API Running"}
|
| 20 |
+
|
| 21 |
+
@app.post("/predict")
|
| 22 |
+
def predict(data: TextInput):
|
| 23 |
+
result = classifier(data.text)
|
| 24 |
+
return {
|
| 25 |
+
"label": result[0]["label"],
|
| 26 |
+
"score": float(result[0]["score"])
|
| 27 |
+
}
|