Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,71 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
img_base64 = base64.b64encode(buf.read()).decode('utf-8')
|
29 |
-
buf.close()
|
30 |
|
31 |
-
|
|
|
|
|
32 |
|
33 |
-
return
|
34 |
|
35 |
-
# Función para
|
36 |
-
def
|
37 |
-
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
# Configurar la interfaz de Gradio
|
41 |
demo = gr.Interface(
|
42 |
-
fn=
|
43 |
-
inputs=
|
44 |
-
|
45 |
-
gr.
|
46 |
-
gr.
|
|
|
|
|
47 |
],
|
48 |
-
|
49 |
-
|
50 |
-
description="Genera un gráfico de barras basado en los valores de A, B y C"
|
51 |
)
|
52 |
|
53 |
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from transformers import pipeline
|
4 |
+
from collections import Counter
|
5 |
+
import re
|
6 |
+
|
7 |
+
# Configurar el clasificador de sentimientos multilingüe
|
8 |
+
classifier = pipeline(task="zero-shot-classification", model="facebook/bart-large-mnli")
|
9 |
+
|
10 |
+
# Función para analizar los sentimientos de una lista de textos
|
11 |
+
def analyze_sentiments(texts):
|
12 |
+
if not texts:
|
13 |
+
return "0.0%", "0.0%", "0.0%", [] # Manejar el caso donde no hay textos para analizar
|
14 |
+
|
15 |
+
positive, negative, neutral = 0, 0, 0
|
16 |
+
all_words = []
|
17 |
+
for text in texts:
|
18 |
+
results = classifier(text, ["positive", "negative", "neutral"], multi_label=True)
|
19 |
+
mx = max(results['scores'])
|
20 |
+
ind = results['scores'].index(mx)
|
21 |
+
result = results['labels'][ind]
|
22 |
+
if result == "positive":
|
23 |
+
positive += 1
|
24 |
+
elif result == "negative":
|
25 |
+
negative += 1
|
26 |
+
else:
|
27 |
+
neutral += 1
|
28 |
+
|
29 |
+
# Procesar palabras del texto
|
30 |
+
words = re.findall(r'\w+', text.lower())
|
31 |
+
all_words.extend(words)
|
32 |
|
33 |
+
total = len(texts)
|
34 |
+
positive_percent = round((positive / total) * 100, 1)
|
35 |
+
negative_percent = round((negative / total) * 100, 1)
|
36 |
+
neutral_percent = round((neutral / total) * 100, 1)
|
|
|
|
|
37 |
|
38 |
+
# Contar las palabras más comunes
|
39 |
+
word_counts = Counter(all_words)
|
40 |
+
most_common_words = word_counts.most_common(10) # Obtener las 10 palabras más comunes
|
41 |
|
42 |
+
return f"{positive_percent}%", f"{negative_percent}%", f"{neutral_percent}%", most_common_words
|
43 |
|
44 |
+
# Función para cargar el archivo CSV y analizar los primeros 100 comentarios
|
45 |
+
def analyze_sentiment_from_csv(file):
|
46 |
+
try:
|
47 |
+
df = pd.read_csv(file.name)
|
48 |
+
if 'content' not in df.columns:
|
49 |
+
raise ValueError("El archivo CSV no contiene una columna 'content'")
|
50 |
+
texts = df['content'].head(100).tolist() # Tomar solo los primeros 100 comentarios
|
51 |
+
return analyze_sentiments(texts)
|
52 |
+
except pd.errors.ParserError as e:
|
53 |
+
return f"Error al analizar el archivo CSV: {e}", "", "", []
|
54 |
+
except Exception as e:
|
55 |
+
return f"Error inesperado: {e}", "", "", []
|
56 |
|
57 |
# Configurar la interfaz de Gradio
|
58 |
demo = gr.Interface(
|
59 |
+
fn=analyze_sentiment_from_csv,
|
60 |
+
inputs=gr.File(file_count="single", label="Sube tu archivo CSV"), # Permitir la carga del archivo CSV
|
61 |
+
outputs=[
|
62 |
+
gr.Textbox(label="Porcentaje Positivo"),
|
63 |
+
gr.Textbox(label="Porcentaje Negativo"),
|
64 |
+
gr.Textbox(label="Porcentaje Neutro"),
|
65 |
+
gr.Textbox(label="Palabras Más Usadas")
|
66 |
],
|
67 |
+
title="Analizador de Sentimientos V.2",
|
68 |
+
description="Porcentaje de comentarios positivos, negativos y neutrales, y palabras más usadas"
|
|
|
69 |
)
|
70 |
|
71 |
demo.launch(share=True)
|