File size: 1,998 Bytes
e0560d9 | 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 | import streamlit as st
import pandas as pd
from agents.langchain_sql_agent import ask_agent
st.set_page_config(
page_title="GenAI Text2SQL Assistant",
page_icon="π€",
layout="wide"
)
st.title("π€ GenAI Text2SQL Analytics Assistant")
st.markdown("""
Query your business database using natural language.
Examples:
- Top selling products
- Revenue by category
- Monthly sales trend
- Customer spending analysis
""")
question = st.text_input(
"Ask your business question:"
)
if st.button("Generate Insights"):
if question:
with st.spinner("Analyzing database..."):
response = ask_agent(question)
# Error Handling
if "error" in response:
st.error(response["error"])
else:
st.subheader("π§ Generated SQL")
st.code(response["sql"], language="sql")
st.subheader("π Query Results")
st.dataframe(
response["data"],
use_container_width=True
)
csv = response["data"].to_csv(index=False)
st.download_button(
label="β¬ Download Results CSV",
data=csv,
file_name="query_results.csv",
mime="text/csv"
)
st.subheader("π€ AI Business Summary")
st.write(response["summary"])
df = response["data"]
if len(df.columns) >= 2:
numeric_cols = df.select_dtypes(
include="number"
).columns
if len(numeric_cols) > 0:
st.subheader("π Visualization")
chart_col = numeric_cols[0]
st.bar_chart(
df.set_index(df.columns[0])[chart_col]
) |