import io import base64 from PIL import Image import streamlit as st from typing import Dict, Any from log_utils import setup_logging logger = setup_logging('pdf_details_page') def display_pdf_details(pdf_details, filename): """ Display detailed information about a specific PDF page. Parameters: - pdf_details: dict containing the PDF information - filename: name of the PDF file """ logger.info(f"Displaying PDF details for file: {filename}") # Initialize reader mode state if 'reader_mode' not in st.session_state: st.session_state.reader_mode = False logger.debug("Initialized reader mode state") def toggle_reader_mode(): """Toggle reader mode state with logging.""" previous_state = st.session_state.reader_mode st.session_state.reader_mode = not previous_state logger.info(f"Reader mode toggled from {previous_state} to {st.session_state.reader_mode}") try: # Enhanced CSS for better styling st.markdown(""" """, unsafe_allow_html=True) logger.debug("Applied CSS styling") # Reader mode display (if active) if st.session_state.reader_mode: logger.info("Displaying reader mode view") st.markdown('
', unsafe_allow_html=True) if st.button("❌ Close Reader Mode", key="close_reader", help="Exit reader mode"): logger.info("Reader mode closed") st.session_state.reader_mode = False st.rerun() # Display zoomed image page_image_bytes = base64.b64decode(pdf_details['page_image']) page_image = Image.open(io.BytesIO(page_image_bytes)) st.image(page_image, use_container_width=True, caption=f"Page {pdf_details['current_page']}") st.markdown('
', unsafe_allow_html=True) return logger.info("Displaying regular interface") # Header st.markdown('

📚 Smart PDF Search

', unsafe_allow_html=True) # Main content col1, col2 = st.columns([1.5, 2]) with col1: logger.debug("Rendering details section") st.markdown("

🖼️ Page Preview

", unsafe_allow_html=True) current_page = pdf_details['current_page'] st.markdown(f"
Page {current_page} of {pdf_details['total_pages']}
", unsafe_allow_html=True) page_image_bytes = base64.b64decode(pdf_details['page_image']) page_image = Image.open(io.BytesIO(page_image_bytes)) st.image(page_image, caption=f"Page {current_page}", use_container_width=True) with col2: st.markdown("
", unsafe_allow_html=True) # Create 3 equal-width columns col1, col2, col3 = st.columns(3) # Action buttons inside the columns with col1: logger.info("Reader mode button clicked") st.button("📖 Reader Mode", on_click=toggle_reader_mode) with col2: if st.button("🔍 Ask a Question"): logger.info("Ask a Question button clicked") st.query_params["page"] = "home" st.rerun() with col3: logger.debug("Rendering Romanized Text link") st.markdown(f""" 📄 Romanized Text """, unsafe_allow_html=True) # Page content in expander with st.expander("📄 Page Content", expanded=True): logger.debug("Displaying page content in expander") st.markdown(pdf_details['page_text'], unsafe_allow_html=True) # Metadata table metadata_html = f"""
PDF Name{pdf_details['metadata'].get('title', filename)}
Page{pdf_details['current_page']}
Author{pdf_details['metadata'].get('author', 'N/A')}
Total Pages{pdf_details['total_pages']}
Language{pdf_details['language']}
File Size{pdf_details['file_size_kb']}
""" st.markdown(metadata_html, unsafe_allow_html=True) logger.info(f"Completed rendering PDF details page for {filename}") except Exception as e: logger.error(f"Error in display_pdf_details: {str(e)}", exc_info=True) st.error(f"An error occurred: {e}") def display_romanized_text_page(data): """ Displays romanized text and PDF details in a Streamlit layout. Takes preprocessed data instead of filename. """ logger.info(f"Displaying romanized text page for file: {data['filename']}") try: st.markdown( """ """, unsafe_allow_html=True ) # Page Title st.markdown("

📚 Smart PDF Search

", unsafe_allow_html=True) # Document Info Section word_count = len(data['full_text'].split()) st.markdown( f"""
Filename: {data['filename']}
Total Pages: {data['total_pages']}
File Size: {data['file_size_kb']}KB
Total Words: {word_count}
""", unsafe_allow_html=True ) # Display Each Page's Text for page in data['pages']: st.markdown( f"""
{page['text']}

""", unsafe_allow_html=True ) except Exception as e: logger.error(f"Unexpected error in display_romanized_text_page: {str(e)}", exc_info=True) st.error(f"An unexpected error occurred: {e}")