fahmiaziz commited on
Commit
37c6c29
·
verified ·
1 Parent(s): 5eda983

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -39
app.py CHANGED
@@ -4,62 +4,68 @@ from lib import Text2SQLRAG
4
  from utils import execute_query_and_return_df
5
 
6
 
7
- st.set_page_config(page_title="Text2SQLRAG")
8
- st.title("Text2SQLRAG")
9
 
10
- # Create an instance of Text2SQLRAG
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  text2sql = Text2SQLRAG()
12
  chromadb.api.client.SharedSystemClient.clear_system_cache()
13
 
14
- # Initialize session state for storing chat messages
15
  if "messages" not in st.session_state:
16
  st.session_state.messages = []
17
-
18
- # Display conversation history from session state
19
  for message in st.session_state.messages:
20
  role = message.get("role", "assistant")
21
  with st.chat_message(role):
22
- if "output" in message:
23
- with st.expander("Reasoning", expanded=True):
 
 
24
  st.markdown(message["reasoning"])
25
- if "sql_query" in message and message["sql_query"]:
26
- with st.expander("SQL Query", expanded=True):
27
- st.code(message["sql_query"])
 
28
 
29
- # Get user input
30
  input_text = st.chat_input("Chat with your bot here...")
31
 
32
  if input_text:
33
- # Display user input
34
  with st.chat_message("user"):
35
  st.markdown(input_text)
36
-
37
- # Add user input to chat history
38
- st.session_state.chat_history.append({"role": "user", "text": input_text})
39
-
40
- # Get chatbot response
41
- response = text2sql.run(input_text)
42
  sql_query = response.query
43
  reasoning = response.reasoning
44
  df = execute_query_and_return_df(sql_query)
45
-
46
- if sql_query:
47
- with st.expander("SQL Query", expanded=True):
48
- st.code(sql_query)
49
-
50
- with st.expander("Reasoning", expanded=True):
51
- st.write(reasoning)
52
-
53
- if df is not None:
54
- st.dataframe(df)
55
- else:
56
- st.error("Error executing query")
57
 
58
- # Append assistant response to session state
59
- st.session_state.messages.append(
60
- {
61
- "role": "assistant",
62
- "reasoning": reasoning,
63
- "sql_query": sql_query,
64
- }
65
- )
 
 
 
 
 
 
 
 
 
 
 
4
  from utils import execute_query_and_return_df
5
 
6
 
 
 
7
 
8
+ st.set_page_config(page_title="Text2SQLRAG", layout="wide")
9
+ st.title("Text2SQLRAG: Conversational SQL Generator")
10
+
11
+ with st.sidebar:
12
+
13
+ st.markdown("""
14
+ # Example Question
15
+ - Calculate the average invoice amount
16
+ - Find all artists whose names start with 'A'
17
+ - Find all playlists containing tracks composed by Bach
18
+ - Count how many different artists are in each playlist
19
+ - Find the 5 tracks that appear on the most invoices
20
+ """)
21
+
22
  text2sql = Text2SQLRAG()
23
  chromadb.api.client.SharedSystemClient.clear_system_cache()
24
 
 
25
  if "messages" not in st.session_state:
26
  st.session_state.messages = []
27
+
 
28
  for message in st.session_state.messages:
29
  role = message.get("role", "assistant")
30
  with st.chat_message(role):
31
+ st.markdown(message.get("text", ""))
32
+
33
+ if "reasoning" in message:
34
+ with st.expander("Reasoning", expanded=False):
35
  st.markdown(message["reasoning"])
36
+
37
+ if "sql_query" in message:
38
+ with st.expander("SQL Query", expanded=False):
39
+ st.code(message["sql_query"], language="sql")
40
 
 
41
  input_text = st.chat_input("Chat with your bot here...")
42
 
43
  if input_text:
 
44
  with st.chat_message("user"):
45
  st.markdown(input_text)
46
+
47
+ st.session_state.messages.append({"role": "user", "text": input_text})
48
+
49
+ response = text2sql.run(input_text, database=selected_db)
 
 
50
  sql_query = response.query
51
  reasoning = response.reasoning
52
  df = execute_query_and_return_df(sql_query)
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
+ with st.chat_message("assistant"):
55
+ if sql_query:
56
+ with st.expander("SQL Query", expanded=True):
57
+ st.code(sql_query, language="sql")
58
+
59
+ with st.expander("Reasoning", expanded=True):
60
+ st.markdown(reasoning)
61
+
62
+ if df is not None and not df.empty:
63
+ st.dataframe(df)
64
+ else:
65
+ st.error("Error executing query or no data returned.")
66
+
67
+ st.session_state.messages.append({
68
+ "role": "assistant",
69
+ "reasoning": reasoning,
70
+ "sql_query": sql_query,
71
+ })