Spaces:
Running
Running
Download server.py from Preprocess/RAG_DV_DEMO: direct link, hf CLI and curl.
- Browser
- Download file 30.3 kB
-
https://huggingface.co/spaces/Preprocess/RAG_DV_DEMO/resolve/main/server.py
- Command line
-
hf download hf://spaces/Preprocess/RAG_DV_DEMO/server.py
-
curl -L -o server.py https://huggingface.co/spaces/Preprocess/RAG_DV_DEMO/resolve/main/server.py
30.3 kB
| """ | |
| FastAPI-sync + Gradio with AJAX (all route handlers are now synchronous with JSON responses) | |
| """ | |
| import logging | |
| import os | |
| import tempfile | |
| import traceback | |
| import uuid | |
| from pathlib import Path | |
| from typing import List, Dict, Any | |
| import gradio as gr | |
| from fastapi import FastAPI, File, HTTPException, Request, Response, UploadFile, status | |
| from fastapi.responses import HTMLResponse, RedirectResponse, FileResponse, JSONResponse | |
| from starlette.middleware.sessions import SessionMiddleware | |
| from starlette.responses import PlainTextResponse | |
| from werkzeug.utils import secure_filename # type: ignore | |
| try: | |
| from rag_document_viewer import RAG_DV | |
| except ImportError as err: | |
| print(f"Warning: Could not import RAG_DV: {err}") | |
| RAG_DV = None | |
| # --------------------------------------------------------------------------- # | |
| # Config / constants # | |
| # --------------------------------------------------------------------------- # | |
| SECRET_KEY = ( | |
| "a&VhLelkAo!dKXm9o5RRHQ@#BxoI3Q5378qwFP&aJKA#PLjO7TU*Aq5Kwg4OTdMI7N3%wFAmnwezlEbPUdEQKhGJD10E8@0gSrS" | |
| ) | |
| MAX_CONTENT_LENGTH = 16 * 1024 * 1024 # 16 MB | |
| ALLOWED_EXTENSIONS = { | |
| "txt", | |
| "pdf", | |
| "doc", | |
| "docx", | |
| "ppt", | |
| "pptx", | |
| "xls", | |
| "xlsx", | |
| "odt", | |
| "odp", | |
| "ods", | |
| } | |
| UPLOADS_DIR = ( | |
| Path(tempfile.gettempdir()) / "uploads" | |
| if os.getenv("SPACE_ID") | |
| else Path("uploads") | |
| ) | |
| UPLOADS_DIR.mkdir(exist_ok=True, parents=True) | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger("rag_dv_demo") | |
| # --------------------------------------------------------------------------- # | |
| # Helpers # | |
| # --------------------------------------------------------------------------- # | |
| def _allowed_file(name: str) -> bool: | |
| return "." in name and name.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS | |
| def _json_response(success: bool = True, message: str = "", data: Dict[Any, Any] = None) -> JSONResponse: | |
| """Helper to create consistent JSON responses""" | |
| return JSONResponse({ | |
| "success": success, | |
| "message": message, | |
| "data": data or {} | |
| }) | |
| def _rewrite_html_urls(content: str, base_url: str, story_id: str) -> str: | |
| """Rewrite relative URLs in HTML content to use absolute URLs""" | |
| import re | |
| logger.info(f"Rewriting URLs with base: {base_url}, story_id: {story_id}") | |
| # Patterns to match various asset references | |
| patterns = [ | |
| # src="assets/..." -> src="[base_url]/..." | |
| (r'src="assets/', f'src="{base_url}/'), | |
| # href="assets/..." -> href="[base_url]/..." | |
| (r'href="assets/', f'href="{base_url}/'), | |
| # CSS url(assets/...) -> url([base_url]/...) | |
| (r'url\(assets/', f'url({base_url}/'), | |
| (r'url\("assets/', f'url("{base_url}/'), | |
| (r"url\('assets/", f"url('{base_url}/"), | |
| # Background images and other asset references | |
| (r'background-image:\s*url\(assets/', f'background-image: url({base_url}/'), | |
| (r'background:\s*url\(assets/', f'background: url({base_url}/'), | |
| # Script src | |
| (r'<script[^>]*src="assets/', f'<script src="{base_url}/'), | |
| # Link href (stylesheets, etc.) | |
| (r'<link[^>]*href="assets/', f'<link href="{base_url}/'), | |
| ] | |
| # Apply all patterns | |
| for pattern, replacement in patterns: | |
| old_content = content | |
| content = re.sub(pattern, replacement, content, flags=re.IGNORECASE) | |
| if content != old_content: | |
| logger.info(f"Applied pattern: {pattern}") | |
| # Add base tag if not present | |
| if '<base' not in content.lower(): | |
| base_tag = f'<base href="{base_url}/">' | |
| if '<head>' in content.lower(): | |
| content = re.sub(r'<head>', f'<head>\n {base_tag}', content, flags=re.IGNORECASE) | |
| else: | |
| # If no head tag, add it | |
| content = re.sub(r'<html[^>]*>', f'<html>\n<head>\n {base_tag}\n</head>', content, flags=re.IGNORECASE) | |
| return content | |
| # --------------------------------------------------------------------------- # | |
| # Template # | |
| # --------------------------------------------------------------------------- # | |
| from jinja2 import Template # noqa | |
| HTML_TEMPLATE = """ | |
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <title>File Upload Demo</title> | |
| <style> | |
| * { | |
| margin: 0; | |
| padding: 0; | |
| box-sizing: border-box; | |
| font-family: 'Helvetica Neue', Arial, sans-serif; | |
| } | |
| body { | |
| background: -webkit-linear-gradient(135deg, hsla(0, 0%, 0%, 1) 0%, hsla(0, 0%, 11%, 1) 100%); | |
| background: linear-gradient(135deg, hsla(0, 0%, 0%, 1) 0%, hsla(0, 0%, 11%, 1) 100%); | |
| background-attachment: fixed; | |
| min-height: 100vh; | |
| position: relative; | |
| color: #f3f3f3; | |
| line-height: 1.6; | |
| } | |
| .container { | |
| max-width: 800px; | |
| margin: 0 auto; | |
| padding: 20px; | |
| } | |
| /* Upload Area Styles */ | |
| .upload-container { | |
| display: flex; | |
| flex-direction: column; | |
| align-items: center; | |
| justify-content: center; | |
| min-height: 100vh; | |
| } | |
| .upload-area { | |
| width: 100%; | |
| max-width: 500px; | |
| height: 300px; | |
| border: 2px dashed #4a5568; | |
| border-radius: 8px; | |
| background: rgba(255, 255, 255, 0.05); | |
| display: flex; | |
| flex-direction: column; | |
| align-items: center; | |
| justify-content: center; | |
| cursor: pointer; | |
| transition: all 0.2s ease; | |
| position: relative; | |
| overflow: hidden; | |
| padding: 0 2rem; | |
| margin: 0 1rem; | |
| } | |
| .upload-area:hover { | |
| border-color: #6b7280; | |
| background: rgba(255, 255, 255, 0.08); | |
| } | |
| .upload-area.dragover { | |
| border-color: #9ca3af; | |
| background: rgba(255, 255, 255, 0.1); | |
| transform: translateY(-2px); | |
| box-shadow: 0 4px 16px rgba(255, 255, 255, 0.1); | |
| } | |
| .upload-area.uploading { | |
| pointer-events: none; | |
| opacity: 0.7; | |
| } | |
| .upload-icon { | |
| font-size: 48px; | |
| color: #9ca3af; | |
| margin-bottom: 20px; | |
| } | |
| .upload-text { | |
| font-size: 18px; | |
| color: #f3f3f3; | |
| margin-bottom: 10px; | |
| font-weight: 500; | |
| } | |
| .upload-subtext { | |
| font-size: 14px; | |
| color: #9ca3af; | |
| text-align: center; | |
| } | |
| .file-input { | |
| display: none; | |
| } | |
| /* Content Area Styles */ | |
| .content-container { | |
| min-height: 90vh; | |
| padding: 20px; | |
| display: none; | |
| } | |
| .content-container.active { | |
| display: block; | |
| } | |
| .content-area { | |
| width: 100%; | |
| height: calc(100vh - 80px); | |
| background: rgba(255, 255, 255, 0.05); | |
| border-radius: 8px; | |
| box-shadow: 0 1px 3px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.2); | |
| overflow: hidden; | |
| border: 1px solid #374151; | |
| } | |
| .content-iframe { | |
| width: 100%; | |
| height: 100%; | |
| border: none; | |
| background: rgba(255, 255, 255, 0.05); | |
| display: block; | |
| box-sizing: border-box; | |
| } | |
| /* Refresh Button Styles */ | |
| .refresh-btn { | |
| font-family: helvetica neue; | |
| font-weight: bold; | |
| position: fixed; | |
| top: 0.5rem; | |
| left: 0.5rem; | |
| background: #212529; | |
| color: #fff; | |
| border: 0px; | |
| border-radius: 6px; | |
| padding: 12px 20px; | |
| font-size: 14px; | |
| cursor: pointer; | |
| backdrop-filter: blur(10px); | |
| transition: all 0.2s ease; | |
| opacity: 0; | |
| z-index: 1000; | |
| white-space: nowrap; | |
| width: auto; | |
| max-width: fit-content; | |
| pointer-events: none; | |
| } | |
| .refresh-btn.visible { | |
| opacity: 1; | |
| pointer-events: all; | |
| } | |
| .refresh-btn:hover { | |
| background: #424649; | |
| border-color: #6b7280; | |
| box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3); | |
| } | |
| /* Message Styles */ | |
| .message { | |
| position: fixed; | |
| top: 25px; | |
| left: 50%; | |
| transform: translateX(-50%); | |
| z-index: 1001; | |
| background: rgba(0, 57, 150, 0.4); | |
| color: #fff; | |
| padding: 12px 20px; | |
| border-radius: 6px; | |
| box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px 2px rgba(0, 0, 0, 0.2); | |
| font-weight: 500; | |
| backdrop-filter: blur(10px); | |
| text-align: center; | |
| opacity: 0; | |
| transition: opacity 0.3s ease; | |
| pointer-events: none; | |
| } | |
| .message.visible { | |
| opacity: 1; | |
| } | |
| /* Loading Animation */ | |
| .loading { | |
| display: none; | |
| position: absolute; | |
| top: 50%; | |
| left: 50%; | |
| transform: translate(-50%, -50%); | |
| } | |
| .loading.active { | |
| display: block; | |
| } | |
| .spinner { | |
| width: 40px; | |
| height: 40px; | |
| border: 3px solid #4a5568; | |
| border-top: 3px solid #f3f3f3; | |
| border-radius: 50%; | |
| animation: spin 1s linear infinite; | |
| } | |
| @keyframes spin { | |
| 0% { transform: rotate(0deg); } | |
| 100% { transform: rotate(360deg); } | |
| } | |
| /* Progress Bar */ | |
| .progress-container { | |
| position: absolute; | |
| bottom: 20px; | |
| left: 20px; | |
| right: 20px; | |
| height: 4px; | |
| background: rgba(255, 255, 255, 0.1); | |
| border-radius: 2px; | |
| overflow: hidden; | |
| display: none; | |
| } | |
| .progress-container.active { | |
| display: block; | |
| } | |
| .progress-bar { | |
| height: 100%; | |
| background: linear-gradient(90deg, #4f46e5, #7c3aed); | |
| width: 0%; | |
| transition: width 0.3s ease; | |
| border-radius: 2px; | |
| } | |
| /* Responsive Design */ | |
| @media (max-width: 768px) { | |
| .upload-area { | |
| height: 250px; | |
| margin: 0 1rem; | |
| padding: 0 1rem; | |
| max-width: calc(100vw - 2rem); | |
| } | |
| .upload-text { | |
| font-size: 16px; | |
| text-align: center; | |
| } | |
| .upload-subtext { | |
| font-size: 13px; | |
| text-align: center; | |
| } | |
| .upload-container { | |
| padding: 0 1rem; | |
| } | |
| .refresh-btn { | |
| top: 10px; | |
| left: 10px; | |
| padding: 8px 12px; | |
| font-size: 11px; | |
| white-space: nowrap; | |
| width: auto; | |
| max-width: fit-content; | |
| } | |
| } | |
| @media (max-width: 480px) { | |
| .upload-area { | |
| height: 220px; | |
| margin: 0 0.5rem; | |
| padding: 0 0.5rem; | |
| max-width: calc(100vw - 1rem); | |
| } | |
| .upload-text { | |
| font-size: 14px; | |
| } | |
| .upload-subtext { | |
| font-size: 12px; | |
| } | |
| .upload-icon { | |
| font-size: 36px; | |
| margin-bottom: 15px; | |
| } | |
| .upload-container { | |
| padding: 0 0.5rem; | |
| } | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <!-- Message Display --> | |
| <div class="message" id="message"></div> | |
| <!-- Refresh Button --> | |
| <button class="refresh-btn" id="refreshBtn" onclick="refreshSession()"> | |
| Start over | |
| </button> | |
| <!-- Upload Interface --> | |
| <div class="upload-container" id="uploadContainer"> | |
| <h2 style="text-align: center; position: absolute; top: 1rem;">Rag Document Viewer Demo</h2> | |
| <div class="upload-area" id="uploadArea"> | |
| <div class="upload-icon">📂</div> | |
| <div class="upload-text">Drop your file here or click to browse</div> | |
| <div class="upload-subtext">Supports: PDFs, Office Documents, OpenOffice Documents <br>(Max: 16MB)</div> | |
| <input type="file" class="file-input" id="fileInput" accept=".txt,.pdf,.doc,.docx,.ppt,.pptx,.xls,.xlsx,.odt,.odp,.ods"> | |
| <div class="loading" id="loading"> | |
| <div class="spinner"></div> | |
| </div> | |
| <div class="progress-container" id="progressContainer"> | |
| <div class="progress-bar" id="progressBar"></div> | |
| </div> | |
| </div> | |
| </div> | |
| <!-- Content Interface --> | |
| <div class="content-container" id="contentContainer"> | |
| <h2 style="text-align: center; margin-top: 0.25rem;">Rag Document Viewer Demo</h2> | |
| <div class="content-area"> | |
| <div class="content-iframe"> | |
| <iframe id="contentFrame" style="width: 100%; height: 100%; border: none; display: block;"> | |
| </iframe> | |
| </div> | |
| </div> | |
| </div> | |
| <script> | |
| class FileUploader { | |
| constructor() { | |
| this.uploadArea = document.getElementById('uploadArea'); | |
| this.fileInput = document.getElementById('fileInput'); | |
| this.loading = document.getElementById('loading'); | |
| this.progressContainer = document.getElementById('progressContainer'); | |
| this.progressBar = document.getElementById('progressBar'); | |
| this.uploadContainer = document.getElementById('uploadContainer'); | |
| this.contentContainer = document.getElementById('contentContainer'); | |
| this.contentFrame = document.getElementById('contentFrame'); | |
| this.refreshBtn = document.getElementById('refreshBtn'); | |
| this.messageEl = document.getElementById('message'); | |
| this.maxFileSize = 16 * 1024 * 1024; // 16MB | |
| this.isUploading = false; | |
| this.initEventListeners(); | |
| this.checkSession(); | |
| } | |
| initEventListeners() { | |
| // Click to browse | |
| this.uploadArea.addEventListener('click', () => { | |
| if (!this.isUploading) { | |
| this.fileInput.click(); | |
| } | |
| }); | |
| // Drag and drop | |
| this.uploadArea.addEventListener('dragover', (e) => { | |
| e.preventDefault(); | |
| if (!this.isUploading) { | |
| this.uploadArea.classList.add('dragover'); | |
| } | |
| }); | |
| this.uploadArea.addEventListener('dragleave', () => { | |
| this.uploadArea.classList.remove('dragover'); | |
| }); | |
| this.uploadArea.addEventListener('drop', (e) => { | |
| e.preventDefault(); | |
| this.uploadArea.classList.remove('dragover'); | |
| if (this.isUploading) return; | |
| const files = e.dataTransfer.files; | |
| if (files.length > 0) { | |
| this.handleFileSelection(files[0]); | |
| } | |
| }); | |
| // File input change | |
| this.fileInput.addEventListener('change', () => { | |
| if (this.fileInput.files.length > 0 && !this.isUploading) { | |
| this.handleFileSelection(this.fileInput.files[0]); | |
| } | |
| }); | |
| } | |
| async checkSession() { | |
| try { | |
| const response = await fetch('/api/session'); | |
| const result = await response.json(); | |
| if (result.success && result.data.has_uploaded_file) { | |
| this.showContentInterface(result.data.filename); | |
| } | |
| } catch (error) { | |
| console.error('Session check failed:', error); | |
| } | |
| } | |
| validateFile(file) { | |
| // Check file size | |
| if (file.size > this.maxFileSize) { | |
| this.showMessage('File size too large. Please upload a file smaller than 16MB.'); | |
| return false; | |
| } | |
| // Check file type | |
| const allowedExtensions = ['txt', 'pdf', 'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx', 'odt', 'odp', 'ods']; | |
| const fileExtension = file.name.split('.').pop().toLowerCase(); | |
| if (!allowedExtensions.includes(fileExtension)) { | |
| this.showMessage('Invalid file type. Please upload a supported document.'); | |
| return false; | |
| } | |
| return true; | |
| } | |
| async handleFileSelection(file) { | |
| if (!this.validateFile(file)) { | |
| this.fileInput.value = ''; | |
| return; | |
| } | |
| await this.uploadFile(file); | |
| } | |
| async uploadFile(file) { | |
| this.isUploading = true; | |
| this.uploadArea.classList.add('uploading'); | |
| this.loading.classList.add('active'); | |
| this.progressContainer.classList.add('active'); | |
| const formData = new FormData(); | |
| formData.append('file', file); | |
| try { | |
| const xhr = new XMLHttpRequest(); | |
| return new Promise((resolve, reject) => { | |
| xhr.upload.addEventListener('progress', (e) => { | |
| if (e.lengthComputable) { | |
| const percentComplete = (e.loaded / e.total) * 100; | |
| this.progressBar.style.width = percentComplete + '%'; | |
| } | |
| }); | |
| xhr.addEventListener('load', () => { | |
| if (xhr.status === 200) { | |
| const result = JSON.parse(xhr.responseText); | |
| if (result.success) { | |
| this.showMessage(result.message); | |
| setTimeout(() => { | |
| this.showContentInterface(result.data.filename); | |
| }, 1000); | |
| } else { | |
| this.showMessage(result.message); | |
| this.resetUploadState(); | |
| } | |
| resolve(result); | |
| } else { | |
| reject(new Error('Upload failed')); | |
| } | |
| }); | |
| xhr.addEventListener('error', () => { | |
| reject(new Error('Upload failed')); | |
| }); | |
| xhr.open('POST', '/api/upload'); | |
| xhr.send(formData); | |
| }); | |
| } catch (error) { | |
| console.error('Upload error:', error); | |
| this.showMessage('Upload failed. Please try again.'); | |
| this.resetUploadState(); | |
| } | |
| } | |
| resetUploadState() { | |
| this.isUploading = false; | |
| this.uploadArea.classList.remove('uploading'); | |
| this.loading.classList.remove('active'); | |
| this.progressContainer.classList.remove('active'); | |
| this.progressBar.style.width = '0%'; | |
| this.fileInput.value = ''; | |
| } | |
| showContentInterface(filename) { | |
| this.uploadContainer.style.display = 'none'; | |
| this.contentContainer.classList.add('active'); | |
| this.refreshBtn.classList.add('visible'); | |
| this.contentFrame.src = `/load/${filename}`; | |
| } | |
| showUploadInterface() { | |
| this.uploadContainer.style.display = 'flex'; | |
| this.contentContainer.classList.remove('active'); | |
| this.refreshBtn.classList.remove('visible'); | |
| this.resetUploadState(); | |
| } | |
| showMessage(message) { | |
| this.messageEl.textContent = message; | |
| this.messageEl.className = `message visible`; | |
| setTimeout(() => { | |
| this.messageEl.classList.remove('visible'); | |
| }, 5000); | |
| } | |
| } | |
| // Initialize uploader | |
| const uploader = new FileUploader(); | |
| // Refresh session function | |
| async function refreshSession() { | |
| try { | |
| const response = await fetch('/api/refresh', { method: 'POST' }); | |
| const result = await response.json(); | |
| if (result.success) { | |
| uploader.showMessage(result.message); | |
| setTimeout(() => { | |
| uploader.showUploadInterface(); | |
| }, 1000); | |
| } else { | |
| uploader.showMessage(result.message); | |
| } | |
| } catch (error) { | |
| console.error('Refresh failed:', error); | |
| uploader.showMessage('Refresh failed. Please try again.'); | |
| } | |
| } | |
| </script> | |
| </body> | |
| </html> | |
| """ | |
| template = Template(HTML_TEMPLATE) | |
| # --------------------------------------------------------------------------- # | |
| # FastAPI (synchronous with AJAX endpoints) # | |
| # --------------------------------------------------------------------------- # | |
| backend = FastAPI() | |
| backend.add_middleware( | |
| SessionMiddleware, | |
| secret_key=SECRET_KEY, | |
| same_site="none", # HF iframe needs SameSite=None | |
| https_only=True, | |
| ) | |
| def index(request: Request): | |
| return HTMLResponse(template.render()) | |
| def get_session_info(request: Request): | |
| """Get current session information""" | |
| has_uploaded_file = "uploaded_file" in request.session | |
| filename = request.session.get("uploaded_file", "") | |
| original_filename = request.session.get("original_filename", "") | |
| return _json_response( | |
| success=True, | |
| data={ | |
| "has_uploaded_file": has_uploaded_file, | |
| "filename": filename, | |
| "original_filename": original_filename | |
| } | |
| ) | |
| def upload_file_ajax(request: Request, file: UploadFile = File(...)): | |
| if RAG_DV is None: | |
| return _json_response( | |
| success=False, | |
| message="RAG Document Viewer is not available. Please check installation." | |
| ) | |
| if not file.filename or not _allowed_file(file.filename): | |
| return _json_response( | |
| success=False, | |
| message="Invalid file type. Please upload a supported document." | |
| ) | |
| # Check file size | |
| file.file.seek(0, 2) # go to end | |
| size = file.file.tell() | |
| file.file.seek(0) | |
| if size > MAX_CONTENT_LENGTH: | |
| return _json_response( | |
| success=False, | |
| message="File is too large. Maximum size is 16MB." | |
| ) | |
| filename = secure_filename(file.filename) | |
| if not filename: | |
| return _json_response(success=False, message="Invalid filename") | |
| unique_filename = f"{uuid.uuid4()}_{filename}" | |
| dir_path = UPLOADS_DIR / Path(unique_filename).stem | |
| dir_path.mkdir(parents=True, exist_ok=True) | |
| filepath = dir_path / unique_filename | |
| # Save upload synchronously | |
| try: | |
| with open(filepath, "wb") as f_out: | |
| f_out.write(file.file.read()) | |
| except Exception as err: | |
| logger.error(f"Failed saving upload: {err}") | |
| return _json_response(success=False, message="Failed to save uploaded file") | |
| # RAG_DV processing | |
| out_path = filepath.parent / "rag_dv" | |
| try: | |
| RAG_DV(filepath, out_path) | |
| except Exception as err: | |
| logger.error(f"RAG_DV failed: {err}\n{traceback.format_exc()}") | |
| filepath.unlink(missing_ok=True) | |
| if not any(dir_path.iterdir()): | |
| dir_path.rmdir() | |
| return _json_response( | |
| success=False, | |
| message="Failed to process document. Please try again." | |
| ) | |
| if not (out_path / "index.html").exists(): | |
| return _json_response(success=False, message="Document processing incomplete.") | |
| request.session["uploaded_file"] = unique_filename | |
| request.session["original_filename"] = filename | |
| return _json_response( | |
| success=True, | |
| message="File successfully uploaded.", | |
| data={"filename": unique_filename, "original_filename": filename} | |
| ) | |
| def refresh_session_ajax(request: Request): | |
| request.session.pop("uploaded_file", None) | |
| request.session.pop("original_filename", None) | |
| return _json_response(success=True, message="Started over successfully.") | |
| def load(request: Request, filename: str): | |
| if filename.startswith("assets"): | |
| asset_path = filename.replace("assets/", "") | |
| if "uploaded_file" not in request.session: | |
| logger.error("No active session for asset request") | |
| raise HTTPException(status_code=404, detail="No active session") | |
| # Log the request for debugging | |
| logger.info(f"Asset request: {asset_path}") | |
| logger.info(f"Uploaded file: {request.session.get('uploaded_file')}") | |
| uploaded_file = request.session["uploaded_file"] | |
| story_id = Path(uploaded_file).stem # This is our story_id | |
| base_dir = UPLOADS_DIR / story_id / "rag_dv" / "assets" | |
| # Construct the full file path | |
| file_path = base_dir / asset_path | |
| logger.info(f"Story ID: {story_id}") | |
| logger.info(f"Looking for asset at: {file_path}") | |
| logger.info(f"Base directory exists: {base_dir.exists()}") | |
| if base_dir.exists(): | |
| logger.info(f"Contents of assets dir: {list(base_dir.rglob('*'))}") | |
| if not file_path.exists() or not file_path.is_file(): | |
| logger.error(f"Asset not found: {file_path}") | |
| raise HTTPException(status_code=404, detail=f"Asset not found: {asset_path}") | |
| # Set appropriate content type based on file extension | |
| from mimetypes import guess_type | |
| content_type, _ = guess_type(str(file_path)) | |
| return FileResponse( | |
| file_path, | |
| media_type=content_type, | |
| headers={ | |
| "Cache-Control": "public, max-age=3600", # Cache for 1 hour | |
| "X-Story-ID": story_id # Custom header for debugging | |
| } | |
| ) | |
| else: | |
| filename = secure_filename(filename) | |
| filepath = UPLOADS_DIR / Path(filename).stem / filename | |
| idx_file = filepath.parent / "rag_dv" / "index.html" | |
| if not idx_file.exists(): | |
| raise HTTPException(status_code=404, detail="Document index not found.") | |
| logger.info(f"Loading document: {filename}") | |
| # Get the story_id (filename without extension and UUID prefix) | |
| story_id = Path(filename).stem | |
| # Construct base URL for assets | |
| base_url = str(request.base_url).rstrip('/') + f"/load" | |
| with open(idx_file, "r", encoding="utf-8") as f: | |
| content = f.read() | |
| # Rewrite URLs to use absolute paths | |
| # content = _rewrite_html_urls(content, base_url, story_id) | |
| logger.info(f"Document loaded with base URL: {base_url}") | |
| return HTMLResponse(content) | |
| # ----------------------------- Error handling ------------------------------ # | |
| def http_exception_handler(request: Request, exc: HTTPException): | |
| if exc.status_code == status.HTTP_413_REQUEST_ENTITY_TOO_LARGE: | |
| return _json_response( | |
| success=False, | |
| message="File is too large. Maximum size is 16MB." | |
| ) | |
| return JSONResponse( | |
| {"success": False, "message": str(exc.detail)}, | |
| status_code=exc.status_code | |
| ) | |
| def unhandled_exception(request: Request, exc: Exception): | |
| logger.error(f"Unhandled error: {exc}\n{traceback.format_exc()}") | |
| return _json_response( | |
| success=False, | |
| message="An unexpected error occurred. Please try again." | |
| ) | |
| # --------------------------------------------------------------------------- # | |
| # Gradio mounting (Blocks path = /gradio) # | |
| # --------------------------------------------------------------------------- # | |
| blocks = gr.Blocks() # no visual elements needed | |
| app = gr.mount_gradio_app(backend, blocks, path="/gradio") | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run( | |
| "server:app", | |
| host="0.0.0.0", | |
| port=7860, | |
| reload=True, | |
| ) |