Serve by file index, not full path
This commit is contained in:
18
main.py
18
main.py
@@ -166,7 +166,7 @@ def query(db_path: str, query_text: str):
|
||||
|
||||
for i, res in enumerate(results, 1):
|
||||
print(f"\n{i}. Distance: {res.distance:.4f}")
|
||||
print(f" Document: {res.document.name}")
|
||||
print(f" Document: {res.document_name}")
|
||||
print(f" Page: {res.record.page}, Chunk: {res.record.chunk}")
|
||||
# Replace all whitespace characters with regular spaces for cleaner display
|
||||
clean_text = ' '.join(res.record.text[:200].split())
|
||||
@@ -201,18 +201,13 @@ 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):
|
||||
@app.route('/file/<int:document_index>')
|
||||
def serve_file(document_index):
|
||||
"""Serve PDF files directly."""
|
||||
try:
|
||||
file_path = Path(document_path)
|
||||
file_path = db.get_document_path(db_file, document_index)
|
||||
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
|
||||
@@ -236,13 +231,12 @@ def start_web_server(db_path: str, host: str = "127.0.0.1", port: int = 5000):
|
||||
for res in results:
|
||||
formatted_results.append({
|
||||
'distance': float(res.distance),
|
||||
'document': res.document.name,
|
||||
'document_path': str(res.document), # Full path for the link
|
||||
'document_name': res.document_name,
|
||||
'document_index': res.record.document_index,
|
||||
'page': res.record.page,
|
||||
'chunk': res.record.chunk,
|
||||
'text': ' '.join(res.record.text[:300].split()) # Clean and truncate text
|
||||
})
|
||||
|
||||
return jsonify({'results': formatted_results})
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user