DemahAlmutairi commited on
Commit
50b5fb0
·
verified ·
1 Parent(s): 47334b4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -131
app.py CHANGED
@@ -1,135 +1,31 @@
1
- from transformers import pipeline
2
  import gradio as gr
3
- import time # To simulate processing time for demonstration
4
-
5
- # Load the summarization pipelines
6
- arabic_summarizer = pipeline("summarization", model="eslamxm/mt5-base-finetuned-persian-finetuned-persian-arabic")
7
- english_summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
8
-
9
- # Initialize the current language
10
- current_language = 'Arabic'
11
-
12
- def summarize(text, max_length, min_length, progress=gr.Progress()):
13
- global current_language
14
- # Simulate a delay to show the progress bar
15
- for step in range(10):
16
- time.sleep(0.1) # Simulating processing time
17
- progress(step / 10) # Update the progress bar
18
-
19
- # Select the summarizer based on the current language
20
- if current_language == 'Arabic':
21
- summary = arabic_summarizer(text, max_length=max_length, min_length=min_length, do_sample=False)[0]['summary_text']
22
- else:
23
- summary = english_summarizer(text, max_length=max_length, min_length=min_length, do_sample=False)[0]['summary_text']
24
-
25
- return summary
26
 
27
- def toggle_language():
28
- global current_language
29
- if current_language == 'Arabic':
30
- current_language = 'English'
31
- return "Switched to English summarization."
32
- else:
33
- current_language = 'Arabic'
34
- return "تم التبديل إلى تلخيص اللغة العربية."
35
 
36
- def update_interface():
37
- if current_language == 'Arabic':
38
- return {
39
- "title": "أداة تلخيص النصوص",
40
- "textbox_label": "ادخل النص هنا",
41
- "max_length_label": "الحد الأقصى لطول الملخص",
42
- "min_length_label": "الحد الأدنى لطول الملخص",
43
- "summarize_button_label": "تلخيص",
44
- "summary_label": "الملخص"
45
- }
46
  else:
47
- return {
48
- "title": "Text Summarization Tool",
49
- "textbox_label": "Enter Text Here",
50
- "max_length_label": "Max Summary Length",
51
- "min_length_label": "Min Summary Length",
52
- "summarize_button_label": "Summarize",
53
- "summary_label": "Summary"
54
- }
55
-
56
- # Custom CSS for the progress bar with animations
57
- custom_css = """
58
- body {
59
- background: linear-gradient(135deg, #667eea, #764ba2);
60
- font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
61
- color: #333;
62
- margin: 0;
63
- padding: 0;
64
- }
65
- .gradio-container {
66
- background: rgba(255, 255, 255, 0.95);
67
- border-radius: 15px;
68
- padding: 30px 40px;
69
- box-shadow: 0 8px 30px rgba(0, 0, 0, 0.3);
70
- margin: 40px auto;
71
- max-width: 1200px;
72
- }
73
- .gradio-container h1 {
74
- color: #333;
75
- text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.2);
76
- }
77
- .fillable {
78
- width: 95% !important;
79
- max-width: unset !important;
80
- }
81
- #examples_container {
82
- margin: auto;
83
- width: 90%;
84
- }
85
- #examples_row {
86
- justify-content: center;
87
- }
88
- .sidebar {
89
- background: rgba(255, 255, 255, 0.98);
90
- border-radius: 10px;
91
- padding: 20px;
92
- box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
93
- }
94
- button, .btn {
95
- background: linear-gradient(90deg, #ff8a00, #e52e71);
96
- border: none;
97
- color: #fff;
98
- padding: 12px 24px;
99
- text-transform: uppercase;
100
- font-weight: bold;
101
- letter-spacing: 1px;
102
- border-radius: 5px;
103
- cursor: pointer;
104
- transition: transform 0.2s ease-in-out;
105
- }
106
- button:hover, .btn:hover {
107
- transform: scale(1.05);
108
- }
109
- """
110
-
111
- # Create the Gradio interface
112
- with gr.Blocks(css=custom_css) as interface:
113
- title = gr.Markdown("<h1 style='text-align: center;'>Text Summarization Tool</h1>")
114
-
115
- text_input = gr.Textbox(label="Enter Text Here", lines=8, text_align="right")
116
- max_length = gr.Slider(minimum=80, maximum=200, value=100, step=1, label="Max Summary Length")
117
- min_length = gr.Slider(minimum=5, maximum=50, value=20, step=1, label="Min Summary Length")
118
-
119
- summarize_button = gr.Button("Summarize")
120
- language_button = gr.Button("Switch Language")
121
- summary_output = gr.Textbox(label="Summary", lines=6, text_align="right")
122
-
123
- summarize_button.click(summarize, inputs=[text_input, max_length, min_length], outputs=summary_output)
124
-
125
- def update_labels():
126
- labels = update_interface()
127
- return labels["title"], labels["textbox_label"], labels["max_length_label"], labels["min_length_label"], labels["summarize_button_label"], labels["summary_label"]
128
-
129
- language_button.click(
130
- lambda: (toggle_language(), update_labels()),
131
- outputs=[title, text_input, max_length, min_length, summarize_button, summary_output]
132
- )
133
-
134
- if __name__ == "__main__":
135
- interface.launch()
 
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # Load sentiment analysis models
6
+ english_sentiment_model = pipeline("sentiment-analysis", model="nlptown/bert-base-multilingual-uncased-sentiment")
7
+ arabic_sentiment_model = pipeline("sentiment-analysis", model="akhooli/arabic-sentiment")
 
 
 
 
 
8
 
9
+ def analyze_sentiment(text, language):
10
+ if language == "English":
11
+ result = english_sentiment_model(text)
 
 
 
 
 
 
 
12
  else:
13
+ result = arabic_sentiment_model(text)
14
+ return result[0]['label'], result[0]['score']
15
+
16
+ # Create Gradio interface
17
+ iface = gr.Interface(
18
+ fn=analyze_sentiment,
19
+ inputs=[
20
+ gr.inputs.Textbox(label="Enter text"),
21
+ gr.inputs.Radio(choices=["English", "Arabic"], label="Select Language")
22
+ ],
23
+ outputs=[
24
+ gr.outputs.Label(label="Sentiment"),
25
+ gr.outputs.Number(label="Confidence Score")
26
+ ],
27
+ title="Sentiment Analysis",
28
+ description="Analyze the sentiment of text in English and Arabic."
29
+ )
30
+
31
+ iface.launch()