Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @app.route('/generate-report/<task_id>', methods=['GET'])
- def generate_report(task_id):
- # logger.info("Received generate report request for Task ID: %s", task_id)
- # Check if the task_id exists in the 'known_tasks' set
- task_exists = redis_client.sismember('known_tasks', task_id)
- if not task_exists:
- return jsonify({'state': 'UNKNOWN', 'error': 'Task ID does not exist.'}), 404
- task = AsyncResult(task_id, app=celery)
- # logger.info("Task State: %s", task.state)
- if task.state == 'SUCCESS':
- result = task.result
- llama_output, merged_buffer = generate_pdf(
- result, result['roi_category'])
- if merged_buffer:
- try:
- # Option 1: Return PDF directly
- # pdf_buffer = merged_buffer
- # pdf_buffer.seek(0) # Ensure buffer is at the beginning
- # return send_file(
- # pdf_buffer,
- # as_attachment=True,
- # download_name='event_report.pdf',
- # mimetype='application/pdf'
- # )
- # Option 2: Return PDF as Base64 (commented out)
- pdf_data = merged_buffer.getvalue()
- pdf_base64 = base64.b64encode(pdf_data).decode('utf-8')
- response = {
- 'state': task.state,
- 'message': 'Report generated successfully.',
- 'pdf_data': pdf_base64 # Base64-encoded PDF
- }
- return jsonify(response), 200
- except Exception as e:
- # logger.exception("Error generating PDF")
- return jsonify({'error': f'Error generating PDF: {str(e)}'}), 500
- else:
- # logger.error("Merged buffer is empty.")
- return jsonify({'error': 'Failed to generate PDF.'}), 500
- elif task.state == 'FAILURE':
- # logger.error("Task Failure: %s", task.info)
- return jsonify({
- 'state': task.state,
- 'error': str(task.info)
- }), 500
- else:
- return jsonify({'state': task.state, 'message': 'Report is not ready yet.'}), 202
Add Comment
Please, Sign In to add comment