Spaces:
Runtime error
Runtime error
universalml
commited on
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from sklearn.preprocessing import LabelEncoder
|
2 |
+
from huggingface_hub import hf_hub_download
|
3 |
+
import pandas as pd
|
4 |
+
import gradio as gr
|
5 |
+
import joblib
|
6 |
+
|
7 |
+
|
8 |
+
MODEL_NAME = "regressiontest"
|
9 |
+
HF_USER = "universalml"
|
10 |
+
REPO_ID = HF_USER + "/" + MODEL_NAME
|
11 |
+
MODEL = joblib.load(hf_hub_download(repo_id=REPO_ID, filename="model.joblib"))
|
12 |
+
SCALER = joblib.load(hf_hub_download(repo_id=REPO_ID, filename="scaler.joblib"))
|
13 |
+
|
14 |
+
|
15 |
+
def encode_categorical_columns(data_frame):
|
16 |
+
label_encoder = LabelEncoder()
|
17 |
+
ordinal_columns = data_frame.select_dtypes(include=['object']).columns
|
18 |
+
|
19 |
+
for col in ordinal_columns:
|
20 |
+
data_frame[col] = label_encoder.fit_transform(data_frame[col])
|
21 |
+
|
22 |
+
nominal_columns = data_frame.select_dtypes(include=['object']).columns.difference(ordinal_columns)
|
23 |
+
data_frame = pd.get_dummies(data_frame, columns=nominal_columns, drop_first=True)
|
24 |
+
|
25 |
+
return data_frame
|
26 |
+
|
27 |
+
|
28 |
+
def prediction_function(*args):
|
29 |
+
values_list = []
|
30 |
+
|
31 |
+
for arg in args:
|
32 |
+
values_list.append(int(arg))
|
33 |
+
|
34 |
+
input_data_frame = pd.DataFrame([values_list], columns=MODEL.data)
|
35 |
+
data_frame = encode_categorical_columns(input_data_frame)
|
36 |
+
scaled_input = SCALER.transform(data_frame)
|
37 |
+
prediction_result = MODEL.predict(scaled_input)[0]
|
38 |
+
|
39 |
+
return prediction_result
|
40 |
+
|
41 |
+
|
42 |
+
def regression_inputs():
|
43 |
+
input_labels = MODEL.data
|
44 |
+
inputs = []
|
45 |
+
|
46 |
+
for input_label in input_labels:
|
47 |
+
value = gr.Textbox(label=input_label, type="text")
|
48 |
+
inputs.append(value)
|
49 |
+
|
50 |
+
return inputs
|
51 |
+
|
52 |
+
|
53 |
+
def regression_output():
|
54 |
+
output_label = MODEL.target
|
55 |
+
output = gr.Textbox(label=output_label, type="text")
|
56 |
+
|
57 |
+
return output
|
58 |
+
|
59 |
+
|
60 |
+
def create_interface():
|
61 |
+
interface = gr.Interface(
|
62 |
+
fn=prediction_function,
|
63 |
+
inputs=regression_inputs(),
|
64 |
+
outputs=regression_output(),
|
65 |
+
title=MODEL_NAME,
|
66 |
+
)
|
67 |
+
|
68 |
+
interface.launch(debug=True)
|
69 |
+
|
70 |
+
|
71 |
+
create_interface()
|