Light-Dav commited on
Commit
3dce035
·
verified ·
1 Parent(s): fbfc3a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -22
app.py CHANGED
@@ -122,18 +122,20 @@ def interpret_sentiment(label, score):
122
  # --- Sentiment Analysis Function ---
123
  def analyze_sentiment(text):
124
  if not model_loaded_successfully:
125
- return {
126
- "Overall Sentiment": "<div class='sentiment-display'>⚠️ Model Not Loaded ⚠️</div><p>Please contact the administrator. The sentiment analysis model failed to load.</p>",
127
- "Confidence Scores": {},
128
- "Raw Output": "Model loading failed."
129
- }
 
130
 
131
  if not text.strip():
132
- return {
133
- "Overall Sentiment": "<div class='sentiment-display'>✍️ Please enter some text! ✍️</div><p>Start typing to analyze its sentiment.</p>",
134
- "Confidence Scores": {},
135
- "Raw Output": ""
136
- }
 
137
 
138
  try:
139
  # Asegúrate de que la salida del pipeline es una lista de listas, y toma la primera.
@@ -153,18 +155,15 @@ def analyze_sentiment(text):
153
  # Generar el HTML para mostrar el sentimiento general
154
  overall_sentiment_display = interpret_sentiment(label, score)
155
 
156
- return {
157
- "Overall Sentiment": overall_sentiment_display,
158
- "Confidence Scores": confidence_scores_output,
159
- "Raw Output": str(results)
160
- }
161
  except Exception as e:
162
- # En caso de cualquier error durante el análisis
163
- return {
164
- "Overall Sentiment": f"<div class='sentiment-display'>❌ Error ❌</div><p>An error occurred during analysis: {e}</p>",
165
- "Confidence Scores": {},
166
- "Raw Output": f"Error: {e}"
167
- }
168
 
169
  # --- Gradio Interface ---
170
  # Al establecer theme=None, Gradio no aplicará ningún tema predefinido.
@@ -198,18 +197,21 @@ with gr.Blocks(css=custom_css, theme=None) as demo:
198
  ],
199
  inputs=text_input,
200
  fn=analyze_sentiment,
 
201
  outputs=[gr.HTML(label="Overall Sentiment"), gr.Label(num_top_classes=3, label="Confidence Scores"), gr.JSON(label="Raw Model Output", visible=False)],
202
- cache_examples=False # <--- ESTE ES EL CAMBIO CLAVE PARA ELIMINAR EL FileNotFoundError
203
  )
204
 
205
  gr.Markdown("<hr style='border-top: 1px solid #424242;'>")
206
  gr.Markdown("<h2 style='color: #80cbc4;'>📊 Analysis Results</h2>")
207
 
 
208
  overall_sentiment_output = gr.HTML(label="Overall Sentiment")
209
  confidence_scores_output = gr.Label(num_top_classes=3, label="Confidence Scores")
210
  raw_output = gr.JSON(label="Raw Model Output", visible=False)
211
 
212
  # --- Event Listeners ---
 
213
  analyze_btn.click(
214
  fn=analyze_sentiment,
215
  inputs=text_input,
 
122
  # --- Sentiment Analysis Function ---
123
  def analyze_sentiment(text):
124
  if not model_loaded_successfully:
125
+ # Devuelve 3 valores: HTML, dict, string
126
+ return (
127
+ "<div class='sentiment-display'>⚠️ Model Not Loaded ⚠️</div><p>Please contact the administrator. The sentiment analysis model failed to load.</p>",
128
+ {}, # Diccionario vacío para Confidence Scores
129
+ "Model loading failed." # String de error para Raw Output
130
+ )
131
 
132
  if not text.strip():
133
+ # Devuelve 3 valores: HTML, dict, string
134
+ return (
135
+ "<div class='sentiment-display'>✍️ Please enter some text! ✍️</div><p>Start typing to analyze its sentiment.</p>",
136
+ {}, # Diccionario vacío para Confidence Scores
137
+ "" # String vacío para Raw Output
138
+ )
139
 
140
  try:
141
  # Asegúrate de que la salida del pipeline es una lista de listas, y toma la primera.
 
155
  # Generar el HTML para mostrar el sentimiento general
156
  overall_sentiment_display = interpret_sentiment(label, score)
157
 
158
+ # ¡CAMBIO CLAVE AQUÍ! Ahora devuelve una tupla con 3 valores separados
159
+ return (overall_sentiment_display, confidence_scores_output, str(results))
 
 
 
160
  except Exception as e:
161
+ # En caso de cualquier error durante el análisis, devuelve 3 valores de error
162
+ return (
163
+ f"<div class='sentiment-display'>❌ Error ❌</div><p>An error occurred during analysis: {e}</p>",
164
+ {}, # Diccionario vacío para Confidence Scores
165
+ f"Error: {e}" # String de error para Raw Output
166
+ )
167
 
168
  # --- Gradio Interface ---
169
  # Al establecer theme=None, Gradio no aplicará ningún tema predefinido.
 
197
  ],
198
  inputs=text_input,
199
  fn=analyze_sentiment,
200
+ # Asegúrate de que estos 3 outputs coinciden con los 3 valores que devuelve analyze_sentiment
201
  outputs=[gr.HTML(label="Overall Sentiment"), gr.Label(num_top_classes=3, label="Confidence Scores"), gr.JSON(label="Raw Model Output", visible=False)],
202
+ cache_examples=False # ESTE ES EL CAMBIO CLAVE PARA ELIMINAR EL FileNotFoundError
203
  )
204
 
205
  gr.Markdown("<hr style='border-top: 1px solid #424242;'>")
206
  gr.Markdown("<h2 style='color: #80cbc4;'>📊 Analysis Results</h2>")
207
 
208
+ # Estas variables de salida deben coincidir en tipo y orden con lo que devuelve analyze_sentiment
209
  overall_sentiment_output = gr.HTML(label="Overall Sentiment")
210
  confidence_scores_output = gr.Label(num_top_classes=3, label="Confidence Scores")
211
  raw_output = gr.JSON(label="Raw Model Output", visible=False)
212
 
213
  # --- Event Listeners ---
214
+ # Los outputs aquí también deben coincidir con los 3 valores que devuelve analyze_sentiment
215
  analyze_btn.click(
216
  fn=analyze_sentiment,
217
  inputs=text_input,