Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import sys
|
| 2 |
import os
|
| 3 |
import re
|
|
@@ -7,7 +8,6 @@ import streamlit as st
|
|
| 7 |
import nltk
|
| 8 |
import tempfile
|
| 9 |
import subprocess
|
| 10 |
-
import base64 # For embedding PDF content
|
| 11 |
|
| 12 |
# Pin NLTK to version 3.9.1
|
| 13 |
REQUIRED_NLTK_VERSION = "3.9.1"
|
|
@@ -129,15 +129,6 @@ def download_pdf(patent_number):
|
|
| 129 |
st.error(f"Failed to download patent PDF: {e}")
|
| 130 |
st.stop()
|
| 131 |
|
| 132 |
-
def embed_pdf(file_path):
|
| 133 |
-
"""Convert PDF file to base64 and embed it in an iframe."""
|
| 134 |
-
with open(file_path, "rb") as f:
|
| 135 |
-
base64_pdf = base64.b64encode(f.read()).decode("utf-8")
|
| 136 |
-
pdf_display = f"""
|
| 137 |
-
<iframe src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" style="border: none;"></iframe>
|
| 138 |
-
"""
|
| 139 |
-
return pdf_display
|
| 140 |
-
|
| 141 |
if __name__ == "__main__":
|
| 142 |
st.set_page_config(
|
| 143 |
page_title="Patent Chat: Google Patents Chat Demo",
|
|
@@ -167,7 +158,6 @@ if __name__ == "__main__":
|
|
| 167 |
|
| 168 |
st.write(f"Patent number: **{patent_number}**")
|
| 169 |
|
| 170 |
-
# Define PDF path in temp directory
|
| 171 |
pdf_path = os.path.join(tempfile.gettempdir(), f"{patent_number}.pdf")
|
| 172 |
if os.path.isfile(pdf_path):
|
| 173 |
st.write("✅ File already downloaded.")
|
|
@@ -176,21 +166,6 @@ if __name__ == "__main__":
|
|
| 176 |
pdf_path = download_pdf(patent_number)
|
| 177 |
st.write(f"✅ File downloaded: {pdf_path}")
|
| 178 |
|
| 179 |
-
# Display a preview of the downloaded PDF
|
| 180 |
-
st.write("📄 Preview of the downloaded patent PDF:")
|
| 181 |
-
if os.path.isfile(pdf_path):
|
| 182 |
-
with open(pdf_path, "rb") as pdf_file:
|
| 183 |
-
st.download_button(
|
| 184 |
-
label="Download PDF",
|
| 185 |
-
data=pdf_file,
|
| 186 |
-
file_name=f"{patent_number}.pdf",
|
| 187 |
-
mime="application/pdf"
|
| 188 |
-
)
|
| 189 |
-
# Embed PDF content using base64
|
| 190 |
-
st.write("📋 PDF Content:")
|
| 191 |
-
pdf_view = embed_pdf(pdf_path)
|
| 192 |
-
st.components.v1.html(pdf_view, height=1000)
|
| 193 |
-
|
| 194 |
st.write("🔄 Loading document into the system...")
|
| 195 |
|
| 196 |
# Persist the chain in session state to prevent reloading
|
|
@@ -210,15 +185,12 @@ if __name__ == "__main__":
|
|
| 210 |
with st.chat_message(message["role"]):
|
| 211 |
st.markdown(message["content"])
|
| 212 |
|
| 213 |
-
# Chat Input Section
|
| 214 |
if "chain" in st.session_state:
|
| 215 |
if user_input := st.chat_input("What is your question?"):
|
| 216 |
-
# Append user message
|
| 217 |
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 218 |
with st.chat_message("user"):
|
| 219 |
st.markdown(user_input)
|
| 220 |
|
| 221 |
-
# Generate assistant response
|
| 222 |
with st.chat_message("assistant"):
|
| 223 |
message_placeholder = st.empty()
|
| 224 |
full_response = ""
|
|
@@ -230,7 +202,6 @@ if __name__ == "__main__":
|
|
| 230 |
except Exception as e:
|
| 231 |
full_response = f"An error occurred: {e}"
|
| 232 |
|
| 233 |
-
# Display assistant response
|
| 234 |
message_placeholder.markdown(full_response)
|
| 235 |
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 236 |
else:
|
|
|
|
| 1 |
+
# to-do: Enable downloading multiple patent PDFs via corresponding links
|
| 2 |
import sys
|
| 3 |
import os
|
| 4 |
import re
|
|
|
|
| 8 |
import nltk
|
| 9 |
import tempfile
|
| 10 |
import subprocess
|
|
|
|
| 11 |
|
| 12 |
# Pin NLTK to version 3.9.1
|
| 13 |
REQUIRED_NLTK_VERSION = "3.9.1"
|
|
|
|
| 129 |
st.error(f"Failed to download patent PDF: {e}")
|
| 130 |
st.stop()
|
| 131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
if __name__ == "__main__":
|
| 133 |
st.set_page_config(
|
| 134 |
page_title="Patent Chat: Google Patents Chat Demo",
|
|
|
|
| 158 |
|
| 159 |
st.write(f"Patent number: **{patent_number}**")
|
| 160 |
|
|
|
|
| 161 |
pdf_path = os.path.join(tempfile.gettempdir(), f"{patent_number}.pdf")
|
| 162 |
if os.path.isfile(pdf_path):
|
| 163 |
st.write("✅ File already downloaded.")
|
|
|
|
| 166 |
pdf_path = download_pdf(patent_number)
|
| 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
|
|
|
|
| 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"):
|
| 192 |
st.markdown(user_input)
|
| 193 |
|
|
|
|
| 194 |
with st.chat_message("assistant"):
|
| 195 |
message_placeholder = st.empty()
|
| 196 |
full_response = ""
|
|
|
|
| 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:
|