GuidK commited on
Commit
6a74d4d
Β·
verified Β·
1 Parent(s): 2a296fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -51
app.py CHANGED
@@ -96,6 +96,11 @@ def translate_en_to_uk(text):
96
  # Return processing error as string
97
  return f"Processing Error: {str(e)[:100]}"
98
 
 
 
 
 
 
99
  # Create Gradio blocks interface with a soft theme
100
  with gr.Blocks(title="NLP Toolkit", theme=gr.themes.Soft()) as demo:
101
  # Add main header
@@ -108,63 +113,68 @@ with gr.Blocks(title="NLP Toolkit", theme=gr.themes.Soft()) as demo:
108
  with gr.Tabs():
109
  # Create Sentiment Analysis tab
110
  with gr.Tab("Sentiment Analysis"):
111
- input_text = gr.Textbox(label="Input Text", lines=3, placeholder="Type something in English... (e.g., I absolutely love this product!)")
112
- output_label = gr.Label(label="Analysis Result")
113
- btn_sentiment = gr.Button("Analyze Sentiment", variant='primary')
114
-
115
- # Link button click to sentiment analysis function
116
- btn_sentiment.click(fn=analyze_sentiment, inputs=input_text, outputs=output_label)
117
-
118
- # Configure examples for sentiment analysis tab
119
- gr.Examples(
120
- examples=[
121
- ["I absolutely love this new update! It's fantastic."],
122
- ["I'm very disappointed with the service, it was terrible."],
123
- ["The weather today is completely ordinary."]
124
- ],
125
- inputs=input_text,
126
- cache_examples=False
127
- )
 
128
 
129
  # Create Summarization tab
130
  with gr.Tab("Summarization"):
131
- input_long = gr.Textbox(label="Long Text (EN)", lines=6, placeholder="Paste a long English article to summarize here...")
132
- output_summary = gr.Textbox(label="Short Summary", lines=3)
133
- btn_summarize = gr.Button("Generate Summary", variant="primary")
134
-
135
- # Link button click to summarization function
136
- btn_summarize.click(fn=summarize_text, inputs=input_long, outputs=output_summary)
137
-
138
- # Configure examples for summarization tab with formatting bug fixes
139
- gr.Examples(
140
- examples=[
141
- ["Artificial intelligence (AI) has become a transformative force in modern society, influencing everything from healthcare to transportation. In the medical field, AI algorithms can analyze complex data to assist doctors in diagnosing diseases more accurately and quickly. For instance, machine learning models are trained on thousands of medical images to detect early signs of conditions like cancer. Meanwhile, in transportation, autonomous vehicles use AI to navigate roads, avoid obstacles, and reduce the likelihood of accidents caused by human error."],
142
- ["The James Webb Space Telescope (JWST) is the largest and most powerful space science telescope ever built. Launched on December 25, 2021, it represents a major leap forward in our quest to understand the universe. Unlike its predecessor, the Hubble Space Telescope, which primarily observes visible light, the JWST is designed to detect infrared radiation. This capability allows it to peer through dense clouds of cosmic dust and observe the very first galaxies that formed after the Big Bang."],
143
- ["Climate change is a long-term shift in global or regional climate patterns. Often climate change refers specifically to the rise in global temperatures from the mid-20th century to present. It is primarily caused by human activities, especially the burning of fossil fuels, which increases levels of heat-trapping greenhouse gases in Earth's atmosphere. The impacts are already being felt globally, including more frequent and severe weather events, rising sea levels, and shifts in wildlife populations."]
144
- ],
145
- inputs=input_long,
146
- cache_examples=False
147
- )
 
 
148
 
149
  # Create Translation tab
150
  with gr.Tab("Translation (EN -> UK)"):
151
- input_trans = gr.Textbox(label="English Text", lines=4, placeholder="Enter text in English to translate into Ukrainian...")
152
- output_trans = gr.Textbox(label="Ukrainian Translation", lines=4)
153
- btn_translate = gr.Button("Translate to Ukrainian", variant="primary")
154
-
155
- # Link button click to translation function
156
- btn_translate.click(fn=translate_en_to_uk, inputs=input_trans, outputs=output_trans)
157
-
158
- # Configure examples for translation tab targeting the correct input component
159
- gr.Examples(
160
- examples=[
161
- ["Artificial intelligence is rapidly changing the world."],
162
- ["I would like to order a cup of coffee, please."],
163
- ["The results of the recent testing phase exceeded all our initial expectations."]
164
- ],
165
- inputs=input_trans,
166
- cache_examples=False
167
- )
 
 
168
 
169
  if __name__ == "__main__":
170
  # Launch application with active debug mode
 
96
  # Return processing error as string
97
  return f"Processing Error: {str(e)[:100]}"
98
 
