NanobotzAI commited on
Commit
b15d87a
·
verified ·
1 Parent(s): 4431829

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -6
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
- pdf_path = f"uploaded_files/{file.filename}"
90
- file.save(pdf_path)
 
 
 
 
 
91
 
92
- # Extract text and create vector database
93
- text_chunks = extract_text_from_pdf(pdf_path)
94
- create_vector_db(text_chunks)
95
 
96
- return jsonify({"message": "PDF uploaded and indexed successfully!"})
 
 
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():