lab04_practice / app.py
jiehou's picture
Update app.py
a2e5fc1 verified
import gradio as gr
from joblib import dump, load
def predict(fea1, fea2, fea3, fea4, fea5, fea6, fea7, fea8):
import numpy as np
# 1. load features
test_feature = np.array([[fea1, fea2, fea3, fea4, fea5, fea6, fea7, fea8]])
# 2. scale the test features
# 2.1 Load scaler file
scaler = load('scaler.joblib')
# 2.2 normalize test features using statistics from training data
test_feature_normalized = scaler.transform(test_feature)
print("shape: ", test_feature_normalized.shape)
# 3. Make a prediction
# 3.1 load the machine learning
model = load('knn_model.joblib')
# 3.2 call model to get prediction
pred = model.predict(test_feature_normalized)
return pred
input_module1 = gr.Slider(-124.35, -114.35, step = 0.5, label = "Longitude")
input_module2 = gr.Slider(32, 41, step = 0.5, label = "Latitude")
input_module3 = gr.Slider(1, 52, step = 1, label = "Housing Median Age (Year)")
input_module4 = gr.Slider(1, 39320, step = 5, label = "Total Rooms")
input_module5 = gr.Slider(1, 6445, step = 5, label = "Total Bedrooms")
input_module6 = gr.Slider(3, 35682, step = 5, label = "Population")
input_module7 = gr.Slider(1, 6081, step = 5, label = "Households")
input_module8 = gr.Slider(0, 15, step = 0.5, label = "Median Income")
output_module1 = gr.Textbox(label = "Predicted Housing Prices")
gr.Interface(fn = predict,
inputs = [input_module1, input_module2, input_module3, input_module4, input_module5, input_module6, input_module7, input_module8],
outputs = [output_module1]).launch()