Spaces:
Sleeping
Sleeping
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Import the required Libraries
|
2 |
+
import gradio as gr
|
3 |
+
import numpy as np
|
4 |
+
import pandas as pd
|
5 |
+
import os, joblib
|
6 |
+
import re
|
7 |
+
|
8 |
+
|
9 |
+
pipeline = joblib.load(r'toolkit\pipeline.joblib')
|
10 |
+
|
11 |
+
|
12 |
+
inputs = ['Origin_lat', 'Origin_lon', 'Destination_lat', 'Destination_lon',
|
13 |
+
'Trip_distance', 'Speed', 'dewpoint_2m_temperature',
|
14 |
+
'mean_2m_air_temperature', 'pickup_weekday', 'pickup_weekofyear',
|
15 |
+
'pickup_hour', 'pickup_minute', 'pickup_week_hour', 'month',
|
16 |
+
'day_of_week', 'cluster_id', 'target_transformed', 'temperature_range',
|
17 |
+
'wind_speed', 'rain']
|
18 |
+
|
19 |
+
def predict(*args, pipeline=pipeline, inputs=inputs):
|
20 |
+
# Check if inputs is provided
|
21 |
+
if inputs is None:
|
22 |
+
raise ValueError("Please provide the 'inputs' parameter.")
|
23 |
+
|
24 |
+
# Creating a DataFrame of inputs
|
25 |
+
input_data = pd.DataFrame([args], columns=inputs)
|
26 |
+
print(input_data)
|
27 |
+
|
28 |
+
# Modeling
|
29 |
+
try:
|
30 |
+
model_output = abs(int(pipeline.predict(input_data)))
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Error during prediction: {str(e)}")
|
33 |
+
model_output = 0
|
34 |
+
|
35 |
+
output_str = 'Hey there, Your ETA is'
|
36 |
+
dist = 'seconds'
|
37 |
+
|
38 |
+
return f"{output_str} {model_output} {dist}"
|
39 |
+
|
40 |
+
|
41 |
+
with gr.Blocks(theme=gr.themes.Monochrome()) as app:
|
42 |
+
gr.Markdown("# ETA PREDICTION")
|
43 |
+
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.""")
|
44 |
+
|
45 |
+
with gr.Row():
|
46 |
+
origin_lat= gr.Slider(2.806,3.381,step = 0.01,interactive=True, value=2.806, label = 'origin_lat')
|
47 |
+
origin_lon = gr.Slider(36.589,36.820,step =0.01,interactive=True, value=36.589,label = 'origin_lon')
|
48 |
+
Destination_lat =gr.Slider(2.807,3.381,step = 0.1,interactive=True, value=2.81,label ='Destination_lat')
|
49 |
+
Destination_lon =gr.Slider(36.592,36.819,step = 0.1,interactive=True, value=36.596,label ='Destination_lon')
|
50 |
+
Trip_distance = gr.Slider(61,958,step =10,interactive=True, value=20,label = 'Trip_distance')
|
51 |
+
Speed = gr.Slider(0.300,85.000, step=0.01, interactive=True, value=20, label = 'Speed')
|
52 |
+
|
53 |
+
with gr.Column():
|
54 |
+
dewpoint_2m_temperature =gr.Slider(280.000, 288.000, step = 0.01,interactive=True, value=282.201,label ='dewpoint_2m_temperature')
|
55 |
+
mean_2m_air_temperature =gr.Slider(285.203, 291.110,step = 0.1,interactive=True, value=287.203,label ='mean_2m_air_temperature')
|
56 |
+
pickup_weekday = gr.Dropdown([0,1,2,3,4,5,6],label ='pickup_weekday', value=3)
|
57 |
+
pickup_weekofyear = gr.Dropdown([47,48,49,50],label="pickup_weekofyear",value=3)
|
58 |
+
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]
|
59 |
+
,label="pickup_hour",value=13)
|
60 |
+
pickup_minute = gr.Slider(0, 59, step = 1,interactive=True, value=5,label ='pickup_minute')
|
61 |
+
pickup_week_hour = gr.Slider(0, 167, step = 1,interactive=True, value=5,label ='pickup_week_hour')
|
62 |
+
month = gr.Dropdown([11,12],label = 'month')
|
63 |
+
day_of_week = gr.Dropdown([0,1,2,3,4,5,6],label ='day_of_week', value=3)
|
64 |
+
cluster_id = gr.Dropdown([1,2,3,4,5,6,7],label="Cluster ID", value=4)
|
65 |
+
|
66 |
+
with gr.Column():
|
67 |
+
target_transformed = gr.Slider(0, 336.426, step = 0.01,interactive=True, value=100,label ='target_transformed')
|
68 |
+
temperature_range = gr.Slider(2.317, 9.166, step = 0.01,interactive=True, value=5,label='temperature_range')
|
69 |
+
wind_speed = gr.Slider(0.803, 9.887, step = 0.01,interactive=True, value=5,label='wind_speed')
|
70 |
+
rain = gr.Dropdown([0,1],label='rain')
|
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_lat: Origin in degree latitude)
|
80 |
+
- origin_lon: Origin in degree longitude
|
81 |
+
- Destination_lat: Destination latitude
|
82 |
+
- Destination_lon: Destination logitude
|
83 |
+
- Trip Distance : Distance in meters on a driving route
|
84 |
+
- Cluster ID : Select the cluster within which you started your trip
|
85 |
+
- Time of the day: What time in the day did your trip start, 1- morning(or daytime),2 - evening 3- midnight
|
86 |
+
""")
|
87 |
+
btn.click(fn = predict,inputs= [origin_lat, origin_lon, Destination_lat, Destination_lon,
|
88 |
+
Trip_distance, Speed, dewpoint_2m_temperature,
|
89 |
+
mean_2m_air_temperature, pickup_weekday, pickup_weekofyear,
|
90 |
+
pickup_hour, pickup_minute, pickup_week_hour, month,
|
91 |
+
day_of_week, cluster_id, target_transformed, temperature_range,
|
92 |
+
wind_speed, rain], outputs = output)
|
93 |
+
|
94 |
+
app.launch(share = True, debug =True)
|