Spaces:
Running
Running
adibak
commited on
Commit
·
90c3309
1
Parent(s):
10c7343
comments
Browse files- app.py +4 -3
- helpers/file_manager.py +13 -3
app.py
CHANGED
|
@@ -291,8 +291,9 @@ def set_up_chat_ui():
|
|
| 291 |
):
|
| 292 |
prompt_text = prompt.text or ''
|
| 293 |
if prompt['files']:
|
|
|
|
| 294 |
uploaded_pdf = prompt['files'][0]
|
| 295 |
-
st.session_state["pdf_file"] = uploaded_pdf
|
| 296 |
# Apparently, Streamlit stores uploaded files in memory and clears on browser close
|
| 297 |
# https://docs.streamlit.io/knowledge-base/using-streamlit/where-file-uploader-store-when-deleted
|
| 298 |
|
|
@@ -300,7 +301,7 @@ def set_up_chat_ui():
|
|
| 300 |
st.session_state["start_page"], st.session_state["end_page"] = filem.validate_page_range(uploaded_pdf,
|
| 301 |
st.session_state["start_page"],
|
| 302 |
st.session_state["end_page"])
|
| 303 |
-
#
|
| 304 |
with st.sidebar:
|
| 305 |
st.text(f"Extracting pages {st.session_state["start_page"]} to {st.session_state["end_page"]} in {uploaded_pdf.name}")
|
| 306 |
|
|
@@ -315,7 +316,7 @@ def set_up_chat_ui():
|
|
| 315 |
st.session_state["start_page"], st.session_state["end_page"] = filem.validate_page_range(st.session_state["pdf_file"],
|
| 316 |
st.session_state["start_page"],
|
| 317 |
st.session_state["end_page"])
|
| 318 |
-
# update sidebar text
|
| 319 |
with st.sidebar:
|
| 320 |
st.text(f"Extracting pages {st.session_state["start_page"]} to {st.session_state["end_page"]} in {st.session_state["pdf_file"].name}")
|
| 321 |
|
|
|
|
| 291 |
):
|
| 292 |
prompt_text = prompt.text or ''
|
| 293 |
if prompt['files']:
|
| 294 |
+
# store uploaded pdf in session state
|
| 295 |
uploaded_pdf = prompt['files'][0]
|
| 296 |
+
st.session_state["pdf_file"] = uploaded_pdf
|
| 297 |
# Apparently, Streamlit stores uploaded files in memory and clears on browser close
|
| 298 |
# https://docs.streamlit.io/knowledge-base/using-streamlit/where-file-uploader-store-when-deleted
|
| 299 |
|
|
|
|
| 301 |
st.session_state["start_page"], st.session_state["end_page"] = filem.validate_page_range(uploaded_pdf,
|
| 302 |
st.session_state["start_page"],
|
| 303 |
st.session_state["end_page"])
|
| 304 |
+
# show sidebar text for page selection and file name
|
| 305 |
with st.sidebar:
|
| 306 |
st.text(f"Extracting pages {st.session_state["start_page"]} to {st.session_state["end_page"]} in {uploaded_pdf.name}")
|
| 307 |
|
|
|
|
| 316 |
st.session_state["start_page"], st.session_state["end_page"] = filem.validate_page_range(st.session_state["pdf_file"],
|
| 317 |
st.session_state["start_page"],
|
| 318 |
st.session_state["end_page"])
|
| 319 |
+
# update sidebar text for name and page selection
|
| 320 |
with st.sidebar:
|
| 321 |
st.text(f"Extracting pages {st.session_state["start_page"]} to {st.session_state["end_page"]} in {st.session_state["pdf_file"].name}")
|
| 322 |
|
helpers/file_manager.py
CHANGED
|
@@ -46,13 +46,23 @@ def get_pdf_contents(
|
|
| 46 |
|
| 47 |
def validate_page_range(pdf_file: st.runtime.uploaded_file_manager.UploadedFile,
|
| 48 |
start:int, end:int) -> tuple[int, int]:
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
n_pages = len(PdfReader(pdf_file).pages)
|
| 51 |
-
|
|
|
|
| 52 |
start = max(1, start)
|
|
|
|
|
|
|
| 53 |
end = min(n_pages, end)
|
| 54 |
|
| 55 |
-
if start >= end:
|
| 56 |
start = 1
|
| 57 |
|
| 58 |
return (start, end)
|
|
|
|
| 46 |
|
| 47 |
def validate_page_range(pdf_file: st.runtime.uploaded_file_manager.UploadedFile,
|
| 48 |
start:int, end:int) -> tuple[int, int]:
|
| 49 |
+
"""
|
| 50 |
+
Validate the page range.
|
| 51 |
+
|
| 52 |
+
:param pdf_file: The uploaded PDF file.
|
| 53 |
+
:param start: The start page
|
| 54 |
+
:param max_pages: The end page
|
| 55 |
+
:return: The validated page range tuple
|
| 56 |
+
"""
|
| 57 |
n_pages = len(PdfReader(pdf_file).pages)
|
| 58 |
+
|
| 59 |
+
# set start to max of 1 or specified start (whichever's higher)
|
| 60 |
start = max(1, start)
|
| 61 |
+
|
| 62 |
+
# set end to min of pdf length or specified end (whichever's lower)
|
| 63 |
end = min(n_pages, end)
|
| 64 |
|
| 65 |
+
if start >= end: # if the start is higher than the end, make it 1
|
| 66 |
start = 1
|
| 67 |
|
| 68 |
return (start, end)
|