boryasbora commited on
Commit
7593922
·
verified ·
1 Parent(s): 5bd5d90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -138,11 +138,10 @@ def run_with_timeout(func, args, timeout):
138
  # In your Streamlit app
139
  def generate_response(chain, query, context):
140
  timeout_seconds = 60
141
- result = run_with_timeout(chain.run, ({"context": context, "question": query},), timeout_seconds)
142
  if result is None:
143
  return "I apologize, but I couldn't generate a response in time. The query might be too complex for me to process quickly. Could you try simplifying your question?"
144
  return result
145
-
146
  # Sidebar
147
  with st.sidebar:
148
  st.title("OHW Assistant")
@@ -151,8 +150,13 @@ with st.sidebar:
151
  st.button('Clear Chat History', on_click=clear_chat_history)
152
 
153
  # Main app
 
154
  if "messages" not in st.session_state:
155
  st.session_state.messages = []
 
 
 
 
156
 
157
 
158
  for q, message in enumerate(st.session_state.messages):
@@ -180,27 +184,28 @@ if prompt := st.chat_input("How may I assist you today?"):
180
  st.markdown(prompt)
181
 
182
  with st.chat_message("assistant"):
183
- query=st.session_state.messages[-1]['content']
184
  tab1, tab2 = st.tabs(["Answer", "Sources"])
185
  with tab1:
186
  with st.spinner("Generating answer..."):
187
  chain = get_chain(temperature)
188
- context = "\n".join(st.session_state.context_content)
189
  start_time = time.time()
190
- full_answer = generate_response(chain, query, context)
191
  end_time = time.time()
192
-
193
  st.markdown(full_answer)
194
  st.caption(f"Response time: {end_time - start_time:.2f} seconds")
195
- with tab2:
196
- for i, source in enumerate(st.session_state.context_sources):
197
- name = f'{source}'
198
- with st.expander(name):
199
- st.markdown(f'{st.session_state.context_content[i]}')
200
-
201
-
202
 
 
 
 
 
 
 
 
 
203
 
204
  st.session_state.messages.append({"role": "assistant", "content": full_answer})
205
  st.session_state.messages[-1]['sources'] = st.session_state.context_sources
206
  st.session_state.messages[-1]['context'] = st.session_state.context_content
 
 
138
  # In your Streamlit app
139
  def generate_response(chain, query, context):
140
  timeout_seconds = 60
141
+ result = run_with_timeout(chain.invoke, ({"question": query, "chat_history": st.session_state.messages},), timeout_seconds)
142
  if result is None:
143
  return "I apologize, but I couldn't generate a response in time. The query might be too complex for me to process quickly. Could you try simplifying your question?"
144
  return result
 
145
  # Sidebar
146
  with st.sidebar:
147
  st.title("OHW Assistant")
 
150
  st.button('Clear Chat History', on_click=clear_chat_history)
151
 
152
  # Main app
153
+ # Initialize session state variables
154
  if "messages" not in st.session_state:
155
  st.session_state.messages = []
156
+ if "context_sources" not in st.session_state:
157
+ st.session_state.context_sources = []
158
+ if "context_content" not in st.session_state:
159
+ st.session_state.context_content = []
160
 
161
 
162
  for q, message in enumerate(st.session_state.messages):
 
184
  st.markdown(prompt)
185
 
186
  with st.chat_message("assistant"):
187
+ query = st.session_state.messages[-1]['content']
188
  tab1, tab2 = st.tabs(["Answer", "Sources"])
189
  with tab1:
190
  with st.spinner("Generating answer..."):
191
  chain = get_chain(temperature)
 
192
  start_time = time.time()
193
+ full_answer = generate_response(chain, query, "") # Context is handled within the chain
194
  end_time = time.time()
195
+
196
  st.markdown(full_answer)
197
  st.caption(f"Response time: {end_time - start_time:.2f} seconds")
 
 
 
 
 
 
 
198
 
199
+ with tab2:
200
+ if st.session_state.context_sources:
201
+ for i, source in enumerate(st.session_state.context_sources):
202
+ name = f'{source}'
203
+ with st.expander(name):
204
+ st.markdown(f'{st.session_state.context_content[i]}')
205
+ else:
206
+ st.write("No sources available for this query.")
207
 
208
  st.session_state.messages.append({"role": "assistant", "content": full_answer})
209
  st.session_state.messages[-1]['sources'] = st.session_state.context_sources
210
  st.session_state.messages[-1]['context'] = st.session_state.context_content
211
+