Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Step 6.1: Define different input components
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# a. define text data type
|
| 5 |
+
input_module1 = gr.inputs.Slider(1, 100, step=5, label = "Longitude")
|
| 6 |
+
|
| 7 |
+
# b. define image data type
|
| 8 |
+
input_module2 = gr.inputs.Slider(1, 100, step=5, label = "Latitude")
|
| 9 |
+
|
| 10 |
+
# c. define Number data type
|
| 11 |
+
input_module3 = gr.inputs.Slider(1, 100, step=5, label = "Feature3")
|
| 12 |
+
|
| 13 |
+
# d. define Slider data type
|
| 14 |
+
input_module4 = gr.inputs.Slider(1, 100, step=5, label = "Feature4")
|
| 15 |
+
|
| 16 |
+
# e. define Checkbox data type
|
| 17 |
+
input_module5 = gr.inputs.Slider(1, 100, step=5, label = "Feature5")
|
| 18 |
+
|
| 19 |
+
# f. define Radio data type
|
| 20 |
+
input_module6 = gr.inputs.Slider(1, 100, step=5, label = "Feature6")
|
| 21 |
+
|
| 22 |
+
# g. define Dropdown data type
|
| 23 |
+
input_module7 = gr.inputs.Slider(1, 100, step=5, label = "Feature7")
|
| 24 |
+
|
| 25 |
+
# g. define Dropdown data type
|
| 26 |
+
input_module8 = gr.inputs.Slider(1, 100, step=5, label = "Feature8")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Step 6.2: Define different output components
|
| 30 |
+
# a. define text data type
|
| 31 |
+
output_module1 = gr.outputs.Textbox(label = "Output Text")
|
| 32 |
+
|
| 33 |
+
# b. define image data type
|
| 34 |
+
output_module2 = gr.outputs.Image(label = "Output Image")
|
| 35 |
+
|
| 36 |
+
# you can define more output components
|
| 37 |
+
|
| 38 |
+
def machine_learning_pipeline(input1, input2, input3, input4, input5, input6, input7, input8):
|
| 39 |
+
print("Start ml processing")
|
| 40 |
+
|
| 41 |
+
import numpy as np
|
| 42 |
+
import pandas as pd
|
| 43 |
+
|
| 44 |
+
print(input1, input2, input3, input4, input5, input6, input7, input8)
|
| 45 |
+
|
| 46 |
+
### 1. process the user submission, collect the features and save them into one numpy array
|
| 47 |
+
new_feature = np.array([[input1, input2, input3, input4, input5, input6, input7, input8]])
|
| 48 |
+
print(new_feature)
|
| 49 |
+
|
| 50 |
+
### 2. follow the data preprocessing steps as we have done in trainig data
|
| 51 |
+
### 2.1 check missing values in total_bedroom
|
| 52 |
+
|
| 53 |
+
### 2.2 feature normalization
|
| 54 |
+
test_set = pd.DataFrame(new_feature, columns = ['longitude', 'latitude', 'housing_median_age', 'total_rooms',
|
| 55 |
+
'total_bedrooms', 'population', 'households', 'median_income'])
|
| 56 |
+
|
| 57 |
+
## 1. clean the missing values in test set
|
| 58 |
+
test_set_clean = test_set.dropna(subset=["total_bedrooms"])
|
| 59 |
+
test_set_clean
|
| 60 |
+
|
| 61 |
+
### reload the scaler from the local file
|
| 62 |
+
|
| 63 |
+
import pickle
|
| 64 |
+
|
| 65 |
+
with open('minmax_scaler.pkl', 'rb') as f:
|
| 66 |
+
scaler = pickle.load(f)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
## 4. scale the numeric features in test set.
|
| 70 |
+
## important note: do not apply fit function on the test set, using same scalar from training set
|
| 71 |
+
test_features_normalized = scaler.transform(test_set_clean)
|
| 72 |
+
print("test_features_normalized: ",test_features_normalized)
|
| 73 |
+
|
| 74 |
+
### 3. load the pre-train machine learning
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# load
|
| 78 |
+
with open('tree_reg.pkl', 'rb') as f:
|
| 79 |
+
tree_reg = pickle.load(f)
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
### 4. apply the loaded model on the features to make a prediction
|
| 83 |
+
### Step 5: make a prediction using tree model
|
| 84 |
+
test_predictions_trees = tree_reg.predict(test_features_normalized)
|
| 85 |
+
print("test_predictions_trees: ",test_predictions_trees)
|
| 86 |
+
|
| 87 |
+
### 5. send back the prediction
|
| 88 |
+
|
| 89 |
+
print("Start processing")
|
| 90 |
+
import numpy as np
|
| 91 |
+
#output1 = 'This is the output'
|
| 92 |
+
output2 = np.random.rand(28,28)
|
| 93 |
+
|
| 94 |
+
import matplotlib.pyplot as plt
|
| 95 |
+
plt.plot([input1], [input2])
|
| 96 |
+
plt.savefig('test.png')
|
| 97 |
+
|
| 98 |
+
return test_predictions_trees, 'test.png'
|