Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 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,7 +9,7 @@ classifier = pipeline(task="zero-shot-classification", model="facebook/bart-larg
|
|
| 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:
|
|
@@ -26,7 +27,15 @@ def analyze_sentiments(texts):
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# Funci贸n para cargar el archivo CSV y analizar los primeros 100 comentarios
|
| 32 |
def analyze_sentiment_from_csv(file):
|
|
@@ -37,9 +46,9 @@ 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,12 +57,12 @@ 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.
|
| 54 |
-
|
| 55 |
)
|
| 56 |
|
| 57 |
demo.launch(share=True)
|
| 58 |
|
| 59 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
from transformers import pipeline
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
|
| 6 |
# Configurar el clasificador de sentimientos multiling眉e
|
| 7 |
classifier = pipeline(task="zero-shot-classification", model="facebook/bart-large-mnli")
|
|
|
|
| 9 |
# Funci贸n para analizar los sentimientos de una lista de textos
|
| 10 |
def analyze_sentiments(texts):
|
| 11 |
if not texts:
|
| 12 |
+
return "0.0%", "0.0%", "0.0%", None # Manejar el caso donde no hay textos para analizar
|
| 13 |
|
| 14 |
positive, negative, neutral = 0, 0, 0
|
| 15 |
for text in texts:
|
|
|
|
| 27 |
positive_percent = round((positive / total) * 100, 1)
|
| 28 |
negative_percent = round((negative / total) * 100, 1)
|
| 29 |
neutral_percent = round((neutral / total) * 100, 1)
|
| 30 |
+
|
| 31 |
+
# Crear el gr谩fico circular
|
| 32 |
+
fig, ax = plt.subplots()
|
| 33 |
+
ax.pie([positive_percent, negative_percent, neutral_percent], labels=["Positivo", "Negativo", "Neutro"], autopct='%1.1f%%', colors=['green', 'red', 'blue'])
|
| 34 |
+
plt.title("Distribuci贸n de Sentimientos")
|
| 35 |
+
plt.savefig("sentiment_pie_chart.png")
|
| 36 |
+
plt.close(fig)
|
| 37 |
+
|
| 38 |
+
return f"{positive_percent}%", f"{negative_percent}%", f"{neutral_percent}%", "sentiment_pie_chart.png"
|
| 39 |
|
| 40 |
# Funci贸n para cargar el archivo CSV y analizar los primeros 100 comentarios
|
| 41 |
def analyze_sentiment_from_csv(file):
|
|
|
|
| 46 |
texts = df['content'].head(100).tolist() # Tomar solo los primeros 100 comentarios
|
| 47 |
return analyze_sentiments(texts)
|
| 48 |
except pd.errors.ParserError as e:
|
| 49 |
+
return f"Error al analizar el archivo CSV: {e}", "", "", None
|
| 50 |
except Exception as e:
|
| 51 |
+
return f"Error inesperado: {e}", "", "", None
|
| 52 |
|
| 53 |
# Configurar la interfaz de Gradio
|
| 54 |
demo = gr.Interface(
|
|
|
|
| 57 |
outputs=[
|
| 58 |
gr.Textbox(label="Porcentaje Positivo"),
|
| 59 |
gr.Textbox(label="Porcentaje Negativo"),
|
| 60 |
+
gr.Textbox(label="Porcentaje Neutro"),
|
| 61 |
+
gr.Image(type="filepath", label="Gr谩fico de Sentimientos")
|
| 62 |
],
|
| 63 |
+
title="Analizador de Sentimientos V.3",
|
| 64 |
+
description="Porcentaje de comentarios positivos, negativos y neutrales. Y GRAFICO CIRCULAR"
|
| 65 |
)
|
| 66 |
|
| 67 |
demo.launch(share=True)
|
| 68 |
|
|
|