|
|
|
import streamlit as st |
|
import pandas as pd |
|
import matplotlib.pyplot as plt |
|
import seaborn as sns |
|
import os |
|
|
|
from analyze import analyze_csv |
|
from plan import generate_cleaning_plan |
|
from execute import execute_plan |
|
from insight import generate_insights |
|
from visual_insight import generate_visual_plan |
|
from report import ReportBuilder |
|
|
|
st.set_page_config(page_title="Smart Data Cleaning Agent", layout="wide") |
|
st.title("π§ Smart Data Cleaning Agent") |
|
|
|
os.makedirs("charts", exist_ok=True) |
|
|
|
uploaded_file = st.file_uploader("π Upload a CSV file", type=["csv"]) |
|
|
|
if uploaded_file: |
|
df = pd.read_csv(uploaded_file) |
|
st.subheader("π Original Data Preview") |
|
st.dataframe(df.head()) |
|
|
|
with st.spinner("π Analyzing CSV..."): |
|
analysis = analyze_csv(uploaded_file) |
|
|
|
with st.spinner("π§Ό Generating Cleaning Plan..."): |
|
cleaning_plan, cleaning_summary = generate_cleaning_plan(analysis) |
|
st.subheader("π§Ή Cleaning Plan") |
|
st.json(cleaning_plan) |
|
st.markdown("### β
Cleaning Summary") |
|
st.markdown(cleaning_summary) |
|
|
|
with st.spinner("π§ͺ Applying cleaning..."): |
|
cleaned_df = execute_plan(df.copy(), cleaning_plan) |
|
st.subheader("π§Ό Cleaned Data Preview") |
|
st.dataframe(cleaned_df.head()) |
|
st.download_button("β¬οΈ Download Cleaned CSV", cleaned_df.to_csv(index=False), file_name="cleaned.csv") |
|
|
|
with st.spinner("π§ Deriving insights..."): |
|
insights = generate_insights(analysis["columns"]) |
|
st.subheader("π EDA Insights") |
|
st.text(insights) |
|
|
|
with st.spinner("π Generating recommended plots..."): |
|
visuals = generate_visual_plan(analysis["columns"]) |
|
for vis in visuals: |
|
st.markdown(f"#### {vis['title']}") |
|
st.markdown(vis['description']) |
|
try: |
|
exec(vis["code"], {"df": cleaned_df, "plt": plt, "sns": sns, "os": os}) |
|
st.pyplot(plt.gcf()) |
|
plt.clf() |
|
except Exception as e: |
|
st.error(f"β Failed to render: {e}") |
|
|
|
if st.button("π Generate PDF Report"): |
|
report = ReportBuilder("report.pdf") |
|
report.add_title("π Smart Data Cleaning Report") |
|
report.add_text("Cleaning Summary", cleaning_summary) |
|
report.add_text("EDA Insights", insights) |
|
|
|
for vis in visuals: |
|
if "savefig('" in vis['code']: |
|
path = vis['code'].split("savefig('")[-1].split("')")[0] |
|
report.add_image(path, vis['description']) |
|
|
|
report.save() |
|
with open("report.pdf", "rb") as f: |
|
st.download_button("β¬οΈ Download PDF Report", f, file_name="smart_data_report.pdf") |
|
|