File size: 2,222 Bytes
950f4ad
 
 
d27e19a
950f4ad
 
 
d27e19a
f9e4334
dc1c5f1
 
 
 
f9e4334
950f4ad
d27e19a
950f4ad
 
d27e19a
 
f9e4334
d27e19a
f9e4334
d27e19a
f9e4334
 
d27e19a
f9e4334
 
d27e19a
f9e4334
 
 
 
 
 
 
 
 
 
 
dc1c5f1
 
 
 
 
f9e4334
 
 
950f4ad
dc1c5f1
950f4ad
d27e19a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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)