Osnly commited on
Commit
a1e3335
Β·
verified Β·
1 Parent(s): 97e87bc

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +73 -50
src/streamlit_app.py CHANGED
@@ -1,50 +1,73 @@
1
- # app.py
2
- import streamlit as st
3
- import pandas as pd
4
- from analyze import analyze_csv
5
- from plan import generate_cleaning_plan
6
- from execute import execute_plan
7
- from insight import generate_insights
8
- from visual_insight import generate_visual_plan
9
- import matplotlib.pyplot as plt
10
- import seaborn as sns
11
- import os
12
-
13
- st.set_page_config(page_title="Smart Data Cleaning Agent", layout="wide")
14
- st.title("🧠 Smart Data Cleaning Agent")
15
-
16
- uploaded_file = st.file_uploader("Upload your CSV file", type=["csv"])
17
-
18
- if uploaded_file:
19
- with st.spinner("Reading file..."):
20
- df = pd.read_csv(uploaded_file)
21
- st.write("### πŸ” Original Data Preview", df.head())
22
-
23
- with st.spinner("πŸ”Ž Analyzing..."):
24
- analysis = analyze_csv(uploaded_file)
25
-
26
- with st.spinner("πŸ›  Generating Cleaning Plan..."):
27
- plan, explanation = generate_cleaning_plan(analysis)
28
- st.json(plan)
29
- st.markdown(f"**Plan Summary:** {explanation}")
30
-
31
- with st.spinner("✨ Applying Cleaning..."):
32
- cleaned_df = execute_plan(df.copy(), plan)
33
- st.write("### βœ… Cleaned Data Preview", cleaned_df.head())
34
- st.download_button("⬇️ Download Cleaned CSV", cleaned_df.to_csv(index=False), "cleaned.csv")
35
-
36
- with st.spinner("πŸ“Š Generating Insights..."):
37
- insight_text = generate_insights(analysis["columns"])
38
- st.markdown("### πŸ“„ Insights")
39
- st.text(insight_text)
40
-
41
- with st.spinner("πŸ“ˆ Generating Visuals..."):
42
- visuals = generate_visual_plan(analysis["columns"])
43
- for vis in visuals:
44
- st.markdown(f"**{vis['title']}** β€” {vis['description']}")
45
- try:
46
- exec(vis["code"])
47
- st.pyplot(plt.gcf())
48
- plt.clf()
49
- except Exception as e:
50
- st.error(f"⚠️ Failed to render: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import streamlit as st
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+ import seaborn as sns
6
+ import os
7
+
8
+ from analyze import analyze_csv
9
+ from plan import generate_cleaning_plan
10
+ from execute import execute_plan
11
+ from insight import generate_insights
12
+ from visual_insight import generate_visual_plan
13
+ from report import ReportBuilder
14
+
15
+ st.set_page_config(page_title="Smart Data Cleaning Agent", layout="wide")
16
+ st.title("🧠 Smart Data Cleaning Agent")
17
+
18
+ os.makedirs("charts", exist_ok=True)
19
+
20
+ uploaded_file = st.file_uploader("πŸ“‚ Upload a CSV file", type=["csv"])
21
+
22
+ if uploaded_file:
23
+ df = pd.read_csv(uploaded_file)
24
+ st.subheader("πŸ” Original Data Preview")
25
+ st.dataframe(df.head())
26
+
27
+ with st.spinner("πŸ“Š Analyzing CSV..."):
28
+ analysis = analyze_csv(uploaded_file)
29
+
30
+ with st.spinner("🧼 Generating Cleaning Plan..."):
31
+ cleaning_plan, cleaning_summary = generate_cleaning_plan(analysis)
32
+ st.subheader("🧹 Cleaning Plan")
33
+ st.json(cleaning_plan)
34
+ st.markdown("### βœ… Cleaning Summary")
35
+ st.markdown(cleaning_summary)
36
+
37
+ with st.spinner("πŸ§ͺ Applying cleaning..."):
38
+ cleaned_df = execute_plan(df.copy(), cleaning_plan)
39
+ st.subheader("🧼 Cleaned Data Preview")
40
+ st.dataframe(cleaned_df.head())
41
+ st.download_button("⬇️ Download Cleaned CSV", cleaned_df.to_csv(index=False), file_name="cleaned.csv")
42
+
43
+ with st.spinner("🧠 Deriving insights..."):
44
+ insights = generate_insights(analysis["columns"])
45
+ st.subheader("πŸ“„ EDA Insights")
46
+ st.text(insights)
47
+
48
+ with st.spinner("πŸ“ˆ Generating recommended plots..."):
49
+ visuals = generate_visual_plan(analysis["columns"])
50
+ for vis in visuals:
51
+ st.markdown(f"#### {vis['title']}")
52
+ st.markdown(vis['description'])
53
+ try:
54
+ exec(vis["code"], {"df": cleaned_df, "plt": plt, "sns": sns, "os": os})
55
+ st.pyplot(plt.gcf())
56
+ plt.clf()
57
+ except Exception as e:
58
+ st.error(f"❌ Failed to render: {e}")
59
+
60
+ if st.button("πŸ“ Generate PDF Report"):
61
+ report = ReportBuilder("report.pdf")
62
+ report.add_title("πŸ“Š Smart Data Cleaning Report")
63
+ report.add_text("Cleaning Summary", cleaning_summary)
64
+ report.add_text("EDA Insights", insights)
65
+
66
+ for vis in visuals:
67
+ if "savefig('" in vis['code']:
68
+ path = vis['code'].split("savefig('")[-1].split("')")[0]
69
+ report.add_image(path, vis['description'])
70
+
71
+ report.save()
72
+ with open("report.pdf", "rb") as f:
73
+ st.download_button("⬇️ Download PDF Report", f, file_name="smart_data_report.pdf")