-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreamlit_app.py
More file actions
117 lines (94 loc) · 4.26 KB
/
Copy pathstreamlit_app.py
File metadata and controls
117 lines (94 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from pathlib import Path
import sys
import pandas as pd
import streamlit as st
ROOT = Path(__file__).resolve().parent
sys.path.insert(0, str(ROOT / "backend"))
from app.analysis import clean_dataframe, full_analysis, generate_chart, load_dataframe_from_upload
from app.main import chart_request_from_prompt, simple_answer
st.set_page_config(
page_title="AnalystGPT Enterprise",
layout="wide",
)
st.title("AnalystGPT Enterprise")
st.caption("Upload a dataset, inspect quality, generate charts, clean data, and ask questions.")
uploaded_file = st.file_uploader(
"Upload dataset",
type=["csv", "tsv", "xlsx", "xls", "json", "jsonl", "parquet"],
)
@st.cache_data(show_spinner=False)
def read_dataset(name: str, contents: bytes) -> pd.DataFrame:
return load_dataframe_from_upload(name, contents)
if not uploaded_file:
st.info("Upload a tabular file to start analysis.")
st.stop()
try:
dataframe = read_dataset(uploaded_file.name, uploaded_file.getvalue())
except Exception as exc:
st.error(f"Could not read dataset: {exc}")
st.stop()
analysis = full_analysis(dataframe)
overview_col, quality_col, numeric_col = st.columns(3)
overview_col.metric("Rows", f"{dataframe.shape[0]:,}")
quality_col.metric("Columns", f"{dataframe.shape[1]:,}")
numeric_col.metric("Quality score", analysis.get("quality", {}).get("score", "N/A"))
tabs = st.tabs(["Preview", "Insights", "Charts", "Ask", "Clean", "Predict"])
with tabs[0]:
st.subheader("Dataset Preview")
st.dataframe(dataframe.head(100), use_container_width=True)
st.subheader("Column Types")
st.dataframe(
pd.DataFrame(
{"column": dataframe.columns, "type": [str(dtype) for dtype in dataframe.dtypes]}
),
use_container_width=True,
)
with tabs[1]:
st.subheader("AI-Ready Summary")
st.markdown(analysis.get("ai_insights", "No insights available."))
stats_col, missing_col, anomaly_col = st.columns(3)
stats_col.write("Mean")
stats_col.json(analysis.get("mean", {}))
missing_col.write("Missing Values")
missing_col.json(analysis.get("missing", {}))
anomaly_col.write("Anomalies")
anomaly_col.json(analysis.get("anomalies", {}))
with tabs[2]:
st.subheader("Chart Builder")
chart_prompt = st.text_input("Chart prompt", placeholder="city wise average price ka bar chart banao")
chart_type = st.selectbox("Chart type", ["histogram", "boxplot", "bar", "heatmap"])
x_column = st.selectbox("X / column", [""] + list(dataframe.columns))
y_column = st.selectbox("Y column", [""] + list(dataframe.columns))
if chart_prompt:
chart_type, x_column, y_column = chart_request_from_prompt(dataframe, chart_prompt)
st.caption(f"Detected: {chart_type}, x={x_column or 'none'}, y={y_column or 'none'}")
if st.button("Generate chart", type="primary"):
try:
chart_url = generate_chart(dataframe, chart_type, x_column or None, y_column or None)
st.image(str(ROOT / chart_url.lstrip("/")), use_container_width=True)
except Exception as exc:
st.error(f"Chart failed: {exc}")
with tabs[3]:
st.subheader("Ask About This Dataset")
query = st.text_input("Question", placeholder="Which city has highest average price?")
if st.button("Ask AnalystGPT") and query:
with st.spinner("Analyzing dataset..."):
st.markdown(simple_answer(dataframe, query))
with tabs[4]:
st.subheader("Clean Dataset")
remove_duplicates = st.checkbox("Remove duplicates", value=True)
drop_missing = st.checkbox("Drop rows with missing values")
fill_missing = st.selectbox("Fill missing values", ["", "mean", "median", "mode", "zero"])
if st.button("Apply cleaning"):
cleaned = clean_dataframe(dataframe, remove_duplicates, fill_missing or None, drop_missing)
st.success(f"Cleaned dataset: {cleaned.shape[0]:,} rows x {cleaned.shape[1]:,} columns")
st.dataframe(cleaned.head(100), use_container_width=True)
st.download_button(
"Download cleaned CSV",
cleaned.to_csv(index=False),
file_name=f"{Path(uploaded_file.name).stem}_cleaned.csv",
mime="text/csv",
)
with tabs[5]:
st.subheader("Baseline Prediction")
st.info("Use the full Next.js + FastAPI app for the complete prediction workflow.")