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