Spaces:
Sleeping
Sleeping
File size: 17,398 Bytes
ee69d5e 9cc64fb aa1280b ee69d5e 9cc64fb 63720b6 9cc64fb aa1280b 9cc64fb 63720b6 9cc64fb aa1280b 9cc64fb 63720b6 9cc64fb aa1280b 9cc64fb aa1280b 9cc64fb aa1280b 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 104b6b9 aa1280b 104b6b9 9cc64fb 104b6b9 9cc64fb 104b6b9 9cc64fb 104b6b9 aa1280b 104b6b9 aa1280b 104b6b9 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb aa1280b 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb aa1280b 9cc64fb aa1280b 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 9cc64fb 63720b6 aa1280b 63720b6 aa1280b 63720b6 aa1280b 9cc64fb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
import streamlit as st
import PyPDF2
import io
import base64
from datetime import datetime
import json
import tempfile
import os
# Page configuration
st.set_page_config(
page_title="Dev LMS",
page_icon="π",
layout="wide",
initial_sidebar_state="expanded"
)
# Initialize session state
if 'uploaded_documents' not in st.session_state:
st.session_state.uploaded_documents = {}
if 'current_user' not in st.session_state:
st.session_state.current_user = "User"
def save_document_info(filename, file_content, file_type, temp_path=None):
"""Save document information to session state"""
if 'documents' not in st.session_state.uploaded_documents:
st.session_state.uploaded_documents['documents'] = []
document_info = {
'filename': filename,
'upload_time': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
'file_type': file_type,
'size': len(file_content),
'content': file_content.decode('latin-1') if isinstance(file_content, bytes) else str(file_content),
'temp_path': temp_path # Store temp path for later use
}
st.session_state.uploaded_documents['documents'].append(document_info)
def extract_pdf_text_from_temp(temp_path):
"""Extract text from PDF file using temporary file path"""
try:
with open(temp_path, "rb") as pdf_file:
pdf_reader = PyPDF2.PdfReader(pdf_file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
return text
except Exception as e:
st.error(f"Error reading PDF: {str(e)}")
return ""
def extract_pdf_text_from_memory(uploaded_file):
"""Extract text from PDF file in memory"""
try:
pdf_reader = PyPDF2.PdfReader(uploaded_file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text() + "\n"
return text
except Exception as e:
st.error(f"Error reading PDF: {str(e)}")
return ""
def cleanup_temp_file(temp_path):
"""Clean up temporary file"""
try:
if temp_path and os.path.exists(temp_path):
os.remove(temp_path)
except Exception as e:
st.warning(f"Could not clean up temporary file: {str(e)}")
def main():
# Sidebar for navigation
with st.sidebar:
st.title("π Dev LMS")
st.markdown("---")
# Navigation
page = st.selectbox(
"Navigation",
["Dashboard", "Upload Documents", "My Documents", "Document Library", "Settings"]
)
# Main content area
if page == "Dashboard":
show_dashboard()
elif page == "Upload Documents":
show_upload_documents()
elif page == "My Documents":
show_my_documents()
elif page == "Document Library":
show_document_library()
elif page == "Settings":
show_settings()
def show_dashboard():
"""Show the main dashboard"""
st.title("π Dashboard")
st.markdown("---")
col1, col2, col3 = st.columns(3)
with col1:
st.metric(
label="Total Documents",
value=len(st.session_state.uploaded_documents.get('documents', [])),
delta="0"
)
with col2:
st.metric(
label="System Status",
value="Active",
delta="0"
)
with col3:
st.metric(
label="Storage Used",
value="Session",
delta="0"
)
st.markdown("---")
# Recent activity
st.subheader("π Recent Activity")
documents = st.session_state.uploaded_documents.get('documents', [])
if documents:
recent_docs = documents[-5:]
for doc in recent_docs:
with st.container():
col1, col2, col3 = st.columns([3, 2, 1])
with col1:
st.write(f"**{doc['filename']}**")
with col2:
st.write(doc['upload_time'])
with col3:
st.write(f"{doc['file_type']}")
st.markdown("---")
else:
st.info("No documents uploaded yet. Start by uploading a PDF document!")
def show_upload_documents():
"""Show document upload interface"""
st.title("π€ Upload Documents")
st.markdown("---")
# Add information about file upload
st.info("π‘ **Note:** File upload uses temporary storage for better compatibility with Hugging Face Spaces.")
uploaded_file = st.file_uploader(
"Choose a PDF file",
type=['pdf'],
help="Upload PDF documents to the LMS (max 200MB)",
accept_multiple_files=False
)
if uploaded_file is not None:
try:
# Display file info
file_details = {
"Filename": uploaded_file.name,
"File size": f"{uploaded_file.size / 1024:.2f} KB",
"File type": uploaded_file.type
}
st.write("**File Details:**")
for key, value in file_details.items():
st.write(f"- {key}: {value}")
# Create temporary file for better PDF processing
temp_path = None
try:
with tempfile.NamedTemporaryFile(mode="wb", suffix=".pdf", delete=False) as temp:
bytes_data = uploaded_file.getvalue()
temp.write(bytes_data)
temp_path = temp.name
st.success(f"π File temporarily stored at: {temp_path}")
# Extract and display PDF content using temporary file
pdf_text = extract_pdf_text_from_temp(temp_path)
if pdf_text.strip():
st.subheader("π Document Preview")
with st.expander("View extracted text"):
st.text_area("PDF Content", pdf_text, height=300)
else:
st.warning("β οΈ Could not extract text from this PDF. The file may be image-based or encrypted.")
# Upload button
if st.button("Upload Document", type="primary"):
try:
# Save document info with temporary file path
save_document_info(
uploaded_file.name,
bytes_data,
"PDF",
temp_path
)
st.success(f"β
Document '{uploaded_file.name}' uploaded successfully!")
st.balloons()
# Clear the file uploader
st.rerun()
except Exception as e:
st.error(f"β Error uploading document: {str(e)}")
st.info("π‘ Try uploading a smaller file or refresh the page.")
# Clean up temp file on error
cleanup_temp_file(temp_path)
except Exception as e:
st.error(f"β Error creating temporary file: {str(e)}")
st.info("π‘ Please try uploading a different PDF file.")
cleanup_temp_file(temp_path)
except Exception as e:
st.error(f"β Error processing file: {str(e)}")
st.info("π‘ Please try uploading a different PDF file.")
# Add helpful tips
with st.expander("π‘ Upload Tips"):
st.markdown("""
**For best results:**
- Use PDF files under 200MB
- Ensure PDFs contain text (not just images)
- Avoid password-protected PDFs
- If upload fails, try refreshing the page
**Technical details:**
- Files are temporarily stored on the server
- Text extraction uses temporary file processing
- Automatic cleanup of temporary files
**Supported formats:** PDF only
""")
def show_my_documents():
"""Show uploaded documents"""
st.title("π My Documents")
st.markdown("---")
documents = st.session_state.uploaded_documents.get('documents', [])
if not documents:
st.info("You haven't uploaded any documents yet.")
return
# Search functionality
search_term = st.text_input("π Search documents", placeholder="Enter filename or content...")
# Filter documents based on search
filtered_docs = documents
if search_term:
filtered_docs = [
doc for doc in documents
if search_term.lower() in doc['filename'].lower() or
search_term.lower() in doc.get('content', '').lower()
]
if not filtered_docs:
st.warning("No documents match your search criteria.")
return
# Display documents
for i, doc in enumerate(filtered_docs):
with st.container():
col1, col2, col3, col4 = st.columns([3, 2, 1, 1])
with col1:
st.write(f"**{doc['filename']}**")
with col2:
st.write(doc['upload_time'])
with col3:
st.write(f"{doc['file_type']}")
with col4:
if st.button(f"View {i}", key=f"view_{i}"):
st.subheader(f"π {doc['filename']}")
st.write(f"**Uploaded:** {doc['upload_time']}")
st.write(f"**Size:** {doc['size']} bytes")
# Check if we have a temporary file path for better content extraction
if doc.get('temp_path') and os.path.exists(doc['temp_path']):
try:
# Extract fresh content from temporary file
fresh_content = extract_pdf_text_from_temp(doc['temp_path'])
if fresh_content.strip():
st.text_area("Document Content (Fresh Extract)", fresh_content, height=400, key=f"fresh_content_{i}")
else:
# Fall back to stored content
if 'content' in doc and doc['content']:
st.text_area("Document Content (Stored)", doc['content'], height=400, key=f"content_{i}")
except Exception as e:
st.warning(f"Could not read from temporary file: {str(e)}")
# Fall back to stored content
if 'content' in doc and doc['content']:
st.text_area("Document Content (Stored)", doc['content'], height=400, key=f"content_{i}")
else:
# Display stored content
if 'content' in doc and doc['content']:
st.text_area("Document Content", doc['content'], height=400, key=f"content_{i}")
st.markdown("---")
def show_document_library():
"""Show all documents in the system"""
st.title("π Document Library")
st.markdown("---")
documents = st.session_state.uploaded_documents.get('documents', [])
if not documents:
st.info("No documents have been uploaded to the system yet.")
return
# Search functionality
search_term = st.text_input("π Search all documents", placeholder="Enter filename or content...")
# Filter documents based on search
filtered_docs = documents
if search_term:
filtered_docs = [
doc for doc in documents
if search_term.lower() in doc['filename'].lower() or
search_term.lower() in doc.get('content', '').lower()
]
if not filtered_docs:
st.warning("No documents match your search criteria.")
return
# Display documents
for i, doc in enumerate(filtered_docs):
with st.container():
col1, col2, col3, col4 = st.columns([3, 2, 1, 1])
with col1:
st.write(f"**{doc['filename']}**")
with col2:
st.write(doc['upload_time'])
with col3:
st.write(f"{doc['file_type']}")
with col4:
if st.button(f"View {i}", key=f"lib_view_{i}"):
st.subheader(f"π {doc['filename']}")
st.write(f"**Uploaded:** {doc['upload_time']}")
st.write(f"**Size:** {doc['size']} bytes")
# Check if we have a temporary file path for better content extraction
if doc.get('temp_path') and os.path.exists(doc['temp_path']):
try:
# Extract fresh content from temporary file
fresh_content = extract_pdf_text_from_temp(doc['temp_path'])
if fresh_content.strip():
st.text_area("Document Content (Fresh Extract)", fresh_content, height=400, key=f"lib_fresh_content_{i}")
else:
# Fall back to stored content
if 'content' in doc and doc['content']:
st.text_area("Document Content (Stored)", doc['content'], height=400, key=f"lib_content_{i}")
except Exception as e:
st.warning(f"Could not read from temporary file: {str(e)}")
# Fall back to stored content
if 'content' in doc and doc['content']:
st.text_area("Document Content (Stored)", doc['content'], height=400, key=f"lib_content_{i}")
else:
# Display stored content
if 'content' in doc and doc['content']:
st.text_area("Document Content", doc['content'], height=400, key=f"lib_content_{i}")
st.markdown("---")
def show_settings():
"""Show user settings"""
st.title("βοΈ Settings")
st.markdown("---")
st.subheader("π§ System Information")
st.write("**Version:** Dev LMS v1.0")
st.write("**Features:**")
st.write("- PDF document upload with temporary storage")
st.write("- Document search and preview")
st.write("- Document library")
st.write("- Session-based storage")
st.markdown("---")
# Export data option
if st.button("π₯ Export All Data"):
documents = st.session_state.uploaded_documents.get('documents', [])
if documents:
# Create JSON export
export_data = {
'export_date': datetime.now().isoformat(),
'documents': documents
}
st.download_button(
label="Download JSON Export",
data=json.dumps(export_data, indent=2),
file_name=f"lms_data_export.json",
mime="application/json"
)
else:
st.info("No data to export.")
st.markdown("---")
# Clear data option
if st.button("ποΈ Clear All Data"):
if st.session_state.uploaded_documents.get('documents'):
# Clean up temporary files before clearing data
documents = st.session_state.uploaded_documents['documents']
for doc in documents:
if doc.get('temp_path'):
cleanup_temp_file(doc['temp_path'])
st.session_state.uploaded_documents['documents'] = []
st.success("All documents and temporary files have been cleared!")
st.rerun()
else:
st.info("No documents to clear.")
st.markdown("---")
# Cleanup temporary files option
if st.button("π§Ή Cleanup Temporary Files"):
documents = st.session_state.uploaded_documents.get('documents', [])
cleaned_count = 0
for doc in documents:
if doc.get('temp_path') and not os.path.exists(doc['temp_path']):
# Remove temp_path reference if file doesn't exist
doc.pop('temp_path', None)
cleaned_count += 1
if cleaned_count > 0:
st.success(f"Cleaned up {cleaned_count} missing temporary file references!")
else:
st.info("No cleanup needed - all temporary files are properly managed.")
st.markdown("---")
# System status
st.subheader("π System Status")
documents = st.session_state.uploaded_documents.get('documents', [])
temp_files_count = sum(1 for doc in documents if doc.get('temp_path') and os.path.exists(doc['temp_path']))
col1, col2 = st.columns(2)
with col1:
st.metric("Total Documents", len(documents))
with col2:
st.metric("Active Temp Files", temp_files_count)
if __name__ == "__main__":
main() |