~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
Improve PDF upload functionality and search strategy
LearningCircuite0660dd0da5e
diff --git a/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.py b/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.pyindex 522588a2e0..dee76d104c 100644--- a/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.py+++ b/src/local_deep_research/advanced_search_system/strategies/source_based_strategy.py@@ -166,6 +166,22 @@ class SourceBasedSearchStrategy(BaseSearchStrategy):# For first iteration, use initial queryif iteration == 1:+ # Check if user query is too long for direct search+ max_query_length = self.get_setting(+ "app.max_user_query_length", 300+ )+ original_search_original_query = self.search_original_query++ if (+ self.search_original_query+ and len(query.strip()) > max_query_length+ ):+ logger.warning(+ f"Long user query detected ({len(query.strip())} chars > {max_query_length} limit), "+ "using LLM questions only for search"+ )+ self.search_original_query = False+# Generate questions for first iterationcontext = (f"""Iteration: {iteration} of {iterations_to_run}"""@@ -197,6 +213,19 @@ class SourceBasedSearchStrategy(BaseSearchStrategy):logger.info(f"Using questions for iteration {iteration}: {all_questions}")++ # Restore original search_original_query setting after first iteration+ if (+ original_search_original_query+ != self.search_original_query+ ):+ self.search_original_query = (+ original_search_original_query+ )+ logger.debug(+ "Restored original search_original_query setting after first iteration"+ )+else:# For subsequent iterations, generate questions based on previous search resultssource_context = self._format_search_results_as_context(diff --git a/src/local_deep_research/defaults/settings_search_config.json b/src/local_deep_research/defaults/settings_search_config.jsonnew file mode 100644index 0000000000..8acc580cbf--- /dev/null+++ b/src/local_deep_research/defaults/settings_search_config.json@@ -0,0 +1,14 @@+{+ "app.max_user_query_length": {+ "category": "search_parameters",+ "description": "Maximum character length for user queries before disabling direct search. Longer queries will use LLM-generated questions only.",+ "editable": true,+ "max_value": 1000,+ "min_value": 50,+ "name": "Max User Query Length",+ "type": "SEARCH",+ "ui_element": "number",+ "value": 300,+ "visible": true+ }+}diff --git a/src/local_deep_research/web/routes/research_routes.py b/src/local_deep_research/web/routes/research_routes.pyindex cf4508f39a..f00eb8a13f 100644--- a/src/local_deep_research/web/routes/research_routes.py+++ b/src/local_deep_research/web/routes/research_routes.py@@ -4,6 +4,7 @@ import platformimport subprocessfrom datetime import datetime, UTCfrom pathlib import Path+import pdfplumberfrom flask import (Blueprint,@@ -1473,3 +1474,103 @@ def get_queue_position(research_id):return jsonify({"status": "error", "message": "Failed to process request"}), 500+++@research_bp.route("/api/upload/pdf", methods=["POST"])+@login_required+def upload_pdf():+ """Upload and extract text from PDF files"""+ username = session.get("username")+ if not username:+ return jsonify({"error": "Not authenticated"}), 401++ try:+ # Check if files are present in the request+ if "files" not in request.files:+ return jsonify({"error": "No files provided"}), 400++ files = request.files.getlist("files")+ if not files or files[0].filename == "":+ return jsonify({"error": "No files selected"}), 400++ extracted_texts = []+ total_files = len(files)+ processed_files = 0+ errors = []++ for file in files:+ if file and file.filename.lower().endswith(".pdf"):+ try:+ # Read file content+ pdf_content = file.read()++ # Basic validation - check if it's actually a PDF+ if not pdf_content.startswith(b"%PDF"):+ errors.append(f"{file.filename}: Not a valid PDF file")+ continue++ # Extract text using pdfplumber+ text = ""+ with pdfplumber.open(io.BytesIO(pdf_content)) as pdf:+ for page in pdf.pages:+ page_text = page.extract_text()+ if page_text:+ text += page_text + "\n"++ if not text.strip():+ errors.append(+ f"{file.filename}: No extractable text found"+ )+ continue++ extracted_texts.append(+ {+ "filename": file.filename,+ "text": text.strip(),+ "size": len(pdf_content),+ "pages": len(+ pdfplumber.open(io.BytesIO(pdf_content)).pages+ ),+ }+ )+ processed_files += 1++ logger.info(+ f"Successfully extracted text from {file.filename} ({len(text)} characters)"+ )++ except Exception as e:+ logger.exception(f"Error processing {file.filename}")+ errors.append(f"{file.filename}: {str(e)}")+ else:+ errors.append(f"{file.filename}: Not a PDF file")++ # Prepare response+ response_data = {+ "status": "success",+ "processed_files": processed_files,+ "total_files": total_files,+ "extracted_texts": extracted_texts,+ "combined_text": "\n\n".join(+ [+ f"--- From {item['filename']} ---\n{item['text']}"+ for item in extracted_texts+ ]+ ),+ "errors": errors,+ }++ if processed_files == 0:+ return jsonify(+ {+ "status": "error",+ "message": "No files were processed successfully",+ "errors": errors,+ }+ ), 400++ return jsonify(response_data)++ except Exception as e:+ logger.exception("Error processing PDF upload")+ return jsonify({"error": f"Failed to process PDF files: {str(e)}"}), 500diff --git a/src/local_deep_research/web/static/js/config/urls.js b/src/local_deep_research/web/static/js/config/urls.jsindex 3a7bb41583..33a54a005c 100644--- a/src/local_deep_research/web/static/js/config/urls.js+++ b/src/local_deep_research/web/static/js/config/urls.js@@ -22,7 +22,8 @@ const URLS = {SAVE_LLM_CONFIG: '/api/save_llm_config',MARKDOWN_EXPORT: '/api/markdown/{id}',HISTORY: '/history/api', // Changed to match backend route- HEALTH: '/api/v1/health' // Added health check endpoint+ HEALTH: '/api/v1/health', // Added health check endpoint+ UPLOAD_PDF: '/api/upload/pdf' // PDF upload endpoint},// Page routesdiff --git a/src/local_deep_research/web/static/js/pdf_upload_handler.js b/src/local_deep_research/web/static/js/pdf_upload_handler.jsnew file mode 100644index 0000000000..2de881e557--- /dev/null+++ b/src/local_deep_research/web/static/js/pdf_upload_handler.js@@ -0,0 +1,502 @@+/**+ * PDF Upload Handler for Research Form+ * Handles drag-and-drop PDF uploads and text extraction+ */++class PDFUploadHandler {+ constructor() {+ console.log('PDF Upload Handler: Initializing...');+ this.queryTextarea = null;+ this.isDragOver = false;+ this.uploadedPDFs = [];+ this.maxFileSize = 10 * 1024 * 1024; // 10MB limit+ this.maxFiles = 100; // Maximum 100 PDFs at once+ this.init();+ }++ /**+ * Initialize the PDF upload handler+ */+ init() {+ console.log('PDF Upload Handler: Starting initialization...');++ // Wait a bit longer for DOM to be ready+ if (document.readyState === 'loading') {+ document.addEventListener('DOMContentLoaded', () => this.init());+ return;+ }++ this.queryTextarea = document.getElementById('query');+ if (!this.queryTextarea) {+ console.error('PDF Upload Handler: Query textarea not found!');+ return;+ }++ console.log('PDF Upload Handler: Found query textarea, setting up drag-and-drop...');+ this.setupDragAndDrop();+ this.setupFileInput();+ this.updatePlaceholder();+ console.log('PDF Upload Handler: Initialization complete!');+ }++ /**+ * Setup drag and drop events for the textarea+ */+ setupDragAndDrop() {+ console.log('PDF Upload Handler: Setting up drag-and-drop events...');++ // Prevent default drag behaviors+ ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {+ this.queryTextarea.addEventListener(eventName, (e) => {+ console.log(`PDF Upload Handler: ${eventName} event detected`);+ this.preventDefaults(e);+ }, false);+ document.body.addEventListener(eventName, this.preventDefaults, false);+ });++ // Highlight drop area when item is dragged over it+ ['dragenter', 'dragover'].forEach(eventName => {+ this.queryTextarea.addEventListener(eventName, (e) => {+ console.log(`PDF Upload Handler: Highlighting for ${eventName}`);+ this.highlight();+ }, false);+ });++ ['dragleave', 'drop'].forEach(eventName => {+ this.queryTextarea.addEventListener(eventName, (e) => {+ console.log(`PDF Upload Handler: Unhighlighting for ${eventName}`);+ this.unhighlight();+ }, false);+ });++ // Handle dropped files+ this.queryTextarea.addEventListener('drop', (e) => {+ console.log('PDF Upload Handler: Drop event detected, handling files...');+ this.handleDrop(e);+ }, false);++ // Handle paste events+ this.queryTextarea.addEventListener('paste', (e) => this.handlePaste(e), false);++ console.log('PDF Upload Handler: Drag-and-drop events setup complete');+ }++ /**+ * Setup a hidden file input as fallback+ */+ setupFileInput() {+ // Create file input button+ const fileInput = document.createElement('input');+ fileInput.type = 'file';+ fileInput.multiple = true;+ fileInput.accept = '.pdf';+ fileInput.style.display = 'none';+ fileInput.id = 'pdf-file-input';++ // Add upload button near the textarea with proper styling+ const uploadButton = document.createElement('button');+ uploadButton.type = 'button';+ uploadButton.className = 'btn btn-sm ldr-btn-outline ldr-pdf-upload-btn';+ uploadButton.id = 'pdf-upload-btn';+ uploadButton.setAttribute('aria-label', 'Upload PDF files');+ uploadButton.innerHTML = '<i class="fas fa-file-pdf"></i> Add PDFs';++ // Find the search hints container to add PDF button inline+ const searchHints = this.queryTextarea.parentNode.querySelector('.ldr-search-hints');+ const hintRow = searchHints?.querySelector('.ldr-hint-row');++ if (hintRow) {+ // Add PDF button as a hint item to existing shortcuts row+ const pdfHintItem = document.createElement('span');+ pdfHintItem.className = 'ldr-hint-item';+ pdfHintItem.style.cssText = `+ margin-left: auto;+ padding-left: 1rem;+ border-left: 1px solid var(--border-color);+ `;++ // Wrap button in hint item structure+ pdfHintItem.appendChild(uploadButton);+ pdfHintItem.appendChild(fileInput);++ // Add to the existing hint row+ hintRow.appendChild(pdfHintItem);+ } else {+ // Fallback: create container if hints not found+ const pdfContainer = document.createElement('div');+ pdfContainer.className = 'ldr-pdf-upload-container';+ pdfContainer.style.cssText = `+ margin-top: 0.25rem;+ display: flex;+ align-items: center;+ gap: 0.5rem;+ flex-wrap: wrap;+ `;+ pdfContainer.appendChild(uploadButton);+ pdfContainer.appendChild(fileInput);+ this.queryTextarea.parentNode.insertBefore(pdfContainer, this.queryTextarea.nextSibling);+ }++ // Handle button click+ uploadButton.addEventListener('click', () => fileInput.click());+ fileInput.addEventListener('change', (e) => this.handleFileSelect(e));+ }++ /**+ * Prevent default drag behaviors+ */+ preventDefaults(e) {+ e.preventDefault();+ e.stopPropagation();+ }++ /**+ * Highlight the drop area+ */+ highlight() {+ if (!this.isDragOver) {+ this.queryTextarea.style.backgroundColor = '#f0f8ff';+ this.queryTextarea.style.border = '2px dashed #007bff';+ this.isDragOver = true;+ }+ }++ /**+ * Remove highlight from the drop area+ */+ unhighlight() {+ if (this.isDragOver) {+ this.queryTextarea.style.backgroundColor = '';+ this.queryTextarea.style.border = '';+ this.isDragOver = false;+ }+ }++ /**+ * Handle file drops+ */+ async handleDrop(e) {+ const dt = e.dataTransfer;+ const files = dt.files;+ this.handleFiles(files);+ }++ /**+ * Handle file paste events+ */+ async handlePaste(e) {+ const items = e.clipboardData.items;+ for (let item of items) {+ if (item.type === 'application/pdf') {+ e.preventDefault();+ const file = item.getAsFile();+ this.handleFiles([file]);+ break;+ }+ }+ }++ /**+ * Handle file selection from input+ */+ async handleFileSelect(e) {+ const files = e.target.files;+ this.handleFiles(files);+ // Clear the input so the same files can be selected again if needed+ e.target.value = '';+ }++ /**+ * Process files+ */+ async handleFiles(files) {+ const pdfFiles = Array.from(files).filter(file =>+ file.type === 'application/pdf' || file.name.toLowerCase().endsWith('.pdf')+ );++ if (pdfFiles.length === 0) {+ this.showError('Please select PDF files only');+ return;+ }++ // Check file count limit+ if (pdfFiles.length > this.maxFiles) {+ this.showError(`Maximum ${this.maxFiles} PDF files allowed at once`);+ return;+ }++ // Check file sizes+ const oversizedFiles = pdfFiles.filter(file => file.size > this.maxFileSize);+ if (oversizedFiles.length > 0) {+ this.showError(`PDF files must be smaller than ${this.formatFileSize(this.maxFileSize)}`);+ return;+ }++ await this.uploadAndExtractPDFs(pdfFiles);+ }++ /**+ * Upload PDFs and extract text+ */+ async uploadAndExtractPDFs(files) {+ this.showProcessing(files.length);++ try {+ const formData = new FormData();+ files.forEach(file => {+ formData.append('files', file);+ });++ // Get CSRF token+ const csrfToken = document.querySelector('meta[name="csrf-token"]')?.getAttribute('content');++ const headers = {};+ if (csrfToken) {+ headers['X-CSRFToken'] = csrfToken;+ }++ // Validate URL before making request+ const uploadUrl = URLS.API.UPLOAD_PDF;+ if (!URLValidator.isSafeUrl(uploadUrl)) {+ throw new Error('Invalid upload URL detected');+ }++ const response = await fetch(uploadUrl, {+ method: 'POST',+ body: formData,+ headers: headers+ });++ if (!response.ok) {+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);+ }++ const result = await response.json();++ if (result.status === 'success') {+ this.addPDFsToUploaded(files, result.extracted_texts);+ this.appendExtractedTextToQuery(result.combined_text);+ this.showSuccess(result.processed_files, result.errors);+ this.updatePlaceholder();+ } else {+ this.showError(result.message || 'Failed to process PDFs');+ }+ } catch (error) {+ console.error('Error uploading PDFs:', error);+ this.showError('Failed to upload PDFs. Please try again.');+ } finally {+ this.hideProcessing();+ }+ }++ /**+ * Add PDFs to the uploaded list+ */+ addPDFsToUploaded(files, extractedTexts) {+ files.forEach((file, index) => {+ const extractedText = extractedTexts[index];+ if (extractedText) {+ this.uploadedPDFs.push({+ filename: file.name,+ size: file.size,+ text: extractedText.text,+ pages: extractedText.pages+ });+ }+ });+ }++ /**+ * Append extracted text to the query textarea+ */+ appendExtractedTextToQuery(combinedText) {+ const currentQuery = this.queryTextarea.value.trim();+ const separator = currentQuery ? '\n\n--- PDF Content ---\n' : '';+ this.queryTextarea.value = currentQuery + separator + combinedText;++ // Focus the textarea at the end+ this.queryTextarea.focus();+ this.queryTextarea.setSelectionRange(this.queryTextarea.value.length, this.queryTextarea.value.length);++ // Trigger input event to let other components know the content changed+ this.queryTextarea.dispatchEvent(new Event('input', { bubbles: true }));+ }++ /**+ * Update the placeholder text based on upload state+ */+ updatePlaceholder() {+ if (this.uploadedPDFs.length > 0) {+ const pdfCount = this.uploadedPDFs.length;+ const totalPages = this.uploadedPDFs.reduce((sum, pdf) => sum + pdf.pages, 0);+ this.queryTextarea.placeholder =+ `Enter your research question... (${pdfCount} PDF${pdfCount > 1 ? 's' : ''} loaded, ${totalPages} pages total)`;+ } else {+ this.queryTextarea.placeholder = 'Enter your research topic or question... (drag and drop PDFs here to include their content)';+ }+ }++ /**+ * Show processing indicator+ */+ showProcessing(fileCount) {+ const statusDiv = this.getOrCreateStatusDiv();+ statusDiv.innerHTML = `+ <div style="display: flex; align-items: center; gap: 8px; color: #007bff;">+ <i class="fas fa-spinner fa-spin"></i>+ <span>Processing ${fileCount} PDF${fileCount > 1 ? 's' : ''}...</span>+ </div>+ `;+ statusDiv.style.display = 'block';+ }++ /**+ * Hide processing indicator+ */+ hideProcessing() {+ const statusDiv = this.getOrCreateStatusDiv();+ statusDiv.style.display = 'none';+ }++ /**+ * Show success message+ */+ showSuccess(processedFiles, errors) {+ const statusDiv = this.getOrCreateStatusDiv();+ let message = `<div style="color: #28a745;">`;+ message += `<i class="fas fa-check-circle"></i> Successfully processed ${processedFiles} PDF${processedFiles > 1 ? 's' : ''}`;++ if (errors.length > 0) {+ message += `<br><small style="color: #6c757d;">Some files had issues: ${errors.join('; ')}</small>`;+ }++ message += `</div>`;++ statusDiv.innerHTML = message;+ statusDiv.style.display = 'block';++ // Auto-hide after 5 seconds+ setTimeout(() => {+ statusDiv.style.display = 'none';+ }, 5000);+ }++ /**+ * Show error message+ */+ showError(message) {+ const statusDiv = this.getOrCreateStatusDiv();+ statusDiv.innerHTML = `+ <div style="color: #dc3545;">+ <i class="fas fa-exclamation-triangle"></i> ${message}+ </div>+ `;+ statusDiv.style.display = 'block';++ // Auto-hide after 5 seconds+ setTimeout(() => {+ statusDiv.style.display = 'none';+ }, 5000);+ }++ /**+ * Get or create status display div+ */+ getOrCreateStatusDiv() {+ let statusDiv = document.getElementById('pdf-upload-status');+ if (!statusDiv) {+ statusDiv = document.createElement('div');+ statusDiv.id = 'pdf-upload-status';+ statusDiv.className = 'ldr-upload-status';+ statusDiv.style.cssText = `+ padding: 0.25rem 0.5rem;+ border-radius: 4px;+ font-size: 0.8rem;+ background: var(--bg-tertiary);+ border: 1px solid var(--border-color);+ color: var(--text-secondary);+ flex: 1;+ min-width: 0;+ max-width: 300px;+ `;++ // Find the PDF hint item and add status div to it+ const pdfHintItem = document.querySelector('.ldr-hint-item:has(.ldr-pdf-upload-btn)');+ if (pdfHintItem) {+ statusDiv.style.cssText += `+ margin-left: 0.5rem;+ max-width: 200px;+ `;+ pdfHintItem.appendChild(statusDiv);+ } else {+ // Fallback: find PDF container or insert after textarea+ const pdfContainer = document.querySelector('.ldr-pdf-upload-container');+ if (pdfContainer) {+ pdfContainer.appendChild(statusDiv);+ } else {+ this.queryTextarea.parentNode.insertBefore(statusDiv, this.queryTextarea.nextSibling);+ }+ }+ }+ return statusDiv;+ }++ /**+ * Clear uploaded PDFs+ */+ clearUploadedPDFs() {+ this.uploadedPDFs = [];+ this.updatePlaceholder();+ }++ /**+ * Format file size for display+ */+ formatFileSize(bytes) {+ if (bytes === 0) return '0 Bytes';+ const k = 1024;+ const sizes = ['Bytes', 'KB', 'MB', 'GB'];+ const i = Math.floor(Math.log(bytes) / Math.log(k));+ return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];+ }++ /**+ * Get list of uploaded PDFs+ */+ getUploadedPDFs() {+ return [...this.uploadedPDFs];+ }+}++// Initialize the PDF upload handler when the DOM is ready+function initializePDFUploadHandler() {+ console.log('PDF Upload Handler: DOM ready, initializing handler...');+ if (window.pdfUploadHandler) {+ console.log('PDF Upload Handler: Already initialized');+ return;+ }++ // Try to initialize immediately+ window.pdfUploadHandler = new PDFUploadHandler();++ // If textarea not found, try again after delay+ if (!window.pdfUploadHandler.queryTextarea) {+ console.log('PDF Upload Handler: Textarea not found, retrying...');+ setTimeout(() => {+ console.log('PDF Upload Handler: Retrying initialization...');+ window.pdfUploadHandler = new PDFUploadHandler();+ }, 500);+ }+}++if (document.readyState === 'loading') {+ document.addEventListener('DOMContentLoaded', initializePDFUploadHandler);+} else {+ // DOM is already ready+ initializePDFUploadHandler();+}++// Make it available globally for other scripts+if (typeof module !== 'undefined' && module.exports) {+ module.exports = PDFUploadHandler;+}diff --git a/src/local_deep_research/web/templates/pages/research.html b/src/local_deep_research/web/templates/pages/research.htmlindex bc9818b023..1e936b7002 100644--- a/src/local_deep_research/web/templates/pages/research.html+++ b/src/local_deep_research/web/templates/pages/research.html@@ -183,4 +183,5 @@<script src="/static/js/components/custom_dropdown.js"></script><script src="/static/js/components/research.js"></script><script src="/static/js/research_form.js"></script>+<script src="/static/js/pdf_upload_handler.js"></script>{% endblock %}