99
+ # Define long text samples for the summarization tab
100
+ ai_sample = "Artificial intelligence (AI) has become a transformative force in modern society, influencing everything from healthcare to transportation. In the medical field, AI algorithms can analyze complex data to assist doctors in diagnosing diseases more accurately and quickly. For instance, machine learning models are trained on thousands of medical images to detect early signs of conditions like cancer. Meanwhile, in transportation, autonomous vehicles use AI to navigate roads, avoid obstacles, and reduce the likelihood of accidents caused by human error."
101
+ space_sample = "The James Webb Space Telescope (JWST) is the largest and most powerful space science telescope ever built. Launched on December 25, 2021, it represents a major leap forward in our quest to understand the universe. Unlike its predecessor, the Hubble Space Telescope, which primarily observes visible light, the JWST is designed to detect infrared radiation. This capability allows it to peer through dense clouds of cosmic dust and observe the very first galaxies that formed after the Big Bang."
102
+ climate_sample = "Climate change is a long-term shift in global or regional climate patterns. Often climate change refers specifically to the rise in global temperatures from the mid-20th century to present. It is primarily caused by human activities, especially the burning of fossil fuels, which increases levels of heat-trapping greenhouse gases in Earth's atmosphere. The impacts are already being felt globally, including more frequent and severe weather events, rising sea levels, and shifts in wildlife populations."
103
+
104
  # Create Gradio blocks interface with a soft theme
105
  with gr.Blocks(title="NLP Toolkit", theme=gr.themes.Soft()) as demo:
106
  # Add main header
 
113
  with gr.Tabs():
114
  # Create Sentiment Analysis tab
115
  with gr.Tab("Sentiment Analysis"):
116
+ with gr.Column():
117
+ input_text = gr.Textbox(label="Input Text", lines=3, placeholder="Type something in English... (e.g., I absolutely love this product!)")
118
+ output_label = gr.Label(label="Analysis Result")
119
+ btn_sentiment = gr.Button("Analyze Sentiment", variant='primary')
120
+
121
+ # Link button click to sentiment analysis function
122
+ btn_sentiment.click(fn=analyze_sentiment, inputs=input_text, outputs=output_label)
123
+
124
+ # Configure native examples for short texts
125
+ gr.Examples(
126
+ examples=[
127
+ ["I absolutely love this new update! It's fantastic."],
128
+ ["I'm very disappointed with the service, it was terrible."],
129
+ ["The weather today is completely ordinary."]
130
+ ],
131
+ inputs=input_text,
132
+ cache_examples=False
133
+ )
134
 
135
  # Create Summarization tab
136
  with gr.Tab("Summarization"):
137
+ with gr.Column():
138
+ input_long = gr.Textbox(label="Long Text (EN)", lines=6, placeholder="Paste a long English article to summarize here...")
139
+ output_summary = gr.Textbox(label="Short Summary", lines=3)
140
+ btn_summarize = gr.Button("Generate Summary", variant="primary")
141
+
142
+ # Link button click to summarization function
143
+ btn_summarize.click(fn=summarize_text, inputs=input_long, outputs=output_summary)
144
+
145
+ # Add examples section header
146
+ gr.Markdown("### πŸ“‹ Click an example button below to load text:")
147
+ with gr.Row():
148
+ sum_ex1 = gr.Button("πŸ’‘ AI Force")
149
+ sum_ex2 = gr.Button("πŸš€ Space Telescope")
150
+ sum_ex3 = gr.Button("🌍 Climate Change")
151
+
152
+ # Link custom buttons to load the long texts into the input field
153
+ sum_ex1.click(fn=lambda: ai_sample, outputs=input_long)
154
+ sum_ex2.click(fn=lambda: space_sample, outputs=input_long)
155
+ sum_ex3.click(fn=lambda: climate_sample, outputs=input_long)
156
 
157
  # Create Translation tab
158
  with gr.Tab("Translation (EN -> UK)"):
159
+ with gr.Column():
160
+ input_trans = gr.Textbox(label="English Text", lines=4, placeholder="Enter text in English to translate into Ukrainian...")
161
+ output_trans = gr.Textbox(label="Ukrainian Translation", lines=4)
162
+ btn_translate = gr.Button("Translate to Ukrainian", variant="primary")
163
+
164
+ # Link button click to translation function
165
+ btn_translate.click(fn=translate_en_to_uk, inputs=input_trans, outputs=output_trans)
166
+
167
+ # Add examples section header
168
+ gr.Markdown("### πŸ“‹ Click an example button below to load text:")
169
+ with gr.Row():
170
+ trans_ex1 = gr.Button("πŸ€– AI Future")
171
+ trans_ex2 = gr.Button("β˜• Coffee Order")
172
+ trans_ex3 = gr.Button("πŸ“Š Testing Phase")
173
+
174
+ # Link custom buttons to load sentences into the correct translation field
175
+ trans_ex1.click(fn=lambda: "Artificial intelligence is rapidly changing the world.", outputs=input_trans)
176
+ trans_ex2.click(fn=lambda: "I would like to order a cup of coffee, please.", outputs=input_trans)
177
+ trans_ex3.click(fn=lambda: "The results of the recent testing phase exceeded all our initial expectations.", outputs=input_trans)
178
 
179
  if __name__ == "__main__":
180
  # Launch application with active debug mode