Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,32 +2,70 @@ import pandas as pd
|
|
| 2 |
import gradio as gr
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def load_data():
|
| 10 |
try:
|
| 11 |
df = pd.read_csv("merged_summary.csv")
|
| 12 |
-
except:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
# DASHBOARD
|
| 27 |
-
# ===============================
|
| 28 |
|
| 29 |
def render_dashboard(city, vehicle):
|
| 30 |
-
df = load_data()
|
| 31 |
|
| 32 |
if city != "All":
|
| 33 |
df = df[df["city"] == city]
|
|
@@ -35,38 +73,54 @@ def render_dashboard(city, vehicle):
|
|
| 35 |
df = df[df["vehicle_type"] == vehicle]
|
| 36 |
|
| 37 |
if df.empty:
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
|
| 40 |
avg_price = df["avg_final_price_eur"].mean()
|
| 41 |
avg_rating = df["avg_rating"].mean()
|
| 42 |
avg_cancel = df["cancellation_rate"].mean()
|
| 43 |
|
| 44 |
kpi = f"""
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
fig1 = go.Figure()
|
| 52 |
-
fig1.add_bar(
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
fig2 = go.Figure()
|
| 56 |
-
fig2.add_bar(
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
fig3 = go.Figure()
|
| 60 |
-
fig3.add_bar(
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
return kpi, fig1, fig2, fig3
|
| 64 |
|
| 65 |
-
|
| 66 |
-
# ===============================
|
| 67 |
-
# PREDICTION
|
| 68 |
-
# ===============================
|
| 69 |
-
|
| 70 |
def predict(price, discount):
|
| 71 |
score = 0.5
|
| 72 |
if price < 5:
|
|
@@ -80,23 +134,18 @@ def predict(price, discount):
|
|
| 80 |
"label": "High" if score > 0.5 else "Low"
|
| 81 |
}
|
| 82 |
|
| 83 |
-
|
| 84 |
-
# ===============================
|
| 85 |
-
# UI
|
| 86 |
-
# ===============================
|
| 87 |
-
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
gr.Markdown("# Urban Mobility App")
|
| 90 |
|
| 91 |
with gr.Tab("Dashboard"):
|
| 92 |
city = gr.Dropdown(
|
| 93 |
-
["All", "Paris", "Berlin", "Madrid", "Warsaw"],
|
| 94 |
value="All",
|
| 95 |
label="City"
|
| 96 |
)
|
| 97 |
|
| 98 |
vehicle = gr.Dropdown(
|
| 99 |
-
["All", "E-Scooter", "E-Bike", "Shared-EV"],
|
| 100 |
value="All",
|
| 101 |
label="Vehicle"
|
| 102 |
)
|
|
@@ -123,6 +172,5 @@ with gr.Blocks() as demo:
|
|
| 123 |
|
| 124 |
btn2.click(predict, inputs=[price, discount], outputs=out)
|
| 125 |
|
| 126 |
-
|
| 127 |
if __name__ == "__main__":
|
| 128 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import plotly.graph_objects as go
|
| 4 |
|
| 5 |
+
def make_demo_df():
|
| 6 |
+
return pd.DataFrame([
|
| 7 |
+
["Paris", "E-Scooter", 4.6, 4.1, 0.12, 0.06],
|
| 8 |
+
["Paris", "E-Bike", 4.3, 4.2, 0.14, 0.05],
|
| 9 |
+
["Berlin", "E-Scooter", 4.9, 3.8, 0.05, 0.08],
|
| 10 |
+
["Berlin", "E-Bike", 4.5, 4.0, 0.09, 0.06],
|
| 11 |
+
["Madrid", "E-Scooter", 4.2, 4.3, 0.17, 0.05],
|
| 12 |
+
["Warsaw", "Shared-EV", 5.0, 4.0, 0.07, 0.05],
|
| 13 |
+
], columns=[
|
| 14 |
+
"city", "vehicle_type", "avg_final_price_eur",
|
| 15 |
+
"avg_rating", "avg_sentiment", "cancellation_rate"
|
| 16 |
+
])
|
| 17 |
|
| 18 |
def load_data():
|
| 19 |
try:
|
| 20 |
df = pd.read_csv("merged_summary.csv")
|
| 21 |
+
except Exception:
|
| 22 |
+
return make_demo_df()
|
| 23 |
+
|
| 24 |
+
df.columns = [str(c).strip() for c in df.columns]
|
| 25 |
+
|
| 26 |
+
rename_map = {}
|
| 27 |
+
|
| 28 |
+
for c in df.columns:
|
| 29 |
+
cl = c.lower().strip()
|
| 30 |
+
|
| 31 |
+
if cl == "city":
|
| 32 |
+
rename_map[c] = "city"
|
| 33 |
+
elif cl in ["vehicle_type", "ride_type", "vehicle", "vehicletype"]:
|
| 34 |
+
rename_map[c] = "vehicle_type"
|
| 35 |
+
elif cl in ["avg_final_price_eur", "final_price_eur", "avg_price", "avg_final_price", "price"]:
|
| 36 |
+
rename_map[c] = "avg_final_price_eur"
|
| 37 |
+
elif cl in ["avg_rating", "rating", "avg_star_rating", "star_rating"]:
|
| 38 |
+
rename_map[c] = "avg_rating"
|
| 39 |
+
elif cl in ["avg_sentiment", "sentiment", "compound", "vader_compound", "avg_compound_score"]:
|
| 40 |
+
rename_map[c] = "avg_sentiment"
|
| 41 |
+
elif cl in ["cancellation_rate", "cancel_rate", "avg_cancellation_rate"]:
|
| 42 |
+
rename_map[c] = "cancellation_rate"
|
| 43 |
+
|
| 44 |
+
df = df.rename(columns=rename_map)
|
| 45 |
+
|
| 46 |
+
required = [
|
| 47 |
+
"city",
|
| 48 |
+
"vehicle_type",
|
| 49 |
+
"avg_final_price_eur",
|
| 50 |
+
"avg_rating",
|
| 51 |
+
"avg_sentiment",
|
| 52 |
+
"cancellation_rate",
|
| 53 |
+
]
|
| 54 |
+
|
| 55 |
+
for col in required:
|
| 56 |
+
if col not in df.columns:
|
| 57 |
+
if col in ["city", "vehicle_type"]:
|
| 58 |
+
df[col] = "Unknown"
|
| 59 |
+
else:
|
| 60 |
+
df[col] = 0.0
|
| 61 |
+
|
| 62 |
+
for col in ["avg_final_price_eur", "avg_rating", "avg_sentiment", "cancellation_rate"]:
|
| 63 |
+
df[col] = pd.to_numeric(df[col], errors="coerce").fillna(0)
|
| 64 |
|
| 65 |
+
return df
|
|
|
|
|
|
|
| 66 |
|
| 67 |
def render_dashboard(city, vehicle):
|
| 68 |
+
df = load_data().copy()
|
| 69 |
|
| 70 |
if city != "All":
|
| 71 |
df = df[df["city"] == city]
|
|
|
|
| 73 |
df = df[df["vehicle_type"] == vehicle]
|
| 74 |
|
| 75 |
if df.empty:
|
| 76 |
+
empty = go.Figure()
|
| 77 |
+
empty.update_layout(title="No data for selected filters")
|
| 78 |
+
return "### No data available", empty, empty, empty
|
| 79 |
|
| 80 |
avg_price = df["avg_final_price_eur"].mean()
|
| 81 |
avg_rating = df["avg_rating"].mean()
|
| 82 |
avg_cancel = df["cancellation_rate"].mean()
|
| 83 |
|
| 84 |
kpi = f"""
|
| 85 |
+
### KPIs
|
| 86 |
+
- Avg Price: EUR {avg_price:.2f}
|
| 87 |
+
- Avg Rating: {avg_rating:.2f}
|
| 88 |
+
- Cancellation Rate: {avg_cancel:.2%}
|
| 89 |
+
"""
|
| 90 |
+
|
| 91 |
+
seg = df.groupby(["city", "vehicle_type"], as_index=False).agg(
|
| 92 |
+
avg_final_price_eur=("avg_final_price_eur", "mean"),
|
| 93 |
+
avg_rating=("avg_rating", "mean"),
|
| 94 |
+
cancellation_rate=("cancellation_rate", "mean"),
|
| 95 |
+
)
|
| 96 |
|
| 97 |
fig1 = go.Figure()
|
| 98 |
+
fig1.add_bar(
|
| 99 |
+
x=[f"{r['city']} - {r['vehicle_type']}" for _, r in seg.iterrows()],
|
| 100 |
+
y=seg["avg_final_price_eur"]
|
| 101 |
+
)
|
| 102 |
+
fig1.update_layout(title="Average Price by City / Vehicle")
|
| 103 |
|
| 104 |
fig2 = go.Figure()
|
| 105 |
+
fig2.add_bar(
|
| 106 |
+
x=[f"{r['city']} - {r['vehicle_type']}" for _, r in seg.iterrows()],
|
| 107 |
+
y=seg["avg_rating"]
|
| 108 |
+
)
|
| 109 |
+
fig2.update_layout(title="Average Rating by City / Vehicle")
|
| 110 |
+
|
| 111 |
+
city_agg = df.groupby("city", as_index=False).agg(
|
| 112 |
+
cancellation_rate=("cancellation_rate", "mean")
|
| 113 |
+
)
|
| 114 |
|
| 115 |
fig3 = go.Figure()
|
| 116 |
+
fig3.add_bar(
|
| 117 |
+
x=city_agg["city"],
|
| 118 |
+
y=city_agg["cancellation_rate"]
|
| 119 |
+
)
|
| 120 |
+
fig3.update_layout(title="Cancellation Rate by City")
|
| 121 |
|
| 122 |
return kpi, fig1, fig2, fig3
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
def predict(price, discount):
|
| 125 |
score = 0.5
|
| 126 |
if price < 5:
|
|
|
|
| 134 |
"label": "High" if score > 0.5 else "Low"
|
| 135 |
}
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
with gr.Blocks() as demo:
|
| 138 |
gr.Markdown("# Urban Mobility App")
|
| 139 |
|
| 140 |
with gr.Tab("Dashboard"):
|
| 141 |
city = gr.Dropdown(
|
| 142 |
+
["All", "Paris", "Berlin", "Madrid", "Warsaw", "Turin"],
|
| 143 |
value="All",
|
| 144 |
label="City"
|
| 145 |
)
|
| 146 |
|
| 147 |
vehicle = gr.Dropdown(
|
| 148 |
+
["All", "E-Scooter", "E-Bike", "Shared-EV", "Bus-Connect"],
|
| 149 |
value="All",
|
| 150 |
label="Vehicle"
|
| 151 |
)
|
|
|
|
| 172 |
|
| 173 |
btn2.click(predict, inputs=[price, discount], outputs=out)
|
| 174 |
|
|
|
|
| 175 |
if __name__ == "__main__":
|
| 176 |
demo.launch()
|