Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import plotly.express as px
|
| 3 |
+
from datasets import load_dataset
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# --- LOAD YOUR DATASET ---
|
| 7 |
+
df = pd.DataFrame(load_dataset("SelmaNajih001/NewsSentiment")["train"])
|
| 8 |
+
df['Date'] = pd.to_datetime(df['Date'], errors='coerce').dt.normalize()
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# --- GRADIO FUNCTION ---
|
| 12 |
+
def show_sentiment(selected_companies):
|
| 13 |
+
if selected_companies: # filtraggio per aziende selezionate
|
| 14 |
+
df_filtered = df[df['Company'].isin(selected_companies)]
|
| 15 |
+
df_grouped = (
|
| 16 |
+
df_filtered.groupby(['Date', 'Company'])['Score']
|
| 17 |
+
.sum()
|
| 18 |
+
.reset_index()
|
| 19 |
+
)
|
| 20 |
+
fig = px.line(
|
| 21 |
+
df_grouped, x="Date", y="Score", color="Company",
|
| 22 |
+
title="Daily Sentiment Score by Company"
|
| 23 |
+
)
|
| 24 |
+
else: # aggregato generale
|
| 25 |
+
df_grouped = (
|
| 26 |
+
df.groupby('Date')['Score']
|
| 27 |
+
.sum()
|
| 28 |
+
.reset_index()
|
| 29 |
+
)
|
| 30 |
+
fig = px.line(
|
| 31 |
+
df_grouped, x="Date", y="Score",
|
| 32 |
+
title="General Daily Sentiment Score"
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
return df_grouped.tail(30), fig # ultime 30 righe per leggibilità
|
| 36 |
+
|
| 37 |
+
# --- GRADIO INTERFACE ---
|
| 38 |
+
companies = sorted(df['Company'].unique().tolist())
|
| 39 |
+
|
| 40 |
+
demo = gr.Interface(
|
| 41 |
+
fn=show_sentiment,
|
| 42 |
+
inputs=[
|
| 43 |
+
gr.Dropdown(
|
| 44 |
+
choices=companies,
|
| 45 |
+
value=None,
|
| 46 |
+
label="Select Companies (leave empty for general sentiment)",
|
| 47 |
+
multiselect=True
|
| 48 |
+
)
|
| 49 |
+
],
|
| 50 |
+
outputs=[
|
| 51 |
+
gr.Dataframe(label="Daily Sentiment", type="pandas"),
|
| 52 |
+
gr.Plot(label="Sentiment Trend"),
|
| 53 |
+
],
|
| 54 |
+
title="Daily Sentiment Dashboard",
|
| 55 |
+
description="Shows daily sentiment scores (positive = +score, negative = -score, neutral = 0)."
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
demo.launch()
|