DrishtiSharma commited on
Commit
cf5a94b
·
verified ·
1 Parent(s): d54fc43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -34
app.py CHANGED
@@ -152,73 +152,85 @@ if __name__ == "__main__":
152
  )
153
  st.header("📖 Patent Chat: Google Patents Chat Demo")
154
 
155
- # Fetch query parameters safely
156
- query_params = st.query_params
157
- default_patent_link = query_params.get("patent_link", "https://patents.google.com/patent/US8676427B1/en")
158
-
159
  # Input for Google Patent Link
160
- patent_link = st.text_area("Enter Google Patent Link:", value=default_patent_link, height=100)
 
 
 
 
161
 
162
- # Button to start processing
 
 
 
 
 
 
 
 
 
 
 
 
163
  if st.button("Load and Process Patent"):
164
  if not patent_link:
165
- st.warning("Please enter a Google patent link to proceed.")
166
  st.stop()
167
 
168
  # Extract patent number
169
  patent_number = extract_patent_number(patent_link)
170
  if not patent_number:
171
- st.error("Invalid patent link format. Please provide a valid Google patent link.")
172
  st.stop()
173
 
174
  st.write(f"Patent number: **{patent_number}**")
175
 
176
- # File download handling
177
  pdf_path = os.path.join(tempfile.gettempdir(), f"{patent_number}.pdf")
178
- if os.path.isfile(pdf_path):
179
- st.write("✅ File already downloaded.")
180
- else:
181
  st.write("📥 Downloading patent file...")
182
  pdf_path = download_pdf(patent_number)
183
  st.write(f"✅ File downloaded: {pdf_path}")
184
-
185
- # Generate and display PDF preview
186
- st.write("🖼️ Generating PDF preview...")
187
- preview_image_path = preview_pdf(pdf_path)
188
-
189
- if preview_image_path:
190
- st.image(preview_image_path, caption="First Page Preview", use_column_width=True)
191
  else:
192
- st.warning("Failed to generate a preview for this PDF.")
193
-
194
- # Load the document into the system
195
- st.write("🔄 Loading document into the system...")
196
 
197
- # Check if the patent is already loaded in session state
198
- if "LOADED_PATENT" not in st.session_state or st.session_state.get("LOADED_PATENT") != patent_number:
 
 
 
 
 
 
 
 
 
199
  st.session_state.chain = load_chain(pdf_path)
200
- st.session_state.LOADED_PATENT = patent_number # Store the current patent number
 
201
  st.session_state.messages = [{"role": "assistant", "content": "Hello! How can I assist you with this patent?"}]
202
- st.success("🚀 Document successfully loaded! You can now start asking questions.")
203
  else:
204
- st.success("✅ This patent is already loaded. You can continue asking questions.")
205
 
206
- # Initialize messages if not already done
207
- if "messages" not in st.session_state:
208
- st.session_state.messages = [{"role": "assistant", "content": "Hello! How can I assist you with this patent?"}]
209
 
210
  # Display previous chat messages
211
  for message in st.session_state.messages:
212
  with st.chat_message(message["role"]):
213
  st.markdown(message["content"])
214
 
215
- # User input and chatbot response
216
- if "chain" in st.session_state:
217
  if user_input := st.chat_input("What is your question?"):
 
218
  st.session_state.messages.append({"role": "user", "content": user_input})
219
  with st.chat_message("user"):
220
  st.markdown(user_input)
221
 
 
222
  with st.chat_message("assistant"):
223
  message_placeholder = st.empty()
224
  full_response = ""
@@ -226,7 +238,7 @@ if __name__ == "__main__":
226
  with st.spinner("Generating response..."):
227
  try:
228
  assistant_response = st.session_state.chain({"question": user_input})
229
- full_response = assistant_response["answer"]
230
  except Exception as e:
231
  full_response = f"An error occurred: {e}"
232
 
 
152
  )
