Web: link and serve the file
This commit is contained in:
19
main.py
19
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/<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
|
||||
|
||||
@@ -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; }
|
||||
</style>
|
||||
@@ -56,7 +58,7 @@
|
||||
resultsDiv.innerHTML = data.results.map((result, i) => `
|
||||
<div class="result">
|
||||
<div class="result-header">
|
||||
Result ${i + 1} - ${result.document}
|
||||
Result ${i + 1} - <a href="/file/${encodeURIComponent(result.document_path)}" class="document-link" target="_blank">${result.document}</a>
|
||||
<span class="distance">(Distance: ${result.distance.toFixed(4)})</span>
|
||||
</div>
|
||||
<div>Page: ${result.page}, Chunk: ${result.chunk}</div>
|
||||
|
||||
Reference in New Issue
Block a user