import gradio as gr import pandas as pd from sklearn import datasets import seaborn as sns import matplotlib.pyplot as plt from sklearn.preprocessing import LabelEncoder def findCorrelation(dataset, target): df = pd.read_csv(dataset.name) non_numeric_cols = df.select_dtypes('object').columns.tolist() if target in non_numeric_cols: label_encoder = LabelEncoder() df[non_numeric_col] = label_encoder.fit_transform(df[target]) d = df.corr()[target].to_dict() d.pop(target) keys = sorted(d.items(), key=lambda x: x[0], reverse=True) fig1 = plt.figure() hm = sns.heatmap(df.corr(), annot = True) hm.set(title = "Correlation matrix of dataset\n") try: fig2 = plt.figure() sns.regplot(x=df[keys[0][0]], y=df[target]) except: fig2 = plt.figure() try: fig3 = plt.figure() sns.regplot(x=df[keys[1][0]], y=df[target]) except: fig3 = plt.figure() try: fig4 = plt.figure() sns.regplot(x=df[keys[2][0]], y=df[target]) except: fig4 = plt.figure() return d, fig1, fig2, fig3, fig4 with gr.Blocks() as demo: with gr.Row(): with gr.Column(): file = gr.File() with gr.Column(): inp = gr.Textbox(placeholder="Enter the target feature name", label="Target Variable") btn = gr.Button("Find Correlation") gr.Markdown( """ ## Correlation with other numeric features """) with gr.Row(): labels = gr.Label(num_top_classes = 10) gr.Markdown( """ ## HeatMap """) with gr.Row(): fig1 = gr.Plot() gr.Markdown( """ ## Plot of top 3 correlated features """) with gr.Row(): with gr.Column(): fig2 = gr.Plot() with gr.Column(): fig3 = gr.Plot() with gr.Row(): fig4 = gr.Plot() with gr.Row(): gr.Examples( examples = [["boston.csv", "MEDV"]], fn=findCorrelation, inputs=[file, inp], outputs=[labels, fig1, fig2, fig3, fig4], cache_examples=True) btn.click( fn=findCorrelation, inputs=[file, inp], outputs=[labels, fig1, fig2, fig3, fig4]) demo.launch()