universalml commited on
Commit
677c490
·
verified ·
1 Parent(s): b36638f

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sklearn.preprocessing import StandardScaler, 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 = "mvs"
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
+
13
+
14
+ def encode_categorical_columns(data_frame):
15
+ label_encoder = LabelEncoder()
16
+ ordinal_columns = data_frame.select_dtypes(include=['object']).columns
17
+
18
+ for col in ordinal_columns:
19
+ data_frame[col] = label_encoder.fit_transform(data_frame[col])
20
+
21
+ nominal_columns = data_frame.select_dtypes(include=['object']).columns.difference(ordinal_columns)
22
+ data_frame = pd.get_dummies(data_frame, columns=nominal_columns, drop_first=True)
23
+
24
+ return data_frame
25
+
26
+
27
+ def prediction_function(*args):
28
+ values_list = []
29
+
30
+ for arg in args:
31
+ values_list.append(int(arg))
32
+
33
+ input_data_frame = pd.DataFrame(values_list, columns=model.input_feature_names)
34
+ data_frame = encode_categorical_columns(input_data_frame)
35
+ scaler = StandardScaler()
36
+ # Scale the input data using the loaded scaler
37
+ scaled_input = scaler.transform(data_frame)
38
+ prediction_result = model.predict(scaled_input)[0]
39
+
40
+ return prediction_result
41
+
42
+
43
+ def regression_inputs():
44
+ input_labels = model.input_feature_names
45
+ inputs = []
46
+
47
+ for input_label in input_labels:
48
+ value = gr.Textbox(label=input_label, type="text")
49
+ inputs.append(value)
50
+
51
+ return inputs
52
+
53
+
54
+ def regression_output():
55
+ output_label = model.output_feature_name
56
+ output = gr.Textbox(label=output_label, type="text")
57
+
58
+ return output
59
+
60
+
61
+ def create_interface():
62
+ interface = gr.Interface(
63
+ fn=prediction_function,
64
+ inputs=regression_inputs(),
65
+ outputs=regression_output(),
66
+ title=MODEL_NAME,
67
+ )
68
+
69
+ interface.launch(debug=True)
70
+
71
+
72
+ create_interface()