File size: 3,002 Bytes
0472dbd
67e3963
 
 
 
b411743
67e3963
72a73ff
 
0472dbd
 
 
67e3963
0472dbd
 
 
 
72a73ff
0472dbd
5cc8a1b
67e3963
0472dbd
5cc8a1b
 
 
 
0472dbd
 
 
 
b411743
0472dbd
b411743
0472dbd
5cc8a1b
0472dbd
 
5cc8a1b
0472dbd
b411743
0472dbd
72a73ff
b411743
0472dbd
 
 
 
 
 
 
 
 
 
 
 
 
 
72a73ff
0472dbd
 
 
 
 
 
 
 
 
 
b411743
 
72a73ff
67e3963
0472dbd
 
5cc8a1b
 
0472dbd
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
import os, tempfile, streamlit as st, inspect
from agents.analytics_pipeline import analytics_coordinator
from db_connector import fetch_data_from_db, list_tables, SUPPORTED_ENGINES

st.set_page_config(page_title="BizIntel AI Ultra", layout="wide")
st.title("πŸ“Š BizIntel AI Ultra – Business Intelligence Agent")

TEMP_DIR = tempfile.gettempdir()

# ── 1. Data source ───────────────────────────────────────────
source = st.radio("Select data source", ["Upload CSV", "Connect to SQL Database"])
csv_path = None

if source == "Upload CSV":
    up = st.file_uploader("Upload CSV", ["csv"])
    if up:
        csv_path = os.path.join(TEMP_DIR, up.name)
        with open(csv_path, "wb") as f:
            f.write(up.read())
        st.success("CSV saved βœ…")

else:
    engine = st.selectbox("DB engine", SUPPORTED_ENGINES)
    conn = st.text_input("SQLAlchemy connection string")
    if conn:
        tables = list_tables(conn)
        table = st.selectbox("Table", tables) if tables else None
        if table:
            csv_path = fetch_data_from_db(conn, table)
            st.success(f"Fetched **{table}** as CSV βœ…")

# ── 2. Optional image preview ────────────────────────────────
st.markdown("---")
img = st.file_uploader("Optional image (PNG/JPG)", ["png", "jpg", "jpeg"])
if img:
    img_p = os.path.join(TEMP_DIR, img.name)
    with open(img_p, "wb") as f:
        f.write(img.read())
    st.image(img_p, use_column_width=True)

# ── 3. Run agent pipeline ────────────────────────────────────
if csv_path:
    st.markdown("---")
    st.info("Running analytics pipeline…")

    # Discover an executable method
    cand_methods = ["invoke", "run", "execute", "chat", "__call__"]
    exec_fn = None
    for m in cand_methods:
        if hasattr(analytics_coordinator, m) and callable(getattr(analytics_coordinator, m)):
            exec_fn = getattr(analytics_coordinator, m)
            break

    if exec_fn is None:
        st.error("Cannot find an execution method on LlmAgent. Available attrs:\n"
                 + ", ".join(dir(analytics_coordinator)))
        st.stop()

    # Determine if function expects keyword or positional arg
    sig = inspect.signature(exec_fn)
    try:
        if "input" in sig.parameters:
            report = exec_fn(input=csv_path)
        else:
            report = exec_fn(csv_path)
    except Exception as e:
        st.error(f"Agent execution failed: {e}")
        st.stop()

    st.subheader("πŸ“ Analysis & Strategy Report")
    st.text(report)

    # Show charts if any were created
    for name, cap in [("sales_plot.png", "Sales Trend"), ("forecast_plot.png", "Forecast")]:
        p = os.path.join(TEMP_DIR, name)
        if os.path.exists(p):
            st.image(p, caption=cap, use_column_width=True)