Update interim/app.py
Browse files- 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
st.success("π Document successfully loaded! You can now start asking questions.")
|
172 |
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
|
182 |
-
|
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 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
full_response = f"An error occurred: {e}"
|
199 |
-
message_placeholder.markdown(full_response)
|
200 |
|
201 |
-
|
|
|
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.")
|