Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import pandas as pd
|
|
| 2 |
import yfinance as yf
|
| 3 |
from datasets import load_dataset
|
| 4 |
from transformers import pipeline
|
| 5 |
-
import plotly.express as px
|
| 6 |
import plotly.graph_objects as go
|
| 7 |
import gradio as gr
|
| 8 |
|
|
@@ -16,7 +15,7 @@ MODEL_FINBERT = "ProsusAI/finbert"
|
|
| 16 |
TICKERS = {
|
| 17 |
"Tesla": "TSLA",
|
| 18 |
"Microsoft": "MSFT",
|
| 19 |
-
|
| 20 |
}
|
| 21 |
companies = list(TICKERS.keys())
|
| 22 |
|
|
@@ -42,7 +41,7 @@ df_multi['FinBERT_Confidence'] = 0.0
|
|
| 42 |
for i, row in df_multi.iterrows():
|
| 43 |
company = row['Company']
|
| 44 |
|
| 45 |
-
#
|
| 46 |
try:
|
| 47 |
res = sentiment_pipeline(row['Summary'])[0]
|
| 48 |
df_multi.at[i,'Sentiment'] = res['label'].upper().strip()
|
|
@@ -51,7 +50,7 @@ for i, row in df_multi.iterrows():
|
|
| 51 |
df_multi.at[i,'Sentiment'] = 'ERROR'
|
| 52 |
df_multi.at[i,'Confidence'] = 0.0
|
| 53 |
|
| 54 |
-
#
|
| 55 |
try:
|
| 56 |
res_f = finbert_pipeline(row['Summary'])[0]
|
| 57 |
df_multi.at[i,'FinBERT_Sentiment'] = res_f['label'].upper().strip()
|
|
@@ -60,7 +59,7 @@ for i, row in df_multi.iterrows():
|
|
| 60 |
df_multi.at[i,'FinBERT_Sentiment'] = 'ERROR'
|
| 61 |
df_multi.at[i,'FinBERT_Confidence'] = 0.0
|
| 62 |
|
| 63 |
-
#
|
| 64 |
try:
|
| 65 |
if company == "Tesla":
|
| 66 |
val = price_pipeline_tesla(row['Summary'])[0]['score']
|
|
@@ -94,7 +93,7 @@ for company in companies:
|
|
| 94 |
df_c['Month'] = df_c['date'].dt.to_period('M').dt.to_timestamp()
|
| 95 |
df_c['Year'] = df_c['date'].dt.year
|
| 96 |
|
| 97 |
-
#
|
| 98 |
df_c['StrategyA_Cumulative'] = 0.0
|
| 99 |
for i in range(1, len(df_c)):
|
| 100 |
pct = df_c.loc[i, 'PctChangeDaily'] if pd.notnull(df_c.loc[i,'PctChangeDaily']) else 0
|
|
@@ -105,10 +104,10 @@ for company in companies:
|
|
| 105 |
else:
|
| 106 |
df_c.loc[i,'StrategyA_Cumulative'] = df_c.loc[i-1,'StrategyA_Cumulative']
|
| 107 |
|
| 108 |
-
#
|
| 109 |
df_c['StrategyB_Cumulative'] = (df_c['Predicted'] * df_c['PctChangeDaily']).cumsum()
|
| 110 |
|
| 111 |
-
#
|
| 112 |
df_c['StrategyC_Cumulative'] = 0.0
|
| 113 |
for i in range(1, len(df_c)):
|
| 114 |
pct = df_c.loc[i, 'PctChangeDaily'] if pd.notnull(df_c.loc[i,'PctChangeDaily']) else 0
|
|
@@ -121,27 +120,63 @@ for company in companies:
|
|
| 121 |
|
| 122 |
dfs_final[company] = df_c.drop(columns=["date", "date_merge"], errors="ignore")
|
| 123 |
|
| 124 |
-
# ---
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
)
|
| 145 |
|
| 146 |
-
|
| 147 |
-
fig_prices.show()
|
|
|
|
| 2 |
import yfinance as yf
|
| 3 |
from datasets import load_dataset
|
| 4 |
from transformers import pipeline
|
|
|
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
import gradio as gr
|
| 7 |
|
|
|
|
| 15 |
TICKERS = {
|
| 16 |
"Tesla": "TSLA",
|
| 17 |
"Microsoft": "MSFT",
|
| 18 |
+
"Nvidia": "NVDA"
|
| 19 |
}
|
| 20 |
companies = list(TICKERS.keys())
|
| 21 |
|
|
|
|
| 41 |
for i, row in df_multi.iterrows():
|
| 42 |
company = row['Company']
|
| 43 |
|
| 44 |
+
# Custom sentiment
|
| 45 |
try:
|
| 46 |
res = sentiment_pipeline(row['Summary'])[0]
|
| 47 |
df_multi.at[i,'Sentiment'] = res['label'].upper().strip()
|
|
|
|
| 50 |
df_multi.at[i,'Sentiment'] = 'ERROR'
|
| 51 |
df_multi.at[i,'Confidence'] = 0.0
|
| 52 |
|
| 53 |
+
# FinBERT
|
| 54 |
try:
|
| 55 |
res_f = finbert_pipeline(row['Summary'])[0]
|
| 56 |
df_multi.at[i,'FinBERT_Sentiment'] = res_f['label'].upper().strip()
|
|
|
|
| 59 |
df_multi.at[i,'FinBERT_Sentiment'] = 'ERROR'
|
| 60 |
df_multi.at[i,'FinBERT_Confidence'] = 0.0
|
| 61 |
|
| 62 |
+
# Regression (Tesla & MSFT)
|
| 63 |
try:
|
| 64 |
if company == "Tesla":
|
| 65 |
val = price_pipeline_tesla(row['Summary'])[0]['score']
|
|
|
|
| 93 |
df_c['Month'] = df_c['date'].dt.to_period('M').dt.to_timestamp()
|
| 94 |
df_c['Year'] = df_c['date'].dt.year
|
| 95 |
|
| 96 |
+
# Strategy A: Custom Sentiment
|
| 97 |
df_c['StrategyA_Cumulative'] = 0.0
|
| 98 |
for i in range(1, len(df_c)):
|
| 99 |
pct = df_c.loc[i, 'PctChangeDaily'] if pd.notnull(df_c.loc[i,'PctChangeDaily']) else 0
|
|
|
|
| 104 |
else:
|
| 105 |
df_c.loc[i,'StrategyA_Cumulative'] = df_c.loc[i-1,'StrategyA_Cumulative']
|
| 106 |
|
| 107 |
+
# Strategy B: Regression
|
| 108 |
df_c['StrategyB_Cumulative'] = (df_c['Predicted'] * df_c['PctChangeDaily']).cumsum()
|
| 109 |
|
| 110 |
+
# Strategy C: FinBERT
|
| 111 |
df_c['StrategyC_Cumulative'] = 0.0
|
| 112 |
for i in range(1, len(df_c)):
|
| 113 |
pct = df_c.loc[i, 'PctChangeDaily'] if pd.notnull(df_c.loc[i,'PctChangeDaily']) else 0
|
|
|
|
| 120 |
|
| 121 |
dfs_final[company] = df_c.drop(columns=["date", "date_merge"], errors="ignore")
|
| 122 |
|
| 123 |
+
# --- FUNZIONE PER GRADIO ---
|
| 124 |
+
def show_company_data(selected_companies, aggregation="Day"):
|
| 125 |
+
if not selected_companies:
|
| 126 |
+
return "Seleziona almeno un'azienda", None
|
| 127 |
+
|
| 128 |
+
fig = go.Figure()
|
| 129 |
+
agg_col = {"Day": "Day", "Month": "Month", "Year": "Year"}.get(aggregation, "Day")
|
| 130 |
+
|
| 131 |
+
for c in selected_companies:
|
| 132 |
+
if c not in dfs_final:
|
| 133 |
+
continue
|
| 134 |
+
df_c = dfs_final[c]
|
| 135 |
+
|
| 136 |
+
df_grouped = df_c.groupby(agg_col).agg({
|
| 137 |
+
'StrategyA_Cumulative':'last',
|
| 138 |
+
'StrategyB_Cumulative':'last',
|
| 139 |
+
'StrategyC_Cumulative':'last',
|
| 140 |
+
f'Close_{TICKERS[c]}':'last'
|
| 141 |
+
}).reset_index()
|
| 142 |
+
|
| 143 |
+
# Strategie
|
| 144 |
+
for strat in ['StrategyA_Cumulative','StrategyB_Cumulative','StrategyC_Cumulative']:
|
| 145 |
+
fig.add_trace(go.Scatter(
|
| 146 |
+
x=df_grouped[agg_col], y=df_grouped[strat],
|
| 147 |
+
mode="lines", name=f"{c} {strat}"
|
| 148 |
+
))
|
| 149 |
+
|
| 150 |
+
# Prezzo (asse Y2)
|
| 151 |
+
fig.add_trace(go.Scatter(
|
| 152 |
+
x=df_grouped[agg_col], y=df_grouped[f'Close_{TICKERS[c]}'],
|
| 153 |
+
mode="lines", name=f"{c} Price", yaxis="y2", line=dict(dash="dot")
|
| 154 |
+
))
|
| 155 |
+
|
| 156 |
+
fig.update_layout(
|
| 157 |
+
title="Confronto Strategie (Sentiment, Regression, FinBERT) + Prezzi",
|
| 158 |
+
xaxis_title=aggregation,
|
| 159 |
+
yaxis_title="Cumulative",
|
| 160 |
+
yaxis2=dict(title="Stock Price", overlaying="y", side="right"),
|
| 161 |
+
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1),
|
| 162 |
+
template="plotly_dark",
|
| 163 |
+
hovermode="x unified"
|
| 164 |
+
)
|
| 165 |
+
return None, fig
|
| 166 |
+
|
| 167 |
+
# --- INTERFACCIA GRADIO ---
|
| 168 |
+
demo = gr.Interface(
|
| 169 |
+
fn=show_company_data,
|
| 170 |
+
inputs=[
|
| 171 |
+
gr.Dropdown(choices=companies, value=["Microsoft","Tesla"], multiselect=True, label="Seleziona Aziende"),
|
| 172 |
+
gr.Radio(choices=["Day","Month","Year"], value="Day", label="Aggregazione")
|
| 173 |
+
],
|
| 174 |
+
outputs=[
|
| 175 |
+
gr.Dataframe(label="Preview", type="pandas"),
|
| 176 |
+
gr.Plot(label="Strategie & Prezzi")
|
| 177 |
+
],
|
| 178 |
+
title="Portfolio Evolution",
|
| 179 |
+
description="Confronto tra Sentiment Custom, Regressione e FinBERT con andamento prezzi"
|
| 180 |
)
|
| 181 |
|
| 182 |
+
demo.launch()
|
|
|