Spaces:
Runtime error
Runtime error
import gradio as gr | |
#Step 1 | |
input1 = gr.inputs.Textbox(label="F1") | |
input2 = gr.inputs.Textbox(label="F2") | |
input3 = gr.inputs.Textbox(label="F3") | |
input4 = gr.inputs.Textbox(label="F4") | |
input5 = gr.inputs.Textbox(label="F5") | |
input6 = gr.inputs.Textbox(label="F6") | |
input7 = gr.inputs.Textbox(label="F7") | |
input8 = gr.inputs.Textbox(label="F8") | |
#output component | |
output1 = gr.outputs.Textbox(label = "Predicted housing prices") | |
#define a function to accept inputs and return outputs | |
def predict_house(input1, input2, input3, input4, input5, input6, input7, input8): | |
#data processing | |
import pandas as pd | |
data = pd.read_csv("housing.csv") | |
## 1. split data to get train and test set | |
from sklearn.model_selection import train_test_split | |
train_set, test_set = train_test_split(data, test_size=0.2, random_state=10) | |
## 2. clean the missing values | |
train_set_clean = train_set.dropna(subset=["total_bedrooms"]) | |
train_set_clean | |
## 2. derive training features and training labels | |
train_labels = train_set_clean["median_house_value"].copy() # get labels for output label Y | |
train_features = train_set_clean.drop("median_house_value", axis=1) # drop labels to get features X for training set | |
## 4. scale the numeric features in training set | |
from sklearn.preprocessing import MinMaxScaler | |
scaler = MinMaxScaler() ## define the transformer | |
scaler.fit(train_features) ## call .fit() method to calculate the min and max value for each column in dataset | |
train_features_normalized = scaler.transform(train_features) | |
train_features_normalized | |
#model training | |
from sklearn.linear_model import LinearRegression ## import the LinearRegression Function | |
lin_reg = LinearRegression() ## Initialize the class | |
lin_reg.fit(train_features_normalized, train_labels) # feed the training data X, and label Y for supervised learning | |
#model predictions | |
import numpy as np | |
test_feature = np.array([[input1, input2, input3, input4, input5, input6, input7, input8]]) | |
normalized_test_features = scaler.transform(test_feature) | |
training_predictions = lin_reg.predict(normalized_test_features) | |
return training_predictions | |
#Step 4: Build connection between front end and back end | |
gr.Interface(fn = predict_house, | |
inputs = [input1, input2, input3, input4, input5, input6, input7, input8], | |
outputs = [output1]).launch(debug = True) |