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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template_string, session, redirect, url_for, flash, send_from_directory
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import uuid
|
| 4 |
+
import logging
|
| 5 |
+
import traceback
|
| 6 |
+
from werkzeug.utils import secure_filename
|
| 7 |
+
from werkzeug.exceptions import RequestEntityTooLarge
|
| 8 |
+
|
| 9 |
+
# Import with proper error handling
|
| 10 |
+
try:
|
| 11 |
+
from rag_document_viewer import RAG_DV
|
| 12 |
+
except ImportError as e:
|
| 13 |
+
print(f"Warning: Could not import RAG_DV: {e}")
|
| 14 |
+
RAG_DV = None
|
| 15 |
+
|
| 16 |
+
app = Flask(__name__)
|
| 17 |
+
app.secret_key = 'a&VhLelkAo!dKXm9o5RRHQ@#BxoI3Q5378qwFP&aJKA#PLjO7TU*Aq5Kwg4OTdMI7N3%wFAmnwezlEbPUdEQKhGJD10E8@0gSrS'
|
| 18 |
+
app.config['UPLOAD_FOLDER'] = Path('uploads')
|
| 19 |
+
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
|
| 20 |
+
|
| 21 |
+
# Configure logging
|
| 22 |
+
logging.basicConfig(level=logging.INFO)
|
| 23 |
+
logger = logging.getLogger(__name__)
|
| 24 |
+
|
| 25 |
+
# Create uploads directory if it doesn't exist
|
| 26 |
+
app.config['UPLOAD_FOLDER'].mkdir(exist_ok=True)
|
| 27 |
+
|
| 28 |
+
# Allowed file extensions
|
| 29 |
+
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'odt', 'odp', 'ods'}
|
| 30 |
+
|
| 31 |
+
def allowed_file(filename):
|
| 32 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 33 |
+
|
| 34 |
+
def safe_flash_and_redirect(message, endpoint='index'):
|
| 35 |
+
"""Safely flash a message and redirect, handling any errors"""
|
| 36 |
+
try:
|
| 37 |
+
flash(message)
|
| 38 |
+
return redirect(url_for(endpoint))
|
| 39 |
+
except Exception as e:
|
| 40 |
+
logger.error(f"Error in flash_and_redirect: {e}")
|
| 41 |
+
# Fallback without flash if flash fails
|
| 42 |
+
return redirect(url_for(endpoint))
|
| 43 |
+
|
| 44 |
+
# Error handler for file too large
|
| 45 |
+
@app.errorhandler(RequestEntityTooLarge)
|
| 46 |
+
def handle_file_too_large(error):
|
| 47 |
+
logger.warning("File upload too large attempted")
|
| 48 |
+
return safe_flash_and_redirect('File is too large. Maximum size is 16MB.')
|
| 49 |
+
|
| 50 |
+
# Error handler for internal server errors (500)
|
| 51 |
+
@app.errorhandler(500)
|
| 52 |
+
def handle_internal_error(error):
|
| 53 |
+
logger.error(f"Internal server error: {error}")
|
| 54 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 55 |
+
return safe_flash_and_redirect('An internal server error occurred. Please try again.')
|
| 56 |
+
|
| 57 |
+
# Error handler for 404 errors
|
| 58 |
+
@app.errorhandler(404)
|
| 59 |
+
def handle_not_found(error):
|
| 60 |
+
logger.warning(f"404 error: {error}")
|
| 61 |
+
return safe_flash_and_redirect('Page not found.')
|
| 62 |
+
|
| 63 |
+
# Error handler for general exceptions
|
| 64 |
+
@app.errorhandler(Exception)
|
| 65 |
+
def handle_general_exception(error):
|
| 66 |
+
logger.error(f"Unhandled exception: {error}")
|
| 67 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 68 |
+
|
| 69 |
+
# Don't handle HTTP exceptions here (let Flask handle them)
|
| 70 |
+
if hasattr(error, 'code'):
|
| 71 |
+
return error
|
| 72 |
+
|
| 73 |
+
return safe_flash_and_redirect('An unexpected error occurred. Please try again.')
|
| 74 |
+
|
| 75 |
+
@app.route('/')
|
| 76 |
+
def index():
|
| 77 |
+
try:
|
| 78 |
+
return render_template_string(HTML_TEMPLATE)
|
| 79 |
+
except Exception as e:
|
| 80 |
+
logger.error(f"Error rendering index template: {e}")
|
| 81 |
+
return safe_flash_and_redirect('Error loading page. Please refresh.')
|
| 82 |
+
|
| 83 |
+
@app.route('/upload', methods=['POST'])
|
| 84 |
+
def upload_file():
|
| 85 |
+
try:
|
| 86 |
+
# Check if RAG_DV is available
|
| 87 |
+
if RAG_DV is None:
|
| 88 |
+
return safe_flash_and_redirect('RAG Document Viewer is not available. Please check the installation.')
|
| 89 |
+
|
| 90 |
+
# Validate request
|
| 91 |
+
if 'file' not in request.files:
|
| 92 |
+
return safe_flash_and_redirect('No file selected')
|
| 93 |
+
|
| 94 |
+
file = request.files['file']
|
| 95 |
+
if file.filename == '' or file.filename is None:
|
| 96 |
+
return safe_flash_and_redirect('No file selected')
|
| 97 |
+
|
| 98 |
+
if not allowed_file(file.filename):
|
| 99 |
+
return safe_flash_and_redirect('Invalid file type. Please upload a supported document.')
|
| 100 |
+
|
| 101 |
+
# Generate unique filename to avoid conflicts
|
| 102 |
+
filename = secure_filename(file.filename)
|
| 103 |
+
if not filename:
|
| 104 |
+
return safe_flash_and_redirect('Invalid filename')
|
| 105 |
+
|
| 106 |
+
unique_filename = f"{uuid.uuid4()}_{filename}"
|
| 107 |
+
|
| 108 |
+
# Create directory structure
|
| 109 |
+
dir_name = Path(str(unique_filename).replace(Path(unique_filename).suffix, ""))
|
| 110 |
+
dir_path = Path(app.config['UPLOAD_FOLDER']) / dir_name
|
| 111 |
+
|
| 112 |
+
try:
|
| 113 |
+
dir_path.mkdir(parents=True, exist_ok=True)
|
| 114 |
+
except OSError as e:
|
| 115 |
+
logger.error(f"Failed to create directory {dir_path}: {e}")
|
| 116 |
+
return safe_flash_and_redirect('Failed to create upload directory')
|
| 117 |
+
|
| 118 |
+
file_path = dir_path / unique_filename
|
| 119 |
+
|
| 120 |
+
# Save file with error handling
|
| 121 |
+
try:
|
| 122 |
+
file.save(str(file_path))
|
| 123 |
+
except Exception as e:
|
| 124 |
+
logger.error(f"Failed to save file {file_path}: {e}")
|
| 125 |
+
return safe_flash_and_redirect('Failed to save uploaded file')
|
| 126 |
+
|
| 127 |
+
# Verify file was saved
|
| 128 |
+
if not file_path.exists():
|
| 129 |
+
return safe_flash_and_redirect('File upload failed - file not saved')
|
| 130 |
+
|
| 131 |
+
# Process with RAG_DV
|
| 132 |
+
rag_output_path = file_path.parent / "rag_dv"
|
| 133 |
+
try:
|
| 134 |
+
RAG_DV(file_path, rag_output_path)
|
| 135 |
+
except Exception as e:
|
| 136 |
+
logger.error(f"RAG_DV processing failed: {e}")
|
| 137 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 138 |
+
|
| 139 |
+
# Clean up the uploaded file if processing fails
|
| 140 |
+
try:
|
| 141 |
+
file_path.unlink()
|
| 142 |
+
if dir_path.exists() and not any(dir_path.iterdir()):
|
| 143 |
+
dir_path.rmdir()
|
| 144 |
+
except Exception as cleanup_error:
|
| 145 |
+
logger.error(f"Failed to cleanup after RAG_DV error: {cleanup_error}")
|
| 146 |
+
|
| 147 |
+
return safe_flash_and_redirect('Failed to process document. Please try again or use a different file.')
|
| 148 |
+
|
| 149 |
+
# Verify RAG processing created the expected output
|
| 150 |
+
index_file = rag_output_path / "index.html"
|
| 151 |
+
if not index_file.exists():
|
| 152 |
+
return safe_flash_and_redirect('Document processing incomplete. Please try again.')
|
| 153 |
+
|
| 154 |
+
# Store in session
|
| 155 |
+
session['uploaded_file'] = unique_filename
|
| 156 |
+
session['original_filename'] = filename
|
| 157 |
+
|
| 158 |
+
return safe_flash_and_redirect('File successfully uploaded.')
|
| 159 |
+
|
| 160 |
+
except Exception as e:
|
| 161 |
+
logger.error(f"Unexpected error in upload_file: {e}")
|
| 162 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 163 |
+
return safe_flash_and_redirect('Upload failed due to an unexpected error')
|
| 164 |
+
|
| 165 |
+
@app.route('/refresh')
|
| 166 |
+
def refresh():
|
| 167 |
+
try:
|
| 168 |
+
# Clear session data
|
| 169 |
+
session.pop('uploaded_file', None)
|
| 170 |
+
session.pop('original_filename', None)
|
| 171 |
+
return redirect(url_for('index'))
|
| 172 |
+
except Exception as e:
|
| 173 |
+
logger.error(f"Error in refresh: {e}")
|
| 174 |
+
return safe_flash_and_redirect('Error refreshing session')
|
| 175 |
+
|
| 176 |
+
@app.route('/load/<filename>')
|
| 177 |
+
def load(filename):
|
| 178 |
+
try:
|
| 179 |
+
if not filename:
|
| 180 |
+
return safe_flash_and_redirect('Invalid filename')
|
| 181 |
+
|
| 182 |
+
# Sanitize filename
|
| 183 |
+
filename = secure_filename(filename)
|
| 184 |
+
file_path = Path(app.config['UPLOAD_FOLDER']) / Path(filename).stem / filename
|
| 185 |
+
|
| 186 |
+
if not file_path.exists():
|
| 187 |
+
logger.warning(f"Attempted to load non-existent file: {file_path}")
|
| 188 |
+
return safe_flash_and_redirect('File not found')
|
| 189 |
+
|
| 190 |
+
index_file = file_path.parent / "rag_dv" / "index.html"
|
| 191 |
+
if not index_file.exists():
|
| 192 |
+
logger.warning(f"RAG index file not found: {index_file}")
|
| 193 |
+
return safe_flash_and_redirect('Document index not found. Please re-upload the file.')
|
| 194 |
+
|
| 195 |
+
try:
|
| 196 |
+
with open(index_file, 'r', encoding='utf-8') as f:
|
| 197 |
+
content = f.read()
|
| 198 |
+
except Exception as e:
|
| 199 |
+
logger.error(f"Failed to read index file {index_file}: {e}")
|
| 200 |
+
return safe_flash_and_redirect('Failed to load document content')
|
| 201 |
+
|
| 202 |
+
return render_template_string(content)
|
| 203 |
+
|
| 204 |
+
except Exception as e:
|
| 205 |
+
logger.error(f"Unexpected error in load: {e}")
|
| 206 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 207 |
+
return safe_flash_and_redirect('Error loading document')
|
| 208 |
+
|
| 209 |
+
@app.route('/load/assets/<asset_type>/<filename>')
|
| 210 |
+
def load_assets(asset_type, filename):
|
| 211 |
+
try:
|
| 212 |
+
if 'uploaded_file' not in session:
|
| 213 |
+
return safe_flash_and_redirect('No active session')
|
| 214 |
+
|
| 215 |
+
# Sanitize inputs
|
| 216 |
+
asset_type = secure_filename(asset_type)
|
| 217 |
+
filename = secure_filename(filename)
|
| 218 |
+
|
| 219 |
+
if not asset_type or not filename:
|
| 220 |
+
return safe_flash_and_redirect('Invalid asset path')
|
| 221 |
+
|
| 222 |
+
file_path = (Path(app.config['UPLOAD_FOLDER']) /
|
| 223 |
+
Path(session['uploaded_file']).stem /
|
| 224 |
+
"rag_dv" / "assets" / asset_type / filename)
|
| 225 |
+
|
| 226 |
+
if not file_path.exists():
|
| 227 |
+
logger.warning(f"Asset not found: {file_path}")
|
| 228 |
+
return safe_flash_and_redirect('Asset not found')
|
| 229 |
+
|
| 230 |
+
# Security check - ensure file is within expected directory
|
| 231 |
+
upload_base = Path(app.config['UPLOAD_FOLDER']).resolve()
|
| 232 |
+
if not file_path.resolve().is_relative_to(upload_base):
|
| 233 |
+
logger.warning(f"Attempted path traversal: {file_path}")
|
| 234 |
+
return safe_flash_and_redirect('Invalid file path')
|
| 235 |
+
|
| 236 |
+
return send_from_directory(str(file_path.parent), filename)
|
| 237 |
+
|
| 238 |
+
except Exception as e:
|
| 239 |
+
logger.error(f"Error loading asset: {e}")
|
| 240 |
+
logger.error(f"Traceback: {traceback.format_exc()}")
|
| 241 |
+
return safe_flash_and_redirect('Error loading asset')
|
| 242 |
+
|
| 243 |
+
# Add new route for file size error
|
| 244 |
+
@app.route('/file_size_error')
|
| 245 |
+
def file_size_error():
|
| 246 |
+
flash('File size too large. Please upload a file smaller than 16MB.')
|
| 247 |
+
return redirect(url_for('index'))
|
| 248 |
+
|
| 249 |
+
# HTML Template
|
| 250 |
+
HTML_TEMPLATE = '''
|
| 251 |
+
<!DOCTYPE html>
|
| 252 |
+
<html lang="en">
|
| 253 |
+
<head>
|
| 254 |
+
<meta charset="UTF-8">
|
| 255 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 256 |
+
<title>File Upload Demo</title>
|
| 257 |
+
<style>
|
| 258 |
+
* {
|
| 259 |
+
margin: 0;
|
| 260 |
+
padding: 0;
|
| 261 |
+
box-sizing: border-box;
|
| 262 |
+
font-family: 'Helvetica Neue', Arial, sans-serif;
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
body {
|
| 266 |
+
background: -webkit-linear-gradient(135deg, hsla(0, 0%, 0%, 1) 0%, hsla(0, 0%, 11%, 1) 100%);
|
| 267 |
+
background: linear-gradient(135deg, hsla(0, 0%, 0%, 1) 0%, hsla(0, 0%, 11%, 1) 100%);
|
| 268 |
+
background-attachment: fixed;
|
| 269 |
+
min-height: 100vh;
|
| 270 |
+
position: relative;
|
| 271 |
+
color: #f3f3f3;
|
| 272 |
+
line-height: 1.6;
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
.container {
|
| 276 |
+
max-width: 800px;
|
| 277 |
+
margin: 0 auto;
|
| 278 |
+
padding: 20px;
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
/* Upload Area Styles */
|
| 282 |
+
.upload-container {
|
| 283 |
+
display: flex;
|
| 284 |
+
align-items: center;
|
| 285 |
+
justify-content: center;
|
| 286 |
+
min-height: 100vh;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
.upload-area {
|
| 290 |
+
width: 100%;
|
| 291 |
+
max-width: 500px;
|
| 292 |
+
height: 300px;
|
| 293 |
+
border: 2px dashed #4a5568;
|
| 294 |
+
border-radius: 8px;
|
| 295 |
+
background: rgba(255, 255, 255, 0.05);
|
| 296 |
+
display: flex;
|
| 297 |
+
flex-direction: column;
|
| 298 |
+
align-items: center;
|
| 299 |
+
justify-content: center;
|
| 300 |
+
cursor: pointer;
|
| 301 |
+
transition: all 0.2s ease;
|
| 302 |
+
position: relative;
|
| 303 |
+
overflow: hidden;
|
| 304 |
+
padding: 0 2rem;
|
| 305 |
+
margin: 0 1rem;
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
.upload-area:hover {
|
| 309 |
+
border-color: #6b7280;
|
| 310 |
+
background: rgba(255, 255, 255, 0.08);
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
.upload-area.dragover {
|
| 314 |
+
border-color: #9ca3af;
|
| 315 |
+
background: rgba(255, 255, 255, 0.1);
|
| 316 |
+
transform: translateY(-2px);
|
| 317 |
+
box-shadow: 0 4px 16px rgba(255, 255, 255, 0.1);
|
| 318 |
+
}
|
| 319 |
+
|
| 320 |
+
.upload-icon {
|
| 321 |
+
font-size: 48px;
|
| 322 |
+
color: #9ca3af;
|
| 323 |
+
margin-bottom: 20px;
|
| 324 |
+
}
|
| 325 |
+
|
| 326 |
+
.upload-text {
|
| 327 |
+
font-size: 18px;
|
| 328 |
+
color: #f3f3f3;
|
| 329 |
+
margin-bottom: 10px;
|
| 330 |
+
font-weight: 500;
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
.upload-subtext {
|
| 334 |
+
font-size: 14px;
|
| 335 |
+
color: #9ca3af;
|
| 336 |
+
text-align: center;
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
.file-input {
|
| 340 |
+
display: none;
|
| 341 |
+
}
|
| 342 |
+
|
| 343 |
+
/* Content Area Styles */
|
| 344 |
+
.content-container {
|
| 345 |
+
min-height: 90vh;
|
| 346 |
+
padding: 20px;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
.content-area {
|
| 350 |
+
width: 100%;
|
| 351 |
+
height: calc(100vh - 80px);
|
| 352 |
+
background: rgba(255, 255, 255, 0.05);
|
| 353 |
+
border-radius: 8px;
|
| 354 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.2);
|
| 355 |
+
overflow: hidden;
|
| 356 |
+
border: 1px solid #374151;
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
.content-iframe {
|
| 360 |
+
width: 100%;
|
| 361 |
+
height: 100%;
|
| 362 |
+
border: none;
|
| 363 |
+
background: rgba(255, 255, 255, 0.05);
|
| 364 |
+
display: block;
|
| 365 |
+
box-sizing: border-box;
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
+
/* Refresh Button Styles */
|
| 369 |
+
.refresh-btn {
|
| 370 |
+
font-family: helvetica neue;
|
| 371 |
+
font-weight: bold;
|
| 372 |
+
position: fixed;
|
| 373 |
+
top: 0.5rem;
|
| 374 |
+
left: 0.5rem;
|
| 375 |
+
background: #212529;
|
| 376 |
+
color: #fff;
|
| 377 |
+
border: 0px;
|
| 378 |
+
border-radius: 6px;
|
| 379 |
+
padding: 12px 20px;
|
| 380 |
+
font-size: 14px;
|
| 381 |
+
cursor: pointer;
|
| 382 |
+
backdrop-filter: blur(10px);
|
| 383 |
+
transition: all 0.2s ease;
|
| 384 |
+
opacity: 1;
|
| 385 |
+
z-index: 1000;
|
| 386 |
+
white-space: nowrap;
|
| 387 |
+
width: auto;
|
| 388 |
+
max-width: fit-content;
|
| 389 |
+
}
|
| 390 |
+
|
| 391 |
+
.refresh-btn:hover {
|
| 392 |
+
background: #424649;
|
| 393 |
+
border-color: #6b7280;
|
| 394 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
|
| 395 |
+
}
|
| 396 |
+
|
| 397 |
+
/* Flash Messages */
|
| 398 |
+
.flash-messages {
|
| 399 |
+
position: fixed;
|
| 400 |
+
top: 25px;
|
| 401 |
+
left: 50%;
|
| 402 |
+
transform: translateX(-50%);
|
| 403 |
+
z-index: 1001;
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
.flash-message {
|
| 407 |
+
background: rgba(0, 57, 150, 0.4);
|
| 408 |
+
color: #fff;
|
| 409 |
+
padding: 12px 20px;
|
| 410 |
+
border-radius: 6px;
|
| 411 |
+
margin-bottom: 10px;
|
| 412 |
+
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px 2px rgba(0, 0, 0, 0.2);
|
| 413 |
+
font-weight: 500;
|
| 414 |
+
backdrop-filter: blur(10px);
|
| 415 |
+
text-align: center;
|
| 416 |
+
}
|
| 417 |
+
|
| 418 |
+
/* Loading Animation */
|
| 419 |
+
.loading {
|
| 420 |
+
display: none;
|
| 421 |
+
position: absolute;
|
| 422 |
+
top: 50%;
|
| 423 |
+
left: 50%;
|
| 424 |
+
transform: translate(-50%, -50%);
|
| 425 |
+
}
|
| 426 |
+
|
| 427 |
+
.loading.active {
|
| 428 |
+
display: block;
|
| 429 |
+
}
|
| 430 |
+
|
| 431 |
+
.spinner {
|
| 432 |
+
width: 40px;
|
| 433 |
+
height: 40px;
|
| 434 |
+
border: 3px solid #4a5568;
|
| 435 |
+
border-top: 3px solid #f3f3f3;
|
| 436 |
+
border-radius: 50%;
|
| 437 |
+
animation: spin 1s linear infinite;
|
| 438 |
+
}
|
| 439 |
+
|
| 440 |
+
@keyframes spin {
|
| 441 |
+
0% { transform: rotate(0deg); }
|
| 442 |
+
100% { transform: rotate(360deg); }
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
/* Responsive Design */
|
| 446 |
+
@media (max-width: 768px) {
|
| 447 |
+
.upload-area {
|
| 448 |
+
height: 250px;
|
| 449 |
+
margin: 0 1rem;
|
| 450 |
+
padding: 0 1rem;
|
| 451 |
+
max-width: calc(100vw - 2rem);
|
| 452 |
+
}
|
| 453 |
+
|
| 454 |
+
.upload-text {
|
| 455 |
+
font-size: 16px;
|
| 456 |
+
text-align: center;
|
| 457 |
+
}
|
| 458 |
+
|
| 459 |
+
.upload-subtext {
|
| 460 |
+
font-size: 13px;
|
| 461 |
+
text-align: center;
|
| 462 |
+
}
|
| 463 |
+
|
| 464 |
+
.upload-container {
|
| 465 |
+
padding: 0 1rem;
|
| 466 |
+
}
|
| 467 |
+
|
| 468 |
+
.refresh-btn {
|
| 469 |
+
top: 10px;
|
| 470 |
+
left: 10px;
|
| 471 |
+
padding: 8px 12px;
|
| 472 |
+
font-size: 11px;
|
| 473 |
+
white-space: nowrap;
|
| 474 |
+
width: auto;
|
| 475 |
+
max-width: fit-content;
|
| 476 |
+
}
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
+
@media (max-width: 480px) {
|
| 480 |
+
.upload-area {
|
| 481 |
+
height: 220px;
|
| 482 |
+
margin: 0 0.5rem;
|
| 483 |
+
padding: 0 0.5rem;
|
| 484 |
+
max-width: calc(100vw - 1rem);
|
| 485 |
+
}
|
| 486 |
+
|
| 487 |
+
.upload-text {
|
| 488 |
+
font-size: 14px;
|
| 489 |
+
}
|
| 490 |
+
|
| 491 |
+
.upload-subtext {
|
| 492 |
+
font-size: 12px;
|
| 493 |
+
}
|
| 494 |
+
|
| 495 |
+
.upload-icon {
|
| 496 |
+
font-size: 36px;
|
| 497 |
+
margin-bottom: 15px;
|
| 498 |
+
}
|
| 499 |
+
|
| 500 |
+
.upload-container {
|
| 501 |
+
padding: 0 0.5rem;
|
| 502 |
+
}
|
| 503 |
+
}
|
| 504 |
+
</style>
|
| 505 |
+
</head>
|
| 506 |
+
<body>
|
| 507 |
+
<!-- Flash Messages -->
|
| 508 |
+
{% with messages = get_flashed_messages() %}
|
| 509 |
+
{% if messages %}
|
| 510 |
+
<div class="flash-messages">
|
| 511 |
+
{% for message in messages %}
|
| 512 |
+
<div class="flash-message">{{ message }}</div>
|
| 513 |
+
{% endfor %}
|
| 514 |
+
</div>
|
| 515 |
+
{% endif %}
|
| 516 |
+
{% endwith %}
|
| 517 |
+
|
| 518 |
+
<!-- Refresh Button (only visible after upload) -->
|
| 519 |
+
{% if session.uploaded_file %}
|
| 520 |
+
<button class="refresh-btn" onclick="refreshSession()">
|
| 521 |
+
Start over
|
| 522 |
+
</button>
|
| 523 |
+
{% endif %}
|
| 524 |
+
|
| 525 |
+
{% if not session.uploaded_file %}
|
| 526 |
+
<!-- Upload Interface -->
|
| 527 |
+
<h2 style="text-align: center; margin-top: 0.25rem;">Rag Document Viewer Demo</h2>
|
| 528 |
+
<div class="upload-container">
|
| 529 |
+
<form id="uploadForm" method="POST" action="/upload" enctype="multipart/form-data">
|
| 530 |
+
<div class="upload-area" id="uploadArea">
|
| 531 |
+
<div class="upload-icon">📂</div>
|
| 532 |
+
<div class="upload-text">Drop your file here or click to browse</div>
|
| 533 |
+
<div class="upload-subtext">Supports: PDFs, Office Documents, OpenOffice Documents <br>(Max: 16MB)</div>
|
| 534 |
+
<input type="file" name="file" class="file-input" id="fileInput" accept=".txt,.pdf,.doc,.docx,.ppt,.pptx,.xls,.xlsx,.odt,.odp,.ods">
|
| 535 |
+
<div class="loading" id="loading">
|
| 536 |
+
<div class="spinner"></div>
|
| 537 |
+
</div>
|
| 538 |
+
</div>
|
| 539 |
+
</form>
|
| 540 |
+
</div>
|
| 541 |
+
{% else %}
|
| 542 |
+
<!-- Content Interface -->
|
| 543 |
+
<h2 style="text-align: center; margin-top: 0.25rem;">Rag Document Viewer Demo</h2>
|
| 544 |
+
<div class="content-container">
|
| 545 |
+
<div class="content-area">
|
| 546 |
+
<div class="content-iframe">
|
| 547 |
+
<iframe src="{{ url_for('load', filename=session.uploaded_file) }}"
|
| 548 |
+
style="width: 100%; height: 100%; border: none; display: block;">
|
| 549 |
+
</iframe>
|
| 550 |
+
</div>
|
| 551 |
+
</div>
|
| 552 |
+
</div>
|
| 553 |
+
{% endif %}
|
| 554 |
+
|
| 555 |
+
<script>
|
| 556 |
+
// File upload handling
|
| 557 |
+
const uploadArea = document.getElementById('uploadArea');
|
| 558 |
+
const fileInput = document.getElementById('fileInput');
|
| 559 |
+
const uploadForm = document.getElementById('uploadForm');
|
| 560 |
+
const loading = document.getElementById('loading');
|
| 561 |
+
|
| 562 |
+
if (uploadArea) {
|
| 563 |
+
// Click to browse
|
| 564 |
+
uploadArea.addEventListener('click', () => {
|
| 565 |
+
fileInput.click();
|
| 566 |
+
});
|
| 567 |
+
|
| 568 |
+
// Drag and drop
|
| 569 |
+
uploadArea.addEventListener('dragover', (e) => {
|
| 570 |
+
e.preventDefault();
|
| 571 |
+
uploadArea.classList.add('dragover');
|
| 572 |
+
});
|
| 573 |
+
|
| 574 |
+
uploadArea.addEventListener('dragleave', () => {
|
| 575 |
+
uploadArea.classList.remove('dragover');
|
| 576 |
+
});
|
| 577 |
+
|
| 578 |
+
uploadArea.addEventListener('drop', (e) => {
|
| 579 |
+
e.preventDefault();
|
| 580 |
+
uploadArea.classList.remove('dragover');
|
| 581 |
+
|
| 582 |
+
const files = e.dataTransfer.files;
|
| 583 |
+
if (files.length > 0) {
|
| 584 |
+
const file = files[0];
|
| 585 |
+
const maxSize = 16 * 1024 * 1024; // 16MB
|
| 586 |
+
|
| 587 |
+
if (file.size > maxSize) {
|
| 588 |
+
// Redirect to file size error route instead of alert
|
| 589 |
+
window.location.href = '/file_size_error';
|
| 590 |
+
return;
|
| 591 |
+
}
|
| 592 |
+
|
| 593 |
+
fileInput.files = files;
|
| 594 |
+
submitForm();
|
| 595 |
+
}
|
| 596 |
+
});
|
| 597 |
+
|
| 598 |
+
// File input change
|
| 599 |
+
fileInput.addEventListener('change', () => {
|
| 600 |
+
if (fileInput.files.length > 0) {
|
| 601 |
+
const file = fileInput.files[0];
|
| 602 |
+
const maxSize = 16 * 1024 * 1024; // 16MB
|
| 603 |
+
|
| 604 |
+
if (file.size > maxSize) {
|
| 605 |
+
// Redirect to file size error route instead of alert
|
| 606 |
+
fileInput.value = ''; // Clear the file input
|
| 607 |
+
window.location.href = '/file_size_error';
|
| 608 |
+
return;
|
| 609 |
+
}
|
| 610 |
+
|
| 611 |
+
submitForm();
|
| 612 |
+
}
|
| 613 |
+
});
|
| 614 |
+
|
| 615 |
+
function submitForm() {
|
| 616 |
+
loading.classList.add('active');
|
| 617 |
+
uploadForm.submit();
|
| 618 |
+
}
|
| 619 |
+
}
|
| 620 |
+
|
| 621 |
+
// Refresh session function
|
| 622 |
+
function refreshSession() {
|
| 623 |
+
window.location.href = '/refresh';
|
| 624 |
+
}
|
| 625 |
+
|
| 626 |
+
// Auto-hide flash messages after 3 seconds
|
| 627 |
+
setTimeout(() => {
|
| 628 |
+
const flashMessages = document.querySelector('.flash-messages');
|
| 629 |
+
if (flashMessages) {
|
| 630 |
+
flashMessages.style.opacity = '0';
|
| 631 |
+
flashMessages.style.transition = 'opacity 0.5s ease';
|
| 632 |
+
setTimeout(() => {
|
| 633 |
+
flashMessages.style.display = 'none';
|
| 634 |
+
}, 500);
|
| 635 |
+
}
|
| 636 |
+
}, 5000);
|
| 637 |
+
</script>
|
| 638 |
+
</body>
|
| 639 |
+
</html>
|
| 640 |
+
'''
|
| 641 |
+
|
| 642 |
+
if __name__ == '__main__':
|
| 643 |
+
app.run(debug=True)
|