# -*- coding: utf-8 -*- """Iris_Flower_Classifier.ipynb Automatically generated by Colab. Original file is located at https://colab.research.google.com/drive/1ddsKnOLQk_nPeF9zu0Qr9yTsvmg-0D8S """ import gradio as gr import numpy as np from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.ensemble import RandomForestClassifier from sklearn.pipeline import make_pipeline # Load the Iris dataset iris = load_iris() X = iris.data y = iris.target feature_names = iris.feature_names target_names = iris.target_names # Split the data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Create and train a RandomForest model model = make_pipeline(StandardScaler(), RandomForestClassifier()) model.fit(X_train, y_train) # Define the prediction function def predict_iris(sepal_length, sepal_width, petal_length, petal_width): feature_values = np.array([sepal_length, sepal_width, petal_length, petal_width]).reshape(1, -1) prediction = model.predict(feature_values) return target_names[prediction[0]] # Create a Gradio interface interface = gr.Interface( fn=predict_iris, inputs=[ gr.Slider(minimum=float(X[:, 0].min()), maximum=float(X[:, 0].max()), value=float(np.mean(X[:, 0])), label="Sepal Length (cm)"), # Changed 'default' to 'value' gr.Slider(minimum=float(X[:, 1].min()), maximum=float(X[:, 1].max()), value=float(np.mean(X[:, 1])), label="Sepal Width (cm)"), # Changed 'default' to 'value' gr.Slider(minimum=float(X[:, 2].min()), maximum=float(X[:, 2].max()), value=float(np.mean(X[:, 2])), label="Petal Length (cm)"), # Changed 'default' to 'value' gr.Slider(minimum=float(X[:, 3].min()), maximum=float(X[:, 3].max()), value=float(np.mean(X[:, 3])), label="Petal Width (cm)") # Changed 'default' to 'value' ], outputs="text", title="Iris Flower Classifier", description="Select the features of the iris flower to predict its species." ) # Launch the interface if __name__ == "__main__": interface.launch(inline=False)