Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import os | |
| import pandas as pd | |
| import seaborn as sns | |
| import matplotlib.pyplot as plt | |
| def dataset_change(dataset): | |
| df = pd.read_csv(dataset.name) | |
| features = df.columns | |
| features_object_list = [feature for feature in features] | |
| describe = df.describe(include='all') | |
| print(describe) | |
| return describe.reset_index(), gr.Dropdown.update(choices = features_object_list), gr.Dropdown.update(choices = features_object_list) | |
| def feature_select(dataset, feature, hue = None): | |
| df = pd.read_csv(dataset.name) | |
| non_numeric_cols = df.select_dtypes('object').columns.tolist() | |
| if feature in non_numeric_cols: | |
| kde = False | |
| plot2 = plt.figure() | |
| if hue: | |
| sns.countplot(x = feature, data = df, palette='rainbow', hue = hue) | |
| else: | |
| sns.countplot(x = feature, data = df, palette='rainbow') | |
| else: | |
| kde = True | |
| plot2 = plt.figure() | |
| if hue: | |
| sns.boxplot(x = feature, data = df, hue = hue) | |
| else: | |
| sns.boxplot(x = feature, data = df ) | |
| plot1 = plt.figure() | |
| if hue: | |
| sns.histplot(data = df, x = feature, kde = kde, hue = hue, multiple="stack") | |
| else: | |
| sns.histplot(data = df, x = feature, kde = kde) | |
| return plot1, plot2 | |
| css = """ | |
| footer {display:none !important} | |
| .overflow-x-scroll { | |
| overflow-x: scroll !important; | |
| height: 15rem !important; | |
| overflow-y: scroll !important; | |
| } | |
| .max-h-\[30rem\] {max-height: 18rem !important;} | |
| .hover\:bg-orange-50:hover { | |
| --tw-bg-opacity: 1 !important; | |
| background-color: rgb(229,225,255) !important; | |
| } | |
| .output-markdown h2{ | |
| z-index: 14; | |
| align-self: flex-start; | |
| min-width: 0px; | |
| order: 5; | |
| min-height: 0px; | |
| height: max-content; | |
| flex-grow: 0; | |
| flex-shrink: 0; | |
| width: calc(100% - 0px); | |
| margin: 5px 0px; | |
| white-space: pre-wrap; | |
| overflow: visible; | |
| word-break: break-word; | |
| font-size: 18px !important; | |
| font-weight: 500 !important; | |
| color: rgb(9, 23, 71) !important; | |
| line-height: 1 !important; | |
| border-radius: 0px !important; | |
| opacity: 1 !important; | |
| } | |
| .gr-button-lg { | |
| z-index: 14; | |
| width: 113px !important; | |
| height: 30px !important; | |
| left: 0px; | |
| top: 0px; | |
| padding: 0px; | |
| cursor: pointer !important; | |
| background: none rgb(17, 20, 45) !important; | |
| border: none !important; | |
| text-align: center !important; | |
| font-size: 14px !important; | |
| font-weight: 500 !important; | |
| color: rgb(255, 255, 255) !important; | |
| line-height: 1 !important; | |
| border-radius: 6px !important; | |
| transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important; | |
| box-shadow: none !important; | |
| } | |
| .gr-button-lg:hover{ | |
| z-index: 14; | |
| width: 113px !important; | |
| height: 30px !important; | |
| left: 0px; | |
| top: 0px; | |
| padding: 0px; | |
| cursor: pointer !important; | |
| background: none rgb(66, 133, 244) !important; | |
| border: none !important; | |
| text-align: center !important; | |
| font-size: 14px !important; | |
| font-weight: 500 !important; | |
| color: rgb(255, 255, 255) !important; | |
| line-height: 1 !important; | |
| border-radius: 6px !important; | |
| transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important; | |
| box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important; | |
| } | |
| """ | |
| with gr.Blocks(title="Describe Dataset | Data Science Dojo", css = css) as demo: | |
| gr.Markdown("""## Input Dataset""") | |
| with gr.Row(): | |
| dataset = gr.File() | |
| gr.Markdown("""## Dataset Description""") | |
| with gr.Row(): | |
| dataframe = gr.Dataframe() | |
| gr.Markdown("""## Select the feature to visualize""") | |
| with gr.Row(): | |
| with gr.Column(): | |
| features = gr.Dropdown(label="Select feature to visualize") | |
| with gr.Column(): | |
| hue = gr.Dropdown(label="Select hue") | |
| with gr.Row(): | |
| btn = gr.Button("Visualize") | |
| gr.Markdown("""## Visualization""") | |
| with gr.Row(): | |
| plot1 = gr.Plot() | |
| with gr.Row(): | |
| plot2 = gr.Plot() | |
| gr.Examples( | |
| examples=[["boston.csv"]], | |
| fn = dataset_change, | |
| inputs = dataset, | |
| outputs = [dataframe, features, hue], | |
| cache_examples=True | |
| ) | |
| dataset.change(fn=dataset_change, | |
| inputs = dataset, | |
| outputs = [dataframe, features, hue] | |
| ) | |
| btn.click(fn=feature_select, | |
| inputs=[dataset, features, hue], | |
| outputs=[plot1, plot2] | |
| ) | |
| demo.launch(debug=True) |