Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
from ydata_profiling import ProfileReport
|
4 |
+
import sweetviz as sv
|
5 |
+
from dataprep.eda import create_report
|
6 |
+
import io
|
7 |
+
|
8 |
+
class DataAnalyzer:
|
9 |
+
def __init__(self):
|
10 |
+
self.current_df = None
|
11 |
+
|
12 |
+
def generate_profile_report(self, df, minimal=False):
|
13 |
+
profile = ProfileReport(
|
14 |
+
df,
|
15 |
+
minimal=minimal,
|
16 |
+
title="Pandas Profiling Report",
|
17 |
+
explorative=True,
|
18 |
+
dark_mode=True
|
19 |
+
)
|
20 |
+
# Get HTML directly as string
|
21 |
+
return profile.to_html()
|
22 |
+
|
23 |
+
def generate_sweetviz_report(self, df):
|
24 |
+
report = sv.analyze(df)
|
25 |
+
# Use StringIO to capture the HTML output
|
26 |
+
html_io = io.StringIO()
|
27 |
+
report.show_html(filepath=html_io, open_browser=False)
|
28 |
+
return html_io.getvalue()
|
29 |
+
|
30 |
+
def generate_dataprep_report(self, df):
|
31 |
+
report = create_report(df)
|
32 |
+
# Get HTML directly as string
|
33 |
+
return report.html()
|
34 |
+
|
35 |
+
def get_dataset_info(self, df):
|
36 |
+
return {
|
37 |
+
"Rows": len(df),
|
38 |
+
"Columns": len(df.columns),
|
39 |
+
"Memory Usage (MB)": df.memory_usage(deep=True).sum() / 1024**2,
|
40 |
+
"Missing Values": df.isnull().sum().sum(),
|
41 |
+
"Data Types": df.dtypes.value_counts().to_dict()
|
42 |
+
}
|
43 |
+
|
44 |
+
def create_interface():
|
45 |
+
analyzer = DataAnalyzer()
|
46 |
+
|
47 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
48 |
+
gr.Markdown("""
|
49 |
+
# Data Analysis Dashboard
|
50 |
+
Upload your CSV file to generate interactive analysis reports
|
51 |
+
""")
|
52 |
+
|
53 |
+
with gr.Row():
|
54 |
+
file_input = gr.File(label="Upload CSV")
|
55 |
+
dataset_info = gr.JSON(label="Dataset Information")
|
56 |
+
|
57 |
+
with gr.Row():
|
58 |
+
report_type = gr.Radio(
|
59 |
+
choices=["Full", "Minimal"],
|
60 |
+
value="Full",
|
61 |
+
label="Report Type"
|
62 |
+
)
|
63 |
+
|
64 |
+
with gr.Tabs():
|
65 |
+
with gr.TabItem("Pandas Profiling"):
|
66 |
+
profile_html = gr.HTML()
|
67 |
+
with gr.TabItem("Sweetviz"):
|
68 |
+
sweet_html = gr.HTML()
|
69 |
+
with gr.TabItem("DataPrep"):
|
70 |
+
prep_html = gr.HTML()
|
71 |
+
|
72 |
+
def process_file(file, report_type):
|
73 |
+
if file is None:
|
74 |
+
return None, None, None, None
|
75 |
+
|
76 |
+
try:
|
77 |
+
df = pd.read_csv(file.name)
|
78 |
+
analyzer.current_df = df
|
79 |
+
|
80 |
+
# Get dataset info
|
81 |
+
info = analyzer.get_dataset_info(df)
|
82 |
+
|
83 |
+
# Generate reports
|
84 |
+
minimal = report_type == "Minimal"
|
85 |
+
|
86 |
+
with gr.Progress() as progress:
|
87 |
+
progress(0, desc="Generating Pandas Profiling report...")
|
88 |
+
profile_html = analyzer.generate_profile_report(df, minimal)
|
89 |
+
|
90 |
+
progress(0.33, desc="Generating Sweetviz report...")
|
91 |
+
sweet_html = analyzer.generate_sweetviz_report(df)
|
92 |
+
|
93 |
+
progress(0.66, desc="Generating DataPrep report...")
|
94 |
+
prep_html = analyzer.generate_dataprep_report(df)
|
95 |
+
|
96 |
+
progress(1.0, desc="Done!")
|
97 |
+
|
98 |
+
return (
|
99 |
+
info,
|
100 |
+
profile_html,
|
101 |
+
sweet_html,
|
102 |
+
prep_html
|
103 |
+
)
|
104 |
+
|
105 |
+
except Exception as e:
|
106 |
+
return str(e), None, None, None
|
107 |
+
|
108 |
+
file_input.change(
|
109 |
+
fn=process_file,
|
110 |
+
inputs=[file_input, report_type],
|
111 |
+
outputs=[dataset_info, profile_html, sweet_html, prep_html]
|
112 |
+
)
|
113 |
+
|
114 |
+
report_type.change(
|
115 |
+
fn=process_file,
|
116 |
+
inputs=[file_input, report_type],
|
117 |
+
outputs=[dataset_info, profile_html, sweet_html, prep_html]
|
118 |
+
)
|
119 |
+
|
120 |
+
return demo
|
121 |
+
|
122 |
+
# Add custom CSS for better HTML rendering
|
123 |
+
custom_css = """
|
124 |
+
<style>
|
125 |
+
.report-container {
|
126 |
+
width: 100%;
|
127 |
+
height: 800px;
|
128 |
+
overflow: auto;
|
129 |
+
}
|
130 |
+
.report-container iframe {
|
131 |
+
width: 100%;
|
132 |
+
height: 100%;
|
133 |
+
border: none;
|
134 |
+
}
|
135 |
+
</style>
|
136 |
+
"""
|
137 |
+
|
138 |
+
# Launch the interface
|
139 |
+
if __name__ == "__main__":
|
140 |
+
demo = create_interface()
|
141 |
+
demo.launch(
|
142 |
+
share=True, # Enable sharing
|
143 |
+
height=1000, # Set interface height
|
144 |
+
show_error=True, # Show detailed error messages
|
145 |
+
custom_css=custom_css # Apply custom styling
|
146 |
+
)
|