Spaces:
Running
Running
File size: 22,943 Bytes
7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7952fb6 662a0b8 7952fb6 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 662a0b8 7ae981c 7952fb6 7ae981c 662a0b8 7ae981c 662a0b8 7952fb6 7ae981c 662a0b8 7952fb6 7ae981c 662a0b8 | 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | """
app.py
======
DataMind Agent β TRUE Agentic AI + Multi-LLM Streamlit UI
Providers: Google Gemini, OpenAI GPT, Anthropic Claude, xAI Grok,
Mistral AI, Meta Llama (via Together AI), Alibaba Qwen (via Together AI)
Run: streamlit run app.py
"""
import os
import io
import json
import streamlit as st
import pandas as pd
import plotly.express as px
from core_agent import (
PROVIDERS,
get_llm, validate_llm,
load_file, profile_dataframe, profile_to_text,
set_dataframe, build_agent, run_agent,
auto_suggest_charts, make_plotly_chart, recommend_chart,
)
# βββ Page config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
st.set_page_config(
page_title="DataMind Agent",
page_icon="π§ ",
layout="wide",
initial_sidebar_state="expanded",
)
# βββ CSS ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=Syne:wght@400;700;800&family=DM+Sans:wght@300;400;500&display=swap');
html, body, [class*="css"] {
font-family: 'DM Sans', sans-serif;
background-color: #0a0a12;
color: #e8e8ff;
}
.main { background-color: #0a0a12; }
.hero-title {
font-family: 'Syne', sans-serif;
font-size: 2.8rem; font-weight: 800;
background: linear-gradient(135deg, #e8e8ff 0%, #6C63FF 50%, #43E97B 100%);
-webkit-background-clip: text; -webkit-text-fill-color: transparent;
background-clip: text; margin-bottom: 0.2rem;
}
.hero-sub { color: #6a6a9a; font-size: 1rem; margin-bottom: 2rem; }
.stat-card {
background: #1a1a2e; border: 1px solid #2a2a45;
border-radius: 16px; padding: 1.2rem 1.5rem; text-align: center;
}
.stat-num { font-family: 'Syne', sans-serif; font-size: 2rem; font-weight: 800; color: #6C63FF; }
.stat-label { color: #6a6a9a; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.1em; }
.provider-badge {
display: inline-block;
padding: 3px 10px; border-radius: 20px;
font-size: 0.72rem; font-weight: 700;
letter-spacing: 0.05em; margin-bottom: 0.5rem;
}
.user-bubble {
background: rgba(108,99,255,0.15); border: 1px solid rgba(108,99,255,0.3);
border-radius: 18px 18px 4px 18px; padding: 0.9rem 1.2rem;
margin: 0.5rem 0; font-size: 0.95rem;
}
.agent-bubble {
background: #1a1a2e; border: 1px solid #2a2a45;
border-radius: 18px 18px 18px 4px; padding: 0.9rem 1.2rem;
margin: 0.5rem 0; font-size: 0.95rem; line-height: 1.6;
}
section[data-testid="stSidebar"] {
background: #10101e; border-right: 1px solid #2a2a45;
}
.stButton > button {
background: linear-gradient(135deg, #6C63FF, #43E97B);
color: white; border: none; border-radius: 12px;
font-family: 'Syne', sans-serif; font-weight: 700;
padding: 0.6rem 1.5rem; transition: opacity 0.2s;
}
.stButton > button:hover { opacity: 0.85; color: white; }
.stTextInput > div > div > input {
background: #1a1a2e; border: 1px solid #2a2a45;
border-radius: 12px; color: #e8e8ff;
}
.stSelectbox > div > div {
background: #1a1a2e; border: 1px solid #2a2a45; border-radius: 12px;
}
.stTabs [data-baseweb="tab-list"] {
background: #10101e; border-radius: 12px; gap: 0.3rem;
}
.stTabs [data-baseweb="tab"] {
background: transparent; color: #6a6a9a;
border-radius: 10px; font-family: 'Syne', sans-serif;
}
.stTabs [aria-selected="true"] {
background: rgba(108,99,255,0.2) !important; color: #6C63FF !important;
}
</style>
""", unsafe_allow_html=True)
# βββ Session state ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
for key, default in {
"df": None,
"profile": None,
"file_type": None,
"chat_history": [],
"llm": None,
"agent_executor": None,
"active_provider": None,
"active_model": None,
"api_key_set": False,
}.items():
if key not in st.session_state:
st.session_state[key] = default
# βββ Sidebar ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with st.sidebar:
st.markdown("### π§ DataMind Agent")
st.markdown("---")
# Provider selector
st.markdown("**π€ Choose AI Provider**")
provider_labels = {k: v["name"] for k, v in PROVIDERS.items()}
selected_provider = st.selectbox(
"Provider",
options=list(provider_labels.keys()),
format_func=lambda k: provider_labels[k],
label_visibility="collapsed",
key="provider_select",
)
pinfo = PROVIDERS[selected_provider]
# Color badge
st.markdown(
f'<span class="provider-badge" style="background:{pinfo["color"]}22;'
f'color:{pinfo["color"]};border:1px solid {pinfo["color"]}55;">'
f'β {pinfo["name"]}</span>',
unsafe_allow_html=True,
)
# Model selector
selected_model = st.selectbox(
"Model", options=pinfo["models"], index=0,
key=f"model_{selected_provider}",
)
if pinfo.get("note"):
st.caption(f"βΉοΈ {pinfo['note']}")
# API Key
st.markdown(f"**π API Key** β [Get free key]({pinfo['key_url']})")
api_key = st.text_input(
"API Key", type="password",
placeholder=pinfo["key_hint"],
label_visibility="collapsed",
key=f"apikey_{selected_provider}",
)
connect_btn = st.button("π Connect", key="connect_btn", use_container_width=True)
if connect_btn and api_key:
with st.spinner(f"Connecting to {pinfo['name']}..."):
try:
llm, msg = validate_llm(selected_provider, api_key, selected_model)
st.session_state.llm = llm
st.session_state.agent_executor = build_agent(llm)
st.session_state.api_key_set = True
st.session_state.active_provider = selected_provider
st.session_state.active_model = selected_model
st.session_state.chat_history = []
st.success(f"β
Connected to {pinfo['name']}!")
except Exception as e:
st.session_state.api_key_set = False
st.error(f"β Connection failed: {e}")
elif connect_btn and not api_key:
st.warning("β οΈ Please enter your API key first.")
# Active connection badge
if st.session_state.api_key_set and st.session_state.active_provider:
ap = st.session_state.active_provider
am = st.session_state.active_model
ac = PROVIDERS[ap]["color"]
st.markdown(
f'<div style="margin-top:8px;padding:8px 12px;border-radius:10px;'
f'background:{ac}15;border:1px solid {ac}40;font-size:0.78rem;">'
f'<span style="color:{ac}">β</span> <b>{PROVIDERS[ap]["name"]}</b><br/>'
f'<span style="color:#6a6a9a">{am}</span></div>',
unsafe_allow_html=True,
)
st.markdown("---")
# File Upload
st.markdown("**π Upload Data File**")
uploaded = st.file_uploader(
"Upload", type=["csv", "xlsx", "xls", "json"],
label_visibility="collapsed",
)
if uploaded and st.session_state.api_key_set:
with st.spinner("π Analyzing your data..."):
try:
df, ftype = load_file(uploaded)
profile = profile_dataframe(df)
st.session_state.df = df
st.session_state.file_type = ftype
st.session_state.profile = profile
st.session_state.chat_history = []
set_dataframe(df, profile)
st.success(f"β
Loaded {ftype} file!")
except Exception as e:
st.error(f"β Error: {e}")
elif uploaded and not st.session_state.api_key_set:
st.warning("β οΈ Connect to an AI provider first.")
st.markdown("---")
st.markdown("""
**How to use:**
1. Choose an AI provider & model
2. Paste your API key β Connect
3. Upload CSV, Excel, or JSON
4. Explore Dashboard tab
5. Chat with your data!
---
**Get API keys:**
- [Gemini](https://aistudio.google.com/app/apikey)
- [OpenAI](https://platform.openai.com/api-keys)
- [Claude](https://console.anthropic.com/)
- [Grok](https://console.x.ai/)
- [Mistral](https://console.mistral.ai/)
- [Llama / Qwen β Together AI](https://api.together.ai/)
""")
# βββ Main content βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
st.markdown('<div class="hero-title">π§ DataMind Agent</div>', unsafe_allow_html=True)
if st.session_state.api_key_set and st.session_state.active_provider:
ap = st.session_state.active_provider
am = st.session_state.active_model
ac = PROVIDERS[ap]["color"]
sub = (f'Autonomous AI data analyst Β· Powered by '
f'<span style="color:{ac};font-weight:600">{PROVIDERS[ap]["name"]} / {am}</span>')
else:
sub = "Autonomous AI data analyst Β· Connect a provider and upload data to begin"
st.markdown(f'<div class="hero-sub">{sub}</div>', unsafe_allow_html=True)
# βββ Landing ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if st.session_state.df is None:
col1, col2, col3 = st.columns(3)
cards = [
("π€", "7 AI Providers", "Gemini, GPT, Claude, Grok, Mistral, Llama, Qwen"),
("π§", "7 Agentic Tools", "Agent plans, picks tools, executes, and self-corrects"),
("π", "Smart Charts", "AI picks the right visualization for every question"),
]
for col, (icon, title, desc) in zip([col1, col2, col3], cards):
with col:
st.markdown(
f'<div class="stat-card"><div class="stat-num">{icon}</div>'
f'<div class="stat-label">{title}</div><br>'
f'<p style="color:#6a6a9a;font-size:0.85rem">{desc}</p></div>',
unsafe_allow_html=True,
)
st.markdown("<br>", unsafe_allow_html=True)
if not st.session_state.api_key_set:
st.info("π Choose a provider, enter your API key, and click **Connect** in the sidebar.")
else:
st.info("π Upload a data file (CSV, Excel, or JSON) in the sidebar to get started!")
else:
df = st.session_state.df
profile = st.session_state.profile
llm = st.session_state.llm
ap_name = PROVIDERS.get(st.session_state.active_provider, {}).get("name", "AI")
tab1, tab2, tab3, tab4 = st.tabs(["π Dashboard", "π¬ Chat", "π¨ Charts", "π Raw Data"])
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TAB 1 β Dashboard
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with tab1:
rows, cols_n = profile["shape"]
nulls = sum(profile["null_counts"].values())
num_c = len(profile["numeric_columns"])
cat_c = len(profile["categorical_columns"])
c1, c2, c3, c4 = st.columns(4)
for col_obj, num, label in [
(c1, f"{rows:,}", "Rows"),
(c2, str(cols_n), "Columns"),
(c3, str(num_c), "Numeric Cols"),
(c4, str(nulls), "Missing Values"),
]:
col_obj.markdown(
f'<div class="stat-card"><div class="stat-num">{num}</div>'
f'<div class="stat-label">{label}</div></div>',
unsafe_allow_html=True,
)
st.markdown("<br>", unsafe_allow_html=True)
st.markdown("#### π Column Overview")
col_info = pd.DataFrame({
"Column": df.columns,
"Type": df.dtypes.astype(str).values,
"Non-Null": df.notnull().sum().values,
"Null %": (df.isnull().mean() * 100).round(1).values,
"Unique": df.nunique().values,
})
st.dataframe(col_info, use_container_width=True, hide_index=True)
st.markdown("#### π€ Auto-Generated Insights")
suggested = auto_suggest_charts(profile)[:3]
chart_cols = st.columns(min(len(suggested), 2))
for i, ctype in enumerate(suggested[:2]):
with chart_cols[i]:
fig = make_plotly_chart(ctype, df, profile)
st.plotly_chart(fig, use_container_width=True, key=f"dash_chart_{i}")
if len(suggested) > 2:
fig = make_plotly_chart(suggested[2], df, profile)
st.plotly_chart(fig, use_container_width=True, key="dash_chart_2")
st.markdown("#### π§ AI Dataset Summary")
if st.button(f"β¨ Generate Summary with {ap_name}"):
with st.spinner(f"π€ Agent is generating full report..."):
set_dataframe(df, profile)
result = run_agent(
"Give me a full insight report on this dataset with key patterns, "
"anomalies, data quality score, and actionable recommendations.",
st.session_state.agent_executor, []
)
st.markdown(f'<div class="agent-bubble">{result["output"]}</div>',
unsafe_allow_html=True)
if result["steps"]:
with st.expander(f"π Agent used {len(result['steps'])} tool(s)"):
for i, (action, res) in enumerate(result["steps"]):
st.markdown(f"**Step {i+1}: `{action.tool}`**")
st.code(str(res)[:300] + ("..." if len(str(res)) > 300 else ""),
language="text")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TAB 2 β Chat (TRUE AGENTIC)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with tab2:
st.markdown("#### π¬ Ask Anything About Your Data")
st.markdown(f"*Autonomous agent powered by **{ap_name} / {st.session_state.active_model}** "
f"β plans, uses tools, reasons step-by-step*")
st.markdown("**Quick questions to try:**")
suggestions = [
"Give me a full insight report on this data",
"Are there any outliers or anomalies?",
"What correlations exist between numeric columns?",
]
q_cols = st.columns(3)
for i, s in enumerate(suggestions):
with q_cols[i]:
if st.button(s, key=f"sug_{i}"):
st.session_state["prefill_q"] = s
# Chat history
for turn in st.session_state.chat_history:
st.markdown(f'<div class="user-bubble">π€ {turn["user"]}</div>',
unsafe_allow_html=True)
if turn.get("steps"):
with st.expander(f"π Agent used {len(turn['steps'])} tool(s) β click to see reasoning"):
for i, (action, res) in enumerate(turn["steps"]):
st.markdown(f"**Step {i+1}: `{action.tool}`**")
st.caption(f"Input: {action.tool_input}")
st.code(str(res)[:400] + ("..." if len(str(res)) > 400 else ""),
language="text")
st.markdown(f'<div class="agent-bubble">π§ {turn["agent"]}</div>',
unsafe_allow_html=True)
prefill = st.session_state.pop("prefill_q", "")
question = st.text_input(
"Ask a question...", value=prefill,
placeholder="e.g. Which category has the highest profit? Find outliers in sales.",
label_visibility="collapsed",
)
col_send, col_clear = st.columns([1, 5])
with col_send:
send = st.button("Send π")
with col_clear:
if st.button("Clear Chat"):
st.session_state.chat_history = []
st.rerun()
if send and question.strip():
# Build LangChain chat history
from langchain_core.messages import HumanMessage as HM, AIMessage
lc_history = []
for turn in st.session_state.chat_history:
lc_history.append(HM(content=turn["user"]))
lc_history.append(AIMessage(content=turn["agent"]))
with st.spinner(f"π€ Agent is planning and executing tools..."):
set_dataframe(df, profile)
result = run_agent(question, st.session_state.agent_executor, lc_history)
answer = result["output"]
steps = result["steps"]
# Get chart recommendation
try:
chart_json = json.loads(recommend_chart.invoke(question))
except Exception:
chart_json = None
st.session_state.chat_history.append({
"user": question, "agent": answer, "steps": steps,
})
st.markdown(f'<div class="user-bubble">π€ {question}</div>', unsafe_allow_html=True)
if steps:
with st.expander(f"π Agent used {len(steps)} tool(s) β click to see reasoning"):
for i, (action, res) in enumerate(steps):
st.markdown(f"**Step {i+1}: `{action.tool}`**")
st.caption(f"Input: {action.tool_input}")
st.code(str(res)[:400] + ("..." if len(str(res)) > 400 else ""),
language="text")
st.markdown(f'<div class="agent-bubble">π§ {answer}</div>', unsafe_allow_html=True)
if chart_json:
try:
fig = make_plotly_chart(
chart_json["chart_type"], df, profile,
x_col=chart_json.get("x_col"),
y_col=chart_json.get("y_col"),
)
st.plotly_chart(fig, use_container_width=True,
key=f"chat_chart_{len(st.session_state.chat_history)}")
except Exception:
pass
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TAB 3 β Charts
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with tab3:
st.markdown("#### π¨ Custom Chart Builder")
chart_options = {
"Correlation Heatmap": "correlation_heatmap",
"Distribution Plot": "distribution_plots",
"Box Plots": "box_plots",
"Bar Chart": "bar_chart",
"Pie Chart": "pie_chart",
"Scatter Plot": "scatter",
"Line Chart": "line",
"Scatter Matrix": "scatter_matrix",
}
if profile["datetime_columns"]:
chart_options["Time Series"] = "time_series"
c1, c2, c3 = st.columns(3)
with c1:
chart_label = st.selectbox("Chart Type", list(chart_options.keys()))
with c2:
all_cols = ["(auto)"] + df.columns.tolist()
x_col = st.selectbox("X Column", all_cols)
with c3:
y_col = st.selectbox("Y Column", all_cols)
x_val = None if x_col == "(auto)" else x_col
y_val = None if y_col == "(auto)" else y_col
if st.button("π¨ Generate Chart"):
with st.spinner("Rendering..."):
fig = make_plotly_chart(
chart_options[chart_label], df, profile,
x_col=x_val, y_col=y_val,
)
st.plotly_chart(fig, use_container_width=True, key="custom_chart")
st.markdown("---")
st.markdown("#### π All Auto-Suggested Charts")
suggested_all = auto_suggest_charts(profile)
for i in range(0, len(suggested_all), 2):
cols = st.columns(2)
for j, ctype in enumerate(suggested_all[i:i+2]):
with cols[j]:
fig = make_plotly_chart(ctype, df, profile)
st.plotly_chart(fig, use_container_width=True,
key=f"suggested_{i}_{j}_{ctype}")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# TAB 4 β Raw Data
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
with tab4:
st.markdown("#### π Raw Data Explorer")
search = st.text_input("π Filter rows containing...", placeholder="Type to filter...")
if search:
mask = df.astype(str).apply(
lambda row: row.str.contains(search, case=False, na=False)
).any(axis=1)
display_df = df[mask]
st.info(f"Showing {len(display_df):,} of {len(df):,} rows matching '{search}'")
else:
display_df = df
st.dataframe(display_df, use_container_width=True, height=500)
csv_buf = io.StringIO()
df.to_csv(csv_buf, index=False)
st.download_button(
"β¬οΈ Download as CSV",
data=csv_buf.getvalue(),
file_name="analyzed_data.csv",
mime="text/csv",
) |