-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
74 lines (58 loc) · 2.21 KB
/
main.py
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
import os
import time
from dotenv import load_dotenv
from flask import Flask, request, jsonify, render_template
from flask_socketio import SocketIO, emit
from utils.sootiai_web import Agent
# Load .env file
load_dotenv()
# Get values from .env
base_api = os.getenv("BASE_API")
base_url = os.getenv("BASE_URL")
app = Flask(__name__)
app.secret_key = 'your_secret_key'
socketio = SocketIO(app)
agent = Agent(base_url=base_url, api_key=base_api)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/execute_task', methods=['POST'])
def execute_task():
agent.task_stopped = False
agent.stop_processing = False
task = request.json.get('task')
try:
agent.execute_task(task)
return jsonify({'status': 'success', 'message': 'Task executed successfully'})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)})
@socketio.on('clear')
def clear():
try:
agent.clear_global_history = True # Clear global history
agent.task_stopped = False
agent.stop_processing = False
print(agent.global_history)
return jsonify({'status': 'success', 'message': 'Tasks cleared successfully'})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)})
@socketio.on('stop_processing')
def stop_processing():
agent.stop_processing = True # Clear global history
emit('receive_message', {'status': 'error', 'message': 'Stopping task... please wait'})
@socketio.on('send_message')
def handle_send_message(data):
task = data.get('task')
try:
# Execute the task
agent.execute_task(task)
if agent.task_stopped is True:
emit('receive_message', {'status': 'error', 'message': 'Task execution stopped'})
else:
emit('receive_message', {'status': 'completed', 'message': 'Task executed successfully'})
agent.task_stopped = False
except Exception as e:
# Handle any errors during task execution
emit('receive_message', {'status': 'error', 'message': str(e)})
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=8080, allow_unsafe_werkzeug=True)