Update pdf_details_page.py
Browse files- pdf_details_page.py +232 -297
pdf_details_page.py
CHANGED
|
@@ -1,7 +1,5 @@
|
|
| 1 |
import io
|
| 2 |
import base64
|
| 3 |
-
import logging
|
| 4 |
-
import requests
|
| 5 |
from PIL import Image
|
| 6 |
import streamlit as st
|
| 7 |
from typing import Dict, Any
|
|
@@ -9,30 +7,220 @@ from log_utils import setup_logging
|
|
| 9 |
|
| 10 |
logger = setup_logging('pdf_details_page')
|
| 11 |
|
| 12 |
-
def
|
| 13 |
"""
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
"""
|
| 16 |
-
|
| 17 |
-
logger.info(f"Making API request to: {url}")
|
| 18 |
-
response = requests.get(url, params=params)
|
| 19 |
-
response.raise_for_status()
|
| 20 |
-
logger.debug(f"API response received successfully from: {url}")
|
| 21 |
-
return response.json()
|
| 22 |
-
except requests.RequestException as e:
|
| 23 |
-
logger.error(f"API request failed: {str(e)}", exc_info=True)
|
| 24 |
-
raise
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
"""
|
| 28 |
-
Displays romanized text and PDF details in a Streamlit layout
|
|
|
|
| 29 |
"""
|
| 30 |
-
logger.info(f"Displaying romanized text page for file: {filename}")
|
| 31 |
try:
|
| 32 |
st.markdown(
|
| 33 |
"""
|
| 34 |
<style>
|
| 35 |
-
/* Styling for metadata section */
|
| 36 |
.metadata {
|
| 37 |
display: flex;
|
| 38 |
justify-content: space-between;
|
|
@@ -45,8 +233,6 @@ def display_romanized_text_page(filename):
|
|
| 45 |
.metadata div {
|
| 46 |
text-align: left;
|
| 47 |
}
|
| 48 |
-
|
| 49 |
-
/* Styling for page text */
|
| 50 |
.page-section {
|
| 51 |
margin-bottom: 40px;
|
| 52 |
}
|
|
@@ -64,8 +250,6 @@ def display_romanized_text_page(filename):
|
|
| 64 |
line-height: 1.5;
|
| 65 |
margin-bottom: 20px;
|
| 66 |
}
|
| 67 |
-
|
| 68 |
-
/* Horizontal rule */
|
| 69 |
hr {
|
| 70 |
border: 0;
|
| 71 |
height: 1px;
|
|
@@ -76,288 +260,39 @@ def display_romanized_text_page(filename):
|
|
| 76 |
""",
|
| 77 |
unsafe_allow_html=True
|
| 78 |
)
|
| 79 |
-
logger.debug("Applied CSS styling")
|
| 80 |
-
# API Endpoint for Romanized Text
|
| 81 |
-
api_url = f"http://127.0.0.1:8000/romanized-text?filename={filename}"
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
logger.info(f"Displaying document info - Pages: {data['total_pages']}, Size: {data['file_size_kb']}KB, Words: {word_count}")
|
| 96 |
-
|
| 97 |
-
# Document Info Section
|
| 98 |
st.markdown(
|
| 99 |
f"""
|
| 100 |
-
<div class='
|
| 101 |
-
<div>
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
<strong>File Size: </strong>{data['file_size_kb']} <br>
|
| 105 |
-
<strong>Total Words: </strong>{len(data['full_text'].split())}
|
| 106 |
-
</div>
|
| 107 |
</div>
|
| 108 |
""",
|
| 109 |
unsafe_allow_html=True
|
| 110 |
-
)
|
| 111 |
-
|
| 112 |
-
# Display Each Page's Text
|
| 113 |
-
logger.info(f"Rendering {len(data['pages'])} pages of text")
|
| 114 |
-
for page in data['pages']:
|
| 115 |
-
st.markdown(
|
| 116 |
-
f"""
|
| 117 |
-
<div class='page-section'>
|
| 118 |
-
<div class='page-header'>Page {page['page_number']}</div>
|
| 119 |
-
<div class='page-text'>{page['text']}</div>
|
| 120 |
-
<hr>
|
| 121 |
-
</div>
|
| 122 |
-
""",
|
| 123 |
-
unsafe_allow_html=True
|
| 124 |
-
)
|
| 125 |
-
logger.debug("Completed rendering all pages")
|
| 126 |
-
|
| 127 |
-
except requests.RequestException as e:
|
| 128 |
-
logger.error(f"API request failed: {str(e)}", exc_info=True)
|
| 129 |
-
st.error(f"Error fetching data: {e}")
|
| 130 |
-
except KeyError as e:
|
| 131 |
-
logger.error(f"Missing key in API response: {str(e)}", exc_info=True)
|
| 132 |
-
st.error(f"Missing key in API response: {e}")
|
| 133 |
-
except Exception as e:
|
| 134 |
-
logger.error(f"Unexpected error in display_romanized_text_page: {str(e)}", exc_info=True)
|
| 135 |
-
st.error(f"An unexpected error occurred: {e}")
|
| 136 |
-
|
| 137 |
-
def display_pdf_details(filename, page_number):
|
| 138 |
-
"""
|
| 139 |
-
Display detailed information about a specific PDF page.
|
| 140 |
-
"""
|
| 141 |
-
logger.info(f"Displaying PDF details for file: {filename}, page: {page_number}")
|
| 142 |
-
|
| 143 |
-
# Initialize reader mode state
|
| 144 |
-
if 'reader_mode' not in st.session_state:
|
| 145 |
-
st.session_state.reader_mode = False
|
| 146 |
-
logger.debug("Initialized reader mode state")
|
| 147 |
-
|
| 148 |
-
def toggle_reader_mode():
|
| 149 |
-
"""Toggle reader mode state with logging."""
|
| 150 |
-
previous_state = st.session_state.reader_mode
|
| 151 |
-
st.session_state.reader_mode = not previous_state
|
| 152 |
-
logger.info(f"Reader mode toggled from {previous_state} to {st.session_state.reader_mode}")
|
| 153 |
-
|
| 154 |
-
try:
|
| 155 |
-
api_url = f"http://127.0.0.1:8000/pdf-details?filename={filename}&page_number={page_number}"
|
| 156 |
-
response = requests.get(api_url)
|
| 157 |
-
logger.debug(f"Retrieved PDF details for page {page_number}")
|
| 158 |
-
|
| 159 |
-
if response.status_code == 200:
|
| 160 |
-
pdf_details = response.json()
|
| 161 |
|
| 162 |
-
# Enhanced CSS for better styling
|
| 163 |
-
st.markdown("""
|
| 164 |
-
<style>
|
| 165 |
-
.page-container {
|
| 166 |
-
background-color: #ffffff;
|
| 167 |
-
padding: 30px;
|
| 168 |
-
margin: 20px auto;
|
| 169 |
-
border-radius: 12px;
|
| 170 |
-
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
| 171 |
-
max-width: 1200px;
|
| 172 |
-
font-family: Arial, sans-serif;
|
| 173 |
-
}
|
| 174 |
-
.stApp {
|
| 175 |
-
background-color: #f8f9fa;
|
| 176 |
-
}
|
| 177 |
-
.detail-box {
|
| 178 |
-
border-radius: 12px;
|
| 179 |
-
padding: 25px;
|
| 180 |
-
margin-bottom: 25px;
|
| 181 |
-
}
|
| 182 |
-
.header {
|
| 183 |
-
text-align: center;
|
| 184 |
-
color: #1a237e;
|
| 185 |
-
margin-bottom: 30px;
|
| 186 |
-
font-family: 'Helvetica Neue', sans-serif;
|
| 187 |
-
}
|
| 188 |
-
.metadata-table {
|
| 189 |
-
width: 100%;
|
| 190 |
-
border-collapse: collapse;
|
| 191 |
-
margin: 20px 0;
|
| 192 |
-
font-family: 'Helvetica Neue', sans-serif;
|
| 193 |
-
}
|
| 194 |
-
.metadata-table td {
|
| 195 |
-
padding: 12px 15px;
|
| 196 |
-
border: 1px solid #e0e0e0;
|
| 197 |
-
}
|
| 198 |
-
.metadata-table tr:nth-child(even) {
|
| 199 |
-
background-color: #f8f9fa;
|
| 200 |
-
}
|
| 201 |
-
.metadata-table tr:hover {
|
| 202 |
-
background-color: #f5f5f5;
|
| 203 |
-
}
|
| 204 |
-
.metadata-table td:first-child {
|
| 205 |
-
font-weight: 600;
|
| 206 |
-
width: 30%;
|
| 207 |
-
color: #2c3e50;
|
| 208 |
-
}
|
| 209 |
-
.stButton>button {
|
| 210 |
-
width: 100%;
|
| 211 |
-
border-radius: 8px;
|
| 212 |
-
height: 45px;
|
| 213 |
-
margin-top: 10px;
|
| 214 |
-
}
|
| 215 |
-
.stTextArea>div>div {
|
| 216 |
-
border-radius: 8px;
|
| 217 |
-
}
|
| 218 |
-
.page-preview {
|
| 219 |
-
border-radius: 8px;
|
| 220 |
-
overflow: hidden;
|
| 221 |
-
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
| 222 |
-
max-width: 100%;
|
| 223 |
-
max-height: 500px;
|
| 224 |
-
margin: auto;
|
| 225 |
-
}
|
| 226 |
-
div[data-baseweb="tab"] {
|
| 227 |
-
padding: 15px !important;
|
| 228 |
-
}
|
| 229 |
-
.stExpander {
|
| 230 |
-
border-radius: 8px;
|
| 231 |
-
border: 1px solid #e0e0e0;
|
| 232 |
-
margin-top: 20px;
|
| 233 |
-
}
|
| 234 |
-
.reader-mode {
|
| 235 |
-
position: fixed;
|
| 236 |
-
top: 0;
|
| 237 |
-
left: 0;
|
| 238 |
-
width: 100vw;
|
| 239 |
-
height: 100vh;
|
| 240 |
-
background: rgba(0, 0, 0, 0.9);
|
| 241 |
-
z-index: 9999;
|
| 242 |
-
display: flex;
|
| 243 |
-
justify-content: center;
|
| 244 |
-
align-items: center;
|
| 245 |
-
padding: 2rem;
|
| 246 |
-
}
|
| 247 |
-
.reader-mode img {
|
| 248 |
-
max-height: 90vh;
|
| 249 |
-
max-width: 90vw;
|
| 250 |
-
object-fit: contain;
|
| 251 |
-
}
|
| 252 |
-
.close-reader {
|
| 253 |
-
position: fixed;
|
| 254 |
-
top: 20px;
|
| 255 |
-
right: 20px;
|
| 256 |
-
color: white;
|
| 257 |
-
font-size: 24px;
|
| 258 |
-
cursor: pointer;
|
| 259 |
-
z-index: 10000;
|
| 260 |
-
}
|
| 261 |
-
</style>
|
| 262 |
-
""", unsafe_allow_html=True)
|
| 263 |
-
logger.debug("Applied CSS styling")
|
| 264 |
-
|
| 265 |
-
# Reader mode display (if active)
|
| 266 |
-
if st.session_state.reader_mode:
|
| 267 |
-
logger.info("Displaying reader mode view")
|
| 268 |
-
st.markdown('<div class="reader-mode-container">', unsafe_allow_html=True)
|
| 269 |
-
if st.button("❌ Close Reader Mode", key="close_reader", help="Exit reader mode"):
|
| 270 |
-
logger.info("Reader mode closed")
|
| 271 |
-
st.session_state.reader_mode = False
|
| 272 |
-
st.rerun()
|
| 273 |
-
|
| 274 |
-
# Display zoomed image
|
| 275 |
-
page_image_bytes = base64.b64decode(pdf_details['page_image'])
|
| 276 |
-
page_image = Image.open(io.BytesIO(page_image_bytes))
|
| 277 |
-
st.image(page_image, use_container_width=True, caption=f"Page {pdf_details['current_page']}")
|
| 278 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
| 279 |
-
return # Exit early as we don't need to show the regular interface in reader mode
|
| 280 |
-
|
| 281 |
-
logger.info("Displaying regular interface")
|
| 282 |
-
# Header
|
| 283 |
-
st.markdown('<h1 class="header">📚 Smart PDF Search</h1>', unsafe_allow_html=True)
|
| 284 |
-
|
| 285 |
-
# Main content
|
| 286 |
-
col1, col2 = st.columns([1.5, 2])
|
| 287 |
-
|
| 288 |
-
with col1:
|
| 289 |
-
logger.debug("Rendering details section")
|
| 290 |
-
st.markdown("<h3 style='color: #1a237e; margin-bottom: 15px;'>🖼️ Page Preview</h3>", unsafe_allow_html=True)
|
| 291 |
-
st.markdown(f"<div style='text-align: center; padding: 15px;'>Page {page_number + 1} of {pdf_details['total_pages']}</div>", unsafe_allow_html=True)
|
| 292 |
-
page_image_bytes = base64.b64decode(pdf_details['page_image'])
|
| 293 |
-
page_image = Image.open(io.BytesIO(page_image_bytes))
|
| 294 |
-
|
| 295 |
-
st.image(page_image, caption=f"Page {pdf_details['current_page']}", use_container_width=True)
|
| 296 |
-
st.markdown("</div>", unsafe_allow_html=True)
|
| 297 |
-
|
| 298 |
-
with col2:
|
| 299 |
-
st.markdown("<div class='detail-box'>", unsafe_allow_html=True)
|
| 300 |
-
|
| 301 |
-
# Create 3 equal-width columns
|
| 302 |
-
col1, col2, col3 = st.columns(3)
|
| 303 |
-
|
| 304 |
-
# Action buttons inside the columns
|
| 305 |
-
with col1:
|
| 306 |
-
logger.info("Reader mode button clicked")
|
| 307 |
-
st.button("📖 Reader Mode", on_click=toggle_reader_mode)
|
| 308 |
-
|
| 309 |
-
with col2:
|
| 310 |
-
if st.button("🔍 Ask a Question"):
|
| 311 |
-
logger.info("Ask a Question button clicked")
|
| 312 |
-
st.query_params["page"] = "home" # Use the new API instead
|
| 313 |
-
st.rerun()
|
| 314 |
-
|
| 315 |
-
with col3:
|
| 316 |
-
logger.debug("Rendering Romanized Text link")
|
| 317 |
-
st.markdown(f"""
|
| 318 |
-
<a href="?page=romanized_text&filename={filename}" style="
|
| 319 |
-
display: inline-block;
|
| 320 |
-
padding: 10px 10px;
|
| 321 |
-
font-size: 16px;
|
| 322 |
-
font-weight: 400;
|
| 323 |
-
color: white;
|
| 324 |
-
background-color: #3498db;
|
| 325 |
-
border: none;
|
| 326 |
-
border-radius: 8px;
|
| 327 |
-
text-align: center;
|
| 328 |
-
text-decoration: none;
|
| 329 |
-
margin-top: 10px;
|
| 330 |
-
transition: all 0.3s ease;
|
| 331 |
-
text-transform: uppercase;
|
| 332 |
-
letter-spacing: 0.5px;
|
| 333 |
-
width: -webkit-fill-available;
|
| 334 |
-
">
|
| 335 |
-
📄 Romanized Text
|
| 336 |
-
</a>
|
| 337 |
-
""", unsafe_allow_html=True)
|
| 338 |
-
|
| 339 |
-
# Page content in expander
|
| 340 |
-
with st.expander("📄 Page Content", expanded=True):
|
| 341 |
-
logger.debug("Displaying page content in expander")
|
| 342 |
-
st.markdown(pdf_details['page_text'], unsafe_allow_html=True)
|
| 343 |
-
|
| 344 |
-
logger.debug("Rendering metadata table")
|
| 345 |
-
# Content tabs
|
| 346 |
-
metadata_html = f"""
|
| 347 |
-
<table class="metadata-table">
|
| 348 |
-
<tr><td>PDF Name</td><td>{pdf_details.get('title', filename)}</td></tr>
|
| 349 |
-
<tr><td>Page</td><td>{page_number + 1}</td></tr>
|
| 350 |
-
<tr><td>Author</td><td>{pdf_details.get('metadata', {}).get('author', 'N/A')}</td></tr>
|
| 351 |
-
<tr><td>Total Pages</td><td>{pdf_details['total_pages']}</td></tr>
|
| 352 |
-
<tr><td>Language</td><td>{pdf_details['language']}</td></tr>
|
| 353 |
-
<tr><td>File Size</td><td>{pdf_details['file_size_kb']} KB</td></tr>
|
| 354 |
-
</table>
|
| 355 |
-
"""
|
| 356 |
-
st.markdown(metadata_html, unsafe_allow_html=True)
|
| 357 |
-
logger.info(f"Completed rendering PDF details page for {filename}")
|
| 358 |
-
else:
|
| 359 |
-
st.error(f"Error fetching PDF details: {response.text}")
|
| 360 |
-
|
| 361 |
except Exception as e:
|
| 362 |
-
logger.error(f"
|
| 363 |
-
st.error(f"An error occurred: {e}")
|
|
|
|
| 1 |
import io
|
| 2 |
import base64
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import streamlit as st
|
| 5 |
from typing import Dict, Any
|
|
|
|
| 7 |
|
| 8 |
logger = setup_logging('pdf_details_page')
|
| 9 |
|
| 10 |
+
def display_pdf_details(pdf_details, filename):
|
| 11 |
"""
|
| 12 |
+
Display detailed information about a specific PDF page.
|
| 13 |
+
Parameters:
|
| 14 |
+
- pdf_details: dict containing the PDF information
|
| 15 |
+
- filename: name of the PDF file
|
| 16 |
"""
|
| 17 |
+
logger.info(f"Displaying PDF details for file: {filename}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Initialize reader mode state
|
| 20 |
+
if 'reader_mode' not in st.session_state:
|
| 21 |
+
st.session_state.reader_mode = False
|
| 22 |
+
logger.debug("Initialized reader mode state")
|
| 23 |
+
|
| 24 |
+
def toggle_reader_mode():
|
| 25 |
+
"""Toggle reader mode state with logging."""
|
| 26 |
+
previous_state = st.session_state.reader_mode
|
| 27 |
+
st.session_state.reader_mode = not previous_state
|
| 28 |
+
logger.info(f"Reader mode toggled from {previous_state} to {st.session_state.reader_mode}")
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
# Enhanced CSS for better styling
|
| 32 |
+
st.markdown("""
|
| 33 |
+
<style>
|
| 34 |
+
.page-container {
|
| 35 |
+
background-color: #ffffff;
|
| 36 |
+
padding: 30px;
|
| 37 |
+
margin: 20px auto;
|
| 38 |
+
border-radius: 12px;
|
| 39 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
|
| 40 |
+
max-width: 1200px;
|
| 41 |
+
font-family: Arial, sans-serif;
|
| 42 |
+
}
|
| 43 |
+
.stApp {
|
| 44 |
+
background-color: #f8f9fa;
|
| 45 |
+
}
|
| 46 |
+
.detail-box {
|
| 47 |
+
border-radius: 12px;
|
| 48 |
+
padding: 25px;
|
| 49 |
+
margin-bottom: 25px;
|
| 50 |
+
}
|
| 51 |
+
.header {
|
| 52 |
+
text-align: center;
|
| 53 |
+
color: #1a237e;
|
| 54 |
+
margin-bottom: 30px;
|
| 55 |
+
font-family: 'Helvetica Neue', sans-serif;
|
| 56 |
+
}
|
| 57 |
+
.metadata-table {
|
| 58 |
+
width: 100%;
|
| 59 |
+
border-collapse: collapse;
|
| 60 |
+
margin: 20px 0;
|
| 61 |
+
font-family: 'Helvetica Neue', sans-serif;
|
| 62 |
+
}
|
| 63 |
+
.metadata-table td {
|
| 64 |
+
padding: 12px 15px;
|
| 65 |
+
border: 1px solid #e0e0e0;
|
| 66 |
+
}
|
| 67 |
+
.metadata-table tr:nth-child(even) {
|
| 68 |
+
background-color: #f8f9fa;
|
| 69 |
+
}
|
| 70 |
+
.metadata-table tr:hover {
|
| 71 |
+
background-color: #f5f5f5;
|
| 72 |
+
}
|
| 73 |
+
.metadata-table td:first-child {
|
| 74 |
+
font-weight: 600;
|
| 75 |
+
width: 30%;
|
| 76 |
+
color: #2c3e50;
|
| 77 |
+
}
|
| 78 |
+
.stButton>button {
|
| 79 |
+
width: 100%;
|
| 80 |
+
border-radius: 8px;
|
| 81 |
+
height: 45px;
|
| 82 |
+
margin-top: 10px;
|
| 83 |
+
}
|
| 84 |
+
.stTextArea>div>div {
|
| 85 |
+
border-radius: 8px;
|
| 86 |
+
}
|
| 87 |
+
.page-preview {
|
| 88 |
+
border-radius: 8px;
|
| 89 |
+
overflow: hidden;
|
| 90 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
| 91 |
+
max-width: 100%;
|
| 92 |
+
max-height: 500px;
|
| 93 |
+
margin: auto;
|
| 94 |
+
}
|
| 95 |
+
.reader-mode {
|
| 96 |
+
position: fixed;
|
| 97 |
+
top: 0;
|
| 98 |
+
left: 0;
|
| 99 |
+
width: 100vw;
|
| 100 |
+
height: 100vh;
|
| 101 |
+
background: rgba(0, 0, 0, 0.9);
|
| 102 |
+
z-index: 9999;
|
| 103 |
+
display: flex;
|
| 104 |
+
justify-content: center;
|
| 105 |
+
align-items: center;
|
| 106 |
+
padding: 2rem;
|
| 107 |
+
}
|
| 108 |
+
.reader-mode img {
|
| 109 |
+
max-height: 90vh;
|
| 110 |
+
max-width: 90vw;
|
| 111 |
+
object-fit: contain;
|
| 112 |
+
}
|
| 113 |
+
</style>
|
| 114 |
+
""", unsafe_allow_html=True)
|
| 115 |
+
logger.debug("Applied CSS styling")
|
| 116 |
+
|
| 117 |
+
# Reader mode display (if active)
|
| 118 |
+
if st.session_state.reader_mode:
|
| 119 |
+
logger.info("Displaying reader mode view")
|
| 120 |
+
st.markdown('<div class="reader-mode-container">', unsafe_allow_html=True)
|
| 121 |
+
if st.button("❌ Close Reader Mode", key="close_reader", help="Exit reader mode"):
|
| 122 |
+
logger.info("Reader mode closed")
|
| 123 |
+
st.session_state.reader_mode = False
|
| 124 |
+
st.rerun()
|
| 125 |
+
|
| 126 |
+
# Display zoomed image
|
| 127 |
+
page_image_bytes = base64.b64decode(pdf_details['page_image'])
|
| 128 |
+
page_image = Image.open(io.BytesIO(page_image_bytes))
|
| 129 |
+
st.image(page_image, use_container_width=True, caption=f"Page {pdf_details['current_page']}")
|
| 130 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 131 |
+
return
|
| 132 |
+
|
| 133 |
+
logger.info("Displaying regular interface")
|
| 134 |
+
# Header
|
| 135 |
+
st.markdown('<h1 class="header">📚 Smart PDF Search</h1>', unsafe_allow_html=True)
|
| 136 |
+
|
| 137 |
+
# Main content
|
| 138 |
+
col1, col2 = st.columns([1.5, 2])
|
| 139 |
+
|
| 140 |
+
with col1:
|
| 141 |
+
logger.debug("Rendering details section")
|
| 142 |
+
st.markdown("<h3 style='color: #1a237e; margin-bottom: 15px;'>🖼️ Page Preview</h3>", unsafe_allow_html=True)
|
| 143 |
+
current_page = pdf_details['current_page']
|
| 144 |
+
st.markdown(f"<div style='text-align: center; padding: 15px;'>Page {current_page} of {pdf_details['total_pages']}</div>", unsafe_allow_html=True)
|
| 145 |
+
|
| 146 |
+
page_image_bytes = base64.b64decode(pdf_details['page_image'])
|
| 147 |
+
page_image = Image.open(io.BytesIO(page_image_bytes))
|
| 148 |
+
st.image(page_image, caption=f"Page {current_page}", use_container_width=True)
|
| 149 |
+
|
| 150 |
+
with col2:
|
| 151 |
+
st.markdown("<div class='detail-box'>", unsafe_allow_html=True)
|
| 152 |
+
|
| 153 |
+
# Create 3 equal-width columns
|
| 154 |
+
col1, col2, col3 = st.columns(3)
|
| 155 |
+
|
| 156 |
+
# Action buttons inside the columns
|
| 157 |
+
with col1:
|
| 158 |
+
logger.info("Reader mode button clicked")
|
| 159 |
+
st.button("📖 Reader Mode", on_click=toggle_reader_mode)
|
| 160 |
+
|
| 161 |
+
with col2:
|
| 162 |
+
if st.button("🔍 Ask a Question"):
|
| 163 |
+
logger.info("Ask a Question button clicked")
|
| 164 |
+
st.query_params["page"] = "home"
|
| 165 |
+
st.rerun()
|
| 166 |
+
|
| 167 |
+
with col3:
|
| 168 |
+
logger.debug("Rendering Romanized Text link")
|
| 169 |
+
st.markdown(f"""
|
| 170 |
+
<a href="?page=romanized_text&filename={filename}" style="
|
| 171 |
+
display: inline-block;
|
| 172 |
+
padding: 10px 10px;
|
| 173 |
+
font-size: 16px;
|
| 174 |
+
font-weight: 400;
|
| 175 |
+
color: white;
|
| 176 |
+
background-color: #3498db;
|
| 177 |
+
border: none;
|
| 178 |
+
border-radius: 8px;
|
| 179 |
+
text-align: center;
|
| 180 |
+
text-decoration: none;
|
| 181 |
+
margin-top: 10px;
|
| 182 |
+
transition: all 0.3s ease;
|
| 183 |
+
text-transform: uppercase;
|
| 184 |
+
letter-spacing: 0.5px;
|
| 185 |
+
width: -webkit-fill-available;
|
| 186 |
+
">
|
| 187 |
+
📄 Romanized Text
|
| 188 |
+
</a>
|
| 189 |
+
""", unsafe_allow_html=True)
|
| 190 |
+
|
| 191 |
+
# Page content in expander
|
| 192 |
+
with st.expander("📄 Page Content", expanded=True):
|
| 193 |
+
logger.debug("Displaying page content in expander")
|
| 194 |
+
st.markdown(pdf_details['page_text'], unsafe_allow_html=True)
|
| 195 |
+
|
| 196 |
+
# Metadata table
|
| 197 |
+
metadata_html = f"""
|
| 198 |
+
<table class="metadata-table">
|
| 199 |
+
<tr><td>PDF Name</td><td>{pdf_details['metadata'].get('title', filename)}</td></tr>
|
| 200 |
+
<tr><td>Page</td><td>{pdf_details['current_page']}</td></tr>
|
| 201 |
+
<tr><td>Author</td><td>{pdf_details['metadata'].get('author', 'N/A')}</td></tr>
|
| 202 |
+
<tr><td>Total Pages</td><td>{pdf_details['total_pages']}</td></tr>
|
| 203 |
+
<tr><td>Language</td><td>{pdf_details['language']}</td></tr>
|
| 204 |
+
<tr><td>File Size</td><td>{pdf_details['file_size_kb']}</td></tr>
|
| 205 |
+
</table>
|
| 206 |
+
"""
|
| 207 |
+
st.markdown(metadata_html, unsafe_allow_html=True)
|
| 208 |
+
logger.info(f"Completed rendering PDF details page for {filename}")
|
| 209 |
+
|
| 210 |
+
except Exception as e:
|
| 211 |
+
logger.error(f"Error in display_pdf_details: {str(e)}", exc_info=True)
|
| 212 |
+
st.error(f"An error occurred: {e}")
|
| 213 |
+
|
| 214 |
+
def display_romanized_text_page(data):
|
| 215 |
"""
|
| 216 |
+
Displays romanized text and PDF details in a Streamlit layout.
|
| 217 |
+
Takes preprocessed data instead of filename.
|
| 218 |
"""
|
| 219 |
+
logger.info(f"Displaying romanized text page for file: {data['filename']}")
|
| 220 |
try:
|
| 221 |
st.markdown(
|
| 222 |
"""
|
| 223 |
<style>
|
|
|
|
| 224 |
.metadata {
|
| 225 |
display: flex;
|
| 226 |
justify-content: space-between;
|
|
|
|
| 233 |
.metadata div {
|
| 234 |
text-align: left;
|
| 235 |
}
|
|
|
|
|
|
|
| 236 |
.page-section {
|
| 237 |
margin-bottom: 40px;
|
| 238 |
}
|
|
|
|
| 250 |
line-height: 1.5;
|
| 251 |
margin-bottom: 20px;
|
| 252 |
}
|
|
|
|
|
|
|
| 253 |
hr {
|
| 254 |
border: 0;
|
| 255 |
height: 1px;
|
|
|
|
| 260 |
""",
|
| 261 |
unsafe_allow_html=True
|
| 262 |
)
|
|
|
|
|
|
|
|
|
|
| 263 |
|
| 264 |
+
# Page Title
|
| 265 |
+
st.markdown("<h1 style='text-align: center; margin-top: -1%;}'>📚 Smart PDF Search</h1>", unsafe_allow_html=True)
|
| 266 |
+
|
| 267 |
+
# Document Info Section
|
| 268 |
+
word_count = len(data['full_text'].split())
|
| 269 |
+
st.markdown(
|
| 270 |
+
f"""
|
| 271 |
+
<div class='metadata'>
|
| 272 |
+
<div>
|
| 273 |
+
<strong>Filename: </strong>{data['filename']} <br>
|
| 274 |
+
<strong>Total Pages: </strong>{data['total_pages']} <br>
|
| 275 |
+
<strong>File Size: </strong>{data['file_size_kb']}KB <br>
|
| 276 |
+
<strong>Total Words: </strong>{word_count}
|
| 277 |
+
</div>
|
| 278 |
+
</div>
|
| 279 |
+
""",
|
| 280 |
+
unsafe_allow_html=True
|
| 281 |
+
)
|
| 282 |
|
| 283 |
+
# Display Each Page's Text
|
| 284 |
+
for page in data['pages']:
|
|
|
|
|
|
|
|
|
|
| 285 |
st.markdown(
|
| 286 |
f"""
|
| 287 |
+
<div class='page-section'>
|
| 288 |
+
<div class='page-header'>Page {page['page_number']}</div>
|
| 289 |
+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