Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
from transformers import pipeline
|
4 |
-
import matplotlib.pyplot as plt
|
5 |
-
import io
|
6 |
-
import base64
|
7 |
|
8 |
# Configurar el clasificador de sentimientos multiling眉e
|
9 |
classifier = pipeline(task="zero-shot-classification", model="facebook/bart-large-mnli")
|
@@ -11,11 +8,11 @@ classifier = pipeline(task="zero-shot-classification", model="facebook/bart-larg
|
|
11 |
# Funci贸n para analizar los sentimientos de una lista de textos
|
12 |
def analyze_sentiments(texts):
|
13 |
if not texts:
|
14 |
-
return "0.0%", "0.0%", "0.0%"
|
15 |
|
16 |
positive, negative, neutral = 0, 0, 0
|
17 |
for text in texts:
|
18 |
-
results = classifier(text,
|
19 |
mx = max(results['scores'])
|
20 |
ind = results['scores'].index(mx)
|
21 |
result = results['labels'][ind]
|
@@ -29,23 +26,7 @@ def analyze_sentiments(texts):
|
|
29 |
positive_percent = round((positive / total) * 100, 1)
|
30 |
negative_percent = round((negative / total) * 100, 1)
|
31 |
neutral_percent = round((neutral / total) * 100, 1)
|
32 |
-
|
33 |
-
# Crear el gr谩fico circular
|
34 |
-
labels = 'Positivo', 'Negativo', 'Neutral'
|
35 |
-
sizes = [positive_percent, negative_percent, neutral_percent]
|
36 |
-
colors = ['#ff9999','#66b3ff','#99ff99']
|
37 |
-
fig, ax = plt.subplots()
|
38 |
-
ax.pie(sizes, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
|
39 |
-
ax.axis('equal') # Para asegurar que el pie sea circular
|
40 |
-
|
41 |
-
# Guardar el gr谩fico en un buffer de memoria
|
42 |
-
buf = io.BytesIO()
|
43 |
-
plt.savefig(buf, format='png')
|
44 |
-
buf.seek(0)
|
45 |
-
img_base64 = base64.b64encode(buf.read()).decode('utf-8')
|
46 |
-
buf.close()
|
47 |
-
|
48 |
-
return f"{positive_percent}%", f"{negative_percent}%", f"{neutral_percent}%", img_base64
|
49 |
|
50 |
# Funci贸n para cargar el archivo CSV y analizar los primeros 100 comentarios
|
51 |
def analyze_sentiment_from_csv(file):
|
@@ -56,9 +37,9 @@ def analyze_sentiment_from_csv(file):
|
|
56 |
texts = df['content'].head(100).tolist() # Tomar solo los primeros 100 comentarios
|
57 |
return analyze_sentiments(texts)
|
58 |
except pd.errors.ParserError as e:
|
59 |
-
return f"Error al analizar el archivo CSV: {e}", "", ""
|
60 |
except Exception as e:
|
61 |
-
return f"Error inesperado: {e}", "", ""
|
62 |
|
63 |
# Configurar la interfaz de Gradio
|
64 |
demo = gr.Interface(
|
@@ -67,14 +48,12 @@ demo = gr.Interface(
|
|
67 |
outputs=[
|
68 |
gr.Textbox(label="Porcentaje Positivo"),
|
69 |
gr.Textbox(label="Porcentaje Negativo"),
|
70 |
-
gr.Textbox(label="Porcentaje Neutro")
|
71 |
-
gr.Image(label="Gr谩fico de Resultados") # A帽adir un output para el gr谩fico
|
72 |
],
|
73 |
title="Analizador de Sentimientos V.2",
|
74 |
-
|
75 |
)
|
76 |
|
77 |
demo.launch(share=True)
|
78 |
|
79 |
|
80 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import pandas as pd
|
3 |
from transformers import pipeline
|
|
|
|
|
|
|
4 |
|
5 |
# Configurar el clasificador de sentimientos multiling眉e
|
6 |
classifier = pipeline(task="zero-shot-classification", model="facebook/bart-large-mnli")
|
|
|
8 |
# Funci贸n para analizar los sentimientos de una lista de textos
|
9 |
def analyze_sentiments(texts):
|
10 |
if not texts:
|
11 |
+
return "0.0%", "0.0%", "0.0%" # Manejar el caso donde no hay textos para analizar
|
12 |
|
13 |
positive, negative, neutral = 0, 0, 0
|
14 |
for text in texts:
|
15 |
+
results = classifier(text, ["positive", "negative", "neutral"], multi_label=True)
|
16 |
mx = max(results['scores'])
|
17 |
ind = results['scores'].index(mx)
|
18 |
result = results['labels'][ind]
|
|
|
26 |
positive_percent = round((positive / total) * 100, 1)
|
27 |
negative_percent = round((negative / total) * 100, 1)
|
28 |
neutral_percent = round((neutral / total) * 100, 1)
|
29 |
+
return f"{positive_percent}%", f"{negative_percent}%", f"{neutral_percent}%"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Funci贸n para cargar el archivo CSV y analizar los primeros 100 comentarios
|
32 |
def analyze_sentiment_from_csv(file):
|
|
|
37 |
texts = df['content'].head(100).tolist() # Tomar solo los primeros 100 comentarios
|
38 |
return analyze_sentiments(texts)
|
39 |
except pd.errors.ParserError as e:
|
40 |
+
return f"Error al analizar el archivo CSV: {e}", "", ""
|
41 |
except Exception as e:
|
42 |
+
return f"Error inesperado: {e}", "", ""
|
43 |
|
44 |
# Configurar la interfaz de Gradio
|
45 |
demo = gr.Interface(
|
|
|
48 |
outputs=[
|
49 |
gr.Textbox(label="Porcentaje Positivo"),
|
50 |
gr.Textbox(label="Porcentaje Negativo"),
|
51 |
+
gr.Textbox(label="Porcentaje Neutro")
|
|
|
52 |
],
|
53 |
title="Analizador de Sentimientos V.2",
|
54 |
+
description="Porcentaje de comentarios positivos, negativos y neutrales"
|
55 |
)
|
56 |
|
57 |
demo.launch(share=True)
|
58 |
|
59 |
|
|