File size: 4,014 Bytes
9605944
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import csv
import numpy as np
import pandas as pd
import gradio as gr
import joblib as jb
import os.path as path

import warnings
warnings.filterwarnings("ignore")

# Loading features names
file_csv = path.join("model" ,"model_features.csv")
with open(file_csv) as f:
    reader = csv.reader(f)
    data = list(reader)

features = data[0]

# Creating a list with accident types
accident_type_list = [None,
                      "type_ATROPELAMENTO",
                      "type_CHOQUE",
                      "type_COLISÃO",
                      "type_OUTROS"]

# Loading the scaler
file_scaler_feridos = path.join("model" ,"scaler_feridos.pkl")
scaler_feridos = jb.load(file_scaler_feridos)

# Loading the model
file_model_feridos = path.join("model" ,"model_feridos.pkl")
model_feridos = jb.load(file_model_feridos)

def fit_inputs_injured(latitude, 
        longitude,
        caminhao,
        moto,
        cars,
        transport,
        others,
        holiday,
        week_day,
        hour_day,
        accident_type) -> np.array:
    """This function will process data input
    from use to use in the model"""
    input_dict = {col: False for col in features}

    input_dict["latitude"] = latitude
    input_dict["longitude"] = longitude
    input_dict["caminhao"] = caminhao
    input_dict["moto"] = moto
    input_dict["cars"] = cars
    input_dict["transport"] = transport
    input_dict["others"] = others
    input_dict["holiday"] = holiday

    if week_day != 0:
        input_dict["day_" + str(week_day)] = True

    if hour_day != 0:
        input_dict["hour_" + str(hour_day)] = True

    if accident_type != 0:
        input_dict[accident_type_list[accident_type]] = True

    input_series = pd.Series(input_dict)

    input_array = input_series.to_numpy().reshape(1,-1)

    input_scaled = scaler_feridos.transform(input_array)

    return input_scaled

def predict(
        latitude, 
        longitude,
        caminhao,
        moto,
        cars,
        transport,
        others,
        holiday,
        week_day,
        hour_day,
        accident_type) -> dict:
    """This function will be call by gradio
    when on submit action."""

    input_to_predict = fit_inputs_injured(latitude, 
        longitude,
        caminhao,
        moto,
        cars,
        transport,
        others,
        holiday,
        week_day,
        hour_day,
        accident_type)

    predic_injured = model_feridos.predict_proba(input_to_predict)

    return {"No": predic_injured[0][0], "Yes": predic_injured[0][1]}

demo = gr.Interface(
    fn=predict,
    inputs=[gr.Slider(
                minimum=-31.054,
                maximum=-29.054,
                step=0.001,
                value=-30.054,
                label="Latitude"),
            gr.Slider(
                minimum=-52.196,
                maximum=-50.196,
                step=0.001,
                value=-51.196,
                label="Longitude"),
            gr.Checkbox(label="Trucks involved?"),
            gr.Checkbox(label="Motorcycle involved?"),    
            gr.Checkbox(label="Cars involved?"),
            gr.Checkbox(label="Bus involved?"),
            gr.Checkbox(label="Other vehicle (i.e. scooter) involved?"),
            gr.Checkbox(label="Is holiday?"),
            gr.Radio(
                choices=["Sun", "Mon",
                         "Tue", "Wed",
                         "Thu", "Fri",
                         "Sat"],
                type="index",
                label="Day of Week"),
            gr.Slider(
                minimum=0,
                maximum=23,
                step=1,
                label="Hour"),
            gr.Dropdown(
                choices=["Violent Collision",
                         "Running over",
                         "Shock",
                         "Collision",
                         "Other"],
                type="index",
                label="Accident type")],
    outputs=gr.Label(
        label="Are there people injured?"))

demo.launch()