-
Notifications
You must be signed in to change notification settings - Fork 1
/
webapp.py
53 lines (37 loc) · 1.52 KB
/
webapp.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
# following three lines should have come first than any other imports.
import gevent.monkey
gevent.monkey.patch_all()
import sys
import json
import os
import gevent
from flask import Flask, render_template
from flask_socketio import SocketIO
from detox_engine import ToxicityClassifier
from joblib import load
import constant
app = Flask(__name__)
app.config['SECRET_KEY'] = 'asdf1234'
socketio = SocketIO(app)
@app.before_first_request
def load_module():
print("initiating before first request...")
# do nothing for now
@app.route('/', methods=['GET', 'POST'] )
def sessions():
return render_template('session.html')
def messageReceived(methods=['GET', 'POST']):
print('message was received!!!')
@socketio.on('my event')
def handle_my_custom_event(msg, methods=['GET', 'POST']):
toxicityClassifier = ToxicityClassifier()
print('received my event: username:' + msg['username'] + ', message:' + msg['message'] )
print('chat toxicity analyzed...' + str(toxicityClassifier.isToxic( msg['message'] )))
if toxicityClassifier.isToxic(msg['message']) == True:
socketio.emit('my response', { 'username': msg['username'], 'message': msg['message'], 'is_toxic': '1'}, callback=messageReceived)
else:
socketio.emit('my response', { 'username': msg['username'], 'message': msg['message'], 'is_toxic': '0'}, callback=messageReceived)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
socketio.run(app, debug=True, host='0.0.0.0', port=port)
print("application has started.")