Download app.py from jiehou/Lab04: direct link, hf CLI and curl.
- Browser
- Download file 1.64 kB
-
https://huggingface.co/spaces/jiehou/Lab04/resolve/main/app.py
- Command line
-
hf download hf://spaces/jiehou/Lab04/app.py
-
curl -L -o app.py https://huggingface.co/spaces/jiehou/Lab04/resolve/main/app.py
1.64 kB
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| import pickle | |
| with open("linear_regression_model.pkl", "rb") as file: | |
| lin_reg = pickle.load(file) | |
| # define the input module | |
| inputs = [ | |
| gr.components.Number(label="Longitude"), | |
| gr.components.Number(label="Latitude"), | |
| gr.components.Number(label="Housing Median Age"), | |
| gr.components.Number(label="Total Rooms"), | |
| gr.components.Number(label="Total Bedrooms"), | |
| gr.components.Number(label="Population"), | |
| gr.components.Number(label="Households"), | |
| gr.components.Number(label="Median Income") | |
| ] | |
| # define the output module | |
| outputs = gr.components.Textbox(label = 'Output') | |
| # define the backend function | |
| from sklearn.preprocessing import MinMaxScaler | |
| def predict_price(longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income): | |
| features = pd.DataFrame({ | |
| 'longitude': [longitude], | |
| 'latitude': [latitude], | |
| 'housing_median_age': [housing_median_age], | |
| 'total_rooms': [total_rooms], | |
| 'total_bedrooms': [total_bedrooms], | |
| 'population': [population], | |
| 'households': [households], | |
| 'median_income': [median_income] | |
| }) | |
| scaler = MinMaxScaler() ## define the transformer | |
| # load your scaler from the training dataset | |
| features=scaler.fit_transform(features) | |
| linear_regression_prediction = lin_reg.predict(features)[0] | |
| #decision_tree_prediction = tree_reg.predict(features)[0] | |
| return linear_regression_prediction | |
| interface = gr.Interface(fn=predict_price, inputs=inputs, outputs=outputs).launch(debug=True) | |