SelmaNajih001 commited on
Commit
1b040a6
·
verified ·
1 Parent(s): 5bb1c41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -22
app.py CHANGED
@@ -2,50 +2,126 @@ import pandas as pd
2
  import plotly.express as px
3
  from datasets import load_dataset
4
  import gradio as gr
 
5
 
6
  # --- LOAD DATASET ---
7
  df = pd.DataFrame(load_dataset("SelmaNajih001/NewsSentiment")["train"])
8
- df=df[(df["Company"]=="Tesla") | (df["Company"]=="Microsoft") | (df["Company"]=="Apple") | (df["Company"]=="Facebook")]
9
 
10
  # --- CONVERT DATE TO DATETIME SAFELY ---
11
  df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
12
-
13
  df['Year'] = df['Date'].dt.year
14
- df['Date'] = pd.to_datetime(df['Date'])
15
  df['Month'] = df['Date'].dt.to_period('M')
16
  df['Day'] = df['Date'].dt.date
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  # --- GRADIO FUNCTION ---
19
  def show_sentiment(selected_companies, aggregation="Day"):
20
- # filtraggio aziende
21
  if selected_companies:
22
- df_filtered = df[df['Company'].isin(selected_companies)].copy()
23
  else:
24
- df_filtered = df.copy()
25
-
26
- # scelta colonna per aggregazione
 
 
 
 
 
 
 
 
27
  if aggregation == "Day":
28
  group_col = "Day"
29
  elif aggregation == "Month":
30
  group_col = "Month"
31
- # Convertiamo Period in datetime per Plotly
32
  df_filtered['Month'] = df_filtered['Month'].dt.to_timestamp()
33
  elif aggregation == "Year":
34
  group_col = "Year"
35
  else:
36
- group_col = "Day" # default
37
 
38
- # raggruppamento
39
- if selected_companies:
40
- df_grouped = df_filtered.groupby([group_col, 'Company'])['Score'].sum().reset_index()
41
- fig = px.line(df_grouped, x=group_col, y='Score', color='Company',
42
- title=f"Sentiment Score by {aggregation} per Company")
 
43
  else:
44
- df_grouped = df_filtered.groupby(group_col)['Score'].sum().reset_index()
45
- fig = px.line(df_grouped, x=group_col, y='Score',
46
- title=f"General Sentiment Score by {aggregation}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
 
48
- return df_grouped.tail(30), fig # ultime 30 righe per leggibilità
 
 
 
 
 
 
 
 
 
 
49
 
50
  # --- GRADIO INTERFACE ---
51
  companies = sorted(df['Company'].unique().tolist())
@@ -67,10 +143,11 @@ demo = gr.Interface(
67
  ],
68
  outputs=[
69
  gr.Dataframe(label="Sentiment Table", type="pandas"),
70
- gr.Plot(label="Sentiment Trend"),
71
  ],
72
- title="Dynamic Sentiment Dashboard",
73
- description="Shows sentiment scores aggregated by day, month, or year. Positive = +score, Negative = -score, Neutral = 0."
74
  )
75
 
76
  demo.launch()
 
 
2
  import plotly.express as px
3
  from datasets import load_dataset
4
  import gradio as gr
5
+ import yfinance as yf
6
 
7
  # --- LOAD DATASET ---
8
  df = pd.DataFrame(load_dataset("SelmaNajih001/NewsSentiment")["train"])
9
+ df = df[df["Company"].isin(["Tesla", "Microsoft", "Apple", "Facebook", "Google"])]
10
 
11
  # --- CONVERT DATE TO DATETIME SAFELY ---
12
  df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
 
13
  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 YAHOO FINANCE ---
18
+ TICKERS = {
19
+ "Tesla": "TSLA",
20
+ "Microsoft": "MSFT",
21
+ "Apple": "AAPL",
22
+ "Facebook": "META", # ex FB
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() # prendi la data minima del dataset per tutti
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 INTO DATASET ---
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['Company'] == company
42
+ df_merged.loc[mask, 'Close'] = pd.merge(
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
+ selected_companies = ["NASDAQ"] # per mostrare anche NASDAQ
57
+ # Creiamo un dataframe NASDAQ per unire al grafico
58
+ df_nasdaq = prices["NASDAQ"].copy()
59
+ df_nasdaq = df_nasdaq.rename(columns={'Close': 'Score'})
60
+ df_nasdaq['Company'] = 'NASDAQ'
61
+ df_nasdaq[group_col := 'Date'] = df_nasdaq['Date']
62
+ df_filtered = pd.concat([df_filtered, df_nasdaq], ignore_index=True, sort=False)
63
+
64
+ # Determina colonna di aggregazione
65
  if aggregation == "Day":
66
  group_col = "Day"
67
  elif aggregation == "Month":
68
  group_col = "Month"
 
69
  df_filtered['Month'] = df_filtered['Month'].dt.to_timestamp()
70
  elif aggregation == "Year":
71
  group_col = "Year"
72
  else:
73
+ group_col = "Day"
74
 
75
+ # Raggruppamento con sentiment e prezzo
76
+ if "NASDAQ" in selected_companies:
77
+ df_grouped = df_filtered.groupby([group_col, 'Company']).agg({
78
+ 'Score': 'sum',
79
+ 'Close': 'last'
80
+ }).reset_index()
81
  else:
82
+ df_grouped = df_filtered.groupby([group_col, 'Company']).agg({
83
+ 'Score': 'sum',
84
+ 'Close': 'last'
85
+ }).reset_index()
86
+
87
+ # --- CREAZIONE FIGURA ---
88
+ fig = px.line(df_grouped, x=group_col, y='Score', color='Company',
89
+ title=f"Sentiment Score by {aggregation} per Company")
90
+
91
+ # Aggiungi linea prezzo sul secondary y
92
+ for company in selected_companies:
93
+ if company == "NASDAQ":
94
+ df_c = df_grouped[df_grouped['Company'] == 'NASDAQ']
95
+ fig.add_scatter(
96
+ x=df_c[group_col],
97
+ y=df_c['Score'],
98
+ mode='lines',
99
+ name="NASDAQ Index",
100
+ yaxis="y2",
101
+ line=dict(dash='dot', color='yellow')
102
+ )
103
+ else:
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
 
126
  # --- GRADIO INTERFACE ---
127
  companies = sorted(df['Company'].unique().tolist())
 
143
  ],
144
  outputs=[
145
  gr.Dataframe(label="Sentiment Table", type="pandas"),
146
+ gr.Plot(label="Sentiment & Stock Price Trend"),
147
  ],
148
+ title="Dynamic Sentiment & Stock Price Dashboard",
149
+ description="Shows sentiment scores aggregated by day, month, or year. Overlay stock prices from Yahoo Finance and NASDAQ index for comparison."
150
  )
151
 
152
  demo.launch()
153
+