Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- 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 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
from
|
9 |
-
|
10 |
-
|
11 |
-
import
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
st.
|
34 |
-
st.
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
st.
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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")
|