Spaces:
Running
Running
File size: 6,790 Bytes
c6c1e2b b038dbc a1951e5 b038dbc c6c1e2b e9af7e9 f5bbade e9af7e9 cd4c36b 1499c69 d20a7d6 e9af7e9 a1951e5 e9af7e9 b038dbc e9af7e9 a1951e5 e9af7e9 bfd6209 e9af7e9 b038dbc e9af7e9 b038dbc e9af7e9 b038dbc e9af7e9 f5bbade |
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 |
import streamlit as st
from apis import generate_sql, generate_chart
from utils import format_sql
def main():
st.title("π Wren AI Cloud API Demo - Chart Generation")
if "api_key" not in st.session_state or "project_id" not in st.session_state:
st.error("Please enter your API Key and Project ID in the sidebar of Home page to get started.")
return
if not st.session_state.api_key or not st.session_state.project_id:
st.error("Please enter your API Key and Project ID in the sidebar of Home page to get started.")
return
api_key = st.session_state.api_key
project_id = st.session_state.project_id
st.markdown('Using APIs: [SQL Generation](https://wrenai.readme.io/reference/cloud_post_generate-sql), [Chart Generation](https://wrenai.readme.io/reference/cloud_post_generate-vega-chart)')
# Sidebar for API configuration
with st.sidebar:
st.header("π§ Configuration")
sample_size = st.slider(
"Chart Sample Size",
min_value=100,
max_value=10000,
value=1000,
step=100,
help="Number of data points to include in charts"
)
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
if "thread_id" not in st.session_state:
st.session_state.thread_id = ""
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
if message["role"] == "user":
st.write(message["content"])
else:
st.write(message["content"])
if "sql" in message:
with st.expander("π Generated SQL Query", expanded=False):
st.code(format_sql(message["sql"]), language="sql")
if "vega_spec" in message:
try:
with st.expander("π Chart Specification", expanded=False):
st.json(message["vega_spec"])
st.vega_lite_chart(message["vega_spec"])
except Exception as e:
st.toast(f"Error rendering chart: {e}", icon="π¨")
# Chat input
if prompt := st.chat_input("Ask a question about your data..."):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display user message
with st.chat_message("user"):
st.write(prompt)
# Generate response
with st.chat_message("assistant"):
with st.spinner("Generating SQL query..."):
sql_response, error = generate_sql(api_key, project_id, prompt, st.session_state.thread_id)
if sql_response:
sql_query = sql_response.get("sql", "")
st.session_state.thread_id = sql_response.get("threadId", "")
if sql_query:
st.toast("SQL query generated successfully!", icon="π")
# Store the response
assistant_message = {
"role": "assistant",
"content": f"I've generated a SQL query for your question: '{prompt}'",
"sql": sql_query
}
st.session_state.messages.append(assistant_message)
st.write(assistant_message["content"])
# Display SQL query
with st.expander("π Generated SQL Query", expanded=False):
st.code(format_sql(sql_query), language="sql")
# Generate chart
with st.spinner("Generating chart..."):
chart_response, error = generate_chart(
api_key,
project_id,
prompt,
sql_query,
thread_id=st.session_state.thread_id,
sample_size=sample_size,
)
if chart_response:
vega_spec = chart_response.get("vegaSpec", {})
if vega_spec:
st.toast("Chart generated successfully!", icon="π")
assistant_message = {
"role": "assistant",
"content": f"I've generated a Chart for your question: '{prompt}'",
"vega_spec": vega_spec
}
st.session_state.messages.append(assistant_message)
st.write(assistant_message["content"])
# Display chart
try:
# Show chart specification in expander
with st.expander("π Chart Specification", expanded=False):
st.json(vega_spec)
st.vega_lite_chart(vega_spec)
except Exception as e:
st.toast(f"Error rendering chart: {e}", icon="π¨")
else:
st.toast("Failed to generate chart. Please check your query and try again.", icon="π¨")
else:
st.toast(f"Failed to generate chart. Please check your query and try again.: {error}", icon="π¨")
else:
st.toast("No SQL query was generated. Please try rephrasing your question.", icon="π¨")
assistant_message = {
"role": "assistant",
"content": "I couldn't generate a SQL query for your question. Please try rephrasing it or make sure it's related to your data."
}
st.session_state.messages.append(assistant_message)
else:
st.toast(f"Error generating SQL: {error}", icon="π¨")
assistant_message = {
"role": "assistant",
"content": "Sorry, I couldn't process your request. Please check your API credentials and try again."
}
st.session_state.messages.append(assistant_message)
# Clear chat button
if st.sidebar.button("ποΈ Clear Chat History"):
st.session_state.messages = []
st.session_state.thread_id = ""
st.rerun()
if __name__ == "__main__":
main() |