Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -82,18 +82,31 @@ def index():
|
|
82 |
"""Serve the HTML page for the user interface"""
|
83 |
return render_template('index.html')
|
84 |
|
|
|
|
|
|
|
85 |
@app.route('/upload_pdf', methods=['POST'])
|
86 |
def upload_pdf():
|
87 |
"""Handle PDF upload"""
|
|
|
|
|
|
|
88 |
file = request.files['pdf']
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
|
|
91 |
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
|
96 |
-
|
|
|
|
|
97 |
|
98 |
@app.route('/ask_question', methods=['POST'])
|
99 |
def ask_question():
|
|
|
82 |
"""Serve the HTML page for the user interface"""
|
83 |
return render_template('index.html')
|
84 |
|
85 |
+
UPLOAD_FOLDER = "uploaded_files"
|
86 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True) # Ensure the folder exists
|
87 |
+
|
88 |
@app.route('/upload_pdf', methods=['POST'])
|
89 |
def upload_pdf():
|
90 |
"""Handle PDF upload"""
|
91 |
+
if 'pdf' not in request.files:
|
92 |
+
return jsonify({"error": "No file part"}), 400 # Handle missing file
|
93 |
+
|
94 |
file = request.files['pdf']
|
95 |
+
if file.filename == "":
|
96 |
+
return jsonify({"error": "No selected file"}), 400 # Handle empty filename
|
97 |
+
|
98 |
+
pdf_path = os.path.join(UPLOAD_FOLDER, file.filename)
|
99 |
+
|
100 |
+
try:
|
101 |
+
file.save(pdf_path) # Save the uploaded PDF
|
102 |
|
103 |
+
# Extract text and create vector database
|
104 |
+
text_chunks = extract_text_from_pdf(pdf_path)
|
105 |
+
create_vector_db(text_chunks)
|
106 |
|
107 |
+
return jsonify({"message": "PDF uploaded and indexed successfully!"}), 200
|
108 |
+
except Exception as e:
|
109 |
+
return jsonify({"error": f"Error processing file: {str(e)}"}), 500
|
110 |
|
111 |
@app.route('/ask_question', methods=['POST'])
|
112 |
def ask_question():
|