from flask import Flask, render_template, request, jsonify from backend import generate_research_report import threading import queue app = Flask(__name__) # Use a queue to store results result_queue = queue.Queue() def background_task(query): try: result = generate_research_report(query) result_queue.put(result) except Exception as e: result_queue.put(f"Error: {str(e)}") @app.route('/') def index(): return render_template('index.html') @app.route('/research', methods=['POST']) def research(): query = request.json['query'] # Start the background task thread = threading.Thread(target=background_task, args=(query,)) thread.start() # Wait for the result with a timeout try: result = result_queue.get(timeout=6000) # 60 second timeout except queue.Empty: result = "The research is taking longer than expected. Please try again with a simpler query." return jsonify({'result': str(result)}) if __name__ == "__main__": app.run(host='0.0.0.0', port=7860)