import gradio as gr import numpy as np from sklearn.datasets import make_circles import matplotlib.pyplot as plt import tempfile # Function to generate the circles dataset def generate_circles(n_samples, noise, random_state, factor): # Generate the dataset X, y = make_circles(n_samples=n_samples, noise=noise, random_state=random_state, factor=factor) # Create the figure fig, ax = plt.subplots(figsize=(6, 6)) # Plot the circles scatter = ax.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.viridis, edgecolor='k') ax.set_title("Circles Dataset") ax.set_xlabel("X") ax.set_ylabel("Y") # Save the plot temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False) plt.savefig(temp_file.name, bbox_inches='tight') plt.close() # Merge data and labels merged_data = np.column_stack((X, y)).tolist() return temp_file.name, merged_data # Function to generate the sklearn code def generate_sklearn_code(n_samples, noise, random_state, factor): random_state_str = 'None' if random_state is None else random_state code = ( f"from sklearn.datasets import make_circles\n\n" f"# Generate circles dataset\n" f"X, y = make_circles(\n" f" n_samples={n_samples},\n" f" noise={noise},\n" f" random_state={random_state_str},\n" f" factor={factor}\n" f")" ) return code # Gradio Interface with gr.Blocks(title="Circles Dataset") as demo: gr.Markdown(""" # Dataset Generator: Circles This tool generates a circles dataset using scikit-learn's `make_circles()` function. You can specify the number of samples, noise, scale factor, and randomness. ## Visualization - The plot shows a 2D circular dataset, where points are colored according to their class. ## Parameters: - **Number of Samples**: The total number of points to generate. - **Noise**: The standard deviation of Gaussian noise added to the data. - **Random State**: Seed for reproducible output. - **Factor**: Scale factor between inner and outer circle. For more information, visit the scikit-learn documentation: [https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_circles.html](https://scikit-learn.org/stable/modules/generated/sklearn.datasets.make_circles.html) """) with gr.Row(): n_samples = gr.Slider(minimum=100, maximum=5000, value=1000, step=100, label="Samples") noise = gr.Slider(minimum=0.0, maximum=1.0, value=0.1, step=0.01, label="Noise") random_state = gr.Slider(minimum=0, maximum=100, value=0, step=1, label="Random State") factor = gr.Slider(minimum=0.1, maximum=0.9, value=0.8, step=0.1, label="Factor") with gr.Row(): generate_btn = gr.Button("Generate") with gr.Row(): output_image = gr.Image(label="Circles Plot") with gr.Row(): output_code = gr.Textbox(label="Python Code", lines=7, interactive=False) with gr.Row(): output_data = gr.Dataframe(label="Generated Data and Labels", headers=["X1", "X2", "Label"]) def handle_output(n_samples, noise, random_state, factor): if n_samples <= 0: return "Error: Number of samples must be a positive integer.", None, "" random_state = int(random_state) if random_state is not None else None try: image, merged_data = generate_circles( n_samples=int(n_samples), noise=float(noise), random_state=random_state, factor=float(factor) ) sklearn_code = generate_sklearn_code(n_samples, noise, random_state, factor) return image, merged_data, sklearn_code except Exception as e: return f"An error occurred: {str(e)}", None, "" generate_btn.click( fn=handle_output, inputs=[n_samples, noise, random_state, factor], outputs=[output_image, output_data, output_code] ) # Launch the app demo.launch()