File size: 1,924 Bytes
b66416d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from sklearn.preprocessing import LabelEncoder
from huggingface_hub import hf_hub_download
import pandas as pd
import gradio as gr
import joblib


MODEL_NAME = "regressiontest"
HF_USER = "universalml"
REPO_ID = HF_USER + "/" + MODEL_NAME
MODEL = joblib.load(hf_hub_download(repo_id=REPO_ID, filename="model.joblib"))
SCALER = joblib.load(hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib"))


def encode_categorical_columns(data_frame):
    label_encoder = LabelEncoder()
    ordinal_columns = data_frame.select_dtypes(include=['object']).columns

    for col in ordinal_columns:
        data_frame[col] = label_encoder.fit_transform(data_frame[col])

    nominal_columns = data_frame.select_dtypes(include=['object']).columns.difference(ordinal_columns)
    data_frame = pd.get_dummies(data_frame, columns=nominal_columns, drop_first=True)

    return data_frame


def prediction_function(*args):
    values_list = []

    for arg in args:
        values_list.append(int(arg))

    input_data_frame = pd.DataFrame([values_list], columns=MODEL.data)
    data_frame = encode_categorical_columns(input_data_frame)
    scaled_input = SCALER.transform(data_frame)
    prediction_result = MODEL.predict(scaled_input)[0]

    return prediction_result


def regression_inputs():
    input_labels = MODEL.data
    inputs = []

    for input_label in input_labels:
        value = gr.Textbox(label=input_label, type="text")
        inputs.append(value)

    return inputs


def regression_output():
    output_label = MODEL.target
    output = gr.Textbox(label=output_label, type="text")

    return output


def create_interface():
    interface = gr.Interface(
        fn=prediction_function,
        inputs=regression_inputs(),
        outputs=regression_output(),
        title=MODEL_NAME,
    )

    interface.launch(debug=True)


create_interface()