Spaces:
Sleeping
Sleeping
DrishtiSharma
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import os
|
|
4 |
import re
|
5 |
import shutil
|
6 |
import time
|
|
|
7 |
import streamlit as st
|
8 |
import nltk
|
9 |
import tempfile
|
@@ -129,6 +130,19 @@ def download_pdf(patent_number):
|
|
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",
|
@@ -151,6 +165,7 @@ if __name__ == "__main__":
|
|
151 |
st.warning("Please enter a Google patent link to proceed.")
|
152 |
st.stop()
|
153 |
|
|
|
154 |
patent_number = extract_patent_number(patent_link)
|
155 |
if not patent_number:
|
156 |
st.error("Invalid patent link format. Please provide a valid Google patent link.")
|
@@ -158,6 +173,7 @@ if __name__ == "__main__":
|
|
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,6 +182,16 @@ if __name__ == "__main__":
|
|
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,6 +211,7 @@ if __name__ == "__main__":
|
|
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})
|
|
|
4 |
import re
|
5 |
import shutil
|
6 |
import time
|
7 |
+
import fitz
|
8 |
import streamlit as st
|
9 |
import nltk
|
10 |
import tempfile
|
|
|
130 |
st.error(f"Failed to download patent PDF: {e}")
|
131 |
st.stop()
|
132 |
|
133 |
+
def preview_pdf(pdf_path):
|
134 |
+
"""Generate and display the first page of the PDF as an image."""
|
135 |
+
try:
|
136 |
+
doc = fitz.open(pdf_path) # Open PDF
|
137 |
+
first_page = doc[0] # Extract the first page
|
138 |
+
pix = first_page.get_pixmap() # Render page to a Pixmap (image)
|
139 |
+
temp_image_path = os.path.join(tempfile.gettempdir(), "pdf_preview.png")
|
140 |
+
pix.save(temp_image_path) # Save the image temporarily
|
141 |
+
return temp_image_path
|
142 |
+
except Exception as e:
|
143 |
+
st.error(f"Error generating PDF preview: {e}")
|
144 |
+
return None
|
145 |
+
|
146 |
if __name__ == "__main__":
|
147 |
st.set_page_config(
|
148 |
page_title="Patent Chat: Google Patents Chat Demo",
|
|
|
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.")
|
|
|
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.")
|
|
|
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 |
# Persist the chain in session state to prevent reloading
|
|
|
211 |
with st.chat_message(message["role"]):
|
212 |
st.markdown(message["content"])
|
213 |
|
214 |
+
# User input and chatbot response
|
215 |
if "chain" in st.session_state:
|
216 |
if user_input := st.chat_input("What is your question?"):
|
217 |
st.session_state.messages.append({"role": "user", "content": user_input})
|