jdingeman's picture
update label for qod
1e4dde8
import gradio as gr
import joblib
import numpy as np
import sklearn
import pandas as pd
model = joblib.load("joblib/rf_classifier_CA.joblib")
qod_encoder = joblib.load("joblib/qod_encoder_CA.joblib")
def predict(pressure, temperature, humidity, visibility, wind_speed, precipitation, amenity,
bump, crossing, give_way, junction, no_exit, railway, station,
stop, traffic_signal, quarter):
encoded_quarter = qod_encoder.transform(pd.DataFrame([[quarter]], columns=qod_encoder.feature_names_in_))[0][0]
# Ensure checkbox values default to False if None
checkbox_values = [amenity, bump, crossing, give_way, junction, no_exit,
railway, station, stop, traffic_signal]
checkbox_values = [bool(value) if value is not None else False for value in checkbox_values]
print("Inputs Received:")
print(
f"quarter: {quarter} - {encoded_quarter}, pressure: {pressure}, temperature: {temperature}, "
f"humidity: {humidity}, visibility: {visibility}, wind_speed: {wind_speed}, "
f"precipitation: {precipitation}, "
f"amenity: {amenity}, bump: {bump}, crossing: {crossing}, give_way: {give_way}, "
f"junction: {junction}, no_exit: {no_exit}, railway: {railway}, "
f"station: {station}, stop: {stop}, traffic_signal: {traffic_signal}")
# Convert features to the required format
input_data = np.array([pressure, temperature, humidity, visibility,
wind_speed, precipitation, amenity, bump, crossing,
give_way, junction, no_exit, railway, station, stop, traffic_signal,
encoded_quarter]).reshape(1, -1)
prediction = model.predict(input_data)
return prediction[0]
# Create Gradio interface
iface = gr.Interface(
fn=predict,
inputs=[
gr.Number(label="Pressure(in) (27.31 - 30.61)", step=0.01, minimum=27.31, maximum=30.61, value=27.31),
gr.Number(label="Temperature(in) (15.8 - 117.0)", step=0.1, minimum=15.8, maximum=117, value=15.8),
gr.Number(label="Humidity(%) (2 - 100)", step=1, minimum=2, maximum=100, value=100),
gr.Number(label="Visibility(mi) (0.0 - 80.0)", step=0.1, minimum=0, maximum=80, value=0),
gr.Number(label="Wind Speed(mph) (0.0 - 73.6)", step=0.1, minimum=0, maximum=73.6, value=0),
gr.Number(label="Precipitation (0.00 - 1.00)", step=0.01,minimum=0, maximum=1, value=0),
gr.Checkbox(label="Amenity"),
gr.Checkbox(label="Bump"),
gr.Checkbox(label="Crossing"),
gr.Checkbox(label="Give Way"),
gr.Checkbox(label="Junction"),
gr.Checkbox(label="No Exit"),
gr.Checkbox(label="Railway"),
gr.Checkbox(label="Station"),
gr.Checkbox(label="Stop"),
gr.Checkbox(label="Traffic_Signal"),
gr.Dropdown(choices=['Q1', 'Q2', 'Q3', 'Q4'], label="Quarter of Day (Q1 = 12:00am-5:59am; Q2 = 6:00am-11:59am; Q3 = 12:00pm-5:59pm; Q4 = 6:00pm-11:59pm)"),
],
outputs=gr.Textbox(label="Predicted Severity (1 - low impact on traffic; 2 - moderate impact on traffic; "
"3 - heavy impact on traffic; 4 - extreme impact on traffic/gridlock)"),
title="CA Traffic Flow Impact Severity Prediction"
)
# Launch the interface
iface.launch()