Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
import os
|
4 |
-
import random
|
5 |
|
6 |
# A帽ade esto para verificar la versi贸n de Gradio en tiempo de ejecuci贸n
|
7 |
print(f"Gradio version at runtime: {gr.__version__}")
|
@@ -87,7 +87,7 @@ p {
|
|
87 |
|
88 |
/* BOTONES PRINCIPALES */
|
89 |
.gr-button.primary {
|
90 |
-
background-color: #00BFFF !important; /*
|
91 |
color: #1E2B38 !important; /* Texto oscuro para el bot贸n primario */
|
92 |
border-radius: 5px;
|
93 |
transition: background-color 0.3s ease;
|
@@ -147,12 +147,8 @@ p {
|
|
147 |
.sentiment-negative { background-color: #dc3545; }
|
148 |
.sentiment-neutral { background-color: #007BFF; }
|
149 |
|
150 |
-
/*
|
151 |
-
.
|
152 |
-
background-color: rgba(0, 191, 255, 0.1) !important; /* Azul Brillante transparente */
|
153 |
-
border: 1px solid #00BFFF !important;
|
154 |
-
}
|
155 |
-
.gradio-example-button {
|
156 |
background-color: #4A5B6C !important; /* Gris Claro para los botones de ejemplo */
|
157 |
color: #FFFFFF !important;
|
158 |
border: 1px solid #4A5B6C;
|
@@ -163,14 +159,12 @@ p {
|
|
163 |
font-size: 0.75em; /* Tama帽o de fuente m谩s peque帽o para los ejemplos */
|
164 |
white-space: nowrap; /* Evitar que el texto se rompa en varias l铆neas */
|
165 |
flex-shrink: 0; /* No permitir que se encojan si hay poco espacio */
|
|
|
166 |
}
|
167 |
-
.
|
168 |
background-color: #007ACC !important; /* Azul Oscuro al pasar el rat贸n por los ejemplos */
|
169 |
border-color: #00BFFF !important;
|
170 |
}
|
171 |
-
/* No necesitamos .example-buttons-row en el CSS porque elem_classes no se usa.
|
172 |
-
El `gr.Row` ya hace que los ejemplos fluyan horizontalmente. */
|
173 |
-
|
174 |
|
175 |
/* L脥NEAS DIVISORIAS */
|
176 |
hr {
|
@@ -265,24 +259,20 @@ def analyze_sentiment(text):
|
|
265 |
|
266 |
# --- Lista completa de ejemplos para selecci贸n aleatoria ---
|
267 |
ALL_EXAMPLES = [
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
]
|
281 |
|
282 |
-
# --- Funci贸n para obtener ejemplos aleatorios ---
|
283 |
-
def get_random_examples():
|
284 |
-
return random.sample(ALL_EXAMPLES, 3) # Selecciona 3 ejemplos al azar
|
285 |
-
|
286 |
# --- Gradio Interface ---
|
287 |
with gr.Blocks(css=custom_css, theme=None) as demo:
|
288 |
gr.Markdown("<h1 style='color: #00BFFF;'>馃寣 Sentiment Analyzer 馃寣</h1>")
|
@@ -290,34 +280,44 @@ with gr.Blocks(css=custom_css, theme=None) as demo:
|
|
290 |
|
291 |
with gr.Column(elem_classes="gradio-container"):
|
292 |
text_input = gr.Textbox(
|
293 |
-
lines=2,
|
294 |
placeholder="Type your English text here...",
|
295 |
label="Your Text",
|
296 |
interactive=True,
|
297 |
-
# Establece un ejemplo aleatorio inicial
|
298 |
-
value=random.choice(ALL_EXAMPLES)[0]
|
299 |
)
|
300 |
analyze_btn = gr.Button("Analyze Sentiment", variant="primary")
|
301 |
|
|
|
302 |
gr.Markdown("<hr>")
|
303 |
gr.Markdown("<h3 style='color: #00BFFF;'>Try examples:</h3>")
|
304 |
|
305 |
-
with gr.Row():
|
306 |
-
#
|
307 |
-
#
|
308 |
-
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
315 |
|
316 |
gr.Markdown("<hr>")
|
317 |
gr.Markdown("<h2 style='color: #00BFFF;'>馃搳 Results</h2>")
|
318 |
|
319 |
-
|
320 |
-
|
|
|
|
|
321 |
raw_output = gr.JSON(label="Raw Model Output", visible=False)
|
322 |
|
323 |
# --- Event Listeners ---
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
import os
|
4 |
+
import random
|
5 |
|
6 |
# A帽ade esto para verificar la versi贸n de Gradio en tiempo de ejecuci贸n
|
7 |
print(f"Gradio version at runtime: {gr.__version__}")
|
|
|
87 |
|
88 |
/* BOTONES PRINCIPALES */
|
89 |
.gr-button.primary {
|
90 |
+
background-color: #00BFFF !important; /* Azul Brillante para el bot贸n primario (Acento) */
|
91 |
color: #1E2B38 !important; /* Texto oscuro para el bot贸n primario */
|
92 |
border-radius: 5px;
|
93 |
transition: background-color 0.3s ease;
|
|
|
147 |
.sentiment-negative { background-color: #dc3545; }
|
148 |
.sentiment-neutral { background-color: #007BFF; }
|
149 |
|
150 |
+
/* NUEVOS BOTONES DE EJEMPLO DIRECTOS (NO gr.Examples) */
|
151 |
+
.example-button {
|
|
|
|
|
|
|
|
|
152 |
background-color: #4A5B6C !important; /* Gris Claro para los botones de ejemplo */
|
153 |
color: #FFFFFF !important;
|
154 |
border: 1px solid #4A5B6C;
|
|
|
159 |
font-size: 0.75em; /* Tama帽o de fuente m谩s peque帽o para los ejemplos */
|
160 |
white-space: nowrap; /* Evitar que el texto se rompa en varias l铆neas */
|
161 |
flex-shrink: 0; /* No permitir que se encojan si hay poco espacio */
|
162 |
+
cursor: pointer; /* Indicar que son clickeables */
|
163 |
}
|
164 |
+
.example-button:hover {
|
165 |
background-color: #007ACC !important; /* Azul Oscuro al pasar el rat贸n por los ejemplos */
|
166 |
border-color: #00BFFF !important;
|
167 |
}
|
|
|
|
|
|
|
168 |
|
169 |
/* L脥NEAS DIVISORIAS */
|
170 |
hr {
|
|
|
259 |
|
260 |
# --- Lista completa de ejemplos para selecci贸n aleatoria ---
|
261 |
ALL_EXAMPLES = [
|
262 |
+
"The product quality is absolutely outstanding and worth every penny!",
|
263 |
+
"I found the customer support unhelpful and quite rude, a terrible experience.",
|
264 |
+
"The new software update introduced several bugs, making it very unstable.",
|
265 |
+
"This book is a captivating read, I couldn't put it down!",
|
266 |
+
"The delivery was late, and the package arrived damaged.",
|
267 |
+
"Despite the bad reviews, I thoroughly enjoyed the film and its unique plot.",
|
268 |
+
"The instructions were unclear, leading to a lot of confusion during assembly.",
|
269 |
+
"What a delicious meal! Every dish was prepared to perfection.",
|
270 |
+
"I'm very disappointed with the recent policy changes; they are unfair.",
|
271 |
+
"The new art exhibition is thought-provoking and visually stunning.",
|
272 |
+
"Traffic was unexpectedly heavy, causing significant delays.",
|
273 |
+
"Overall, a solid performance, though there's room for improvement."
|
274 |
]
|
275 |
|
|
|
|
|
|
|
|
|
276 |
# --- Gradio Interface ---
|
277 |
with gr.Blocks(css=custom_css, theme=None) as demo:
|
278 |
gr.Markdown("<h1 style='color: #00BFFF;'>馃寣 Sentiment Analyzer 馃寣</h1>")
|
|
|
280 |
|
281 |
with gr.Column(elem_classes="gradio-container"):
|
282 |
text_input = gr.Textbox(
|
283 |
+
lines=2,
|
284 |
placeholder="Type your English text here...",
|
285 |
label="Your Text",
|
286 |
interactive=True,
|
287 |
+
value=random.choice(ALL_EXAMPLES) # Establece un ejemplo aleatorio inicial
|
|
|
288 |
)
|
289 |
analyze_btn = gr.Button("Analyze Sentiment", variant="primary")
|
290 |
|
291 |
+
# --- Ejemplos din谩micos como botones individuales ---
|
292 |
gr.Markdown("<hr>")
|
293 |
gr.Markdown("<h3 style='color: #00BFFF;'>Try examples:</h3>")
|
294 |
|
295 |
+
with gr.Row(wrap=True, elem_classes="example-buttons-row"): # wrap=True permite que los botones salten de l铆nea
|
296 |
+
# Generar 3 botones de ejemplo aleatorios
|
297 |
+
# Nota: Esto se genera una vez al inicio del Space. Para que cambie con cada recarga
|
298 |
+
# del usuario, se necesita un enfoque de state o una funci贸n para recargar la interfaz.
|
299 |
+
# Para un iframe simple, se recargar谩 al recargar la p谩gina web.
|
300 |
+
for example_text in random.sample(ALL_EXAMPLES, 3):
|
301 |
+
gr.Button(
|
302 |
+
example_text,
|
303 |
+
elem_classes="example-button" # Aplica el estilo definido en CSS
|
304 |
+
).click(
|
305 |
+
fn=lambda x: x, # Funci贸n simple para pasar el texto del bot贸n al textbox
|
306 |
+
inputs=[gr.State(example_text)], # Usamos gr.State para pasar el texto del bot贸n
|
307 |
+
outputs=text_input,
|
308 |
+
).then(
|
309 |
+
fn=analyze_sentiment, # Luego de cargar el texto, ejecuta el an谩lisis
|
310 |
+
inputs=text_input,
|
311 |
+
outputs=[overall_sentiment_output, confidence_scores_output, raw_output]
|
312 |
+
)
|
313 |
|
314 |
gr.Markdown("<hr>")
|
315 |
gr.Markdown("<h2 style='color: #00BFFF;'>馃搳 Results</h2>")
|
316 |
|
317 |
+
# Output principal de sentimiento que se expande horizontalmente
|
318 |
+
overall_sentiment_output = gr.HTML(label="Overall Sentiment", scale=3) # scale puede ayudar con el layout
|
319 |
+
# Estos se mantienen ocultos
|
320 |
+
confidence_scores_output = gr.Label(num_top_classes=3, label="Confidence Scores", visible=False)
|
321 |
raw_output = gr.JSON(label="Raw Model Output", visible=False)
|
322 |
|
323 |
# --- Event Listeners ---
|