-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_handle.py
55 lines (39 loc) · 1.36 KB
/
server_handle.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
from flask import Flask, render_template, request
from flask_socketio import SocketIO
import logging
import yaml
import pipeline
import utils.mongo as mongo
server = Flask(__name__)
server.config['SECRET_KEY'] = 'edubotkey'
socketio = SocketIO(server, cors_allowed_origins="*")
# Logging
conf = yaml.safe_load(open("config.yaml"))
logger = logging.getLogger(__name__)
logger.setLevel(conf["log_level"])
logging.basicConfig(level=conf["log_level"])
@server.route('/')
def index():
return render_template('index.html')
@socketio.on('connection')
def connection(msg):
socket_id = request.sid
# Send a welcome message
socketio.emit('ai_message', 'Bonjour, je suis StudyMate, comment puis-je vous aider ?', to=socket_id)
@socketio.on('disconnection')
def disconnect(msg):
logger.info('User disconnected')
@socketio.on('user_message')
def handle_message(msg):
logger.debug('User message: ' + msg['message'])
# Get the user ID and message
user_id = msg['user_id']
user_message = msg['message']
socket_id = request.sid
# Get the bot response
response = pipeline.educhat(user_id, user_message)
# Save the conversation
mongo.insert_message(user_message, user_id, is_bot=False)
mongo.insert_message(response, user_id, is_bot=True)
# Send the response to the user
socketio.emit('ai_message', response, to=socket_id)