-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
38 lines (34 loc) · 1.36 KB
/
app.py
File metadata and controls
38 lines (34 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from flask import Flask, request, jsonify
import os
app = Flask(__name__)
UPLOAD_FOLDER = 'documents'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
# Route for uploading documents
@app.route('/upload', methods=['POST'])
def upload_file():
if 'file' not in request.files:
return jsonify({"message": "No file part"}), 400
file = request.files['file']
if file.filename == '':
return jsonify({"message": "No selected file"}), 400
file.save(os.path.join(UPLOAD_FOLDER, file.filename))
return jsonify({"message": f"File {file.filename} uploaded successfully"})
# Route for asking questions
@app.route('/ask', methods=['POST'])
def ask_question():
data = request.get_json()
question = data.get('question', '')
# Placeholder logic: check your documents folder for content
doc_path = os.path.join(UPLOAD_FOLDER, 'sample_doc.txt')
if os.path.exists(doc_path):
with open(doc_path, 'r') as f:
content = f.read()
if question.lower() in content.lower():
answer = f"Found relevant info in sample_doc.txt: {content[:200]}..." # return snippet
else:
answer = "I could not find relevant information in the document to answer your question."
else:
answer = "No documents uploaded."
return jsonify({"answer": answer})
if __name__ == '__main__':
app.run(port=5001, debug=True)