Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,9 @@ import gradio as gr
|
|
2 |
from transformers import pipeline
|
3 |
import os
|
4 |
|
|
|
|
|
|
|
5 |
# --- Model Loading ---
|
6 |
MODEL_ID = "Light-Dav/sentiment-analysis-full-project"
|
7 |
|
@@ -14,6 +17,7 @@ except Exception as e:
|
|
14 |
model_loaded_successfully = False
|
15 |
|
16 |
# --- Custom CSS for a dark look inspired by the website ---
|
|
|
17 |
custom_css = """
|
18 |
body {
|
19 |
background-color: #121212; /* Dark background */
|
@@ -75,9 +79,14 @@ h1, h2, h3 {
|
|
75 |
from { opacity: 0; }
|
76 |
to { opacity: 1; }
|
77 |
}
|
|
|
78 |
gr-textbox > label {
|
79 |
color: #80cbc4;
|
80 |
}
|
|
|
|
|
|
|
|
|
81 |
"""
|
82 |
|
83 |
# --- Helper Function for Sentiment Interpretation ---
|
@@ -102,7 +111,7 @@ def interpret_sentiment(label, score):
|
|
102 |
emoji = "❓"
|
103 |
description = "Could not confidently determine sentiment. Unexpected label."
|
104 |
color_class = ""
|
105 |
-
|
106 |
return f"<div class='sentiment-display {color_class}'>{emoji} {label.upper()} ({score:.2f})</div>" + \
|
107 |
f"<p>{description}</p>"
|
108 |
|
@@ -123,11 +132,11 @@ def analyze_sentiment(text):
|
|
123 |
}
|
124 |
|
125 |
try:
|
126 |
-
results = sentiment_analyzer(text)[0]
|
127 |
|
128 |
results_sorted = sorted(results, key=lambda x: x['score'], reverse=True)
|
129 |
|
130 |
-
top_sentiment = results_sorted(0)
|
131 |
label = top_sentiment['label']
|
132 |
score = top_sentiment['score']
|
133 |
|
@@ -148,7 +157,9 @@ def analyze_sentiment(text):
|
|
148 |
}
|
149 |
|
150 |
# --- Gradio Interface ---
|
151 |
-
|
|
|
|
|
152 |
gr.Markdown("<h1 style='color: #80cbc4; text-align: center;'>🌌 Sentiment Analyzer 🌌</h1>")
|
153 |
gr.Markdown("<p style='color: #f8f8f2; text-align: center;'>Uncover the emotional tone of your English text instantly.</p>")
|
154 |
|
@@ -164,6 +175,8 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Dark()) as demo:
|
|
164 |
|
165 |
gr.Markdown("<hr style='border-top: 1px solid #424242;'>")
|
166 |
gr.Markdown("<h3 style='color: #80cbc4; text-align: center;'>Try some examples:</h3>")
|
|
|
|
|
167 |
examples = gr.Examples(
|
168 |
examples=[
|
169 |
["This product exceeded my expectations, truly amazing!"],
|
@@ -181,10 +194,12 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Dark()) as demo:
|
|
181 |
|
182 |
gr.Markdown("<hr style='border-top: 1px solid #424242;'>")
|
183 |
gr.Markdown("<h2 style='color: #80cbc4;'>📊 Analysis Results</h2>")
|
|
|
184 |
overall_sentiment_output = gr.HTML(label="Overall Sentiment")
|
185 |
confidence_scores_output = gr.Label(num_top_classes=3, label="Confidence Scores")
|
186 |
raw_output = gr.JSON(label="Raw Model Output", visible=False)
|
187 |
|
|
|
188 |
analyze_btn.click(
|
189 |
fn=analyze_sentiment,
|
190 |
inputs=text_input,
|
@@ -197,4 +212,5 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Dark()) as demo:
|
|
197 |
# live=True # Puedes descomentar si quieres actualizaciones en vivo (consume más recursos)
|
198 |
)
|
199 |
|
|
|
200 |
demo.launch()
|
|
|
2 |
from transformers import pipeline
|
3 |
import os
|
4 |
|
5 |
+
# Añade esto para verificar la versión de Gradio en tiempo de ejecución
|
6 |
+
print(f"Gradio version at runtime: {gr.__version__}")
|
7 |
+
|
8 |
# --- Model Loading ---
|
9 |
MODEL_ID = "Light-Dav/sentiment-analysis-full-project"
|
10 |
|
|
|
17 |
model_loaded_successfully = False
|
18 |
|
19 |
# --- Custom CSS for a dark look inspired by the website ---
|
20 |
+
# Este CSS define todo el aspecto visual sin depender de un tema de Gradio
|
21 |
custom_css = """
|
22 |
body {
|
23 |
background-color: #121212; /* Dark background */
|
|
|
79 |
from { opacity: 0; }
|
80 |
to { opacity: 1; }
|
81 |
}
|
82 |
+
/* Estilos para las etiquetas de los componentes de entrada */
|
83 |
gr-textbox > label {
|
84 |
color: #80cbc4;
|
85 |
}
|
86 |
+
/* Asegúrate de que las etiquetas de salida también tengan color */
|
87 |
+
.gradio-output .label {
|
88 |
+
color: #80cbc4; /* Color de acento para las etiquetas de salida */
|
89 |
+
}
|
90 |
"""
|
91 |
|
92 |
# --- Helper Function for Sentiment Interpretation ---
|
|
|
111 |
emoji = "❓"
|
112 |
description = "Could not confidently determine sentiment. Unexpected label."
|
113 |
color_class = ""
|
114 |
+
|
115 |
return f"<div class='sentiment-display {color_class}'>{emoji} {label.upper()} ({score:.2f})</div>" + \
|
116 |
f"<p>{description}</p>"
|
117 |
|
|
|
132 |
}
|
133 |
|
134 |
try:
|
135 |
+
results = sentiment_analyzer(text)[0] # Obtener la primera (y única) lista de resultados
|
136 |
|
137 |
results_sorted = sorted(results, key=lambda x: x['score'], reverse=True)
|
138 |
|
139 |
+
top_sentiment = results_sorted[0] # <--- CORRECCIÓN IMPORTANTE AQUÍ (era results_sorted(0))
|
140 |
label = top_sentiment['label']
|
141 |
score = top_sentiment['score']
|
142 |
|
|
|
157 |
}
|
158 |
|
159 |
# --- Gradio Interface ---
|
160 |
+
# Al establecer theme=None, Gradio no aplicará ningún tema predefinido.
|
161 |
+
# Todo el estilo visual vendrá de nuestro `custom_css`.
|
162 |
+
with gr.Blocks(css=custom_css, theme=None) as demo: # <--- CAMBIO CLAVE AQUÍ: theme=None
|
163 |
gr.Markdown("<h1 style='color: #80cbc4; text-align: center;'>🌌 Sentiment Analyzer 🌌</h1>")
|
164 |
gr.Markdown("<p style='color: #f8f8f2; text-align: center;'>Uncover the emotional tone of your English text instantly.</p>")
|
165 |
|
|
|
175 |
|
176 |
gr.Markdown("<hr style='border-top: 1px solid #424242;'>")
|
177 |
gr.Markdown("<h3 style='color: #80cbc4; text-align: center;'>Try some examples:</h3>")
|
178 |
+
# Aquí definimos los outputs directamente al crear el gr.Examples,
|
179 |
+
# esto es una forma robusta de manejarlo.
|
180 |
examples = gr.Examples(
|
181 |
examples=[
|
182 |
["This product exceeded my expectations, truly amazing!"],
|
|
|
194 |
|
195 |
gr.Markdown("<hr style='border-top: 1px solid #424242;'>")
|
196 |
gr.Markdown("<h2 style='color: #80cbc4;'>📊 Analysis Results</h2>")
|
197 |
+
# Asegúrate de que las variables de salida estén definidas aquí para los listeners
|
198 |
overall_sentiment_output = gr.HTML(label="Overall Sentiment")
|
199 |
confidence_scores_output = gr.Label(num_top_classes=3, label="Confidence Scores")
|
200 |
raw_output = gr.JSON(label="Raw Model Output", visible=False)
|
201 |
|
202 |
+
# --- Event Listeners ---
|
203 |
analyze_btn.click(
|
204 |
fn=analyze_sentiment,
|
205 |
inputs=text_input,
|
|
|
212 |
# live=True # Puedes descomentar si quieres actualizaciones en vivo (consume más recursos)
|
213 |
)
|
214 |
|
215 |
+
# Launch the Gradio application
|
216 |
demo.launch()
|