Spaces:
Runtime error
Runtime error
Commit
·
3d2732d
1
Parent(s):
9b4ec2e
Making everything look fine (using valid files and valid app.py)
Browse files- All_Beauty_5.json.gz +0 -0
- Appliances_5.json.gz +0 -0
- Gift_Cards_5.json.gz +0 -0
- Magazine_Subscriptions_5.json.gz +0 -0
- app.py +46 -27
- requirements.txt +1 -0
All_Beauty_5.json.gz
ADDED
|
Binary file (634 kB). View file
|
|
|
Appliances_5.json.gz
ADDED
|
Binary file (73 kB). View file
|
|
|
Gift_Cards_5.json.gz
ADDED
|
Binary file (177 kB). View file
|
|
|
Magazine_Subscriptions_5.json.gz
ADDED
|
Binary file (402 kB). View file
|
|
|
app.py
CHANGED
|
@@ -5,40 +5,59 @@ from transformers import pipeline
|
|
| 5 |
# Inicializa la pipeline de análisis de sentimientos
|
| 6 |
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 7 |
|
| 8 |
-
# Función
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
# Función para
|
| 15 |
-
def
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
return result[0]['label'], round(result[0]['score'], 4)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# Función para
|
| 25 |
-
def
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Crear la interfaz usando gr.Blocks
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
with gr.Row():
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
analyze_button.click(analyze_sentiment, inputs=input_review, outputs=[output_label, output_score])
|
| 41 |
-
show_button.click(show_sample, inputs=None, outputs=output_df)
|
| 42 |
|
| 43 |
# Lanza la interfaz
|
| 44 |
if __name__ == "__main__":
|
|
|
|
| 5 |
# Inicializa la pipeline de análisis de sentimientos
|
| 6 |
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 7 |
|
| 8 |
+
# Función que toma un Dataframe y crea un gráfico visual con matplotlib
|
| 9 |
+
def plot_sentiment_distribution(df_category):
|
| 10 |
+
sentiment_counts = df_category['sentiment'].value_counts()
|
| 11 |
+
fig, ax = plt.subplots()
|
| 12 |
+
sentiment_counts.plot(kind='bar', ax=ax)
|
| 13 |
+
ax.set_title("Distribución de Sentimientos")
|
| 14 |
+
ax.set_xlabel("Sentimiento")
|
| 15 |
+
ax.set_ylabel("Número de reseñas")
|
| 16 |
+
return fig
|
| 17 |
+
|
| 18 |
+
# Mapeo de categorías a nombres de archivos
|
| 19 |
+
category_to_file_path = {
|
| 20 |
+
'Fashion': 'AMAZON_FASHION_5.json.gz',
|
| 21 |
+
'Appliances': 'Appliances_5.json.gz',
|
| 22 |
+
'Gift Cards': 'Gift_Cards_5.json.gz',
|
| 23 |
+
'Magazine subscriptions': 'Magazine_Subscriptions_5.json.gz',
|
| 24 |
+
'All beauty': 'All_Beauty_5.json.gz'
|
| 25 |
+
}
|
| 26 |
|
| 27 |
+
# Función para cargar los datos por categoría
|
| 28 |
+
def load_reviews(category):
|
| 29 |
+
# Asegúrate de que la categoría proporcionada es válida
|
| 30 |
+
if category not in category_to_file_path:
|
| 31 |
+
raise ValueError("Categoría no encontrada. Asegúrate de que la categoría sea correcta.")
|
|
|
|
| 32 |
|
| 33 |
+
file_name = category_to_file_path[category]
|
| 34 |
+
file_path = f"{file_name}" # Asegúrate de que este sea el path correcto a tus archivos
|
| 35 |
+
df = pd.read_json(file_path, lines=True, compression='gzip')
|
| 36 |
+
return df
|
| 37 |
|
| 38 |
+
# Función que realiza el análisis de sentimientos para una categoría específica
|
| 39 |
+
def sentiment_counts_by_category(category):
|
| 40 |
+
df_category = load_reviews(category)
|
| 41 |
+
df_category['sentiment'] = df_category['reviewText'].apply(lambda x: sentiment_pipeline(x[:512])[0]['label'])
|
| 42 |
+
# Ahora llama a la función de trazado y devuelve el gráfico
|
| 43 |
+
fig = plot_sentiment_distribution(df_category)
|
| 44 |
+
return fig
|
| 45 |
+
|
| 46 |
+
def show_first_five(category):
|
| 47 |
+
df_category = load_reviews(category)
|
| 48 |
+
return df_category.head(5) # Muestra las primeras 5 filas del DataFrame de la categoría
|
| 49 |
|
| 50 |
# Crear la interfaz usando gr.Blocks
|
| 51 |
with gr.Blocks() as demo:
|
| 52 |
with gr.Row():
|
| 53 |
+
category = gr.Dropdown(choices=list(category_to_file_path.keys()), label="Seleccione una categoría")
|
| 54 |
+
show_button = gr.Button("Mostrar Datos")
|
| 55 |
+
plot_button = gr.Button("Graficar Distribución de Sentimientos")
|
| 56 |
+
output_df = gr.Dataframe()
|
| 57 |
+
output_plot = gr.Plot()
|
| 58 |
+
|
| 59 |
+
show_button.click(show_first_five, inputs=category, outputs=output_df)
|
| 60 |
+
plot_button.click(sentiment_counts_by_category, inputs=category, outputs=output_plot)
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# Lanza la interfaz
|
| 63 |
if __name__ == "__main__":
|
requirements.txt
CHANGED
|
@@ -2,3 +2,4 @@ torch
|
|
| 2 |
gradio
|
| 3 |
pandas
|
| 4 |
transformers
|
|
|
|
|
|
| 2 |
gradio
|
| 3 |
pandas
|
| 4 |
transformers
|
| 5 |
+
matplotlib
|