Download app.py from Abdu-GH/Arabic-Reviews-Sentiment-Analysis: direct link, hf CLI and curl.
- Browser
- Download file 2.22 kB
-
https://huggingface.co/spaces/Abdu-GH/Arabic-Reviews-Sentiment-Analysis/resolve/main/app.py
- Command line
-
hf download hf://spaces/Abdu-GH/Arabic-Reviews-Sentiment-Analysis/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Abdu-GH/Arabic-Reviews-Sentiment-Analysis/resolve/main/app.py
2.22 kB
| import gradio as gr | |
| from transformers import pipeline | |
| # Load the sentiment analysis model | |
| model_name = "Abduuu/ArabReview-Sentiment" | |
| sentiment_pipeline = pipeline("text-classification", model=model_name, tokenizer=model_name) | |
| # Define label mapping (handling both possible label formats) | |
| label_mapping = { | |
| "LABEL_0": "سلبي", | |
| "LABEL_1": "إيجابي", | |
| "Negative": "سلبي", | |
| "Positive": "إيجابي" | |
| } | |
| # Function to predict sentiment | |
| def predict_sentiment(review): | |
| result = sentiment_pipeline(review)[0] | |
| sentiment_label = label_mapping.get(result["label"], "غير معروف") # Handle unexpected labels | |
| confidence = f"{(result['score'] * 100):.2f}%" | |
| return sentiment_label, confidence # Return as separate values | |
| # Define Gradio interface | |
| with gr.Blocks(theme=gr.themes.Default()) as iface: | |
| gr.Markdown("<h1 style='text-align: center;'>🍽️ تحليل مشاعر مراجعات المطاعم 🚀</h1>") | |
| gr.Markdown("<p style='text-align: center;'> 🤖 أدخل مراجعة مطعم بالعربية، وسيقوم النموذج بتحليل المشاعر.</p>") | |
| with gr.Row(): | |
| review_input = gr.Textbox(label="✍️ أدخل مراجعتك", placeholder="اكتب مراجعتك هنا...", lines=2) | |
| submit_button = gr.Button("🔍 تحليل المراجعة") | |
| with gr.Row(): | |
| output_label = gr.Textbox(label="🔹 التصنيف", interactive=False) | |
| output_confidence = gr.Textbox(label="📊 نسبة الثقة", interactive=False) | |
| submit_button.click(predict_sentiment, inputs=review_input, outputs=[output_label, output_confidence]) | |
| gr.Examples( | |
| examples=[ | |
| ["الطعام لذيذ جدًا والخدمة ممتازة"], | |
| ["التجربة كانت سيئة والطعام غير نظيف"], | |
| ["الخدمة كانت بطيئة والأسعار مرتفعة جدًا"], | |
| ["تجربة رائعة، سأعود مجددًا"], | |
| ["أفضل مطعم جربته على الإطلاق"], | |
| ], | |
| inputs=review_input, | |
| ) | |
| # Launch the app with public sharing enabled | |
| if __name__ == "__main__": | |
| iface.launch(share=True) | |