-
Notifications
You must be signed in to change notification settings - Fork 81
/
app.py
153 lines (128 loc) · 5.51 KB
/
app.py
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
import time
import streamlit as st
from vanna_calls import (
generate_questions_cached,
generate_sql_cached,
run_sql_cached,
generate_plotly_code_cached,
generate_plot_cached,
generate_followup_cached,
should_generate_chart_cached,
is_sql_valid_cached,
generate_summary_cached
)
avatar_url = "https://vanna.ai/img/vanna.svg"
st.set_page_config(layout="wide")
st.sidebar.title("Output Settings")
st.sidebar.checkbox("Show SQL", value=True, key="show_sql")
st.sidebar.checkbox("Show Table", value=True, key="show_table")
st.sidebar.checkbox("Show Plotly Code", value=True, key="show_plotly_code")
st.sidebar.checkbox("Show Chart", value=True, key="show_chart")
st.sidebar.checkbox("Show Summary", value=True, key="show_summary")
st.sidebar.checkbox("Show Follow-up Questions", value=True, key="show_followup")
st.sidebar.button("Reset", on_click=lambda: set_question(None), use_container_width=True)
st.title("Vanna AI")
# st.sidebar.write(st.session_state)
def set_question(question):
st.session_state["my_question"] = question
assistant_message_suggested = st.chat_message(
"assistant", avatar=avatar_url
)
if assistant_message_suggested.button("Click to show suggested questions"):
st.session_state["my_question"] = None
questions = generate_questions_cached()
for i, question in enumerate(questions):
time.sleep(0.05)
button = st.button(
question,
on_click=set_question,
args=(question,),
)
my_question = st.session_state.get("my_question", default=None)
if my_question is None:
my_question = st.chat_input(
"Ask me a question about your data",
)
if my_question:
st.session_state["my_question"] = my_question
user_message = st.chat_message("user")
user_message.write(f"{my_question}")
sql = generate_sql_cached(question=my_question)
if sql:
if is_sql_valid_cached(sql=sql):
if st.session_state.get("show_sql", True):
assistant_message_sql = st.chat_message(
"assistant", avatar=avatar_url
)
assistant_message_sql.code(sql, language="sql", line_numbers=True)
else:
assistant_message = st.chat_message(
"assistant", avatar=avatar_url
)
assistant_message.write(sql)
st.stop()
df = run_sql_cached(sql=sql)
if df is not None:
st.session_state["df"] = df
if st.session_state.get("df") is not None:
if st.session_state.get("show_table", True):
df = st.session_state.get("df")
assistant_message_table = st.chat_message(
"assistant",
avatar=avatar_url,
)
if len(df) > 10:
assistant_message_table.text("First 10 rows of data")
assistant_message_table.dataframe(df.head(10))
else:
assistant_message_table.dataframe(df)
if should_generate_chart_cached(question=my_question, sql=sql, df=df):
code = generate_plotly_code_cached(question=my_question, sql=sql, df=df)
if st.session_state.get("show_plotly_code", False):
assistant_message_plotly_code = st.chat_message(
"assistant",
avatar=avatar_url,
)
assistant_message_plotly_code.code(
code, language="python", line_numbers=True
)
if code is not None and code != "":
if st.session_state.get("show_chart", True):
assistant_message_chart = st.chat_message(
"assistant",
avatar=avatar_url,
)
fig = generate_plot_cached(code=code, df=df)
if fig is not None:
assistant_message_chart.plotly_chart(fig)
else:
assistant_message_chart.error("I couldn't generate a chart")
if st.session_state.get("show_summary", True):
assistant_message_summary = st.chat_message(
"assistant",
avatar=avatar_url,
)
summary = generate_summary_cached(question=my_question, df=df)
if summary is not None:
assistant_message_summary.text(summary)
if st.session_state.get("show_followup", True):
assistant_message_followup = st.chat_message(
"assistant",
avatar=avatar_url,
)
followup_questions = generate_followup_cached(
question=my_question, sql=sql, df=df
)
st.session_state["df"] = None
if len(followup_questions) > 0:
assistant_message_followup.text(
"Here are some possible follow-up questions"
)
# Print the first 5 follow-up questions
for question in followup_questions[:5]:
assistant_message_followup.button(question, on_click=set_question, args=(question,))
else:
assistant_message_error = st.chat_message(
"assistant", avatar=avatar_url
)
assistant_message_error.error("I wasn't able to generate SQL for that question")