Shatha2030 commited on
Commit
144a909
·
verified ·
1 Parent(s): 93f1909

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
5
+
6
+ def analyze_sentiment(text):
7
+ result = sentiment_analyzer(text)
8
+ sentiment = result[0]['label']
9
+ return sentiment
10
+
11
+ def update_language(language):
12
+ if language == "العربية":
13
+ return (
14
+ gr.update(label="أدخل النص", placeholder="اكتب جملة أو فقرة هنا ^^..."),
15
+ gr.update(label="المشاعر المتوقعة (من 1 إلى 5)"),
16
+ "تحليل المشاعر"
17
+ )
18
+ else: # Default to English
19
+ return (
20
+ gr.update(label="Enter Text", placeholder="Type a sentence or paragraph here ^^..."),
21
+ gr.update(label="Predicted Sentiment (1-5) star"),
22
+ "Analyze Sentiment"
23
+ )
24
+
25
+ examples = [
26
+ ["I love this product! It's amazing!"],
27
+ ["This was the worst experience I've ever had."],
28
+ ["The movie was okay, not great but not bad either."],
29
+ ["Absolutely fantastic! I would recommend it to everyone."]
30
+ ]
31
+
32
+ # Gradio
33
+ with gr.Blocks() as demo:
34
+ gr.Markdown("## Sentiment Analysis - Choose Language (Arabic or English)")
35
+
36
+ language_choice = gr.Radio(["العربية", "English"], label="اختر اللغة", value="English")
37
+
38
+ with gr.Row():
39
+ with gr.Column():
40
+ text_input = gr.Textbox(label="Enter Text", placeholder="Type a sentence or paragraph here ^^...")
41
+
42
+ with gr.Column():
43
+ output = gr.Textbox(label="Predicted Sentiment (1-5)", interactive=False)
44
+
45
+ analyze_button = gr.Button("Analyze Sentiment")
46
+
47
+ gr.Examples(examples=examples, inputs=text_input)
48
+
49
+ analyze_button.click(analyze_sentiment, inputs=[text_input], outputs=output)
50
+
51
+ language_choice.change(update_language, inputs=[language_choice], outputs=[text_input, output, analyze_button])
52
+
53
+ demo.launch()