Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,38 @@
|
|
| 1 |
import os
|
| 2 |
-
|
|
|
|
| 3 |
from transformers import pipeline
|
| 4 |
|
|
|
|
|
|
|
| 5 |
token = os.environ.get("HF_TOKEN")
|
| 6 |
|
| 7 |
english_model = None
|
| 8 |
urdu_model = None
|
| 9 |
|
| 10 |
-
|
| 11 |
def get_english_model():
|
| 12 |
global english_model
|
| 13 |
if english_model is None:
|
| 14 |
-
english_model = pipeline(
|
| 15 |
-
"text-classification",
|
| 16 |
-
model="mrgmd01/Finetuned_siebert-sentiment-roberta-large-english",
|
| 17 |
-
token=token
|
| 18 |
-
)
|
| 19 |
return english_model
|
| 20 |
|
| 21 |
-
|
| 22 |
def get_urdu_model():
|
| 23 |
global urdu_model
|
| 24 |
if urdu_model is None:
|
| 25 |
-
urdu_model = pipeline(
|
| 26 |
-
"text-classification",
|
| 27 |
-
model="mrgmd01/SA_Model_bert-base-multilingual-uncased",
|
| 28 |
-
token=token
|
| 29 |
-
)
|
| 30 |
return urdu_model
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
if language in ["urdu", "roman_urdu"]:
|
| 35 |
result = get_urdu_model()(text)[0]
|
| 36 |
else:
|
| 37 |
result = get_english_model()(text)[0]
|
| 38 |
-
|
| 39 |
-
return {
|
| 40 |
-
"label": result["label"],
|
| 41 |
-
"score": float(result["score"])
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
iface = gr.Interface(
|
| 46 |
-
fn=predict,
|
| 47 |
-
inputs=[
|
| 48 |
-
gr.Textbox(label="Input Text"),
|
| 49 |
-
gr.Dropdown(
|
| 50 |
-
choices=["english", "urdu", "roman_urdu"],
|
| 51 |
-
value="english",
|
| 52 |
-
label="Language"
|
| 53 |
-
)
|
| 54 |
-
],
|
| 55 |
-
outputs="json",
|
| 56 |
-
title="Multilingual Sentiment Analysis"
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
iface.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
+
from fastapi import FastAPI, Request
|
| 3 |
+
from fastapi.responses import JSONResponse
|
| 4 |
from transformers import pipeline
|
| 5 |
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
token = os.environ.get("HF_TOKEN")
|
| 9 |
|
| 10 |
english_model = None
|
| 11 |
urdu_model = None
|
| 12 |
|
|
|
|
| 13 |
def get_english_model():
|
| 14 |
global english_model
|
| 15 |
if english_model is None:
|
| 16 |
+
english_model = pipeline("text-classification", model="mrgmd01/Finetuned_siebert-sentiment-roberta-large-english", token=token)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
return english_model
|
| 18 |
|
|
|
|
| 19 |
def get_urdu_model():
|
| 20 |
global urdu_model
|
| 21 |
if urdu_model is None:
|
| 22 |
+
urdu_model = pipeline("text-classification", model="mrgmd01/SA_Model_bert-base-multilingual-uncased", token=token)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
return urdu_model
|
| 24 |
|
| 25 |
+
@app.get("/")
|
| 26 |
+
def root():
|
| 27 |
+
return {"status": "running"}
|
| 28 |
|
| 29 |
+
@app.post("/predict")
|
| 30 |
+
async def predict(request: Request):
|
| 31 |
+
body = await request.json()
|
| 32 |
+
text = body.get("text", "")
|
| 33 |
+
language = body.get("language", "english")
|
| 34 |
if language in ["urdu", "roman_urdu"]:
|
| 35 |
result = get_urdu_model()(text)[0]
|
| 36 |
else:
|
| 37 |
result = get_english_model()(text)[0]
|
| 38 |
+
return JSONResponse({"label": result["label"], "score": float(result["score"])})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|