import gradio as gr import pandas as pd import sweetviz as sv import io import base64 class DataAnalyzer: def __init__(self): self.current_df = None def generate_sweetviz_report(self, df): report = sv.analyze(df) html_io = io.StringIO() report.show_html(filepath=html_io, open_browser=False) return html_io.getvalue() def get_dataset_info(self, df): info_dict = { "Rows": len(df), "Columns": len(df.columns), "Memory Usage (MB)": round(df.memory_usage(deep=True).sum() / 1024**2, 2), "Missing Values": int(df.isnull().sum().sum()), "Data Types": df.dtypes.astype(str).to_dict() } return str(info_dict) # Convert to string to avoid JSON serialization issues def create_interface(): analyzer = DataAnalyzer() with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown("# CSV Data Analysis Dashboard") with gr.Row(): file_input = gr.File(label="Upload CSV") info_output = gr.Textbox(label="Dataset Information", lines=10) report_html = gr.HTML() def process_file(file): if file is None: return "No file uploaded.", None try: df = pd.read_csv(file.name) info = analyzer.get_dataset_info(df) report = analyzer.generate_sweetviz_report(df) # Create download link b64 = base64.b64encode(report.encode()).decode() download_link = f'Download Report' return info, report + download_link except Exception as e: return f"Error: {str(e)}", None file_input.change( fn=process_file, inputs=[file_input], outputs=[info_output, report_html] ) return demo if __name__ == "__main__": demo = create_interface() demo.launch()