DrishtiSharma commited on
Commit
c56150f
Β·
verified Β·
1 Parent(s): 8a05c24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -55
app.py CHANGED
@@ -128,59 +128,64 @@ if __name__ == "__main__":
128
  )
129
  st.header("πŸ“– Patent Chat: Google Patents Chat Demo")
130
 
131
- patent_link = st.text_input("Enter Google Patent Link:", "https://patents.google.com/patent/US8676427B1/en")
132
-
133
- if not patent_link:
134
- st.warning("Please enter a Google patent link to proceed.")
135
- st.stop()
136
-
137
- patent_number = extract_patent_number(patent_link)
138
- if not patent_number:
139
- st.error("Invalid patent link format. Please provide a valid Google patent link.")
140
- st.stop()
141
-
142
- st.write(f"Patent number: **{patent_number}**")
143
-
144
- pdf_path = os.path.join(tempfile.gettempdir(), f"{patent_number}.pdf")
145
- if os.path.isfile(pdf_path):
146
- st.write("βœ… File already downloaded.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  else:
148
- st.write("πŸ“₯ Downloading patent file...")
149
- pdf_path = download_pdf(patent_number)
150
- st.write(f"βœ… File downloaded: {pdf_path}")
151
-
152
- st.write("πŸ”„ Loading document into the system...")
153
- chain = load_chain(pdf_path)
154
- st.success("πŸš€ Document successfully loaded! You can now start asking questions.")
155
-
156
- if "messages" not in st.session_state:
157
- st.session_state["messages"] = [
158
- {"role": "assistant", "content": "Hello! How can I assist you with this patent?"}
159
- ]
160
-
161
- for message in st.session_state.messages:
162
- with st.chat_message(message["role"]):
163
- st.markdown(message["content"])
164
-
165
- if user_input := st.chat_input("What is your question?"):
166
- st.session_state.messages.append({"role": "user", "content": user_input})
167
- with st.chat_message("user"):
168
- st.markdown(user_input)
169
-
170
- with st.chat_message("assistant"):
171
- message_placeholder = st.empty()
172
- full_response = ""
173
-
174
- with st.spinner("Generating response..."):
175
- try:
176
- assistant_response = chain({"question": user_input})
177
- for chunk in assistant_response["answer"].split():
178
- full_response += chunk + " "
179
- time.sleep(0.05)
180
- message_placeholder.markdown(full_response + "β–Œ")
181
- except Exception as e:
182
- full_response = f"An error occurred: {e}"
183
- finally:
184
- message_placeholder.markdown(full_response)
185
-
186
- st.session_state.messages.append({"role": "assistant", "content": full_response})
 
128
  )
129
  st.header("πŸ“– Patent Chat: Google Patents Chat Demo")
130
 
131
+ # Input for Google Patent Link
132
+ query_params = st.experimental_get_query_params()
133
+ default_patent_link = query_params.get("patent_link", ["https://patents.google.com/patent/US8676427B1/en"])[0]
134
+ patent_link = st.text_area("Enter Google Patent Link:", value=default_patent_link, height=100)
135
+
136
+ # Button to start processing
137
+ if st.button("Load and Process Patent"):
138
+ if not patent_link:
139
+ st.warning("Please enter a Google patent link to proceed.")
140
+ st.stop()
141
+
142
+ patent_number = extract_patent_number(patent_link)
143
+ if not patent_number:
144
+ st.error("Invalid patent link format. Please provide a valid Google patent link.")
145
+ st.stop()
146
+
147
+ st.write(f"Patent number: **{patent_number}**")
148
+
149
+ pdf_path = os.path.join(tempfile.gettempdir(), f"{patent_number}.pdf")
150
+ if os.path.isfile(pdf_path):
151
+ st.write("βœ… File already downloaded.")
152
+ else:
153
+ st.write("πŸ“₯ Downloading patent file...")
154
+ pdf_path = download_pdf(patent_number)
155
+ st.write(f"βœ… File downloaded: {pdf_path}")
156
+
157
+ st.write("πŸ”„ Loading document into the system...")
158
+ chain = load_chain(pdf_path)
159
+ st.success("πŸš€ Document successfully loaded! You can now start asking questions.")
160
+
161
+ # Initialize chat messages
162
+ if "messages" not in st.session_state:
163
+ st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you with this patent?"}]
164
+
165
+ # Display previous chat messages
166
+ for message in st.session_state.messages:
167
+ with st.chat_message(message["role"]):
168
+ st.markdown(message["content"])
169
+
170
+ # Chat Input
171
+ if user_input := st.chat_input("What is your question?"):
172
+ st.session_state.messages.append({"role": "user", "content": user_input})
173
+ with st.chat_message("user"):
174
+ st.markdown(user_input)
175
+
176
+ with st.chat_message("assistant"):
177
+ message_placeholder = st.empty()
178
+ full_response = ""
179
+
180
+ with st.spinner("Generating response..."):
181
+ try:
182
+ assistant_response = chain({"question": user_input})
183
+ full_response = assistant_response["answer"]
184
+ message_placeholder.markdown(full_response)
185
+ except Exception as e:
186
+ full_response = f"An error occurred: {e}"
187
+ message_placeholder.markdown(full_response)
188
+
189
+ st.session_state.messages.append({"role": "assistant", "content": full_response})
190
  else:
191
+ st.info("Press the 'Load and Process Patent' button to start processing.")