| import gradio as gr |
| import pandas as pd |
| import matplotlib.pyplot as plt |
| import seaborn as sns |
| import io |
|
|
| |
| def analyze_csv(file): |
| """Reads a CSV file, calculates statistics, and prepares components for plotting.""" |
| if file is None: |
| |
| return None, "Please upload a CSV file.", gr.Dropdown(choices=[], label="Select Column to Plot"), None |
|
|
| try: |
| |
| df = pd.read_csv(file.name) |
|
|
| |
| stats = df.describe().round(2).T |
| |
| |
| stats_markdown = stats.to_markdown() |
|
|
| |
| numeric_cols = df.select_dtypes(include=['number']).columns.tolist() |
| |
| |
| return df, stats_markdown, gr.Dropdown(choices=numeric_cols, label="Select Column to Plot"), df |
| |
| except Exception as e: |
| error_message = f"Error processing file: {e}" |
| |
| return None, error_message, gr.Dropdown(choices=[], label="Select Column to Plot"), None |
|
|
|
|
| |
| def generate_plot(df_state, column_name): |
| """Generates a distribution plot (histogram) for the selected column.""" |
| if df_state is None or column_name is None or column_name == "": |
| return None |
|
|
| try: |
| |
| plt.figure(figsize=(8, 5)) |
| |
| |
| sns.histplot(df_state[column_name], kde=True) |
| |
| plt.title(f'Distribution of {column_name}') |
| plt.xlabel(column_name) |
| plt.ylabel('Frequency') |
| |
| |
| buf = io.BytesIO() |
| plt.savefig(buf, format='png') |
| plt.close() |
| buf.seek(0) |
| |
| return buf.read() |
|
|
| except Exception as e: |
| print(f"Plotting Error: {e}") |
| return None |
|
|
|
|
| |
| with gr.Blocks(title="CSV Data Analyzer") as demo: |
| gr.Markdown("## ๐ CSV Data Analyzer") |
| gr.Markdown("Upload your CSV file and see instant statistics and visualizations.") |
|
|
| |
| |
| df_state = gr.State(None) |
|
|
| |
| csv_file = gr.File(label="Upload CSV File (.csv)", file_types=[".csv"]) |
| |
| with gr.Row(): |
| |
| |
| df_output = gr.Dataframe(label="Uploaded Data Preview", interactive=False) |
| stats_output = gr.Markdown(label="Descriptive Statistics") |
| |
| gr.HTML("<hr>") |
|
|
| |
| with gr.Row(): |
| |
| column_dropdown = gr.Dropdown(label="Select Column to Plot", interactive=True) |
| plot_button = gr.Button("Generate Plot") |
| |
| plot_output = gr.Plot(label="Column Distribution Plot") |
|
|
|
|
| |
| |
| |
| csv_file.upload( |
| analyze_csv, |
| inputs=[csv_file], |
| |
| outputs=[df_output, stats_output, column_dropdown, df_state] |
| ) |
|
|
| |
| plot_button.click( |
| generate_plot, |
| inputs=[df_state, column_dropdown], |
| outputs=[plot_output] |
| ) |
|
|
|
|
| |
| if __name__ == "__main__": |
| demo.launch(server_name="0.0.0.0", server_port=7860) |