DrishtiSharma commited on
Commit
3fac447
Β·
verified Β·
1 Parent(s): a4abe5f

Update interim/app.py

Browse files
Files changed (1) hide show
  1. interim/app.py +23 -18
interim/app.py CHANGED
@@ -167,19 +167,25 @@ if __name__ == "__main__":
167
  st.write(f"βœ… File downloaded: {pdf_path}")
168
 
169
  st.write("πŸ”„ Loading document into the system...")
170
- chain = load_chain(pdf_path)
 
 
 
 
 
 
171
  st.success("πŸš€ Document successfully loaded! You can now start asking questions.")
172
 
173
- # Initialize chat messages
174
- if "messages" not in st.session_state:
175
- st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you with this patent?"}]
176
 
177
- # Display previous chat messages
178
- for message in st.session_state.messages:
179
- with st.chat_message(message["role"]):
180
- st.markdown(message["content"])
181
 
182
- # Chat Input
183
  if user_input := st.chat_input("What is your question?"):
184
  st.session_state.messages.append({"role": "user", "content": user_input})
185
  with st.chat_message("user"):
@@ -189,15 +195,14 @@ if __name__ == "__main__":
189
  message_placeholder = st.empty()
190
  full_response = ""
191
 
192
- with st.spinner("Generating response..."):
193
- try:
194
- assistant_response = chain({"question": user_input})
195
- full_response = assistant_response["answer"]
196
- message_placeholder.markdown(full_response)
197
- except Exception as e:
198
- full_response = f"An error occurred: {e}"
199
- message_placeholder.markdown(full_response)
200
 
201
- st.session_state.messages.append({"role": "assistant", "content": full_response})
 
202
  else:
203
  st.info("Press the 'Load and Process Patent' button to start processing.")
 
167
  st.write(f"βœ… File downloaded: {pdf_path}")
168
 
169
  st.write("πŸ”„ Loading document into the system...")
170
+
171
+ # Persist the chain in session state to prevent reloading
172
+ if "chain" not in st.session_state or st.session_state.get("loaded_file") != pdf_path:
173
+ st.session_state.chain = load_chain(pdf_path)
174
+ st.session_state.loaded_file = pdf_path
175
+ st.session_state.messages = [{"role": "assistant", "content": "Hello! How can I assist you with this patent?"}]
176
+
177
  st.success("πŸš€ Document successfully loaded! You can now start asking questions.")
178
 
179
+ # Initialize messages if not already done
180
+ if "messages" not in st.session_state:
181
+ st.session_state.messages = [{"role": "assistant", "content": "Hello! How can I assist you with this patent?"}]
182
 
183
+ # Display previous chat messages
184
+ for message in st.session_state.messages:
185
+ with st.chat_message(message["role"]):
186
+ st.markdown(message["content"])
187
 
188
+ if "chain" in st.session_state:
189
  if user_input := st.chat_input("What is your question?"):
190
  st.session_state.messages.append({"role": "user", "content": user_input})
191
  with st.chat_message("user"):
 
195
  message_placeholder = st.empty()
196
  full_response = ""
197
 
198
+ with st.spinner("Generating response..."):
199
+ try:
200
+ assistant_response = st.session_state.chain({"question": user_input})
201
+ full_response = assistant_response["answer"]
202
+ except Exception as e:
203
+ full_response = f"An error occurred: {e}"
 
 
204
 
205
+ message_placeholder.markdown(full_response)
206
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
207
  else:
208
  st.info("Press the 'Load and Process Patent' button to start processing.")