Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import numpy as np | |
| from sklearn.datasets import load_iris | |
| from sklearn.ensemble import GradientBoostingClassifier | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.metrics import accuracy_score, confusion_matrix | |
| # 1. Load dataset | |
| iris = load_iris() | |
| X, y = iris.data, iris.target | |
| feature_names = iris.feature_names | |
| class_names = iris.target_names | |
| # Split into train/test | |
| X_train, X_test, y_train, y_test = train_test_split( | |
| X, y, test_size=0.3, random_state=42 | |
| ) | |
| # 2. Define a function that trains & evaluates a model given hyperparameters | |
| def train_and_evaluate(learning_rate, n_estimators, max_depth): | |
| clf = GradientBoostingClassifier( | |
| learning_rate=learning_rate, | |
| n_estimators=n_estimators, | |
| max_depth=int(max_depth), | |
| random_state=42 | |
| ) | |
| clf.fit(X_train, y_train) | |
| y_pred = clf.predict(X_test) | |
| accuracy = accuracy_score(y_test, y_pred) | |
| cm = confusion_matrix(y_test, y_pred) | |
| cm_display = "\n".join([str(row) for row in cm]) | |
| return f"Accuracy: {accuracy:.3f}\nConfusion Matrix:\n{cm_display}" | |
| # 3. Define a prediction function for user-supplied feature values | |
| def predict_species(sepal_length, sepal_width, petal_length, petal_width, | |
| learning_rate, n_estimators, max_depth): | |
| clf = GradientBoostingClassifier( | |
| learning_rate=learning_rate, | |
| n_estimators=n_estimators, | |
| max_depth=int(max_depth), | |
| random_state=42 | |
| ) | |
| clf.fit(X_train, y_train) | |
| user_sample = np.array([[sepal_length, sepal_width, petal_length, petal_width]]) | |
| prediction = clf.predict(user_sample)[0] | |
| return f"Predicted species: {class_names[prediction]}" | |
| # 4. Build the Gradio interface | |
| with gr.Blocks() as demo: | |
| with gr.Tab("Train & Evaluate"): | |
| gr.Markdown("## Train a GradientBoostingClassifier on the Iris dataset") | |
| learning_rate_slider = gr.Slider(0.01, 1.0, value=0.1, step=0.01, label="learning_rate") | |
| n_estimators_slider = gr.Slider(50, 300, value=100, step=50, label="n_estimators") | |
| max_depth_slider = gr.Slider(1, 10, value=3, step=1, label="max_depth") | |
| train_button = gr.Button("Train & Evaluate") | |
| output_text = gr.Textbox(label="Results") | |
| train_button.click( | |
| fn=train_and_evaluate, | |
| inputs=[learning_rate_slider, n_estimators_slider, max_depth_slider], | |
| outputs=output_text, | |
| ) | |
| with gr.Tab("Predict"): | |
| gr.Markdown("## Predict Iris Species with GradientBoostingClassifier") | |
| sepal_length_input = gr.Number(value=5.1, label=feature_names[0]) | |
| sepal_width_input = gr.Number(value=3.5, label=feature_names[1]) | |
| petal_length_input = gr.Number(value=1.4, label=feature_names[2]) | |
| petal_width_input = gr.Number(value=0.2, label=feature_names[3]) | |
| # Hyperparams for the model that will do the prediction | |
| learning_rate_slider2 = gr.Slider(0.01, 1.0, value=0.1, step=0.01, label="learning_rate") | |
| n_estimators_slider2 = gr.Slider(50, 300, value=100, step=50, label="n_estimators") | |
| max_depth_slider2 = gr.Slider(1, 10, value=3, step=1, label="max_depth") | |
| predict_button = gr.Button("Predict") | |
| prediction_text = gr.Textbox(label="Prediction") | |
| predict_button.click( | |
| fn=predict_species, | |
| inputs=[ | |
| sepal_length_input, | |
| sepal_width_input, | |
| petal_length_input, | |
| petal_width_input, | |
| learning_rate_slider2, | |
| n_estimators_slider2, | |
| max_depth_slider2, | |
| ], | |
| outputs=prediction_text | |
| ) | |
| demo.launch() | |