Spaces:
Runtime error
Runtime error
File size: 1,097 Bytes
2376116 d7ab09b 2376116 59d5d05 2376116 59d5d05 f4af02c 2376116 6cb4a02 f7be4fc 2376116 f7be4fc 6cb4a02 aa2ddf1 2376116 aa9cf31 |
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
from flask_socketio import SocketIO, emit
from flask_cors import cross_origin, CORS
from main import run
import concurrent.futures
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, cors_allowed_origins="*")
cors = CORS(app)
max_workers=4
executor = concurrent.futures.ThreadPoolExecutor(max_workers)
@app.route('/')
def index():
return render_template('index.html')
@socketio.on('message')
def handle_message(data):
question = data['question']
print("question: " + question)
if executor._work_queue.qsize() >= max_workers:
emit('response', {'response': 'Server is busy, please try again later'})
return
try:
future = executor.submit(run, question)
response = future.result()
emit('response', {'response': response})
except Exception as e:
print(f"Error processing request: {str(e)}")
emit('response', {'response': 'Server is busy. Please try again later.'})
if __name__ == '__main__':
socketio.run(app, host="0.0.0.0", port=7860)
|