import gradio as gr # a. define text data type input_module1 = gr.inputs.Textbox(label = "Feature1") input_module2 = gr.inputs.Textbox(label = "Feature2") input_module3 = gr.inputs.Textbox(label = "Feature3") input_module4 = gr.inputs.Textbox(label = "Feature4") input_module5 = gr.inputs.Textbox(label = "Feature5") input_module6 = gr.inputs.Textbox(label = "Feature6") input_module7 = gr.inputs.Textbox(label = "Feature7") input_module8 = gr.inputs.Textbox(label = "Feature8") # Step 6.2: Define different output components # a. define text data type output_module1 = gr.outputs.Textbox(label = "Predicted housing prices") def predict_housing_price(input_module1,input_module2,input_module3,input_module4,input_module5,input_module6,input_module7,input_module8 ): import pandas as pd import numpy as np import sklearn housing=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(housing, 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 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 test_feature= np.array([[input_module1, input_module2, input_module3, input_module4, input_module5, input_module6, input_module7,input_module8]]) normalized_test_feature= scaler.transform(test_feature) training_predictions = lin_reg.predict(normalized_test_feature) training_predictions return training_predictions #gardio Interface gr.Interface(fn=predict_housing_price, inputs=[input_module1, input_module2, input_module3, input_module4, input_module5, input_module6, input_module7,input_module8], outputs=[output_module1] ).launch()