Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,45 +24,45 @@ TICKERS = {
|
|
| 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)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
prices[company] = df_prices
|
| 37 |
|
| 38 |
-
# --- MERGE PRICES
|
| 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
|
| 44 |
-
df_merged
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
how='left'
|
| 48 |
-
)['Close'].values
|
| 49 |
|
| 50 |
# --- GRADIO FUNCTION ---
|
| 51 |
def show_sentiment(selected_companies, aggregation="Day"):
|
| 52 |
-
#
|
| 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 |
-
#
|
| 66 |
if aggregation == "Day":
|
| 67 |
group_col = "Day"
|
| 68 |
elif aggregation == "Month":
|
|
@@ -73,7 +73,7 @@ def show_sentiment(selected_companies, aggregation="Day"):
|
|
| 73 |
else:
|
| 74 |
group_col = "Day"
|
| 75 |
|
| 76 |
-
#
|
| 77 |
if selected_companies:
|
| 78 |
df_grouped = df_filtered.groupby([group_col, 'Company'])['Score'].sum().reset_index()
|
| 79 |
else:
|
|
@@ -85,15 +85,14 @@ def show_sentiment(selected_companies, aggregation="Day"):
|
|
| 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 |
-
#
|
| 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",
|
|
@@ -130,6 +129,4 @@ demo = gr.Interface(
|
|
| 130 |
description="Shows sentiment scores aggregated by day, month, or year. Positive = +score, Negative = -score, Neutral = 0."
|
| 131 |
)
|
| 132 |
|
| 133 |
-
demo.launch()
|
| 134 |
-
|
| 135 |
-
|
|
|
|
| 24 |
"NASDAQ": "^IXIC" # indice NASDAQ per confronto aggregato
|
| 25 |
}
|
| 26 |
|
| 27 |
+
# --- FETCH STOCK PRICES ROBUSTLY ---
|
| 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)
|
| 33 |
+
|
| 34 |
+
# Ensure 'Close' column exists
|
| 35 |
+
if 'Close' not in df_prices.columns:
|
| 36 |
+
if 'Adj Close' in df_prices.columns:
|
| 37 |
+
df_prices['Close'] = df_prices['Adj Close']
|
| 38 |
+
else:
|
| 39 |
+
df_prices['Close'] = None
|
| 40 |
+
|
| 41 |
+
df_prices = df_prices.reset_index()[['Date', 'Close']]
|
| 42 |
prices[company] = df_prices
|
| 43 |
|
| 44 |
+
# --- MERGE PRICES INTO DATASET ---
|
| 45 |
df_merged = df.copy()
|
| 46 |
for company, df_price in prices.items():
|
|
|
|
| 47 |
if company == "NASDAQ":
|
| 48 |
+
continue # NASDAQ only for comparison
|
| 49 |
+
mask = df_merged['Company'] == company
|
| 50 |
+
temp = df_merged[mask].merge(df_price, on='Date', how='left', suffixes=('', '_tmp'))
|
| 51 |
+
df_merged.loc[mask, 'Close'] = temp['Close'].values
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# --- GRADIO FUNCTION ---
|
| 54 |
def show_sentiment(selected_companies, aggregation="Day"):
|
| 55 |
+
# Filter selected companies
|
| 56 |
if selected_companies:
|
| 57 |
df_filtered = df_merged[df_merged['Company'].isin(selected_companies)].copy()
|
| 58 |
else:
|
| 59 |
df_filtered = df_merged.copy()
|
| 60 |
+
# Add NASDAQ data for general sentiment
|
|
|
|
|
|
|
| 61 |
df_nasdaq = prices['NASDAQ'].copy()
|
| 62 |
df_nasdaq['Company'] = "NASDAQ"
|
|
|
|
| 63 |
df_filtered = pd.concat([df_filtered, df_nasdaq], ignore_index=True, sort=False)
|
| 64 |
|
| 65 |
+
# Determine aggregation column
|
| 66 |
if aggregation == "Day":
|
| 67 |
group_col = "Day"
|
| 68 |
elif aggregation == "Month":
|
|
|
|
| 73 |
else:
|
| 74 |
group_col = "Day"
|
| 75 |
|
| 76 |
+
# Aggregate sentiment
|
| 77 |
if selected_companies:
|
| 78 |
df_grouped = df_filtered.groupby([group_col, 'Company'])['Score'].sum().reset_index()
|
| 79 |
else:
|
|
|
|
| 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 |
+
# Add stock price as secondary y-axis if available
|
| 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 |
fig.update_layout(
|
| 97 |
yaxis2=dict(
|
| 98 |
title="Stock Price",
|
|
|
|
| 129 |
description="Shows sentiment scores aggregated by day, month, or year. Positive = +score, Negative = -score, Neutral = 0."
|
| 130 |
)
|
| 131 |
|
| 132 |
+
demo.launch()
|
|
|
|
|
|