Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	
		adibak
		
	commited on
		
		
					Commit 
							
							·
						
						8880077
	
1
								Parent(s):
							
							a075c1d
								
make expander and slider work for upload
Browse files
    	
        app.py
    CHANGED
    
    | @@ -13,8 +13,10 @@ import httpx | |
| 13 | 
             
            import huggingface_hub
         | 
| 14 | 
             
            import json5
         | 
| 15 | 
             
            import ollama
         | 
|  | |
| 16 | 
             
            import requests
         | 
| 17 | 
             
            import streamlit as st
         | 
|  | |
| 18 | 
             
            from dotenv import load_dotenv
         | 
| 19 | 
             
            from langchain_community.chat_message_histories import StreamlitChatMessageHistory
         | 
| 20 | 
             
            from langchain_core.messages import HumanMessage
         | 
| @@ -29,6 +31,7 @@ load_dotenv() | |
| 29 |  | 
| 30 | 
             
            RUN_IN_OFFLINE_MODE = os.getenv('RUN_IN_OFFLINE_MODE', 'False').lower() == 'true'
         | 
| 31 |  | 
|  | |
| 32 |  | 
| 33 | 
             
            @st.cache_data
         | 
| 34 | 
             
            def _load_strings() -> dict:
         | 
| @@ -141,7 +144,7 @@ CHAT_MESSAGES = 'chat_messages' | |
| 141 | 
             
            DOWNLOAD_FILE_KEY = 'download_file_name'
         | 
| 142 | 
             
            IS_IT_REFINEMENT = 'is_it_refinement'
         | 
| 143 | 
             
            ADDITIONAL_INFO = 'additional_info'
         | 
| 144 | 
            -
             | 
| 145 |  | 
| 146 | 
             
            logger = logging.getLogger(__name__)
         | 
| 147 |  | 
| @@ -221,45 +224,6 @@ with st.sidebar: | |
| 221 | 
             
                            ),
         | 
| 222 | 
             
                            value='2024-05-01-preview',
         | 
| 223 | 
             
                        )
         | 
| 224 | 
            -
                    
         | 
| 225 | 
            -
                    # -- file upload and slider --
         | 
| 226 | 
            -
                    from pypdf import PdfReader
         | 
| 227 | 
            -
                    uploaded_pdf = st.file_uploader("4: Upload a PDF file", type=["pdf"])
         | 
| 228 | 
            -
             | 
| 229 | 
            -
                    # Check if user cleared the file input
         | 
| 230 | 
            -
                    if uploaded_pdf is None and "current_pdf_hash" in st.session_state:
         | 
| 231 | 
            -
                        st.session_state.pop("current_pdf_hash", None)
         | 
| 232 | 
            -
                        st.session_state.pop("pdf_page_count", None)
         | 
| 233 | 
            -
                        st.session_state.pop("page_range", None)
         | 
| 234 | 
            -
             | 
| 235 | 
            -
                    # Handle file upload and range tracking
         | 
| 236 | 
            -
                    if uploaded_pdf:
         | 
| 237 | 
            -
                        new_file_hash = hash(uploaded_pdf.getvalue())
         | 
| 238 | 
            -
             | 
| 239 | 
            -
                        if st.session_state.get("current_pdf_hash") != new_file_hash:  # if new file
         | 
| 240 | 
            -
                            reader = PdfReader(uploaded_pdf)
         | 
| 241 | 
            -
                            total_pages = len(reader.pages)
         | 
| 242 | 
            -
             | 
| 243 | 
            -
                            # update states
         | 
| 244 | 
            -
                            st.session_state["pdf_page_count"] = total_pages
         | 
| 245 | 
            -
                            st.session_state["current_pdf_hash"] = new_file_hash
         | 
| 246 | 
            -
                            st.session_state.pop("page_range", None)
         | 
| 247 | 
            -
             | 
| 248 | 
            -
                    page_count = st.session_state.get("pdf_page_count", 50)
         | 
| 249 | 
            -
                    max_slider = min(50, page_count)                            # enforce 50 page limmit
         | 
| 250 | 
            -
             | 
| 251 | 
            -
                    if "pdf_page_count" in st.session_state:
         | 
| 252 | 
            -
                        # make the range slider
         | 
| 253 | 
            -
                        page_range_slider = st.slider(
         | 
| 254 | 
            -
                            label="5: Specify a page range to examine:",
         | 
| 255 | 
            -
                            min_value=1,
         | 
| 256 | 
            -
                            max_value=max_slider,
         | 
| 257 | 
            -
                            value=(1, max_slider),
         | 
| 258 | 
            -
                            key="page_range"
         | 
| 259 | 
            -
                        )
         | 
