Spaces:
Running
Running
ML model
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import os, joblib
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
# load model pipeline
|
| 8 |
+
file_path = os.path.abspath('toolkit/pipeline.joblib')
|
| 9 |
+
pipeline = joblib.load(file_path)
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
#function to calculate week hour from weekday and hour
|
| 13 |
+
def calculate_pickup_week_hour(pickup_hour, pickup_weekday):
|
| 14 |
+
return pickup_weekday * 24 + pickup_hour
|
| 15 |
+
|
| 16 |
+
def predict(origin_lat, origin_lon, Destination_lat, Destination_lon,
|
| 17 |
+
Trip_distance, dewpoint_2m_temperature,
|
| 18 |
+
minimum_2m_air_temperature, pickup_weekday, pickup_hour,
|
| 19 |
+
cluster_id, temperature_range, rain):
|
| 20 |
+
|
| 21 |
+
# Calculate pickup_week_hour
|
| 22 |
+
pickup_week_hour = calculate_pickup_week_hour(pickup_hour, pickup_weekday)
|
| 23 |
+
|
| 24 |
+
# Modeling
|
| 25 |
+
try:
|
| 26 |
+
model_output = abs(int(pipeline.predict(pd.DataFrame([[origin_lat, origin_lon, Destination_lat, Destination_lon,
|
| 27 |
+
Trip_distance, dewpoint_2m_temperature,
|
| 28 |
+
minimum_2m_air_temperature, pickup_weekday, pickup_hour,
|
| 29 |
+
pickup_week_hour, cluster_id, temperature_range,
|
| 30 |
+
rain]], columns=['Origin_lat', 'Origin_lon', 'Destination_lat',
|
| 31 |
+
'Destination_lon', 'Trip_distance',
|
| 32 |
+
'dewpoint_2m_temperature',
|
| 33 |
+
'minimum_2m_air_temperature',
|
| 34 |
+
'pickup_weekday', 'pickup_hour',
|
| 35 |
+
'pickup_week_hour', 'cluster_id',
|
| 36 |
+
'temperature_range', 'rain']))))
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Error during prediction: {str(e)}")
|
| 39 |
+
model_output = 0
|
| 40 |
+
|
| 41 |
+
output_str = 'Hey there, Your ETA is'
|
| 42 |
+
dist = 'seconds'
|
| 43 |
+
|
| 44 |
+
return f"{output_str} {model_output} {dist}"
|
| 45 |
+
|
| 46 |
+
# UI layout
|
| 47 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 48 |
+
gr.Markdown("# ETA PREDICTION")
|
| 49 |
+
gr.Markdown("""This app uses a machine learning model to predict the ETA of trips on the Yassir Hailing App.Refer to the expander at the bottom for more information on the inputs.""")
|
| 50 |
+
|
| 51 |
+
with gr.Row():
|
| 52 |
+
origin_lat = gr.Slider(2.806, 3.373, step=0.001, interactive=True, value=2.806, label='Origin latitude')
|
| 53 |
+
origin_lon = gr.Slider(36.589, 36.820, step=0.001, interactive=True, value=36.589, label='Origin longitude')
|
| 54 |
+
Destination_lat = gr.Slider(2.807, 3.381, step=0.001, interactive=True, value=2.810, label='Destination latitude')
|
| 55 |
+
Destination_lon = gr.Slider(36.592, 36.819, step=0.001, interactive=True, value=36.596, label='Destination longitude')
|
| 56 |
+
Trip_distance = gr.Slider(0, 62028, step=1, interactive=True, value=1000, label='Trip distance (M)')
|
| 57 |
+
cluster_id = gr.Dropdown([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], label="Cluster ID", value=4)
|
| 58 |
+
|
| 59 |
+
with gr.Column():
|
| 60 |
+
pickup_weekday = gr.Dropdown([0, 1, 2, 3, 4, 5, 6], value=3, label='Pickup weekday')
|
| 61 |
+
pickup_hour = gr.Dropdown([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
|
| 62 |
+
value=13, label='Pickup hour')
|
| 63 |
+
|
| 64 |
+
with gr.Column():
|
| 65 |
+
dewpoint_2m_temperature = gr.Slider(279.129, 286.327, step=0.001, interactive=True, value=282.201,
|
| 66 |
+
label='dewpoint_2m_temperature')
|
| 67 |
+
minimum_2m_air_temperature = gr.Slider(282.037, 292.543, step=0.01, interactive=True, value=285.203,
|
| 68 |
+
label='minimum_2m_air_temperature')
|
| 69 |
+
temperature_range = gr.Slider(1.663, 10.022, step=0.01, interactive=True, value=5, label='temperature_range')
|
| 70 |
+
rain = gr.Dropdown([0, 1], label='Is it raining (0=No, 1=Yes)')
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
btn = gr.Button("Predict")
|
| 74 |
+
output = gr.Textbox(label="Prediction")
|
| 75 |
+
|
| 76 |
+
# Expander for more info on columns
|
| 77 |
+
with gr.Accordion("Information on inputs"):
|
| 78 |
+
gr.Markdown("""These are information on the inputs the app takes for predicting a rides ETA.
|
| 79 |
+
- Origin latitude: Origin in degree latitude)
|
| 80 |
+
- Origin longitude: Origin in degree longitude
|
| 81 |
+
- Destination latitude: Destination latitude
|
| 82 |
+
- Destination longitude: Destination logitude
|
| 83 |
+
- Trip distance (M): Distance in meters on a driving route
|
| 84 |
+
- Cluster ID: Select the cluster within which you started your trip
|
| 85 |
+
- Pickup weekday: Day of the week
|
| 86 |
+
Monday=0, Tuesday=1, Wednesday=2, Thursday=3, Friday=4, Saturday=5, Sunday=6
|
| 87 |
+
- Pickup hour: The hour of the day (24hr clock)
|
| 88 |
+
- dewpoint_2m_temperature: The temperature at 2 meters above the ground where the air temperature would be
|
| 89 |
+
low enough for dew to form. It gives an indication of humidity.
|
| 90 |
+
- minimum_2m_air_temperature: The lowest air temperature recorded at 2 meters above the ground during the specified date.
|
| 91 |
+
- temperature_range: The air temperature range recorded at 2 meters above the ground on the day
|
| 92 |
+
- rain: Is it raining? yes=1, no=2
|
| 93 |
+
""")
|
| 94 |
+
|
| 95 |
+
btn.click(fn=predict, inputs=[origin_lat, origin_lon, Destination_lat, Destination_lon,
|
| 96 |
+
Trip_distance, dewpoint_2m_temperature,
|
| 97 |
+
minimum_2m_air_temperature, pickup_weekday, pickup_hour,
|
| 98 |
+
cluster_id, temperature_range,
|
| 99 |
+
rain], outputs=output)
|
| 100 |
+
|
| 101 |
+
app.launch(share=True, debug=True)
|