Web: link and serve the file

This commit is contained in:
Jan Mrna
2025-11-03 15:40:34 +01:00
parent b2ad6ed885
commit 03057bc4d2
2 changed files with 21 additions and 2 deletions

19
main.py
View File

@@ -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/<path:document_path>')
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