153
  st.header("📖 Patent Chat: Google Patents Chat Demo")
154
 
 
 
 
 
155
  # Input for Google Patent Link
156
+ patent_link = st.text_area(
157
+ "Enter Google Patent Link:",
158
+ value="https://patents.google.com/patent/US8676427B1/en",
159
+ height=100
160
+ )
161
 
162
+ # Initialize session state
163
+ if "LOADED_PATENT" not in st.session_state:
164
+ st.session_state.LOADED_PATENT = None
165
+ if "pdf_preview" not in st.session_state:
166
+ st.session_state.pdf_preview = None
167
+ if "loaded_pdf_path" not in st.session_state:
168
+ st.session_state.loaded_pdf_path = None
169
+ if "chain" not in st.session_state:
170
+ st.session_state.chain = None
171
+ if "messages" not in st.session_state:
172
+ st.session_state.messages = [{"role": "assistant", "content": "Hello! How can I assist you with this patent?"}]
173
+
174
+ # Load and process patent
175
  if st.button("Load and Process Patent"):
176
  if not patent_link:
177
+ st.warning("Please enter a valid Google patent link.")
178
  st.stop()
179
 
180
  # Extract patent number
181
  patent_number = extract_patent_number(patent_link)
182
  if not patent_number:
183
+ st.error("Invalid patent link format.")
184
  st.stop()
185
 
186
  st.write(f"Patent number: **{patent_number}**")
187
 
188
+ # File handling
189
  pdf_path = os.path.join(tempfile.gettempdir(), f"{patent_number}.pdf")
190
+ if not os.path.isfile(pdf_path):
 
 
191
  st.write("📥 Downloading patent file...")
192
  pdf_path = download_pdf(patent_number)
193
  st.write(f"✅ File downloaded: {pdf_path}")
 
 
 
 
 
 
 
194
  else:
195
+ st.write(" File already downloaded.")
 
 
 
196
 
197
+ # Generate preview if not already stored
198
+ if st.session_state.LOADED_PATENT != patent_number:
199
+ st.write("🖼️ Generating PDF preview...")
200
+ preview_image_path = preview_pdf(pdf_path)
201
+ if preview_image_path:
202
+ st.session_state.pdf_preview = preview_image_path
203
+ else:
204
+ st.warning("Failed to generate PDF preview.")
205
+ st.session_state.pdf_preview = None
206
+
207
+ # Load the chain and store metadata
208
  st.session_state.chain = load_chain(pdf_path)
209
+ st.session_state.LOADED_PATENT = patent_number
210
+ st.session_state.loaded_pdf_path = pdf_path
211
  st.session_state.messages = [{"role": "assistant", "content": "Hello! How can I assist you with this patent?"}]
212
+ st.success("🚀 Document successfully loaded!")
213
  else:
214
+ st.info("✅ This patent is already loaded.")
215
 
216
+ # Display the PDF preview if available
217
+ if st.session_state.pdf_preview:
218
+ st.image(st.session_state.pdf_preview, caption="First Page Preview", use_container_width=True)
219
 
220
  # Display previous chat messages
221
  for message in st.session_state.messages:
222
  with st.chat_message(message["role"]):
223
  st.markdown(message["content"])
224
 
225
+ # User input for questions
226
+ if st.session_state.chain:
227
  if user_input := st.chat_input("What is your question?"):
228
+ # User message
229
  st.session_state.messages.append({"role": "user", "content": user_input})
230
  with st.chat_message("user"):
231
  st.markdown(user_input)
232
 
233
+ # Assistant response
234
  with st.chat_message("assistant"):
235
  message_placeholder = st.empty()
236
  full_response = ""
 
238
  with st.spinner("Generating response..."):
239
  try:
240
  assistant_response = st.session_state.chain({"question": user_input})
241
+ full_response = assistant_response.get("answer", "I couldn't process that question.")
242
  except Exception as e:
243
  full_response = f"An error occurred: {e}"
244