-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
85 lines (74 loc) · 3.07 KB
/
app.py
File metadata and controls
85 lines (74 loc) · 3.07 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
from flask import Flask, request, jsonify, render_template
from werkzeug.utils import secure_filename
from specterragchain1 import ResearchPaperRAG
import logging
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
app.config['ALLOWED_EXTENSIONS'] = {'pdf'}
# Ensure upload folder exists
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
# Initialize RAG (will be set after file upload)
rag = None
API_KEY = os.environ.get('GOOGLE_API_KEY', "AIzaSyAVzwqMt0edserFCtiGHlb5g2iOkxZf2SA")
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def allowed_file(filename):
"""Check if the file is a PDF."""
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
@app.route('/')
def index():
"""Render the main page."""
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
"""Handle PDF upload and initialize RAG."""
global rag
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
try:
rag = ResearchPaperRAG(api_key=API_KEY)
rag.load_research_paper(file_path)
logger.info(f"Successfully loaded {file_path}")
return jsonify({'message': 'File uploaded and processed successfully'}), 200
except Exception as e:
logger.error(f"Error processing file: {e}")
return jsonify({'error': f"Error processing file: {str(e)}"}), 500
return jsonify({'error': 'Invalid file type'}), 400
@app.route('/query', methods=['POST'])
def query():
"""Handle query and return results."""
global rag
if rag is None:
return jsonify({'error': 'No document loaded. Please upload a PDF first.'}), 400
data = request.get_json()
question = data.get('question', '')
if not question.strip():
return jsonify({'error': 'Please provide a valid question'}), 400
try:
result = rag.query(question)
return jsonify(result), 200
except Exception as e:
logger.error(f"Error processing query: {e}")
return jsonify({'error': f"Error processing query: {str(e)}"}), 500
@app.route('/results', methods=['GET'])
def results():
"""Render results page (for manual navigation, if needed)."""
return render_template('results.html')
# Health check endpoint for Cloud Run
@app.route('/health', methods=['GET'])
def health_check():
"""Health check endpoint."""
return jsonify({'status': 'healthy'}), 200
if __name__ == '__main__':
# For local development only
app.run(host='0.0.0.0', port=8080, debug=True)