Spaces:
Runtime error
Runtime error
Commit
·
fedcae5
1
Parent(s):
517309b
Adding pie chart
Browse files
app.py
CHANGED
|
@@ -43,14 +43,26 @@ def analyze_sentiment(text):
|
|
| 43 |
else:
|
| 44 |
return "Unknown" # O algún valor por defecto para datos no procesables
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
def sentiment_counts_by_category(category):
|
| 47 |
df_category = load_reviews(category)
|
| 48 |
-
df_category = df_category.head(30)
|
| 49 |
-
# Aplica la función de análisis de sentimientos modificada
|
| 50 |
df_category['sentiment'] = df_category['reviewText'].apply(analyze_sentiment)
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
def show_first_five(category):
|
| 56 |
df_category = load_reviews(category)
|
|
@@ -71,10 +83,11 @@ with gr.Blocks() as demo:
|
|
| 71 |
plot_button = gr.Button("Graficar Distribución de Sentimientos")
|
| 72 |
with gr.Column():
|
| 73 |
output_df = gr.Dataframe()
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
show_button.click(show_first_five, inputs=category, outputs=output_df)
|
| 77 |
-
plot_button.click(sentiment_counts_by_category, inputs=category, outputs=
|
| 78 |
|
| 79 |
# Lanza la interfaz
|
| 80 |
if __name__ == "__main__":
|
|
|
|
| 43 |
else:
|
| 44 |
return "Unknown" # O algún valor por defecto para datos no procesables
|
| 45 |
|
| 46 |
+
def plot_pie_chart(sentiment_counts):
|
| 47 |
+
labels = sentiment_counts.keys()
|
| 48 |
+
sizes = sentiment_counts.values()
|
| 49 |
+
fig, ax = plt.subplots()
|
| 50 |
+
ax.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90)
|
| 51 |
+
ax.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle.
|
| 52 |
+
return fig
|
| 53 |
+
|
| 54 |
def sentiment_counts_by_category(category):
|
| 55 |
df_category = load_reviews(category)
|
| 56 |
+
df_category = df_category.head(30) # Limita el análisis a las primeras 30 reseñas
|
|
|
|
| 57 |
df_category['sentiment'] = df_category['reviewText'].apply(analyze_sentiment)
|
| 58 |
+
# Conteo de sentimientos
|
| 59 |
+
sentiment_counts = df_category['sentiment'].value_counts(normalize=True) # Obtiene porcentajes
|
| 60 |
+
# Genera la gráfica de barras para la distribución de sentimientos
|
| 61 |
+
bar_fig = plot_sentiment_distribution(df_category)
|
| 62 |
+
# Genera la gráfica de pastel para la distribución porcentual de sentimientos
|
| 63 |
+
pie_fig = plot_pie_chart(sentiment_counts)
|
| 64 |
+
# Retorna ambas figuras
|
| 65 |
+
return bar_fig, pie_fig
|
| 66 |
|
| 67 |
def show_first_five(category):
|
| 68 |
df_category = load_reviews(category)
|
|
|
|
| 83 |
plot_button = gr.Button("Graficar Distribución de Sentimientos")
|
| 84 |
with gr.Column():
|
| 85 |
output_df = gr.Dataframe()
|
| 86 |
+
output_bar_plot = gr.Plot()
|
| 87 |
+
output_pie_plot = gr.Plot()
|
| 88 |
|
| 89 |
show_button.click(show_first_five, inputs=category, outputs=output_df)
|
| 90 |
+
plot_button.click(sentiment_counts_by_category, inputs=category, outputs=[output_bar_plot, output_pie_plot])
|
| 91 |
|
| 92 |
# Lanza la interfaz
|
| 93 |
if __name__ == "__main__":
|