Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify, render_template
|
2 |
+
from flask_cors import CORS
|
3 |
+
import os
|
4 |
+
from werkzeug.utils import secure_filename
|
5 |
+
from chat_pdf import ChatPDF #importing ChatPDF class
|
6 |
+
|
7 |
+
app = Flask(__name__)
|
8 |
+
CORS(app)
|
9 |
+
app.config['UPLOAD_FOLDER'] = 'uploads'
|
10 |
+
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
|
11 |
+
|
12 |
+
# Ensure upload directory exists
|
13 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
14 |
+
|
15 |
+
# Initialize ChatPDF
|
16 |
+
chat_pdf = ChatPDF()
|
17 |
+
|
18 |
+
@app.route('/')
|
19 |
+
def index():
|
20 |
+
return render_template('index.html')
|
21 |
+
|
22 |
+
@app.route('/upload', methods=['POST'])
|
23 |
+
def upload_file():
|
24 |
+
if 'file' not in request.files:
|
25 |
+
return jsonify({'error': 'No file part'}), 400
|
26 |
+
|
27 |
+
file = request.files['file']
|
28 |
+
if file.filename == '':
|
29 |
+
return jsonify({'error': 'No selected file'}), 400
|
30 |
+
|
31 |
+
if file and file.filename.endswith('.pdf'):
|
32 |
+
filename = secure_filename(file.filename)
|
33 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
34 |
+
file.save(filepath)
|
35 |
+
|
36 |
+
# Process the file with ChatPDF
|
37 |
+
chat_pdf.ingest(file_path=filepath, file_name=filename)
|
38 |
+
|
39 |
+
return jsonify({'message': 'File uploaded successfully'}), 200
|
40 |
+
|
41 |
+
return jsonify({'error': 'Invalid file type'}), 400
|
42 |
+
|
43 |
+
@app.route('/chat', methods=['POST'])
|
44 |
+
def chat():
|
45 |
+
data = request.json
|
46 |
+
if not data or 'message' not in data:
|
47 |
+
return jsonify({'error': 'No message provided'}), 400
|
48 |
+
|
49 |
+
response = chat_pdf.ask(data['message'])
|
50 |
+
return jsonify({'response': response}), 200
|
51 |
+
|
52 |
+
if __name__ == '__main__':
|
53 |
+
app.run(debug=True)
|