Spaces:
Runtime error
Runtime error
File size: 1,065 Bytes
d1f375f df71c7b d1f375f |
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 |
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) |