Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,54 +14,55 @@ df['Year'] = df['Date'].dt.year
|
|
| 14 |
df['Month'] = df['Date'].dt.to_period('M')
|
| 15 |
df['Day'] = df['Date'].dt.date
|
| 16 |
|
| 17 |
-
# --- TICKERS
|
| 18 |
TICKERS = {
|
| 19 |
"Tesla": "TSLA",
|
| 20 |
"Microsoft": "MSFT",
|
| 21 |
"Apple": "AAPL",
|
| 22 |
-
"Facebook": "META",
|
| 23 |
"Google": "GOOGL",
|
| 24 |
-
"NASDAQ": "^IXIC"
|
| 25 |
}
|
| 26 |
|
| 27 |
# --- FETCH STOCK PRICES ---
|
| 28 |
prices = {}
|
| 29 |
for company, ticker in TICKERS.items():
|
| 30 |
-
start_date = df['Date'].min()
|
| 31 |
end_date = pd.Timestamp.today()
|
| 32 |
df_prices = yf.download(ticker, start=start_date, end=end_date)[['Close']].reset_index()
|
|
|
|
|
|
|
| 33 |
df_prices['Date'] = pd.to_datetime(df_prices['Date'])
|
| 34 |
prices[company] = df_prices
|
| 35 |
|
| 36 |
-
# --- MERGE PRICES
|
| 37 |
df_merged = df.copy()
|
| 38 |
for company, df_price in prices.items():
|
|
|
|
| 39 |
if company == "NASDAQ":
|
| 40 |
continue # NASDAQ lo useremo solo per confronto aggregato
|
| 41 |
-
mask = df_merged[
|
| 42 |
-
|
| 43 |
-
df_merged[mask],
|
| 44 |
-
df_price,
|
| 45 |
on='Date',
|
| 46 |
how='left'
|
| 47 |
)['Close'].values
|
| 48 |
|
| 49 |
# --- GRADIO FUNCTION ---
|
| 50 |
def show_sentiment(selected_companies, aggregation="Day"):
|
|
|
|
| 51 |
if selected_companies:
|
| 52 |
df_filtered = df_merged[df_merged['Company'].isin(selected_companies)].copy()
|
| 53 |
else:
|
| 54 |
-
# Se non viene selezionata alcuna azienda, usa tutte le aziende per sentiment aggregato
|
| 55 |
df_filtered = df_merged.copy()
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
df_nasdaq =
|
| 60 |
-
df_nasdaq['Company'] =
|
| 61 |
-
df_nasdaq
|
| 62 |
df_filtered = pd.concat([df_filtered, df_nasdaq], ignore_index=True, sort=False)
|
| 63 |
|
| 64 |
-
#
|
| 65 |
if aggregation == "Day":
|
| 66 |
group_col = "Day"
|
| 67 |
elif aggregation == "Month":
|
|
@@ -72,54 +73,34 @@ def show_sentiment(selected_companies, aggregation="Day"):
|
|
| 72 |
else:
|
| 73 |
group_col = "Day"
|
| 74 |
|
| 75 |
-
#
|
| 76 |
-
if
|
| 77 |
-
df_grouped = df_filtered.groupby([group_col, 'Company']).
|
| 78 |
-
'Score': 'sum',
|
| 79 |
-
'Close': 'last'
|
| 80 |
-
}).reset_index()
|
| 81 |
else:
|
| 82 |
df_grouped = df_filtered.groupby([group_col, 'Company']).agg({
|
| 83 |
-
'Score':
|
| 84 |
-
'Close':
|
| 85 |
}).reset_index()
|
| 86 |
|
| 87 |
-
# ---
|
| 88 |
fig = px.line(df_grouped, x=group_col, y='Score', color='Company',
|
| 89 |
title=f"Sentiment Score by {aggregation} per Company")
|
| 90 |
-
|
| 91 |
-
#
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
df_c = df_grouped[df_grouped['Company']
|
| 95 |
-
|
| 96 |
-
x=df_c[group_col],
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
)
|
| 103 |
-
|
| 104 |
-
df_c = df_grouped[df_grouped['Company'] == company]
|
| 105 |
-
fig.add_scatter(
|
| 106 |
-
x=df_c[group_col],
|
| 107 |
-
y=df_c['Close'],
|
| 108 |
-
mode='lines',
|
| 109 |
-
name=f"{company} Price",
|
| 110 |
-
yaxis="y2",
|
| 111 |
-
line=dict(dash='dot')
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
fig.update_layout(
|
| 115 |
-
yaxis_title="Sentiment Score",
|
| 116 |
-
yaxis2=dict(
|
| 117 |
-
title="Stock Price / NASDAQ Index",
|
| 118 |
-
overlaying="y",
|
| 119 |
-
side="right"
|
| 120 |
-
),
|
| 121 |
-
hovermode="x unified"
|
| 122 |
-
)
|
| 123 |
|
| 124 |
return df_grouped.tail(30), fig
|
| 125 |
|
|
@@ -132,7 +113,7 @@ demo = gr.Interface(
|
|
| 132 |
gr.Dropdown(
|
| 133 |
choices=companies,
|
| 134 |
value=None,
|
| 135 |
-
label="Select Companies (leave empty for general sentiment)",
|
| 136 |
multiselect=True
|
| 137 |
),
|
| 138 |
gr.Radio(
|
|
@@ -143,11 +124,12 @@ demo = gr.Interface(
|
|
| 143 |
],
|
| 144 |
outputs=[
|
| 145 |
gr.Dataframe(label="Sentiment Table", type="pandas"),
|
| 146 |
-
gr.Plot(label="Sentiment
|
| 147 |
],
|
| 148 |
-
title="Dynamic Sentiment
|
| 149 |
-
description="Shows sentiment scores aggregated by day, month, or year.
|
| 150 |
)
|
| 151 |
|
| 152 |
demo.launch()
|
| 153 |
|
|
|
|
|
|
| 14 |
df['Month'] = df['Date'].dt.to_period('M')
|
| 15 |
df['Day'] = df['Date'].dt.date
|
| 16 |
|
| 17 |
+
# --- TICKERS ---
|
| 18 |
TICKERS = {
|
| 19 |
"Tesla": "TSLA",
|
| 20 |
"Microsoft": "MSFT",
|
| 21 |
"Apple": "AAPL",
|
| 22 |
+
"Facebook": "META",
|
| 23 |
"Google": "GOOGL",
|
| 24 |
+
"NASDAQ": "^IXIC" # indice NASDAQ per confronto aggregato
|
| 25 |
}
|
| 26 |
|
| 27 |
# --- FETCH STOCK PRICES ---
|
| 28 |
prices = {}
|
| 29 |
for company, ticker in TICKERS.items():
|
| 30 |
+
start_date = df['Date'].min()
|
| 31 |
end_date = pd.Timestamp.today()
|
| 32 |
df_prices = yf.download(ticker, start=start_date, end=end_date)[['Close']].reset_index()
|
| 33 |
+
# Flatten multiindex columns if present
|
| 34 |
+
df_prices.columns = [col if not isinstance(col, tuple) else '_'.join(col) for col in df_prices.columns]
|
| 35 |
df_prices['Date'] = pd.to_datetime(df_prices['Date'])
|
| 36 |
prices[company] = df_prices
|
| 37 |
|
| 38 |
+
# --- MERGE PRICES IN DATASET ---
|
| 39 |
df_merged = df.copy()
|
| 40 |
for company, df_price in prices.items():
|
| 41 |
+
mask = df_merged['Company'] == company
|
| 42 |
if company == "NASDAQ":
|
| 43 |
continue # NASDAQ lo useremo solo per confronto aggregato
|
| 44 |
+
df_merged.loc[mask, 'Close'] = df_merged[mask].merge(
|
| 45 |
+
df_price[['Date','Close']],
|
|
|
|
|
|
|
| 46 |
on='Date',
|
| 47 |
how='left'
|
| 48 |
)['Close'].values
|
| 49 |
|
| 50 |
# --- GRADIO FUNCTION ---
|
| 51 |
def show_sentiment(selected_companies, aggregation="Day"):
|
| 52 |
+
# filtro aziende selezionate
|
| 53 |
if selected_companies:
|
| 54 |
df_filtered = df_merged[df_merged['Company'].isin(selected_companies)].copy()
|
| 55 |
else:
|
|
|
|
| 56 |
df_filtered = df_merged.copy()
|
| 57 |
+
|
| 58 |
+
# aggiungi NASDAQ se non selezionata nessuna azienda
|
| 59 |
+
if not selected_companies:
|
| 60 |
+
df_nasdaq = prices['NASDAQ'].copy()
|
| 61 |
+
df_nasdaq['Company'] = "NASDAQ"
|
| 62 |
+
df_nasdaq.rename(columns={'Close':'Close'}, inplace=True)
|
| 63 |
df_filtered = pd.concat([df_filtered, df_nasdaq], ignore_index=True, sort=False)
|
| 64 |
|
| 65 |
+
# scelta colonna per aggregazione
|
| 66 |
if aggregation == "Day":
|
| 67 |
group_col = "Day"
|
| 68 |
elif aggregation == "Month":
|
|
|
|
| 73 |
else:
|
| 74 |
group_col = "Day"
|
| 75 |
|
| 76 |
+
# raggruppamento per sentiment
|
| 77 |
+
if selected_companies:
|
| 78 |
+
df_grouped = df_filtered.groupby([group_col, 'Company'])['Score'].sum().reset_index()
|
|
|
|
|
|
|
|
|
|
| 79 |
else:
|
| 80 |
df_grouped = df_filtered.groupby([group_col, 'Company']).agg({
|
| 81 |
+
'Score':'sum',
|
| 82 |
+
'Close':'last'
|
| 83 |
}).reset_index()
|
| 84 |
|
| 85 |
+
# --- PLOT ---
|
| 86 |
fig = px.line(df_grouped, x=group_col, y='Score', color='Company',
|
| 87 |
title=f"Sentiment Score by {aggregation} per Company")
|
| 88 |
+
|
| 89 |
+
# aggiungi linea prezzi secondaria se disponibile
|
| 90 |
+
if 'Close' in df_grouped.columns:
|
| 91 |
+
for c in df_grouped['Company'].unique():
|
| 92 |
+
df_c = df_grouped[df_grouped['Company']==c]
|
| 93 |
+
if df_c['Close'].notnull().any():
|
| 94 |
+
fig.add_scatter(x=df_c[group_col], y=df_c['Close'], mode='lines',
|
| 95 |
+
name=f"{c} Price", yaxis="y2", line=dict(dash='dot'))
|
| 96 |
+
|
| 97 |
+
fig.update_layout(
|
| 98 |
+
yaxis2=dict(
|
| 99 |
+
title="Stock Price",
|
| 100 |
+
overlaying="y",
|
| 101 |
+
side="right"
|
| 102 |
)
|
| 103 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
return df_grouped.tail(30), fig
|
| 106 |
|
|
|
|
| 113 |
gr.Dropdown(
|
| 114 |
choices=companies,
|
| 115 |
value=None,
|
| 116 |
+
label="Select Companies (leave empty for general sentiment with NASDAQ)",
|
| 117 |
multiselect=True
|
| 118 |
),
|
| 119 |
gr.Radio(
|
|
|
|
| 124 |
],
|
| 125 |
outputs=[
|
| 126 |
gr.Dataframe(label="Sentiment Table", type="pandas"),
|
| 127 |
+
gr.Plot(label="Sentiment Trend"),
|
| 128 |
],
|
| 129 |
+
title="Dynamic Sentiment Dashboard",
|
| 130 |
+
description="Shows sentiment scores aggregated by day, month, or year. Positive = +score, Negative = -score, Neutral = 0."
|
| 131 |
)
|
| 132 |
|
| 133 |
demo.launch()
|
| 134 |
|
| 135 |
+
|