import gradio as gr import matplotlib.pyplot as plt from sklearn.datasets import make_blobs from sklearn.cluster import KMeans from sklearn.preprocessing import StandardScaler def compute(number_of_samples, number_of_clusters, randomised_state): # create synthetic data if randomised_state: seed = None else: seed = 42 features, true_labels = make_blobs(n_samples=number_of_samples, centers=number_of_clusters, cluster_std=2.75, random_state=seed) scaler = StandardScaler() scaled_features = scaler.fit_transform(features) kmeans = KMeans(init="random", n_clusters=number_of_clusters, n_init=10, max_iter=300, random_state=seed) kmeans.fit(scaled_features) fig = plt.figure() ax1 = fig.add_subplot(111) ax1.scatter(scaled_features[:,0], scaled_features[:,1], c=kmeans.labels_, s=8, marker="o", label='dataset') ax1.scatter(kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1], s=50, c='r', marker="o", label='cluster centers') plt.legend(loc='upper left') return fig iface = gr.Interface(fn=compute, inputs=[gr.Number(minimum=100, maximum=900, value=100, label="Number of Samples", info="Number of samples must be between 100 and 900"), gr.Number(minimum=2, maximum=20, value=2, label="Number of Clusters", info="Number of clusters must range between 2 and 20"), gr.Checkbox(label="randomise samples")], outputs=gr.Plot(label="Plot")) iface.launch()