Spaces:
Running
Running
Upload 3 files
Browse files- app.py +75 -0
- requirements.txt +4 -0
- sa-dataset.csv +0 -0
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import plotly.express as px
|
5 |
+
from itertools import combinations
|
6 |
+
from collections import Counter
|
7 |
+
|
8 |
+
# Caricamento del dataset
|
9 |
+
def load_data(file):
|
10 |
+
df = pd.read_csv(file.name)
|
11 |
+
df['anno_pubblicazione'] = pd.to_numeric(df['anno_pubblicazione'], errors='coerce')
|
12 |
+
df['num_parole'] = pd.to_numeric(df['num_parole'], errors='coerce')
|
13 |
+
return df
|
14 |
+
|
15 |
+
# Funzione per calcolare la co-occorrenza delle parole chiave
|
16 |
+
def calcola_cooccorrenza_keywords(df, top_n=30):
|
17 |
+
keywords_list = df['keyword'].dropna().str.split(', ')
|
18 |
+
all_keywords = [kw for sublist in keywords_list for kw in sublist]
|
19 |
+
common_keywords = Counter(all_keywords).most_common(top_n)
|
20 |
+
common_keywords = [kw[0] for kw in common_keywords]
|
21 |
+
|
22 |
+
cooccurrence = Counter()
|
23 |
+
for keywords in keywords_list:
|
24 |
+
filtered_keywords = [kw for kw in keywords if kw in common_keywords]
|
25 |
+
cooccurrence.update(combinations(sorted(filtered_keywords), 2))
|
26 |
+
|
27 |
+
co_matrix = pd.DataFrame(np.zeros((len(common_keywords), len(common_keywords))),
|
28 |
+
index=common_keywords, columns=common_keywords)
|
29 |
+
for (kw1, kw2), count in cooccurrence.items():
|
30 |
+
co_matrix.loc[kw1, kw2] = count
|
31 |
+
co_matrix.loc[kw2, kw1] = count
|
32 |
+
|
33 |
+
fig = px.imshow(co_matrix, x=co_matrix.columns, y=co_matrix.index,
|
34 |
+
color_continuous_scale='Blues', labels=dict(color="Co-occorrenza"),
|
35 |
+
title="Mappa di Co-occorrenza delle Parole Chiave")
|
36 |
+
|
37 |
+
return fig
|
38 |
+
|
39 |
+
# Funzione per calcolare il trend delle keyword
|
40 |
+
def calcola_trend_keyword(df, top_n=10):
|
41 |
+
trend_df = df[['anno_pubblicazione', 'keyword']].dropna()
|
42 |
+
trend_df['keyword'] = trend_df['keyword'].str.split(', ')
|
43 |
+
trend_df = trend_df.explode('keyword')
|
44 |
+
keyword_counts = trend_df['keyword'].value_counts().head(top_n).index
|
45 |
+
trend_df = trend_df[trend_df['keyword'].isin(keyword_counts)]
|
46 |
+
trend_df = trend_df.groupby(['anno_pubblicazione', 'keyword']).size().reset_index(name='count')
|
47 |
+
|
48 |
+
fig = px.line(trend_df, x='anno_pubblicazione', y='count', color='keyword',
|
49 |
+
title='Trend Temporale delle Keyword',
|
50 |
+
labels={'anno_pubblicazione': 'Anno', 'count': 'Frequenza'})
|
51 |
+
|
52 |
+
return fig
|
53 |
+
|
54 |
+
# Funzione per visualizzare i grafici dopo il caricamento del file
|
55 |
+
def visualizza_grafici(file):
|
56 |
+
df = load_data(file)
|
57 |
+
fig1 = calcola_cooccorrenza_keywords(df)
|
58 |
+
fig2 = calcola_trend_keyword(df)
|
59 |
+
return fig1, fig2
|
60 |
+
|
61 |
+
# Creazione dell'interfaccia Gradio
|
62 |
+
with gr.Blocks() as app:
|
63 |
+
gr.Markdown("## Dashboard per l'Analisi dei Dati degli Articoli di Storia dell'Arte")
|
64 |
+
|
65 |
+
file_input = gr.File(label="Carica il file CSV con i dati")
|
66 |
+
|
67 |
+
output1 = gr.Plot(label="Mappa di Co-occorrenza delle Parole Chiave")
|
68 |
+
output2 = gr.Plot(label="Trend Temporale delle Keyword")
|
69 |
+
|
70 |
+
btn = gr.Button("Analizza")
|
71 |
+
btn.click(visualizza_grafici, inputs=file_input, outputs=[output1, output2])
|
72 |
+
|
73 |
+
# Avvio dell'interfaccia
|
74 |
+
if __name__ == "__main__":
|
75 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
pandas
|
3 |
+
numpy
|
4 |
+
plotly
|
sa-dataset.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|