charlottegers commited on
Commit
508cbce
·
verified ·
1 Parent(s): 85ea865

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +137 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import gradio as gr
4
+ import plotly.express as px
5
+ import random
6
+ import warnings
7
+
8
+ warnings.filterwarnings("ignore")
9
+ random.seed(2025)
10
+ np.random.seed(2025)
11
+
12
+ def process_data(file):
13
+ df = pd.read_csv(file.name)
14
+
15
+ df.columns = df.columns.str.strip().str.lower()
16
+ df = df.drop_duplicates()
17
+
18
+ for col in ["delivery_time_hours", "expected_time_hours"]:
19
+ df[col] = df[col].astype(str).str.split(".").str[-1]
20
+ df[col] = pd.to_numeric(df[col], errors="coerce")
21
+
22
+ numeric_cols = [
23
+ "distance_km","package_weight_kg",
24
+ "delivery_time_hours","expected_time_hours",
25
+ "delivery_rating","delivery_cost"
26
+ ]
27
+
28
+ for col in numeric_cols:
29
+ df[col] = pd.to_numeric(df[col], errors="coerce")
30
+ df[col] = df[col].fillna(df[col].median())
31
+
32
+ categorical_cols = [
33
+ "delivery_partner","package_type","vehicle_type",
34
+ "delivery_mode","region","weather_condition",
35
+ "delayed","delivery_status"
36
+ ]
37
+
38
+ for col in categorical_cols:
39
+ df[col] = df[col].astype(str).fillna(df[col].mode()[0])
40
+
41
+ df["expected_time_hours"] = df["distance_km"] / 45
42
+ df["delivery_time_hours"] = df["expected_time_hours"] * np.random.uniform(0.9, 1.2, len(df))
43
+
44
+ df["delay_hours"] = df["delivery_time_hours"] - df["expected_time_hours"]
45
+
46
+ df["delay_score"] = df["delay_hours"].apply(
47
+ lambda x: 5 if x <= 0 else 4 if x <= 2 else 3 if x <= 5 else 2 if x <= 8 else 1
48
+ )
49
+
50
+ df["performance_label"] = df["delay_score"].map({
51
+ 5:"Excellent",4:"Good",3:"Average",2:"Poor",1:"Critical"
52
+ })
53
+
54
+ return df
55
+
56
+ def kpi_section(df):
57
+ avg_delay = round(df["delay_hours"].mean(),2)
58
+ delay_rate = round((df["delay_hours"] > 0).mean()*100,2)
59
+ score = round(df["delay_score"].mean(),2)
60
+
61
+ return f"""### KPI Overview
62
+
63
+ - Average Delay: {avg_delay} hours
64
+ - Delay Rate: {delay_rate}%
65
+ - Performance Score: {score}
66
+ """
67
+
68
+ def quantitative_section(df):
69
+ fig1 = px.bar(df.groupby("vehicle_type")["delay_hours"].mean().reset_index(),
70
+ x="vehicle_type", y="delay_hours", title="Delay by Vehicle Type")
71
+
72
+ fig2 = px.bar(df.groupby("weather_condition")["delay_hours"].mean().reset_index(),
73
+ x="weather_condition", y="delay_hours", title="Delay by Weather")
74
+
75
+ return fig1, fig2
76
+
77
+ def qualitative_section(df):
78
+ worst_vehicle = df.groupby("vehicle_type")["delay_hours"].mean().idxmax()
79
+ worst_weather = df.groupby("weather_condition")["delay_hours"].mean().idxmax()
80
+
81
+ return f"""### Qualitative Insights
82
+
83
+ The analysis shows that **{worst_vehicle}** vehicles are associated with the highest delays.
84
+
85
+ Additionally, **{worst_weather}** conditions significantly increase delivery variability.
86
+
87
+ This suggests both internal and external factors drive delays.
88
+ """
89
+
90
+ def recommendation_section(df):
91
+ best_vehicle = df.groupby("vehicle_type")["delay_score"].mean().idxmax()
92
+ worst_vehicle = df.groupby("vehicle_type")["delay_score"].mean().idxmin()
93
+
94
+ return f"""### AI Management Recommendations
95
+
96
+ - Use more **{best_vehicle}** vehicles
97
+ - Reduce reliance on **{worst_vehicle}**
98
+ - Optimize routes under difficult weather conditions
99
+ - Improve weakest operational segments
100
+ """
101
+
102
+ def run_dashboard(file):
103
+ df = process_data(file)
104
+
105
+ kpi = kpi_section(df)
106
+ fig1, fig2 = quantitative_section(df)
107
+ qual = qualitative_section(df)
108
+ rec = recommendation_section(df)
109
+
110
+ return kpi, fig1, fig2, qual, rec
111
+
112
+ with gr.Blocks() as demo:
113
+ gr.Markdown("# AI Delivery Performance Dashboard")
114
+
115
+ file_input = gr.File(label="Upload CSV")
116
+ btn = gr.Button("Generate Dashboard")
117
+
118
+ with gr.Tab("1. KPI Overview"):
119
+ kpi_out = gr.Markdown()
120
+
121
+ with gr.Tab("2. Quantitative Analysis"):
122
+ chart1 = gr.Plot()
123
+ chart2 = gr.Plot()
124
+
125
+ with gr.Tab("3. Qualitative Analysis"):
126
+ qual_out = gr.Markdown()
127
+
128
+ with gr.Tab("4. AI Management Recommendations"):
129
+ rec_out = gr.Markdown()
130
+
131
+ btn.click(
132
+ run_dashboard,
133
+ inputs=file_input,
134
+ outputs=[kpi_out, chart1, chart2, qual_out, rec_out]
135
+ )
136
+
137
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ pandas
3
+ numpy
4
+ plotly