Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from mapbox import Geocoder
|
4 |
+
|
5 |
+
# Mapbox token
|
6 |
+
MAPBOX_TOKEN = 'pk.eyJ1IjoiYWRpbDk4IiwiYSI6ImNtYjdzODZ1NzBkcXcybHM2OHcwZTZ5NzAifQ.SlPs3gdrGqD7iU3Fk35AFw'
|
7 |
+
|
8 |
+
# Initialize Mapbox geocoder
|
9 |
+
geocoder = Geocoder(access_token=MAPBOX_TOKEN)
|
10 |
+
|
11 |
+
# Get coordinates from location name
|
12 |
+
def get_coordinates(location_name):
|
13 |
+
response = geocoder.forward(location_name, limit=1)
|
14 |
+
if response.status_code == 200:
|
15 |
+
data = response.json()
|
16 |
+
if data['features']:
|
17 |
+
coords = data['features'][0]['center'] # [lon, lat]
|
18 |
+
return coords[1], coords[0]
|
19 |
+
return None
|
20 |
+
|
21 |
+
# Get distance and duration
|
22 |
+
def get_route_info(coord1, coord2):
|
23 |
+
url = f"http://router.project-osrm.org/route/v1/driving/{coord1[1]},{coord1[0]};{coord2[1]},{coord2[0]}?overview=false"
|
24 |
+
response = requests.get(url)
|
25 |
+
data = response.json()
|
26 |
+
if 'routes' in data and data['routes']:
|
27 |
+
distance_km = data['routes'][0]['distance'] / 1000
|
28 |
+
duration_min = data['routes'][0]['duration'] / 60
|
29 |
+
return distance_km, duration_min
|
30 |
+
return None, None
|
31 |
+
|
32 |
+
# Fuel estimation
|
33 |
+
def estimate_fuel(distance_km, avg_kmpl):
|
34 |
+
return round(distance_km / avg_kmpl, 2)
|
35 |
+
|
36 |
+
# Main function to integrate
|
37 |
+
def estimate_all(loc1, loc2, user_kmpl):
|
38 |
+
coord1 = get_coordinates(loc1)
|
39 |
+
coord2 = get_coordinates(loc2)
|
40 |
+
|
41 |
+
if not coord1 or not coord2:
|
42 |
+
return "Failed to get location coordinates. Please check input names.", None, None, None
|
43 |
+
|
44 |
+
distance, duration = get_route_info(coord1, coord2)
|
45 |
+
if distance is None:
|
46 |
+
return "Failed to retrieve route data.", None, None, None
|
47 |
+
|
48 |
+
fuel = estimate_fuel(distance, user_kmpl)
|
49 |
+
return (
|
50 |
+
"Here are the results:",
|
51 |
+
f"📍 Distance: {distance:.2f} km",
|
52 |
+
f"⏱️ Estimated Time: {duration:.2f} minutes",
|
53 |
+
f"⛽ Fuel Used: {fuel:.2f} litres (at {user_kmpl} km/l)"
|
54 |
+
)
|
55 |
+
|
56 |
+
# Gradio Interface
|
57 |
+
demo = gr.Interface(
|
58 |
+
fn=estimate_all,
|
59 |
+
inputs=[
|
60 |
+
gr.Textbox(label="Enter Location 1"),
|
61 |
+
gr.Textbox(label="Enter Location 2"),
|
62 |
+
gr.Slider(1.0, 100.0, value=15.0, label="Vehicle Mileage (km/l)")
|
63 |
+
],
|
64 |
+
outputs=[
|
65 |
+
gr.Textbox(label="Status"),
|
66 |
+
gr.Textbox(label="Distance"),
|
67 |
+
gr.Textbox(label="Time"),
|
68 |
+
gr.Textbox(label="Fuel Used")
|
69 |
+
],
|
70 |
+
title="Distance, Time & Fuel Estimator 🚗",
|
71 |
+
description="App Developed by Koshur AI"
|
72 |
+
)
|
73 |
+
|
74 |
+
demo.launch()
|