elifsara commited on
Commit
cfdd931
·
verified ·
1 Parent(s): ebbc51d

Upload 4 files

Browse files
Files changed (4) hide show
  1. Taxi_Trip_Duration.h5 +3 -0
  2. app.py +109 -0
  3. preprocessor.pkl +3 -0
  4. requirements.txt +10 -0
Taxi_Trip_Duration.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f926d184068226ee17d20dc0bc381de5fa2937717b78e88c290c7d4a38f4c447
3
+ size 300120
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import folium as fl
2
+ from streamlit_folium import st_folium
3
+ import streamlit as st
4
+ import googlemaps
5
+ import pandas as pd
6
+ from tensorflow.keras.models import load_model
7
+ import numpy as np
8
+ import pickle
9
+
10
+ st.title('Taxi Trip Duration Prediction 🚖')
11
+
12
+ # Load trained model and preprocessor
13
+ model = load_model('Taxi_Trip_Duration.h5', compile=False)
14
+ with open('preprocessor.pkl', 'rb') as f:
15
+ preprocessor = pickle.load(f)
16
+
17
+ # Google Maps API client
18
+ gmaps_api_key = 'AIzaSyCmyJwPBalqt1djDKK09YY2iYL7_cVA7e8'
19
+ gmaps = googlemaps.Client(key=gmaps_api_key)
20
+
21
+ def calculate_distance(pickup_lat, pickup_lng, dropoff_lat, dropoff_lng):
22
+ directions_result = gmaps.directions((pickup_lat, pickup_lng), (dropoff_lat, dropoff_lng), mode="driving")
23
+ distance = directions_result[0]['legs'][0]['distance']['value'] / 1000.0 # Distance in kilometers
24
+ return distance
25
+
26
+ def predict_duration(features):
27
+ predicted_duration = model.predict(features)[0][0]
28
+ return predicted_duration
29
+
30
+ # Initialize session state variables
31
+ if 'pickup_location' not in st.session_state:
32
+ st.session_state.pickup_location = None
33
+ if 'dropoff_location' not in st.session_state:
34
+ st.session_state.dropoff_location = None
35
+ if 'last_clicked' not in st.session_state:
36
+ st.session_state.last_clicked = None
37
+ if 'step' not in st.session_state:
38
+ st.session_state.step = 0
39
+ if 'distance' not in st.session_state:
40
+ st.session_state.distance = None
41
+ if 'predicted_duration' not in st.session_state:
42
+ st.session_state.predicted_duration = None
43
+
44
+ # Function to save location based on map click
45
+ def save_location(location_type):
46
+ if st.session_state[location_type] is None and st.session_state.last_clicked:
47
+ st.session_state[location_type] = (st.session_state.last_clicked['lat'], st.session_state.last_clicked['lng'])
48
+ st.session_state.last_clicked = None
49
+ st.session_state.step += 1
50
+ st.experimental_rerun() # Trigger a rerun to update the state and UI
51
+
52
+ # Step 0: Select pickup location on the map
53
+ if st.session_state.step == 0:
54
+ st.write('Please select the pickup location on the map 🚖')
55
+ starting_point = (37.0, 35.3213) # Adana coordinates
56
+ m = fl.Map(location=starting_point, zoom_start=12)
57
+ m.add_child(fl.LatLngPopup())
58
+ map_data = st_folium(m, height=500)
59
+ if map_data and 'last_clicked' in map_data and map_data['last_clicked']:
60
+ st.session_state.last_clicked = map_data['last_clicked']
61
+ if st.button("Save Pickup Location"):
62
+ save_location('pickup_location')
63
+
64
+ # Step 1: Select dropoff location on the map
65
+ if st.session_state.step == 1 and st.session_state.pickup_location is not None:
66
+ st.write('Pickup location selected. Now select the dropoff location 📍')
67
+ m = fl.Map(location=st.session_state.pickup_location, zoom_start=12)
68
+ fl.Marker(st.session_state.pickup_location, popup="Pickup Location").add_to(m)
69
+ m.add_child(fl.LatLngPopup())
70
+ map_data = st_folium(m, height=500)
71
+ if map_data and 'last_clicked' in map_data and map_data['last_clicked']:
72
+ st.session_state.last_clicked = map_data['last_clicked']
73
+ if st.button("Save Dropoff Location"):
74
+ save_location('dropoff_location')
75
+
76
+ # Step 2: Enter additional trip details and display prediction
77
+ if st.session_state.step == 2 and st.session_state.pickup_location is not None and st.session_state.dropoff_location is not None:
78
+ st.write('Dropoff location selected. Please enter additional trip details.')
79
+
80
+ passenger_count = st.slider('Passenger Count', min_value=1, max_value=6, value=1)
81
+ hour = st.slider('Hour of Day', min_value=0, max_value=23, value=12)
82
+ day = st.slider('Day of Month', min_value=1, max_value=31, value=15)
83
+ weekday = st.selectbox('Weekday', ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'])
84
+
85
+ weekday_dict = {'Monday': 0, 'Tuesday': 1, 'Wednesday': 2, 'Thursday': 3, 'Friday': 4, 'Saturday': 5, 'Sunday': 6}
86
+ weekday_numeric = weekday_dict[weekday]
87
+
88
+ # Calculate distance between pickup and dropoff locations
89
+ pickup_lat, pickup_lng = st.session_state.pickup_location
90
+ dropoff_lat, dropoff_lng = st.session_state.dropoff_location
91
+ distance = calculate_distance(pickup_lat, pickup_lng, dropoff_lat, dropoff_lng)
92
+ st.session_state.distance = distance
93
+
94
+ # Prepare input features for prediction
95
+ features = np.array([[pickup_lat, pickup_lng, dropoff_lat, dropoff_lng, passenger_count, distance, hour, day, weekday_numeric]])
96
+ features_scaled = preprocessor.transform(features)
97
+
98
+ # Predict trip duration
99
+ predicted_duration = predict_duration(features_scaled)
100
+ st.session_state.predicted_duration = predicted_duration
101
+
102
+ st.write(f'Predicted Trip Duration: {predicted_duration:.2f} seconds')
103
+
104
+ # Show map with pickup and dropoff locations and route
105
+ m = fl.Map(location=st.session_state.pickup_location, zoom_start=12)
106
+ fl.Marker(st.session_state.pickup_location, popup="Pickup Location").add_to(m)
107
+ fl.Marker(st.session_state.dropoff_location, popup="Dropoff Location").add_to(m)
108
+ fl.PolyLine([st.session_state.pickup_location, st.session_state.dropoff_location], color="blue").add_to(m)
109
+ st_folium(m, height=500)
preprocessor.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9e7dc5f43a8bff9c0b8d878f0867167fe22439937dedad7425e2fb068ce1e398
3
+ size 2820
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ streamlit-folium
3
+ folium
4
+ pandas
5
+ tensorflow
6
+ requests
7
+ googlemaps
8
+ numpy
9
+ scikit-learn
10
+ matplotlib