Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,59 @@
|
|
| 1 |
import os
|
| 2 |
-
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
return english_model
|
| 18 |
|
|
|
|
| 19 |
def get_urdu_model():
|
| 20 |
global urdu_model
|
| 21 |
if urdu_model is None:
|
| 22 |
-
urdu_model = pipeline(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
return urdu_model
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
body = await request.json()
|
| 28 |
-
text = body.get("text", "")
|
| 29 |
-
language = body.get("language", "english")
|
| 30 |
-
|
| 31 |
if language in ["urdu", "roman_urdu"]:
|
| 32 |
result = get_urdu_model()(text)[0]
|
| 33 |
else:
|
| 34 |
result = get_english_model()(text)[0]
|
| 35 |
-
|
| 36 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
|
|
|
| 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 |
+
def predict(text, language):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|