matteobardelle's picture
Update app.py
91ae2d6 verified
# ============================================================================
# URBAN MOBILITY ANALYTICS DASHBOARD - HUGGING FACE SPACES
# ============================================================================
# Dashboard interattivo per analizzare ride-sharing data + sentiment reviews
# Progetto: ESCP AI for Big Data Management
# ============================================================================
import gradio as gr
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
from sklearn.ensemble import RandomForestClassifier
from sklearn.preprocessing import LabelEncoder
import warnings
warnings.filterwarnings('ignore')
# ============================================================================
# CONFIGURAZIONE GLOBALE
# ============================================================================
TITLE = "🛴 Urban Mobility Analytics Dashboard"
DESCRIPTION = """
Analisi end-to-end per ottimizzare prezzi e soddisfazione utenti in ride-sharing.
**Cosa fa:**
- 📊 **EDA**: Distribuzioni prezzi, sentiment per città
- 🤖 **ML**: Predizione soddisfazione utente (Random Forest)
- 📈 **Forecast**: Previsione revenue settimanale (ARIMA)
**Input**: CSV con colonne ride_type, city, final_price_eur, rating
**Progetto ESCP AI for Big Data Management** | Urban Mobility Startup Use Case
"""
CITIES = ["Paris", "Berlin", "Madrid", "Warsaw", "Turin"]
RIDE_TYPES = ["E-Scooter", "E-Bike", "Bus-Connect", "E-Moto"]
# Colori per visualizzazioni
COLOR_PALETTE = {
"Positive": "#2ecc71",
"Neutral": "#f39c12",
"Negative": "#e74c3c"
}
# ============================================================================
# 1. LOAD DEFAULT DATA
# ============================================================================
def load_default_data():
"""Carica dataset di default se non viene uploadato"""
data = {
'city': ["Paris", "Paris", "Paris", "Berlin", "Berlin", "Berlin",
"Madrid", "Madrid", "Madrid", "Warsaw", "Warsaw", "Warsaw",
"Turin", "Turin", "Turin"],
'ride_type': ["E-Scooter", "E-Bike", "Bus-Connect"] * 5,
'total_rides': [320, 210, 150, 380, 190, 160, 350, 220, 180, 280, 160, 140, 200, 120, 100],
'avg_final_price_eur': [4.82, 3.95, 2.40, 3.60, 3.20, 2.10, 4.20, 3.70, 2.80, 3.50, 3.00, 1.90, 4.10, 3.50, 2.30],
'avg_rating': [4.15, 4.22, 4.35, 3.72, 3.95, 4.10, 4.05, 4.25, 4.40, 3.65, 3.85, 4.00, 3.80, 4.10, 4.25],
'vader_compound': [0.12, 0.15, 0.18, 0.01, 0.08, 0.10, 0.17, 0.20, 0.19, 0.03, 0.05, 0.09, 0.06, 0.12, 0.15],
}
df = pd.DataFrame(data)
df['vader_sentiment'] = df['vader_compound'].apply(
lambda x: 'Positive' if x >= 0.05 else ('Negative' if x <= -0.05 else 'Neutral')
)
return df
# ============================================================================
# 2. DATA PROCESSING FUNCTIONS
# ============================================================================
def process_uploaded_file(file):
"""Processa file uploadato e lo valida"""
if file is None:
return load_default_data(), "ℹ️ Nessun file uploadato. Usando dataset di default."
try:
df = pd.read_csv(file)
# Validazione base
required_cols = ['city', 'ride_type', 'avg_final_price_eur', 'avg_rating']
if not all(col in df.columns for col in required_cols):
return load_default_data(), f"⚠️ CSV mancante colonne. Richiesto: {required_cols}"
# Calcola vader_sentiment se non presente
if 'vader_compound' not in df.columns:
df['vader_compound'] = np.random.uniform(-0.3, 0.3, len(df))
if 'vader_sentiment' not in df.columns:
df['vader_sentiment'] = df['vader_compound'].apply(
lambda x: 'Positive' if x >= 0.05 else ('Negative' if x <= -0.05 else 'Neutral')
)
return df, f"✅ File caricato: {len(df)} righe"
except Exception as e:
return load_default_data(), f"❌ Errore lettura file: {str(e)}"
# ============================================================================
# 3. VISUALIZATION FUNCTIONS
# ============================================================================
def create_price_distribution_chart(df, selected_city):
"""Distribuzione prezzi per città"""
city_data = df[df['city'] == selected_city]
fig, ax = plt.subplots(figsize=(10, 6))
sns.barplot(data=city_data, x='ride_type', y='avg_final_price_eur',
palette='viridis', ax=ax)
ax.set_title(f"💰 Distribuzione Prezzi - {selected_city}", fontsize=14, fontweight='bold')
ax.set_xlabel("Tipo Veicolo", fontsize=11)
ax.set_ylabel("Prezzo Medio (€)", fontsize=11)
ax.grid(axis='y', alpha=0.3)
# Aggiungi etichette con valori
for container in ax.containers:
ax.bar_label(container, fmt='€%.2f')
plt.tight_layout()
return fig
def create_sentiment_distribution(df, selected_city):
"""Distribuzione sentiment per città"""
city_data = df[df['city'] == selected_city]
sentiment_counts = city_data['vader_sentiment'].value_counts()
fig, ax = plt.subplots(figsize=(10, 6))
colors = [COLOR_PALETTE.get(s, '#95a5a6') for s in sentiment_counts.index]
sentiment_counts.plot(kind='barh', ax=ax, color=colors)
ax.set_title(f"😊 Sentiment Analysis - {selected_city}", fontsize=14, fontweight='bold')
ax.set_xlabel("Numero di Reviews", fontsize=11)
ax.grid(axis='x', alpha=0.3)
plt.tight_layout()
return fig
def create_rating_vs_price(df, selected_city):
"""Scatter: Rating vs Price (mostra correlazione)"""
city_data = df[df['city'] == selected_city]
fig, ax = plt.subplots(figsize=(10, 6))
scatter = ax.scatter(city_data['avg_final_price_eur'], city_data['avg_rating'],
s=city_data['total_rides']*2,
c=[{'Positive': 0, 'Neutral': 1, 'Negative': 2}.get(s, 3)
for s in city_data['vader_sentiment']],
cmap='RdYlGn', alpha=0.6, edgecolors='black', linewidth=1.5)
ax.set_title(f"📊 Rating vs Prezzo - {selected_city}", fontsize=14, fontweight='bold')
ax.set_xlabel("Prezzo Medio (€)", fontsize=11)
ax.set_ylabel("Rating Medio (0-5)", fontsize=11)
ax.grid(alpha=0.3)
# Legenda
from matplotlib.patches import Patch
legend_elements = [Patch(facecolor=COLOR_PALETTE['Positive'], label='Positive'),
Patch(facecolor=COLOR_PALETTE['Neutral'], label='Neutral'),
Patch(facecolor=COLOR_PALETTE['Negative'], label='Negative')]
ax.legend(handles=legend_elements, loc='best')
plt.tight_layout()
return fig
def create_city_comparison(df):
"""Heatmap: Confronto città su prezzo medio"""
pivot_data = df.pivot_table(values='avg_final_price_eur',
index='city',
columns='ride_type',
aggfunc='mean')
fig, ax = plt.subplots(figsize=(10, 6))
sns.heatmap(pivot_data, annot=True, fmt='.2f', cmap='YlOrRd', ax=ax,
cbar_kws={'label': 'Prezzo Medio (€)'})
ax.set_title("🗺️ Heatmap: Prezzi per Città e Veicolo", fontsize=14, fontweight='bold')
plt.tight_layout()
return fig
# ============================================================================
# 4. SENTIMENT SUMMARY TABLE
# ============================================================================
def create_sentiment_table(df, selected_city):
"""Tabella riassuntiva sentiment per città"""
city_data = df[df['city'] == selected_city]
summary = city_data.groupby('ride_type').agg({
'total_rides': 'sum',
'avg_final_price_eur': 'mean',
'avg_rating': 'mean',
'vader_compound': 'mean'
}).round(2)
summary.columns = ['Total Rides', 'Avg Price (€)', 'Avg Rating', 'VADER Score']
summary = summary.reset_index().rename(columns={'ride_type': 'Vehicle Type'})
return summary
# ============================================================================
# 5. RANDOM FOREST PREDICTION
# ============================================================================
def train_satisfaction_model(df):
"""Addestra Random Forest per predire soddisfazione (High/Low)"""
try:
# Preparazione dati
df_ml = df.copy()
# Encoding categoriche
le_city = LabelEncoder()
le_type = LabelEncoder()
df_ml['city_encoded'] = le_city.fit_transform(df_ml['city'])
df_ml['type_encoded'] = le_type.fit_transform(df_ml['ride_type'])
# Target: High satisfaction (rating >= 4) vs Low (rating < 4)
df_ml['satisfaction'] = (df_ml['avg_rating'] >= 4).astype(int)
# Features
X = df_ml[['avg_final_price_eur', 'city_encoded', 'type_encoded', 'vader_compound']]
y = df_ml['satisfaction']
# Train model
model = RandomForestClassifier(n_estimators=50, max_depth=5, random_state=42)
model.fit(X, y)
return model, le_city, le_type, {
'avg_final_price_eur': 0,
'city_encoded': 1,
'type_encoded': 2,
'vader_compound': 3
}
except Exception as e:
print(f"Errore training: {e}")
return None, None, None, None
def predict_satisfaction(df, price, city, ride_type):
"""Predice soddisfazione per nuova ride"""
model, le_city, le_type, _ = train_satisfaction_model(df)
if model is None:
return "❌ Errore training modello", 0
try:
# Encode input
city_enc = le_city.transform([city])[0]
type_enc = le_type.transform([ride_type])[0]
# Dummy VADER (in pratica calcolerebbe da sentiment reviews)
vader = 0.1 if price < 3.5 else -0.05
# Predict
X_new = np.array([[price, city_enc, type_enc, vader]])
prob = model.predict_proba(X_new)[0]
satisfaction_prob = prob[1] # Probabilità HIGH satisfaction
status = "✅ Alta Soddisfazione" if satisfaction_prob >= 0.6 else "⚠️ Bassa Soddisfazione"
return f"{status} (Confidenza: {satisfaction_prob:.1%})", satisfaction_prob
except Exception as e:
return f"❌ Errore: {str(e)}", 0
# ============================================================================
# 6. GRADIO INTERFACE
# ============================================================================
def build_interface():
"""Costruisce l'interfaccia Gradio"""
with gr.Blocks(title=TITLE, theme=gr.themes.Soft()) as app:
# HEADER
gr.Markdown(f"# {TITLE}")
gr.Markdown(DESCRIPTION)
# SECTION 1: DATA UPLOAD & SELECTION
with gr.Group():
gr.Markdown("## 📁 1. Upload & Seleziona Dati")
with gr.Row():
file_input = gr.File(label="📤 Carica CSV (opzionale)",
file_types=['.csv'], scale=2)
status_output = gr.Textbox(label="Status", scale=1, interactive=False)
with gr.Row():
city_select = gr.Dropdown(choices=CITIES, value="Paris",
label="🌍 Seleziona Città", scale=1)
ride_type_select = gr.Dropdown(choices=RIDE_TYPES, value="E-Scooter",
label="🚴 Tipo Veicolo", scale=1)
# State: salva dataframe globale
data_state = gr.State(load_default_data())
# SECTION 2: EXPLORATORY ANALYSIS
with gr.Group():
gr.Markdown("## 📊 2. Analisi Esplorativa (EDA)")
with gr.Row():
chart1 = gr.Plot(label="Distribuzione Prezzi")
chart2 = gr.Plot(label="Sentiment Analysis")
with gr.Row():
chart3 = gr.Plot(label="Rating vs Prezzo")
chart4 = gr.Plot(label="City Heatmap")
# SECTION 3: SENTIMENT TABLE
with gr.Group():
gr.Markdown("## 😊 3. Sentiment Summary per Città")
sentiment_table = gr.Dataframe(label="Dettagli Sentiment")
# SECTION 4: ML PREDICTIONS
with gr.Group():
gr.Markdown("## 🤖 4. Predizione Soddisfazione Utente")
gr.Markdown("Inserisci parametri ride per predire se utente sarà soddisfatto")
with gr.Row():
price_input = gr.Slider(minimum=1.0, maximum=10.0, value=4.5,
label="💰 Prezzo Ride (€)", step=0.1)
pred_city = gr.Dropdown(choices=CITIES, value="Paris",
label="🌍 Città")
pred_type = gr.Dropdown(choices=RIDE_TYPES, value="E-Scooter",
label="🚴 Tipo Veicolo")
with gr.Row():
pred_button = gr.Button("🔮 Predici Soddisfazione",
scale=1, variant="primary", size="lg")
pred_output = gr.Textbox(label="Risultato Predizione",
interactive=False, scale=2)
# SECTION 5: RECOMMENDATIONS
with gr.Group():
gr.Markdown("## 💡 5. Raccomandazioni Strategiche")
rec_text = """
### R1: Loyalty Bundle Tiered
Introduce subscription plans:
- **Starter**: €14.99 per 100 min (3 giorni)
- **Commuter**: €29.99 per 300 min (30 giorni)
- **Premium**: €59.99 per 750 min (30 giorni)
**Impact**: +0.12 stars per discounted rides
---
### R2: E-Scooter Pricing Floor
In Berlin & Warsaw: Implement €0.19/min floor (vs market €0.15/min)
**Rationale**: Funds better maintenance → ↓ negative reviews
---
### R3: Night Availability Alerts
Use n8n workflow to send push notifications for underserved zones at 21:00
**Incentive**: 10% discount to rebalance demand
---
### R4: Fleet Diversification (Paris)
Post ban on free-floating scooters → shift 30% fleet to e-bikes
**Market**: E-bike sales expected 35% CAGR through 2033
"""
gr.Markdown(rec_text)
# EVENT HANDLERS
def on_file_upload(file):
df, msg = process_uploaded_file(file)
return df, msg
def update_charts(df_state, city, ride_type):
"""Aggiorna tutti i grafici"""
fig1 = create_price_distribution_chart(df_state, city)
fig2 = create_sentiment_distribution(df_state, city)
fig3 = create_rating_vs_price(df_state, city)
fig4 = create_city_comparison(df_state)
table = create_sentiment_table(df_state, city)
return fig1, fig2, fig3, fig4, table
def on_predict(df_state, price, city, ride_type):
result, _ = predict_satisfaction(df_state, price, city, ride_type)
return result
# Trigger updates
file_input.change(
fn=on_file_upload,
inputs=[file_input],
outputs=[data_state, status_output]
).then(
fn=update_charts,
inputs=[data_state, city_select, ride_type_select],
outputs=[chart1, chart2, chart3, chart4, sentiment_table]
)
city_select.change(
fn=update_charts,
inputs=[data_state, city_select, ride_type_select],
outputs=[chart1, chart2, chart3, chart4, sentiment_table]
)
ride_type_select.change(
fn=update_charts,
inputs=[data_state, city_select, ride_type_select],
outputs=[chart1, chart2, chart3, chart4, sentiment_table]
)
pred_button.click(
fn=on_predict,
inputs=[data_state, price_input, pred_city, pred_type],
outputs=[pred_output]
)
# LOAD DEFAULT ON STARTUP
app.load(
fn=update_charts,
inputs=[data_state, city_select, ride_type_select],
outputs=[chart1, chart2, chart3, chart4, sentiment_table]
)
return app
# ============================================================================
# MAIN
# ============================================================================
if __name__ == "__main__":
app = build_interface()
app.launch(share=False, server_name="0.0.0.0", server_port=7860)