logasanjeev commited on
Commit
4df5a3d
·
verified ·
1 Parent(s): 0ce1d0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -15
app.py CHANGED
@@ -29,7 +29,7 @@ default_thresholds = inference_module.THRESHOLDS
29
  # Prediction function with export capability
30
  def predict_emotions_with_details(text, confidence_threshold=0.0, chart_type="bar"):
31
  if not text.strip():
32
- return "Please enter some text.", "", "", None, None
33
 
34
  predictions_str, processed_text = predict_emotions(text)
35
 
@@ -106,7 +106,7 @@ def predict_emotions_with_details(text, confidence_threshold=0.0, chart_type="ba
106
  fig.update_traces(textinfo='percent+label', pull=[0.1] + [0] * (len(df) - 1))
107
  fig.update_layout(margin=dict(t=40, b=40))
108
 
109
- return processed_text, thresholded_output, top_5_output, fig, df_export
110
 
111
  # Custom CSS for enhanced styling
112
  custom_css = """
@@ -193,6 +193,10 @@ body {
193
  font-style: italic;
194
  color: #888;
195
  text-align: center;
 
 
 
 
196
  }
197
  #examples-title {
198
  font-size: 1.5em;
@@ -222,7 +226,7 @@ footer a:hover {
222
  }
223
  """
224
 
225
- # JavaScript for theme toggle and loading spinner
226
  theme_js = """
227
  function toggleTheme() {
228
  document.body.classList.toggle('dark-mode');
@@ -230,10 +234,10 @@ function toggleTheme() {
230
  toggleBtn.innerHTML = document.body.classList.contains('dark-mode') ? '☀️' : '🌙';
231
  }
232
  function showLoading() {
233
- document.getElementById('loading').style.display = 'block';
234
  }
235
  function hideLoading() {
236
- document.getElementById('loading').style.display = 'none';
237
  }
238
  """
239
 
@@ -291,7 +295,7 @@ with gr.Blocks(css=custom_css) as demo:
291
  reset_btn = gr.Button("Reset", variant="secondary")
292
 
293
  # Loading indicator
294
- gr.HTML("<div id='loading' style='display:none;'>Predicting emotions, please wait...</div>")
295
 
296
  # Output Section
297
  with gr.Row():
@@ -334,24 +338,31 @@ with gr.Blocks(css=custom_css) as demo:
334
  """
335
  )
336
 
 
 
 
337
  # Bind predictions with loading spinner
 
 
 
 
 
 
338
  submit_btn.click(
 
 
 
 
339
  fn=predict_emotions_with_details,
340
  inputs=[text_input, confidence_slider, chart_type],
341
- outputs=[processed_text_output, thresholded_output, top_5_output, output_plot, export_btn],
342
- _js="showLoading(); return [arguments[0], arguments[1], arguments[2]]"
343
- ).then(
344
- fn=None,
345
- inputs=None,
346
- outputs=None,
347
- _js="hideLoading"
348
  )
349
 
350
  # Reset functionality
351
  reset_btn.click(
352
- fn=lambda: ("", "", "", None, None),
353
  inputs=[],
354
- outputs=[text_input, processed_text_output, thresholded_output, top_5_output, output_plot, export_btn]
355
  )
356
 
357
  # Launch
 
29
  # Prediction function with export capability
30
  def predict_emotions_with_details(text, confidence_threshold=0.0, chart_type="bar"):
31
  if not text.strip():
32
+ return "Please enter some text.", "", "", None, None, False
33
 
34
  predictions_str, processed_text = predict_emotions(text)
35
 
 
106
  fig.update_traces(textinfo='percent+label', pull=[0.1] + [0] * (len(df) - 1))
107
  fig.update_layout(margin=dict(t=40, b=40))
108
 
109
+ return processed_text, thresholded_output, top_5_output, fig, df_export, True
110
 
111
  # Custom CSS for enhanced styling
112
  custom_css = """
 
193
  font-style: italic;
194
  color: #888;
195
  text-align: center;
196
+ display: none;
197
+ }
198
+ #loading.visible {
199
+ display: block;
200
  }
201
  #examples-title {
202
  font-size: 1.5em;
 
226
  }
227
  """
228
 
229
+ # JavaScript for theme toggle
230
  theme_js = """
231
  function toggleTheme() {
232
  document.body.classList.toggle('dark-mode');
 
234
  toggleBtn.innerHTML = document.body.classList.contains('dark-mode') ? '☀️' : '🌙';
235
  }
236
  function showLoading() {
237
+ document.getElementById('loading').classList.add('visible');
238
  }
239
  function hideLoading() {
240
+ document.getElementById('loading').classList.remove('visible');
241
  }
242
  """
243
 
 
295
  reset_btn = gr.Button("Reset", variant="secondary")
296
 
297
  # Loading indicator
298
+ loading_indicator = gr.HTML("<div id='loading'>Predicting emotions, please wait...</div>")
299
 
300
  # Output Section
301
  with gr.Row():
 
338
  """
339
  )
340
 
341
+ # State to manage loading visibility
342
+ loading_state = gr.State(value=False)
343
+
344
  # Bind predictions with loading spinner
345
+ def start_loading():
346
+ return True
347
+
348
+ def stop_loading(processed_text, thresholded_output, top_5_output, fig, df_export, loading_state):
349
+ return processed_text, thresholded_output, top_5_output, fig, df_export, False
350
+
351
  submit_btn.click(
352
+ fn=start_loading,
353
+ inputs=[],
354
+ outputs=[loading_state]
355
+ ).then(
356
  fn=predict_emotions_with_details,
357
  inputs=[text_input, confidence_slider, chart_type],
358
+ outputs=[processed_text_output, thresholded_output, top_5_output, output_plot, export_btn, loading_state]
 
 
 
 
 
 
359
  )
360
 
361
  # Reset functionality
362
  reset_btn.click(
363
+ fn=lambda: ("", "", "", None, None, False),
364
  inputs=[],
365
+ outputs=[text_input, processed_text_output, thresholded_output, top_5_output, output_plot, export_btn, loading_state]
366
  )
367
 
368
  # Launch