Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
|
| 4 |
+
# Load the decision tree model from the pickle file
|
| 5 |
+
with open('best_tree.pkl', 'rb') as file:
|
| 6 |
+
model = pickle.load(file)
|
| 7 |
+
|
| 8 |
+
# Define the predict function
|
| 9 |
+
def predict(latitude, longitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income):
|
| 10 |
+
# Prepare the input features
|
| 11 |
+
features = [[longitude, latitude, housing_median_age, total_rooms, total_bedrooms, population, households, median_income]]
|
| 12 |
+
|
| 13 |
+
# Make predictions using the loaded model
|
| 14 |
+
prediction = model.predict(features)
|
| 15 |
+
|
| 16 |
+
# Return the predicted output
|
| 17 |
+
return prediction[0]
|
| 18 |
+
|
| 19 |
+
# Create the input interface using Gradio
|
| 20 |
+
inputs = [
|
| 21 |
+
gr.inputs.Number(label="Longitude"),
|
| 22 |
+
gr.inputs.Number(label="Latitude"),
|
| 23 |
+
gr.inputs.Number(label="Housing Median Age"),
|
| 24 |
+
gr.inputs.Number(label="Total Rooms"),
|
| 25 |
+
gr.inputs.Number(label="Total Bedrooms"),
|
| 26 |
+
gr.inputs.Number(label="Population"),
|
| 27 |
+
gr.inputs.Number(label="Households"),
|
| 28 |
+
gr.inputs.Number(label="Median Income")
|
| 29 |
+
]
|
| 30 |
+
|
| 31 |
+
# Create the output interface using Gradio
|
| 32 |
+
output = gr.outputs.Label(num_top_classes=1)
|
| 33 |
+
|
| 34 |
+
# Define example data for demonstration
|
| 35 |
+
examples = [
|
| 36 |
+
[37.88, -122.23, 41, 880, 129, 322, 126, 8.3252],
|
| 37 |
+
[37.84, -122.27, 48, 1922, 409, 1026, 335, 1.7969],
|
| 38 |
+
[37.83, -122.26, 52, 1656, 420, 718, 382, 2.6768]
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
# Create the Gradio interface
|
| 42 |
+
interface = gr.Interface(fn=predict, inputs=inputs, outputs=output, title="Decision Tree Predictor", examples=examples).launch()
|