Sukumar2005 commited on
Commit
ea78b1e
·
verified ·
1 Parent(s): 206e14f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -17
app.py CHANGED
@@ -1,27 +1,26 @@
1
- # delivery_time_estimator.py
2
 
3
  import numpy as np
4
  import pandas as pd
5
  from sklearn.linear_model import LinearRegression
6
  import gradio as gr
 
7
 
8
  # -------------------------------------------
9
  # 1️⃣ Create sample dataset
10
  # -------------------------------------------
11
 
12
- # Let's say delivery time depends on distance (km), order size (items), and hour of day
13
  np.random.seed(42)
14
  num_samples = 200
15
 
16
- distance = np.random.uniform(1, 30, num_samples) # 1–30 km
17
- order_size = np.random.randint(1, 10, num_samples) # 1–9 items
18
- hour_of_day = np.random.randint(8, 23, num_samples) # deliveries between 8 AM and 10 PM
19
 
20
- # Hypothetical formula: delivery_time = 5 + 1.2*distance + 2*order_size + 0.5*hour + noise
21
  noise = np.random.normal(0, 5, num_samples)
22
  delivery_time = 5 + 1.2*distance + 2*order_size + 0.5*hour_of_day + noise
23
 
24
- # Build DataFrame
25
  df = pd.DataFrame({
26
  "distance": distance,
27
  "order_size": order_size,
@@ -40,29 +39,57 @@ model = LinearRegression()
40
  model.fit(X, y)
41
 
42
  # -------------------------------------------
43
- # 3️⃣ Define Gradio interface
44
  # -------------------------------------------
45
 
46
- def predict_delivery_time(distance, order_size, hour_of_day):
 
47
  features = np.array([[distance, order_size, hour_of_day]])
48
  prediction = model.predict(features)[0]
49
- return f"⏱️ Estimated delivery time: {prediction:.2f} minutes"
50
 
51
- # Create Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  iface = gr.Interface(
53
- fn=predict_delivery_time,
54
  inputs=[
55
  gr.Number(label="Distance (km)", value=5),
56
  gr.Number(label="Order Size (number of items)", value=3),
57
- gr.Number(label="Hour of Day (24h format, e.g., 14 for 2 PM)", value=12)
 
 
 
 
58
  ],
59
- outputs="text",
60
- title="🚚 Delivery Time Estimator",
61
- description="Enter distance, order size, and hour to predict estimated delivery time using linear regression."
62
  )
63
 
64
  # -------------------------------------------
65
- # 4️⃣ Launch app (locally or on Hugging Face Spaces)
66
  # -------------------------------------------
67
 
68
  if __name__ == "__main__":
 
1
+ # app.py
2
 
3
  import numpy as np
4
  import pandas as pd
5
  from sklearn.linear_model import LinearRegression
6
  import gradio as gr
7
+ import matplotlib.pyplot as plt
8
 
9
  # -------------------------------------------
10
  # 1️⃣ Create sample dataset
11
  # -------------------------------------------
12
 
 
13
  np.random.seed(42)
14
  num_samples = 200
15
 
16
+ distance = np.random.uniform(1, 30, num_samples)
17
+ order_size = np.random.randint(1, 10, num_samples)
18
+ hour_of_day = np.random.randint(8, 23, num_samples)
19
 
20
+ # delivery_time = base + 1.2*distance + 2*order_size + 0.5*hour + noise
21
  noise = np.random.normal(0, 5, num_samples)
22
  delivery_time = 5 + 1.2*distance + 2*order_size + 0.5*hour_of_day + noise
23
 
 
24
  df = pd.DataFrame({
25
  "distance": distance,
26
  "order_size": order_size,
 
39
  model.fit(X, y)
40
 
41
  # -------------------------------------------
42
+ # 3️⃣ Define prediction + graph function
43
  # -------------------------------------------
44
 
45
+ def predict_and_plot(distance, order_size, hour_of_day):
46
+ # Predict delivery time
47
  features = np.array([[distance, order_size, hour_of_day]])
48
  prediction = model.predict(features)[0]
 
49
 
50
+ # Create graph: vary distance from 1 to 30, keep other inputs fixed
51
+ distances = np.linspace(1, 30, 100)
52
+ inputs = np.column_stack((distances, np.full(100, order_size), np.full(100, hour_of_day)))
53
+ predicted_times = model.predict(inputs)
54
+
55
+ # Plot
56
+ plt.figure(figsize=(6, 4))
57
+ plt.plot(distances, predicted_times, label='Predicted delivery time', color='blue')
58
+ plt.scatter([distance], [prediction], color='red', label='Your input', zorder=5)
59
+ plt.xlabel('Distance (km)')
60
+ plt.ylabel('Estimated delivery time (minutes)')
61
+ plt.title('Delivery Time vs Distance')
62
+ plt.legend()
63
+ plt.tight_layout()
64
+
65
+ # Save plot to file
66
+ plot_path = "delivery_plot.png"
67
+ plt.savefig(plot_path)
68
+ plt.close()
69
+
70
+ return f"⏱️ Estimated delivery time: {prediction:.2f} minutes", plot_path
71
+
72
+ # -------------------------------------------
73
+ # 4️⃣ Gradio interface
74
+ # -------------------------------------------
75
+
76
  iface = gr.Interface(
77
+ fn=predict_and_plot,
78
  inputs=[
79
  gr.Number(label="Distance (km)", value=5),
80
  gr.Number(label="Order Size (number of items)", value=3),
81
+ gr.Number(label="Hour of Day (24h, e.g., 14 for 2 PM)", value=12)
82
+ ],
83
+ outputs=[
84
+ gr.Text(label="Prediction"),
85
+ gr.Image(label="Delivery Time vs Distance Graph")
86
  ],
87
+ title="🚚 Delivery Time Estimator with Graph",
88
+ description="Predicts delivery time and shows how it changes with distance (using linear regression)."
 
89
  )
90
 
91
  # -------------------------------------------
92
+ # 5️⃣ Launch app
93
  # -------------------------------------------
94
 
95
  if __name__ == "__main__":