Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from sklearn.datasets import load_iris | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.metrics import accuracy_score | |
| # Load dataset | |
| iris = load_iris() | |
| X, y = iris.data, iris.target | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # Train the model | |
| model = LogisticRegression(max_iter=200) | |
| model.fit(X_train, y_train) | |
| # Test the model | |
| y_pred = model.predict(X_test) | |
| accuracy = accuracy_score(y_test, y_pred) | |
| # Prediction function | |
| def predict_iris(sepal_length, sepal_width, petal_length, petal_width): | |
| input_data = [[sepal_length, sepal_width, petal_length, petal_width]] | |
| prediction = model.predict(input_data) | |
| prediction_name = iris.target_names[prediction[0]] | |
| return f"Predicted Iris class: {prediction_name}" | |
| # Gradio Interface | |
| description = f""" | |
| ### Logistic Regression on Iris Dataset | |
| Model Accuracy on Test Data: {accuracy * 100:.2f}% | |
| """ | |
| interface = gr.Interface( | |
| fn=predict_iris, | |
| inputs=[ | |
| gr.inputs.Number(label="Sepal Length (cm)"), | |
| gr.inputs.Number(label="Sepal Width (cm)"), | |
| gr.inputs.Number(label="Petal Length (cm)"), | |
| gr.inputs.Number(label="Petal Width (cm)") | |
| ], | |
| outputs="text", | |
| title="Iris Flower Classification", | |
| description=description | |
| ) | |
| # Launch the Gradio app | |
| if __name__ == "__main__": | |
| interface.launch() | |