App
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Import the necessary libraries
|
| 2 |
+
import gradio as gr # Gradio is a library to quickly build and share demos for ML models
|
| 3 |
+
import joblib # joblib is used here to load the trained model from a file
|
| 4 |
+
import numpy as np # NumPy for numerical operations (if needed for array manipulation)
|
| 5 |
+
|
| 6 |
+
# Load the pre-trained Decision Tree classifier from the joblib file
|
| 7 |
+
pipeline = joblib.load("./models/iris_dt.joblib")
|
| 8 |
+
|
| 9 |
+
# Define a function that takes the four iris measurements as input
|
| 10 |
+
# and returns the predicted iris species label.
|
| 11 |
+
def predict_iris(sepal_length, sepal_width, petal_length, petal_width):
|
| 12 |
+
# Convert the input parameters into a 2D list/array because
|
| 13 |
+
# scikit-learn's predict() expects a 2D array of shape (n_samples, n_features)
|
| 14 |
+
input = np.array([[sepal_length, sepal_width, petal_length, petal_width]])
|
| 15 |
+
prediction = pipeline.predict(input)
|
| 16 |
+
|
| 17 |
+
# Convert the prediction to the string label
|
| 18 |
+
if prediction == 0:
|
| 19 |
+
return 'iris-setosa'
|
| 20 |
+
elif prediction == 1:
|
| 21 |
+
return 'Iris-versicolor'
|
| 22 |
+
elif prediction == 2:
|
| 23 |
+
return 'Iris-virginica'
|
| 24 |
+
else:
|
| 25 |
+
return "Invalid prediction"
|
| 26 |
+
|
| 27 |
+
# Create a Gradio Interface:
|
| 28 |
+
# - fn: the function to call for inference
|
| 29 |
+
# - inputs: a list of component types to collect user input (in this case, four numeric values)
|
| 30 |
+
# - outputs: how the prediction is displayed (in this case, as text)
|
| 31 |
+
# - live: whether to update the output in real-time as the user types
|
| 32 |
+
interface = gr.Interface(
|
| 33 |
+
fn=predict_iris,
|
| 34 |
+
inputs=["number", "number", "number", "number"],
|
| 35 |
+
outputs="text",
|
| 36 |
+
live=True,
|
| 37 |
+
title="Iris Species Identifier",
|
| 38 |
+
description="Enter the four measurements to predict the Iris species."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Run the interface when this script is executed directly.
|
| 42 |
+
# This will launch a local Gradio server and open a user interface in the browser.
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
# To create a public link, set the parameter share=True
|
| 45 |
+
interface.launch()
|
| 46 |
+
|
| 47 |
+
'''
|
| 48 |
+
# The Flag button allows users (or testers) to mark or “flag”
|
| 49 |
+
# a particular input-output interaction for later review.
|
| 50 |
+
# When someone clicks Flag, Gradio saves the input values (and often the output) to a log.csv file
|
| 51 |
+
# letting you keep track of interesting or potentially problematic cases for debugging or analysis later on
|
| 52 |
+
'''
|