Spaces:
Running
Running
Merge pull request #104 from AdiBak/main
Browse filesAdd dynamic page range slider for uploaded PDFs with validation
(Results in a run-time error for the offline mode; to be fixed separately)
- app.py +37 -3
- global_config.py +1 -0
- helpers/file_manager.py +40 -7
app.py
CHANGED
@@ -222,6 +222,11 @@ with st.sidebar:
|
|
222 |
value='2024-05-01-preview',
|
223 |
)
|
224 |
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
def build_ui():
|
227 |
"""
|
@@ -255,6 +260,9 @@ def set_up_chat_ui():
|
|
255 |
"""
|
256 |
Prepare the chat interface and related functionality.
|
257 |
"""
|
|
|
|
|
|
|
258 |
|
259 |
with st.expander('Usage Instructions'):
|
260 |
st.markdown(GlobalConfig.CHAT_USAGE_INSTRUCTIONS)
|
@@ -282,11 +290,38 @@ def set_up_chat_ui():
|
|
282 |
):
|
283 |
prompt_text = prompt.text or ''
|
284 |
if prompt['files']:
|
|
|
|
|
|
|
285 |
# Apparently, Streamlit stores uploaded files in memory and clears on browser close
|
286 |
# https://docs.streamlit.io/knowledge-base/using-streamlit/where-file-uploader-store-when-deleted
|
287 |
-
st.session_state[ADDITIONAL_INFO] = filem.get_pdf_contents(prompt['files'][0])
|
288 |
-
print(f'{prompt["files"]=}')
|
289 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
290 |
provider, llm_name = llm_helper.get_provider_model(
|
291 |
llm_provider_to_use,
|
292 |
use_ollama=RUN_IN_OFFLINE_MODE
|
@@ -593,4 +628,3 @@ def main():
|
|
593 |
|
594 |
if __name__ == '__main__':
|
595 |
main()
|
596 |
-
|
|
|
222 |
value='2024-05-01-preview',
|
223 |
)
|
224 |
|
225 |
+
# Make slider with initial values
|
226 |
+
page_range_slider = st.slider('7: Specify a page range for the PDF file:',
|
227 |
+
1, GlobalConfig.MAX_ALLOWED_PAGES, [1, GlobalConfig.MAX_ALLOWED_PAGES])
|
228 |
+
st.session_state['page_range_slider'] = page_range_slider
|
229 |
+
|
230 |
|
231 |
def build_ui():
|
232 |
"""
|
|
|
260 |
"""
|
261 |
Prepare the chat interface and related functionality.
|
262 |
"""
|
263 |
+
# Set start and end page
|
264 |
+
st.session_state['start_page'] = st.session_state['page_range_slider'][0]
|
265 |
+
st.session_state['end_page'] = st.session_state['page_range_slider'][1]
|
266 |
|
267 |
with st.expander('Usage Instructions'):
|
268 |
st.markdown(GlobalConfig.CHAT_USAGE_INSTRUCTIONS)
|
|
|
290 |
):
|
291 |
prompt_text = prompt.text or ''
|
292 |
if prompt['files']:
|
293 |
+
# Store uploaded pdf in session state
|
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 |
|
299 |
+
# Check if pdf file is uploaded
|
300 |
+
# (we can use the same file if the user doesn't upload a new one)
|
301 |
+
if 'pdf_file' in st.session_state:
|
302 |
+
# Get validated page range
|
303 |
+
st.session_state['start_page'], st.session_state['end_page'] = filem.validate_page_range(
|
304 |
+
st.session_state['pdf_file'],
|
305 |
+
st.session_state['start_page'],
|
306 |
+
st.session_state['end_page']
|
307 |
+
)
|
308 |
+
# Show sidebar text for page selection and file name
|
309 |
+
with st.sidebar:
|
310 |
+
if st.session_state['end_page'] is None: # If the PDF has only one page
|
311 |
+
st.text('Extracting page %d in %s' % (
|
312 |
+
st.session_state['start_page'], st.session_state['pdf_file'].name
|
313 |
+
))
|
314 |
+
else:
|
315 |
+
st.text('Extracting pages %d to %d in %s' % (
|
316 |
+
st.session_state['start_page'], st.session_state['end_page'], st.session_state['pdf_file'].name
|
317 |
+
))
|
318 |
+
|
319 |
+
# Get pdf contents
|
320 |
+
st.session_state[ADDITIONAL_INFO] = filem.get_pdf_contents(
|
321 |
+
st.session_state['pdf_file'],
|
322 |
+
(st.session_state['start_page'],
|
323 |
+
st.session_state['end_page'])
|
324 |
+
)
|
325 |
provider, llm_name = llm_helper.get_provider_model(
|
326 |
llm_provider_to_use,
|
327 |
use_ollama=RUN_IN_OFFLINE_MODE
|
|
|
628 |
|
629 |
if __name__ == '__main__':
|
630 |
main()
|
|
global_config.py
CHANGED
@@ -108,6 +108,7 @@ class GlobalConfig:
|
|
108 |
DEFAULT_MODEL_INDEX = int(os.environ.get('DEFAULT_MODEL_INDEX', '4'))
|
109 |
LLM_MODEL_TEMPERATURE = 0.2
|
110 |
MAX_PAGE_COUNT = 50
|
|
|
111 |
LLM_MODEL_MAX_INPUT_LENGTH = 1000 # characters
|
112 |
|
113 |
LOG_LEVEL = 'DEBUG'
|
|
|
108 |
DEFAULT_MODEL_INDEX = int(os.environ.get('DEFAULT_MODEL_INDEX', '4'))
|
109 |
LLM_MODEL_TEMPERATURE = 0.2
|
110 |
MAX_PAGE_COUNT = 50
|
111 |
+
MAX_ALLOWED_PAGES = 150
|
112 |
LLM_MODEL_MAX_INPUT_LENGTH = 1000 # characters
|
113 |
|
114 |
LOG_LEVEL = 'DEBUG'
|
helpers/file_manager.py
CHANGED
@@ -19,22 +19,55 @@ logger = logging.getLogger(__name__)
|
|
19 |
|
20 |
def get_pdf_contents(
|
21 |
pdf_file: st.runtime.uploaded_file_manager.UploadedFile,
|
22 |
-
|
23 |
-
) -> str:
|
24 |
"""
|
25 |
Extract the text contents from a PDF file.
|
26 |
|
27 |
:param pdf_file: The uploaded PDF file.
|
28 |
-
:param
|
29 |
:return: The contents.
|
30 |
"""
|
31 |
|
32 |
reader = PdfReader(pdf_file)
|
33 |
-
|
|
|
|
|
34 |
text = ''
|
35 |
|
36 |
-
|
37 |
-
page =
|
38 |
-
|
39 |
|
|
|
|
|
|
|
|
|
|
|
40 |
return text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
def get_pdf_contents(
|
21 |
pdf_file: st.runtime.uploaded_file_manager.UploadedFile,
|
22 |
+
page_range: tuple[int, int]) -> str:
|
|
|
23 |
"""
|
24 |
Extract the text contents from a PDF file.
|
25 |
|
26 |
:param pdf_file: The uploaded PDF file.
|
27 |
+
:param page_range: The range of pages to extract contents from.
|
28 |
:return: The contents.
|
29 |
"""
|
30 |
|
31 |
reader = PdfReader(pdf_file)
|
32 |
+
|
33 |
+
start, end = page_range # Set start and end per the range (user-specified values)
|
34 |
+
|
35 |
text = ''
|
36 |
|
37 |
+
if end is None:
|
38 |
+
# If end is None (where PDF has only 1 page or start = end), extract start
|
39 |
+
end = start
|
40 |
|
41 |
+
# Get the text from the specified page range
|
42 |
+
for page_num in range(start - 1, end):
|
43 |
+
text += reader.pages[page_num].extract_text()
|
44 |
+
|
45 |
+
|
46 |
return text
|
47 |
+
|
48 |
+
def validate_page_range(pdf_file: st.runtime.uploaded_file_manager.UploadedFile,
|
49 |
+
start:int, end:int) -> tuple[int, int]:
|
50 |
+
"""
|
51 |
+
Validate the page range.
|
52 |
+
|
53 |
+
:param pdf_file: The uploaded PDF file.
|
54 |
+
:param start: The start page
|
55 |
+
:param end: The end page
|
56 |
+
:return: The validated page range tuple
|
57 |
+
"""
|
58 |
+
n_pages = len(PdfReader(pdf_file).pages)
|
59 |
+
|
60 |
+
# Set start to max of 1 or specified start (whichever's higher)
|
61 |
+
start = max(1, start)
|
62 |
+
|
63 |
+
# Set end to min of pdf length or specified end (whichever's lower)
|
64 |
+
end = min(n_pages, end)
|
65 |
+
|
66 |
+
if start > end: # If the start is higher than the end, make it 1
|
67 |
+
start = 1
|
68 |
+
|
69 |
+
if start == end:
|
70 |
+
# If start = end (including when PDF is 1 page long), set end to None
|
71 |
+
return start, None
|
72 |
+
|
73 |
+
return start, end
|