Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def predict(fea1, fea2, fea3, fea4, fea5, fea6, fea7, fea8):
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# 1. load features
|
| 8 |
+
test_feature = np.array([[fea1, fea2, fea3, fea4, fea5, fea6, fea7, fea8]])
|
| 9 |
+
|
| 10 |
+
# 2. scale the test features
|
| 11 |
+
# 2.1 Load scaler file
|
| 12 |
+
scaler = load('scaler.joblib')
|
| 13 |
+
|
| 14 |
+
# 2.2 normalize test features using statistics from training data
|
| 15 |
+
test_feature_normalized = scaler.transform(test_feature)
|
| 16 |
+
|
| 17 |
+
print("shape: ", test_feature_normalized.shape)
|
| 18 |
+
|
| 19 |
+
# 3. Make a prediction
|
| 20 |
+
# 3.1 load the machine learning
|
| 21 |
+
model = load('knn_model.joblib')
|
| 22 |
+
|
| 23 |
+
# 3.2 call model to get prediction
|
| 24 |
+
|
| 25 |
+
pred = model.predict(test_feature_normalized)
|
| 26 |
+
|
| 27 |
+
return pred
|
| 28 |
+
|
| 29 |
+
input_module1 = gr.Slider(-124.35, -114.35, step = 0.5, label = "Longitude")
|
| 30 |
+
input_module2 = gr.Slider(32, 41, step = 0.5, label = "Latitude")
|
| 31 |
+
input_module3 = gr.Slider(1, 52, step = 1, label = "Housing Median Age (Year)")
|
| 32 |
+
input_module4 = gr.Slider(1, 39320, step = 5, label = "Total Rooms")
|
| 33 |
+
input_module5 = gr.Slider(1, 6445, step = 5, label = "Total Bedrooms")
|
| 34 |
+
input_module6 = gr.Slider(3, 35682, step = 5, label = "Population")
|
| 35 |
+
input_module7 = gr.Slider(1, 6081, step = 5, label = "Households")
|
| 36 |
+
input_module8 = gr.Slider(0, 15, step = 0.5, label = "Median Income")
|
| 37 |
+
|
| 38 |
+
output_module1 = gr.Textbox(label = "Predicted Housing Prices")
|
| 39 |
+
|
| 40 |
+
gr.Interface(fn = predict,
|
| 41 |
+
inputs = [input_module1, input_module2, input_module3, input_module4, input_module5, input_module6, input_module7, input_module8],
|
| 42 |
+
outputs = [output_module1]).launch()
|