-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb.py
68 lines (52 loc) · 1.8 KB
/
web.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
from distutils.log import debug
import flask
from flask import Flask, request, render_template, Response
import logging
import argparse
import serial
from numpy import broadcast
from persona_bot import persona_bot
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = Flask(__name__)
app.config['SERIAL_TIMEOUT'] = 0.2
app.config['SERIAL_PORT'] = '/dev/cu.usbmodemHIDPC1'
app.config['SERIAL_BAUDRATE'] = 9600
app.config['SERIAL_BYTESIZE'] = 8
app.config['SERIAL_PARITY'] = 'N'
app.config['SERIAL_STOPBITS'] = 1
logging.info("Starting up bot")
parser = argparse.ArgumentParser(description=None)
parser.add_argument("-p", "--persona", default="guru")
args = parser.parse_args()
persona = args.persona
bot = persona_bot(persona_name=persona, log_level=logging.DEBUG)
@app.route('/get')
def bot_response(question=None):
question = request.args.get("msg")
logger.info("Q: " + question)
response = bot.ask(question)
logger.info("A: " + response)
return response
@app.route('/')
def chat():
# template largely based off of chatterbot python implementation
# https://github.com/chamkank/flask-chatterbot
return render_template('index.html', persona=bot.persona)
def event_barcode():
ser = serial.Serial()
ser.port = app.config['SERIAL_PORT']
ser.baudrate = app.config['SERIAL_BAUDRATE']
ser.bytesize = app.config['SERIAL_BYTESIZE']
ser.parity = app.config['SERIAL_PARITY']
ser.stopbits = app.config['SERIAL_STOPBITS']
ser.open()
s = ser.read(1)
yield 'data: %s\n\n' % s
@app.route('/barcode')
def barcode():
newresponse = flask.Response(event_barcode(), mimetype="text/event-stream")
newresponse.headers.add('Access-Control-Allow-Origin', '*')
return newresponse
if __name__ == '__main__':
app.run(debug=True, port=8000)