| 260 | 
            -
                    else:
         | 
| 261 | 
            -
                        st.info("📄 Upload a PDF to specify a page range.")
         | 
| 262 | 
            -
             | 
| 263 |  | 
| 264 | 
             
            def build_ui():
         | 
| 265 | 
             
                """
         | 
| @@ -312,15 +276,67 @@ def set_up_chat_ui(): | |
| 312 | 
             
                for msg in history.messages:
         | 
| 313 | 
             
                    st.chat_message(msg.type).code(msg.content, language='json')
         | 
| 314 |  | 
| 315 | 
            -
                 | 
| 316 | 
            -
             | 
| 317 | 
            -
                     | 
| 318 | 
            -
                     | 
| 319 | 
            -
             | 
| 320 | 
            -
             | 
| 321 | 
            -
                     | 
| 322 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 323 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
| 324 | 
             
                    # if the user uploaded a pdf and specified a range, get the contents
         | 
| 325 | 
             
                    if uploaded_pdf and "page_range" in st.session_state:
         | 
| 326 | 
             
                        st.session_state[ADDITIONAL_INFO] = filem.get_pdf_contents(
         | 
| @@ -614,7 +630,6 @@ def _display_download_button(file_path: pathlib.Path): | |
| 614 |  | 
| 615 | 
             
                :param file_path: The path of the .pptx file.
         | 
| 616 | 
             
                """
         | 
| 617 | 
            -
             | 
| 618 | 
             
                with open(file_path, 'rb') as download_file:
         | 
| 619 | 
             
                    st.download_button(
         | 
| 620 | 
             
                        'Download PPTX file ⬇️',
         | 
| @@ -633,5 +648,4 @@ def main(): | |
| 633 |  | 
| 634 |  | 
| 635 | 
             
            if __name__ == '__main__':
         | 
| 636 | 
            -
                main()
         | 
| 637 | 
            -
             | 
|  | |
| 13 | 
             
            import huggingface_hub
         | 
| 14 | 
             
            import json5
         | 
| 15 | 
             
            import ollama
         | 
| 16 | 
            +
            from pypdf import PdfReader
         | 
| 17 | 
             
            import requests
         | 
| 18 | 
             
            import streamlit as st
         | 
| 19 | 
            +
            from streamlit_float import * # for floating UI elements
         | 
| 20 | 
             
            from dotenv import load_dotenv
         | 
| 21 | 
             
            from langchain_community.chat_message_histories import StreamlitChatMessageHistory
         | 
| 22 | 
             
            from langchain_core.messages import HumanMessage
         | 
|  | |
| 31 |  | 
| 32 | 
             
            RUN_IN_OFFLINE_MODE = os.getenv('RUN_IN_OFFLINE_MODE', 'False').lower() == 'true'
         | 
| 33 |  | 
| 34 | 
            +
            float_init()  # Initialize streamlit_float
         | 
| 35 |  | 
| 36 | 
             
            @st.cache_data
         | 
| 37 | 
             
            def _load_strings() -> dict:
         | 
|  | |
| 144 | 
             
            DOWNLOAD_FILE_KEY = 'download_file_name'
         | 
| 145 | 
             
            IS_IT_REFINEMENT = 'is_it_refinement'
         | 
| 146 | 
             
            ADDITIONAL_INFO = 'additional_info'
         | 
| 147 | 
            +
            UPLOAD_CONTAINER_OPEN = 'upload_container_open'
         | 
| 148 |  | 
| 149 | 
             
            logger = logging.getLogger(__name__)
         | 
| 150 |  | 
|  | |
| 224 | 
             
                            ),
         | 
| 225 | 
             
                            value='2024-05-01-preview',
         | 
| 226 | 
             
                        )
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 227 |  | 
| 228 | 
             
            def build_ui():
         | 
