Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,13 +1,128 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
with gr.Blocks() as demo:
|
| 7 |
-
gr.Markdown("#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
if __name__ == "__main__":
|
| 13 |
demo.launch()
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
import gradio as gr
|
| 3 |
+
import plotly.graph_objects as go
|
| 4 |
|
| 5 |
+
# ===============================
|
| 6 |
+
# DATA
|
| 7 |
+
# ===============================
|
| 8 |
+
|
| 9 |
+
def load_data():
|
| 10 |
+
try:
|
| 11 |
+
df = pd.read_csv("merged_summary.csv")
|
| 12 |
+
except:
|
| 13 |
+
df = pd.DataFrame([
|
| 14 |
+
["Paris", "E-Scooter", 4.6, 4.1, 0.12, 0.06],
|
| 15 |
+
["Berlin", "E-Bike", 4.5, 4.0, 0.09, 0.06],
|
| 16 |
+
["Madrid", "E-Scooter", 4.2, 4.3, 0.17, 0.05],
|
| 17 |
+
["Warsaw", "Shared-EV", 5.0, 4.0, 0.07, 0.05],
|
| 18 |
+
], columns=[
|
| 19 |
+
"city", "vehicle_type", "avg_final_price_eur",
|
| 20 |
+
"avg_rating", "avg_sentiment", "cancellation_rate"
|
| 21 |
+
])
|
| 22 |
+
return df
|
| 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]
|
| 34 |
+
if vehicle != "All":
|
| 35 |
+
df = df[df["vehicle_type"] == vehicle]
|
| 36 |
+
|
| 37 |
+
if df.empty:
|
| 38 |
+
return "No data", go.Figure(), go.Figure(), go.Figure()
|
| 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 |
+
### KPIs
|
| 46 |
+
- Avg Price: €{avg_price:.2f}
|
| 47 |
+
- Avg Rating: {avg_rating:.2f}
|
| 48 |
+
- Cancellation Rate: {avg_cancel:.2%}
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
fig1 = go.Figure()
|
| 52 |
+
fig1.add_bar(x=df["city"], y=df["avg_final_price_eur"])
|
| 53 |
+
fig1.update_layout(title="Average Price")
|
| 54 |
+
|
| 55 |
+
fig2 = go.Figure()
|
| 56 |
+
fig2.add_bar(x=df["city"], y=df["avg_rating"])
|
| 57 |
+
fig2.update_layout(title="Average Rating")
|
| 58 |
+
|
| 59 |
+
fig3 = go.Figure()
|
| 60 |
+
fig3.add_bar(x=df["city"], y=df["cancellation_rate"])
|
| 61 |
+
fig3.update_layout(title="Cancellation Rate")
|
| 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:
|
| 73 |
+
score += 0.2
|
| 74 |
+
if discount > 10:
|
| 75 |
+
score += 0.1
|
| 76 |
+
score = min(max(score, 0), 1)
|
| 77 |
+
|
| 78 |
+
return {
|
| 79 |
+
"satisfaction_probability": round(score, 2),
|
| 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 |
+
)
|
| 103 |
+
|
| 104 |
+
btn = gr.Button("Refresh")
|
| 105 |
+
|
| 106 |
+
kpi = gr.Markdown()
|
| 107 |
+
chart1 = gr.Plot()
|
| 108 |
+
chart2 = gr.Plot()
|
| 109 |
+
chart3 = gr.Plot()
|
| 110 |
+
|
| 111 |
+
btn.click(
|
| 112 |
+
render_dashboard,
|
| 113 |
+
inputs=[city, vehicle],
|
| 114 |
+
outputs=[kpi, chart1, chart2, chart3]
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
with gr.Tab("Prediction"):
|
| 118 |
+
price = gr.Number(label="Price", value=4.0)
|
| 119 |
+
discount = gr.Number(label="Discount %", value=10)
|
| 120 |
+
|
| 121 |
+
btn2 = gr.Button("Predict")
|
| 122 |
+
out = gr.JSON()
|
| 123 |
+
|
| 124 |
+
btn2.click(predict, inputs=[price, discount], outputs=out)
|
| 125 |
+
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
demo.launch()
|