ofermend commited on
Commit
154da3c
1 Parent(s): d7f8ae3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -23
app.py CHANGED
@@ -7,11 +7,21 @@ import os
7
  import streamlit as st
8
  from PIL import Image
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def launch_bot():
12
- def generate_response(question):
13
- response = vq.submit_query(question)
14
- return response
15
 
16
  if 'cfg' not in st.session_state:
17
  corpus_ids = str(os.environ['corpus_ids']).split(',')
@@ -27,7 +37,6 @@ def launch_bot():
27
  })
28
  st.session_state.cfg = cfg
29
  st.session_state.vq = VectaraQuery(cfg.api_key, cfg.customer_id, cfg.corpus_ids)
30
- st.session_state.first_round = True
31
 
32
  cfg = st.session_state.cfg
33
  vq = st.session_state.vq
@@ -60,26 +69,33 @@ def launch_bot():
60
  with st.chat_message(message["role"]):
61
  st.write(message["content"])
62
 
63
- if st.session_state.first_round:
64
- st.markdown("**Example questions:**")
 
 
 
 
65
  for example in cfg.examples:
66
- if st.button(example):
67
- prompt = example
68
- st.session_state.first_round = False
69
- process_prompt(prompt)
70
- else:
71
- if prompt := st.chat_input("Ask me anything..."):
72
- process_prompt(prompt) # Process the user-provided prompt
 
 
 
 
 
 
 
 
73
 
74
- def process_prompt(prompt):
75
- # Your logic to process the prompt and generate response
76
- st.session_state.messages.append({"role": "user", "content": prompt})
77
- with st.chat_message("assistant"):
78
- with st.spinner("Thinking..."):
79
- response = generate_response(prompt)
80
- st.write(response)
81
- message = {"role": "assistant", "content": response}
82
- st.session_state.messages.append(message)
83
-
84
  if __name__ == "__main__":
85
  launch_bot()
 
7
  import streamlit as st
8
  from PIL import Image
9
 
10
+ def generate_response(question):
11
+ response = vq.submit_query(question)
12
+ return response
13
+
14
+ def process_prompt(prompt):
15
+ # Your logic to process the prompt and generate response
16
+ st.session_state.messages.append({"role": "user", "content": prompt})
17
+ with st.chat_message("assistant"):
18
+ with st.spinner("Thinking..."):
19
+ response = generate_response(prompt)
20
+ st.write(response)
21
+ message = {"role": "assistant", "content": response}
22
+ st.session_state.messages.append(message)
23
 
24
  def launch_bot():
 
 
 
25
 
26
  if 'cfg' not in st.session_state:
27
  corpus_ids = str(os.environ['corpus_ids']).split(',')
 
37
  })
38
  st.session_state.cfg = cfg
39
  st.session_state.vq = VectaraQuery(cfg.api_key, cfg.customer_id, cfg.corpus_ids)
 
40
 
41
  cfg = st.session_state.cfg
42
  vq = st.session_state.vq
 
69
  with st.chat_message(message["role"]):
70
  st.write(message["content"])
71
 
72
+ # Always allow typing in the chat input box
73
+ user_input = st.text_input("Type your question here...", key="user_input")
74
+
75
+ # Display example questions only during the first round
76
+ if len(st.session_state.messages) <= 1: # Adjust based on when you want examples to disappear
77
+ st.markdown("**Or choose an example question:**")
78
  for example in cfg.examples:
79
+ if st.button(example, key=example):
80
+ user_input = example
81
+ st.session_state['user_input'] = user_input
82
+
83
+ # Check if there's input to process (either typed or selected from examples)
84
+ if user_input:
85
+ # This block prevents immediate processing when the page loads. Adjust as necessary.
86
+ if "processed" not in st.session_state or not st.session_state["processed"]:
87
+ st.session_state.messages.append({"role": "user", "content": user_input})
88
+ with st.chat_message("assistant"):
89
+ with st.spinner("Thinking..."):
90
+ response = generate_response(user_input)
91
+ st.write(response)
92
+ st.session_state.messages.append({"role": "assistant", "content": response})
93
+ st.session_state["processed"] = True # Prevent re-processing the same input on refresh
94
 
95
+ # Clear the input for the next message
96
+ st.session_state['user_input'] = ''
97
+ st.experimental_rerun() # Rerun the app to clear the input box
98
+
99
+
 
 
 
 
 
100
  if __name__ == "__main__":
101
  launch_bot()