Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
#
|
| 5 |
model_name = "Abduuu/ArabReview-Sentiment"
|
| 6 |
sentiment_pipeline = pipeline("text-classification", model=model_name, tokenizer=model_name)
|
| 7 |
|
| 8 |
# Define label mapping for better readability
|
| 9 |
-
label_mapping = {
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
def predict_sentiment(review):
|
| 13 |
result = sentiment_pipeline(review)[0]
|
| 14 |
sentiment_label = label_mapping[result["label"]]
|
| 15 |
-
confidence =
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Launch the app
|
| 36 |
if __name__ == "__main__":
|
| 37 |
-
iface.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Ensure the model is correctly loaded
|
| 5 |
model_name = "Abduuu/ArabReview-Sentiment"
|
| 6 |
sentiment_pipeline = pipeline("text-classification", model=model_name, tokenizer=model_name)
|
| 7 |
|
| 8 |
# Define label mapping for better readability
|
| 9 |
+
label_mapping = {
|
| 10 |
+
"LABEL_0": "سـلـبـي 😞",
|
| 11 |
+
"LABEL_1": "إيـجـابـي 😊"
|
| 12 |
+
}
|
| 13 |
|
| 14 |
+
# Function to predict sentiment with confidence visualization
|
| 15 |
def predict_sentiment(review):
|
| 16 |
result = sentiment_pipeline(review)[0]
|
| 17 |
sentiment_label = label_mapping[result["label"]]
|
| 18 |
+
confidence = round(result["score"] * 100, 2)
|
| 19 |
+
|
| 20 |
+
return {
|
| 21 |
+
"التصنيف": sentiment_label,
|
| 22 |
+
"نسبة الثقة": f"{confidence}%",
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# Define Gradio interface with better UI
|
| 26 |
+
with gr.Blocks(theme=gr.themes.Default()) as iface:
|
| 27 |
+
gr.Markdown("<h1 style='text-align: center;'>🍽️ تحليل مشاعر مراجعات المطاعم 🚀</h1>")
|
| 28 |
+
gr.Markdown(
|
| 29 |
+
"<p style='text-align: center;'> 🤖 أدخل مراجعة مطعم بالعربية، وسيقوم النموذج بتحليل المشاعر وتحديدها **إيجابية** أو **سلبية**.</p>"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
with gr.Row():
|
| 33 |
+
review_input = gr.Textbox(
|
| 34 |
+
label="✍️ أدخل مراجعتك",
|
| 35 |
+
placeholder="اكتب مراجعتك هنا...",
|
| 36 |
+
lines=2
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
submit_button = gr.Button("🔍 تحليل المراجعة")
|
| 40 |
+
|
| 41 |
+
with gr.Row():
|
| 42 |
+
output_label = gr.Textbox(label="🔹 التصنيف", interactive=False)
|
| 43 |
+
output_confidence = gr.Textbox(label="📊 نسبة الثقة", interactive=False)
|
| 44 |
+
|
| 45 |
+
submit_button.click(predict_sentiment, inputs=review_input, outputs=[output_label, output_confidence])
|
| 46 |
+
|
| 47 |
+
gr.Examples(
|
| 48 |
+
examples=[
|
| 49 |
+
["🍛 الطعام لذيذ جدًا والخدمة ممتازة!"],
|
| 50 |
+
["🤢 للأسف، التجربة كانت سيئة والطعام غير نظيف!"],
|
| 51 |
+
["👎 الخدمة كانت بطيئة والأسعار مرتفعة جدًا."],
|
| 52 |
+
["👌 تجربة رائعة، سأعود مجددًا!"],
|
| 53 |
+
["🔥 أفضل مطعم جربته على الإطلاق!"],
|
| 54 |
+
],
|
| 55 |
+
inputs=review_input,
|
| 56 |
+
)
|
| 57 |
|
| 58 |
# Launch the app
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
iface.launch()
|