MBG0903 commited on
Commit
94036ea
·
verified ·
1 Parent(s): bf6085d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -6
app.py CHANGED
@@ -1,13 +1,73 @@
1
  import gradio as gr
 
 
 
2
 
3
- def test(name):
4
- return f"Hello {name}, your Gradio Space is working!"
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  interface = gr.Interface(
7
- fn=test,
8
- inputs="text",
9
- outputs="text",
10
- title="Procelevate Route Planner Test"
 
 
 
 
 
 
 
 
11
  )
12
 
13
  interface.launch()
 
1
  import gradio as gr
2
+ import pandas as pd
3
+ import numpy as np
4
+ from ortools.constraint_solver import pywrapcp, routing_enums_pb2
5
 
6
+ def optimize_route(address_text):
 
7
 
8
+ # Parse addresses
9
+ addresses = [a.strip() for a in address_text.split("\n") if a.strip()]
10
+ if len(addresses) < 2:
11
+ return "❌ Please enter at least two locations.", None
12
+
13
+ # Generate fake symmetric distance matrix (Phase 1 demo)
14
+ n = len(addresses)
15
+ np.random.seed(42)
16
+ distance_matrix = np.random.randint(5, 50, size=(n, n))
17
+ np.fill_diagonal(distance_matrix, 0)
18
+
19
+ # OR-Tools Manager
20
+ manager = pywrapcp.RoutingIndexManager(n, 1, 0)
21
+ routing = pywrapcp.RoutingModel(manager)
22
+
23
+ def distance_callback(from_idx, to_idx):
24
+ return distance_matrix[manager.IndexToNode(from_idx)][manager.IndexToNode(to_idx)]
25
+
26
+ callback_idx = routing.RegisterTransitCallback(distance_callback)
27
+ routing.SetArcCostEvaluatorOfAllVehicles(callback_idx)
28
+
29
+ search_params = pywrapcp.DefaultRoutingSearchParameters()
30
+ search_params.first_solution_strategy = (
31
+ routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC
32
+ )
33
+
34
+ solution = routing.SolveWithParameters(search_params)
35
+ if not solution:
36
+ return "❌ Route optimization failed.", None
37
+
38
+ # Extract route
39
+ index = routing.Start(0)
40
+ ordered_stops = []
41
+ while not routing.IsEnd(index):
42
+ ordered_stops.append(manager.IndexToNode(index))
43
+ index = solution.Value(routing.NextVar(index))
44
+
45
+ ordered_addresses = [addresses[i] for i in ordered_stops]
46
+
47
+ # Build readable text
48
+ route_text = "🚚 **Optimized Route:**\n\n"
49
+ for i, loc in enumerate(ordered_addresses):
50
+ route_text += f"**{i+1}. {loc}**\n"
51
+
52
+ df = pd.DataFrame(distance_matrix, columns=addresses, index=addresses)
53
+
54
+ return route_text, df
55
+
56
+
57
+ # Gradio interface
58
  interface = gr.Interface(
59
+ fn=optimize_route,
60
+ inputs=gr.Textbox(
61
+ lines=8,
62
+ label="Enter addresses (one per line):",
63
+ placeholder="Example:\nBangalore Airport\nMG Road\nElectronic City\nWhitefield",
64
+ ),
65
+ outputs=[
66
+ gr.Markdown(label="Optimized Route"),
67
+ gr.Dataframe(label="Distance Matrix (Demo Mode)")
68
+ ],
69
+ title="Procelevate AI Route Planner",
70
+ description="AI-powered optimization for logistics & 3PL route planning.",
71
  )
72
 
73
  interface.launch()