From b45a6e4f115b186135f8649430c59af63655e479 Mon Sep 17 00:00:00 2001 From: Mrna Date: Mon, 3 Nov 2025 15:40:34 +0100 Subject: [PATCH] Web: link and serve the file --- main.py | 19 ++++++++++++++++++- templates/index.html | 4 +++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 6dfb164..b764c8a 100644 --- a/main.py +++ b/main.py @@ -182,7 +182,7 @@ def query(db_path: str, query_text: str): def start_web_server(db_path: str, host: str = "127.0.0.1", port: int = 5000): """Start a web server for the semantic search tool.""" try: - from flask import Flask, request, jsonify, render_template + from flask import Flask, request, jsonify, render_template, send_file except ImportError: print("❌ Flask not found. Please install it first:") print(" pip install flask") @@ -201,6 +201,22 @@ def start_web_server(db_path: str, host: str = "127.0.0.1", port: int = 5000): def index(): return render_template("index.html", results=None) + @app.route('/file/') + def serve_file(document_path): + """Serve PDF files directly.""" + try: + file_path = Path(document_path) + if not file_path.exists(): + return jsonify({'error': 'File not found'}), 404 + + # Check if it's a PDF file for security + if file_path.suffix.lower() != '.pdf': + return jsonify({'error': 'Only PDF files are allowed'}), 403 + + return send_file(file_path, as_attachment=False) + except Exception as e: + return jsonify({'error': str(e)}), 500 + @app.route('/api/search', methods=['POST']) def search(): try: @@ -221,6 +237,7 @@ def start_web_server(db_path: str, host: str = "127.0.0.1", port: int = 5000): formatted_results.append({ 'distance': float(distance), 'document': record.document.name, + 'document_path': str(record.document), # Full path for the link 'page': record.page, 'chunk': record.chunk, 'text': ' '.join(record.text[:300].split()) # Clean and truncate text diff --git a/templates/index.html b/templates/index.html index 9dc6302..bb74687 100644 --- a/templates/index.html +++ b/templates/index.html @@ -12,6 +12,8 @@ .result-header { font-weight: bold; color: #333; margin-bottom: 10px; } .result-text { background: #f9f9f9; padding: 10px; border-radius: 3px; } .distance { color: #666; font-size: 0.9em; } + .document-link { color: #007cba; text-decoration: none; } + .document-link:hover { text-decoration: underline; } .no-results { text-align: center; color: #666; margin: 40px 0; } .loading { text-align: center; color: #007cba; margin: 20px 0; } @@ -56,7 +58,7 @@ resultsDiv.innerHTML = data.results.map((result, i) => `
- Result ${i + 1} - ${result.document} + Result ${i + 1} - ${result.document} (Distance: ${result.distance.toFixed(4)})
Page: ${result.page}, Chunk: ${result.chunk}