File size: 3,180 Bytes
7630c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from __future__ import annotations

from pathlib import Path

import gradio as gr
import joblib

from taxi_fare import prepare_inference_frame

MODEL_PATH = Path("artifacts/taxi_fare_ann_model.joblib")


def load_model():
    if not MODEL_PATH.exists():
        raise FileNotFoundError(
            f"Model file not found at {MODEL_PATH}. Train the model first with train.py."
        )
    return joblib.load(MODEL_PATH)


try:
    model = load_model()
except FileNotFoundError:
    model = None


def predict_fare(pickup_datetime, pickup_longitude, pickup_latitude, dropoff_longitude, dropoff_latitude, passenger_count):
    if model is None:
        raise gr.Error("Model file is missing. Train the model first and place artifacts/taxi_fare_ann_model.joblib in the project.")

    features = prepare_inference_frame(
        pickup_datetime=pickup_datetime,
        pickup_longitude=pickup_longitude,
        pickup_latitude=pickup_latitude,
        dropoff_longitude=dropoff_longitude,
        dropoff_latitude=dropoff_latitude,
        passenger_count=passenger_count,
    )
    prediction = float(model.predict(features)[0])
    return round(max(prediction, 0.0), 2)


with gr.Blocks() as demo:
    gr.Markdown(
        """
        # NYC Taxi Fare Prediction
        Predict taxi fare using an Artificial Neural Network trained on the NYC Taxi Fare Prediction dataset.
        """
    )

    with gr.Row():
        pickup_datetime = gr.Textbox(
            label="Pickup datetime",
            value="2015-01-01 12:00:00",
            placeholder="YYYY-MM-DD HH:MM:SS",
        )
        passenger_count = gr.Number(label="Passenger count", value=1, precision=0)

    with gr.Row():
        pickup_longitude = gr.Number(label="Pickup longitude", value=-73.985428)
        pickup_latitude = gr.Number(label="Pickup latitude", value=40.748817)

    with gr.Row():
        dropoff_longitude = gr.Number(label="Dropoff longitude", value=-73.985130)
        dropoff_latitude = gr.Number(label="Dropoff latitude", value=40.758896)

    predict_button = gr.Button("Predict Fare")
    output = gr.Number(label="Predicted fare amount ($)")

    predict_button.click(
        fn=predict_fare,
        inputs=[
            pickup_datetime,
            pickup_longitude,
            pickup_latitude,
            dropoff_longitude,
            dropoff_latitude,
            passenger_count,
        ],
        outputs=output,
    )

    gr.Examples(
        examples=[
            ["2015-01-01 12:00:00", -73.985428, 40.748817, -73.985130, 40.758896, 1],
            ["2015-06-18 18:30:00", -73.985656, 40.758896, -73.971249, 40.7831, 2],
        ],
        inputs=[
            pickup_datetime,
            pickup_longitude,
            pickup_latitude,
            dropoff_longitude,
            dropoff_latitude,
            passenger_count,
        ],
        label="Sample trips",
    )


if __name__ == "__main__":
    # Some Gradio versions (on Spaces) may not accept the `theme` kwarg for `launch()`.
    # Try to launch with the theme and fall back to a plain launch on TypeError.
    try:
        demo.launch(theme=gr.themes.Soft())
    except TypeError:
        demo.launch()