-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
285 lines (237 loc) · 11.1 KB
/
Copy pathapp.py
File metadata and controls
285 lines (237 loc) · 11.1 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
from flask import Flask, render_template, request, jsonify, Response, session, make_response, send_from_directory
from summarize_url import fetch_url_content, generate_all_mcqs, extract_file_content
import json
import queue
import threading
import logging
from threading import Lock
from datetime import datetime, timedelta
import time
import os
import uuid
from werkzeug.utils import secure_filename
import gc
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('app.log'),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)
# Global rate limiting
REQUESTS_PER_MINUTE = 10
request_timestamps = []
rate_limit_lock = Lock()
def check_rate_limit():
"""Check if we can proceed with the request or need to wait.
Returns (can_proceed, wait_time_seconds)"""
with rate_limit_lock:
now = datetime.now()
# Remove timestamps older than 1 minute
global request_timestamps
request_timestamps = [ts for ts in request_timestamps if now - ts < timedelta(minutes=1)]
# If we haven't hit the limit, add timestamp and proceed
if len(request_timestamps) < REQUESTS_PER_MINUTE:
request_timestamps.append(now)
return True, 0
# Calculate wait time if we've hit the limit
oldest_timestamp = request_timestamps[0]
wait_time = 61 - (now - oldest_timestamp).total_seconds() # 61 to be safe
return False, max(0, wait_time)
app = Flask(__name__)
app.secret_key = 'your-secret-key-here' # Required for session management
progress_queues = {}
queue_lock = Lock()
# Add these configurations after app initialization
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'pdf', 'doc', 'docx', 'txt'}
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Create uploads directory if it doesn't exist
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
def cleanup_queue(queue_id):
"""Safely remove a queue after ensuring all data has been read."""
with queue_lock:
if queue_id in progress_queues:
logger.info(f"Cleaning up queue {queue_id}")
del progress_queues[queue_id]
def generate_with_progress(content_or_url, num_questions, queue_id, difficulty='medium', is_url=True):
try:
logger.info(f"Starting generation for queue_id: {queue_id} with difficulty: {difficulty}")
# Fetch content if URL provided
if is_url:
progress_queues[queue_id].put(('status', 'Fetching content...'))
content = fetch_url_content(content_or_url)
if not content:
logger.error("No content fetched from URL")
progress_queues[queue_id].put(('error', 'Failed to fetch content from URL'))
return None
if isinstance(content, str) and content.startswith('Error'):
logger.error(f"Error fetching content: {content}")
progress_queues[queue_id].put(('error', content))
return None
else:
content = content_or_url
# Generate MCQs with progress updates
logger.info("Starting MCQ generation")
logger.info(f"Will generate {num_questions} questions in batches of 5")
progress_queues[queue_id].put(('status', f'Initializing generation of {num_questions} {difficulty} level questions...'))
progress_queues[queue_id].put(('progress', 0))
# Process in smaller batches of 5 questions
batch_size = 5
num_batches = (num_questions + batch_size - 1) // batch_size
logger.info(f"Total batches needed: {num_batches}")
progress_queues[queue_id].put(('status', f'Starting generation in {num_batches} batches...'))
# Generate all questions at once with proper batch size
all_questions = generate_all_mcqs(
content,
total_questions=num_questions,
batch_size=batch_size,
difficulty=difficulty,
progress_queue=progress_queues[queue_id]
)
# Check if we got an error string back
if isinstance(all_questions, str):
logger.error(f"Failed to generate questions: {all_questions}")
progress_queues[queue_id].put(('error', all_questions))
return
if all_questions:
logger.info(f"Successfully generated {len(all_questions)} questions")
questions_with_meta = {
'questions': all_questions,
'difficulty': difficulty
}
progress_queues[queue_id].put(('complete', questions_with_meta))
else:
logger.error("Failed to generate questions")
progress_queues[queue_id].put(('error', 'Failed to generate questions'))
except Exception as e:
logger.error(f"Error in generate_with_progress: {str(e)}", exc_info=True)
progress_queues[queue_id].put(('error', f'An error occurred: {str(e)}'))
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def index():
return render_template('index.html', default_questions=5)
@app.route('/privacy-policy')
def privacy_policy():
from datetime import datetime
current_date = datetime.now().strftime('%B %d, %Y')
return render_template('privacy-policy.html', current_date=current_date)
@app.route('/generate', methods=['POST'])
def generate():
try:
# Check rate limit first
can_proceed, wait_time = check_rate_limit()
if not can_proceed:
return jsonify({
'error': f'Rate limit reached. Please wait {int(wait_time)} seconds before trying again.'
}), 429
# Check master password
# master_password = os.getenv('MASTER_PASSWORD')
# provided_password = request.form.get('master_password')
# if not provided_password or provided_password != master_password:
# return jsonify({'error': 'Invalid master password'}), 403
num_questions = int(request.form.get('num_questions', 5))
difficulty = request.form.get('difficulty', 'medium')
input_method = request.form.get('input_method', 'url')
logger.info(f"Received request - Method: {input_method}, Questions: {num_questions}, Difficulty: {difficulty}")
content = None
is_url = True
if input_method == 'url':
url = request.form.get('url')
if not url:
return jsonify({'error': 'Please provide a URL'})
content = url # Pass URL directly
else:
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'})
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected'})
if not allowed_file(file.filename):
return jsonify({'error': 'Invalid file format'})
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
try:
content = extract_file_content(filepath)
is_url = False # Content is already extracted
os.remove(filepath) # Clean up the uploaded file
except Exception as e:
if os.path.exists(filepath):
os.remove(filepath) # Clean up on error
raise Exception(f"Error processing file: {str(e)}")
if not content:
return jsonify({'error': 'Failed to extract content'})
if isinstance(content, str) and content.startswith('Error'):
return jsonify({'error': content})
# Create a unique queue ID
queue_id = uuid.uuid4().hex
logger.info(f"Created queue_id: {queue_id}")
with queue_lock:
progress_queues[queue_id] = queue.Queue()
# Start generation in a background thread
thread = threading.Thread(
target=generate_with_progress,
args=(content, num_questions, queue_id, difficulty),
kwargs={'is_url': is_url}
)
thread.start()
return jsonify({'queue_id': queue_id})
except Exception as e:
logger.error(f"Error in generate endpoint: {str(e)}", exc_info=True)
return jsonify({'error': f'An error occurred: {str(e)}'})
@app.route('/progress/<queue_id>')
def progress(queue_id):
def generate():
if queue_id not in progress_queues:
logger.error(f"Invalid queue_id requested: {queue_id}")
yield "data: " + json.dumps({"error": "Invalid queue ID"}) + "\n\n"
return
try:
while True:
try:
with queue_lock:
if queue_id not in progress_queues:
break
current_queue = progress_queues[queue_id]
msg_type, data = current_queue.get(timeout=1)
logger.debug(f"Progress update for {queue_id}: {msg_type}")
if msg_type == 'progress':
yield "data: " + json.dumps({"progress": data}) + "\n\n"
elif msg_type == 'status':
yield "data: " + json.dumps({"status": data}) + "\n\n"
elif msg_type == 'complete':
logger.info(f"Generation complete for {queue_id}")
yield "data: " + json.dumps({"complete": True, "questions": data}) + "\n\n"
cleanup_queue(queue_id)
break
elif msg_type == 'error':
logger.error(f"Error for {queue_id}: {data}")
yield "data: " + json.dumps({"error": data}) + "\n\n"
cleanup_queue(queue_id)
break
except queue.Empty:
continue
except Exception as e:
logger.error(f"Error in progress stream for {queue_id}: {str(e)}", exc_info=True)
yield "data: " + json.dumps({"error": str(e)}) + "\n\n"
cleanup_queue(queue_id)
return Response(generate(), mimetype='text/event-stream')
@app.route('/service-worker.js')
def service_worker():
response = make_response(send_from_directory('static', 'service-worker.js'))
response.headers['Content-Type'] = 'application/javascript'
response.headers['Service-Worker-Allowed'] = '/'
return response
@app.route('/offline.html')
def offline():
return render_template('offline.html')
if __name__ == '__main__':
# Get port from environment variable or default to 8000
port = int(os.environ.get('PORT', 8000))
# In production, host should be '0.0.0.0' to accept all incoming connections
app.run(host='0.0.0.0', port=port, debug=False)