| 229 | 
             
                """
         | 
|  | |
| 276 | 
             
                for msg in history.messages:
         | 
| 277 | 
             
                    st.chat_message(msg.type).code(msg.content, language='json')
         | 
| 278 |  | 
| 279 | 
            +
                prompt_container = st.container()
         | 
| 280 | 
            +
                with prompt_container:
         | 
| 281 | 
            +
                    # Chat input below the uploader
         | 
| 282 | 
            +
                    prompt = st.chat_input(
         | 
| 283 | 
            +
                        placeholder=APP_TEXT['chat_placeholder'],
         | 
| 284 | 
            +
                        max_chars=GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH,
         | 
| 285 | 
            +
                    )
         | 
| 286 | 
            +
                prompt_container.float("bottom:20px;z-index:999;font-size:10pt;")
         | 
| 287 | 
            +
             | 
| 288 | 
            +
                # Use a container for the file uploader and slider
         | 
| 289 | 
            +
                if UPLOAD_CONTAINER_OPEN not in st.session_state:
         | 
| 290 | 
            +
                    st.session_state[UPLOAD_CONTAINER_OPEN] = True
         | 
| 291 | 
            +
             | 
| 292 | 
            +
                # Use a container with fixed height for the file uploader
         | 
| 293 | 
            +
                # This will create a container that acts as a sticky element
         | 
| 294 | 
            +
                
         | 
| 295 | 
            +
                # Custom CSS 
         | 
| 296 | 
            +
                st.markdown(
         | 
| 297 | 
            +
                    '''
         | 
| 298 | 
            +
                    <style>
         | 
| 299 | 
            +
                        div[data-testid="stFileUploaderDropzoneInstructions"]{
         | 
| 300 | 
            +
                            height:30px;
         | 
| 301 | 
            +
                        }
         | 
| 302 | 
            +
                    </style>
         | 
| 303 | 
            +
                    ''',
         | 
| 304 | 
            +
                    unsafe_allow_html=True
         | 
| 305 | 
            +
                )
         | 
| 306 | 
            +
                upload_container = st.container()
         | 
| 307 | 
            +
                with upload_container:
         | 
| 308 | 
            +
                    expander = st.expander("📄 Upload PDF", expanded=st.session_state[UPLOAD_CONTAINER_OPEN])
         | 
| 309 | 
            +
                    with expander:
         | 
| 310 | 
            +
                        # File upload widget spans full width but with reduced height
         | 
| 311 | 
            +
                        uploaded_pdf = st.file_uploader(
         | 
| 312 | 
            +
                            "",
         | 
| 313 | 
            +
                            type=["pdf"],
         | 
| 314 | 
            +
                            label_visibility="visible",
         | 
| 315 | 
            +
                        )
         | 
| 316 | 
            +
             | 
| 317 | 
            +
                    # -- PDF Processing and Slider Logic --
         | 
| 318 | 
            +
                    if uploaded_pdf:
         | 
| 319 | 
            +
                        reader = PdfReader(uploaded_pdf)
         | 
| 320 | 
            +
                        total_pages = len(reader.pages)
         | 
| 321 | 
            +
                        st.session_state["pdf_page_count"] = total_pages
         | 
| 322 | 
            +
             | 
| 323 | 
            +
                        # Slider for page range
         | 
| 324 | 
            +
                        max_slider = min(50, total_pages)  # enforce 50 page limit
         | 
| 325 | 
            +
             | 
| 326 | 
            +
                        with st.sidebar:
         | 
| 327 | 
            +
                            st.slider(
         | 
| 328 | 
            +
                                label="5: Specify a page range to examine:",
         | 
| 329 | 
            +
                                min_value=1,
         | 
| 330 | 
            +
                                max_value=max_slider,
         | 
| 331 | 
            +
                                value=(1, max_slider),
         | 
| 332 | 
            +
                                key="page_range"
         | 
| 333 | 
            +
                            )
         | 
| 334 |  | 
| 335 | 
            +
                upload_container.float("bottom:70px;width:40%;font-size:10pt;right:7.5%;")  # position above chat input
         | 
| 336 | 
            +
             | 
| 337 | 
            +
                if prompt:
         | 
| 338 | 
            +
                    prompt_text = prompt
         | 
| 339 | 
            +
             | 
| 340 | 
             
                    # if the user uploaded a pdf and specified a range, get the contents
         | 
| 341 | 
             
                    if uploaded_pdf and "page_range" in st.session_state:
         | 
| 342 | 
             
                        st.session_state[ADDITIONAL_INFO] = filem.get_pdf_contents(
         | 
|  | |
| 630 |  | 
| 631 | 
             
                :param file_path: The path of the .pptx file.
         | 
| 632 | 
             
                """
         | 
|  | |
| 633 | 
             
                with open(file_path, 'rb') as download_file:
         | 
| 634 | 
             
                    st.download_button(
         | 
| 635 | 
             
                        'Download PPTX file ⬇️',
         | 
|  | |
| 648 |  | 
| 649 |  | 
| 650 | 
             
            if __name__ == '__main__':
         | 
| 651 | 
            +
                main()
         | 
|  |