Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import pipeline
|
3 |
+
import folium
|
4 |
+
import numpy as np
|
5 |
+
from scipy.optimize import linear_sum_assignment
|
6 |
+
import pandas as pd
|
7 |
+
|
8 |
+
def calculate_distances(coordinates):
|
9 |
+
"""Calculate distances between all points using Haversine formula"""
|
10 |
+
n = len(coordinates)
|
11 |
+
distances = np.zeros((n, n))
|
12 |
+
|
13 |
+
for i in range(n):
|
14 |
+
for j in range(n):
|
15 |
+
if i != j:
|
16 |
+
# Haversine formula
|
17 |
+
lat1, lon1 = coordinates[i]
|
18 |
+
lat2, lon2 = coordinates[j]
|
19 |
+
|
20 |
+
R = 6371 # Earth's radius in kilometers
|
21 |
+
|
22 |
+
dlat = np.radians(lat2 - lat1)
|
23 |
+
dlon = np.radians(lon2 - lon1)
|
24 |
+
|
25 |
+
a = np.sin(dlat/2)**2 + np.cos(np.radians(lat1)) * np.cos(np.radians(lat2)) * np.sin(dlon/2)**2
|
26 |
+
c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1-a))
|
27 |
+
distances[i,j] = R * c
|
28 |
+
|
29 |
+
return distances
|
30 |
+
|
31 |
+
def optimize_route(coordinates):
|
32 |
+
"""Optimize route using Hungarian algorithm"""
|
33 |
+
distances = calculate_distances(coordinates)
|
34 |
+
row_ind, col_ind = linear_sum_assignment(distances)
|
35 |
+
return col_ind
|
36 |
+
|
37 |
+
def get_place_coordinates(place_name):
|
38 |
+
"""Get coordinates using Nominatim geocoding"""
|
39 |
+
try:
|
40 |
+
from geopy.geocoders import Nominatim
|
41 |
+
geolocator = Nominatim(user_agent="my_travel_app")
|
42 |
+
location = geolocator.geocode(place_name)
|
43 |
+
return (location.latitude, location.longitude) if location else None
|
44 |
+
except:
|
45 |
+
return None
|
46 |
+
|
47 |
+
def main():
|
48 |
+
st.title("AI Travel Route Optimizer")
|
49 |
+
|
50 |
+
# Sidebar for inputs
|
51 |
+
st.sidebar.header("Travel Parameters")
|
52 |
+
|
53 |
+
# Input for places
|
54 |
+
places = []
|
55 |
+
num_places = st.sidebar.number_input("Number of places to visit", min_value=2, max_value=10, value=3)
|
56 |
+
|
57 |
+
for i in range(num_places):
|
58 |
+
place = st.sidebar.text_input(f"Place {i+1}")
|
59 |
+
if place:
|
60 |
+
coordinates = get_place_coordinates(place)
|
61 |
+
if coordinates:
|
62 |
+
places.append((place, coordinates))
|
63 |
+
|
64 |
+
# Only proceed if we have all places
|
65 |
+
if len(places) == num_places and num_places >= 2:
|
66 |
+
|
67 |
+
# Extract coordinates for optimization
|
68 |
+
coordinates = [coord for _, coord in places]
|
69 |
+
|
70 |
+
# Optimize route
|
71 |
+
optimized_indices = optimize_route(coordinates)
|
72 |
+
|
73 |
+
# Create map
|
74 |
+
center_lat = sum(coord[0] for coord in coordinates) / len(coordinates)
|
75 |
+
center_lon = sum(coord[1] for coord in coordinates) / len(coordinates)
|
76 |
+
|
77 |
+
m = folium.Map(location=[center_lat, center_lon], zoom_start=4)
|
78 |
+
|
79 |
+
# Add markers and route lines
|
80 |
+
for i in range(len(optimized_indices)):
|
81 |
+
current_idx = optimized_indices[i]
|
82 |
+
next_idx = optimized_indices[(i + 1) % len(optimized_indices)]
|
83 |
+
|
84 |
+
# Add marker
|
85 |
+
place_name, (lat, lon) = places[current_idx]
|
86 |
+
folium.Marker(
|
87 |
+
[lat, lon],
|
88 |
+
popup=f"Stop {i+1}: {place_name}",
|
89 |
+
icon=folium.Icon(color='red', icon='info-sign')
|
90 |
+
).add_to(m)
|
91 |
+
|
92 |
+
# Add line to next point
|
93 |
+
next_lat, next_lon = coordinates[next_idx]
|
94 |
+
folium.PolyLine(
|
95 |
+
locations=[[lat, lon], [next_lat, next_lon]],
|
96 |
+
weight=2,
|
97 |
+
color='blue',
|
98 |
+
opacity=0.8
|
99 |
+
).add_to(m)
|
100 |
+
|
101 |
+
# Display map
|
102 |
+
st.components.v1.html(m._repr_html_(), height=600)
|
103 |
+
|
104 |
+
# Display itinerary
|
105 |
+
st.header("Optimized Itinerary")
|
106 |
+
for i in range(len(optimized_indices)):
|
107 |
+
st.write(f"{i+1}. {places[optimized_indices[i]][0]}")
|
108 |
+
|
109 |
+
# Calculate total distance
|
110 |
+
total_distance = 0
|
111 |
+
distances = calculate_distances(coordinates)
|
112 |
+
for i in range(len(optimized_indices)):
|
113 |
+
current_idx = optimized_indices[i]
|
114 |
+
next_idx = optimized_indices[(i + 1) % len(optimized_indices)]
|
115 |
+
total_distance += distances[current_idx][next_idx]
|
116 |
+
|
117 |
+
st.write(f"\nTotal distance: {total_distance:.2f} km")
|
118 |
+
|
119 |
+
if __name__ == "__main__":
|
120 |
+
main()
|