Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Fixes audio rest api #205

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 45 additions & 17 deletions lucida/botframework-interface/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ Description : This file handles dialogs from bot framework, forwards messages
var restify = require('restify')
var server = restify.createServer()
var builder = require('botbuilder')
var calling = require('botbuilder-calling')
var request = require('request')
var credentials = require('./credentials')
var url = require("url")
var url = require('url')
var bfw_port
var cc_host
var cc_api_host
var cc_ws_host
var WebSocket = require('ws')

var util = require('util')

//=========================================================
// Bot Setup
Expand All @@ -37,12 +42,16 @@ function check_bfw_port(str_port) {
function check_cc_host(host) {
match = host.match(/^((\d+\.\d+\.\d+\.\d+)|localhost)(:\d+)?(.*)?$/)
if ( match != null ) {
cc_host = "http://" + host.replace(/\/$/, "")
console.log("[INFO] Remote command center host is set to " + cc_host)
cc_api_host = "http://" + host.replace(/\/$/, "") + '/api'
cc_ws_host = "ws://" + host.replace(/\/$/, "") + '/ws'
console.log("[INFO] Remote command center API host is set to " + cc_api_host)
console.log("[INFO] Remote command center WS host is set to " + cc_ws_host)
return true
} else if ( url.parse(host)['host'] != null ) {
cc_host = host.replace(/\/$/, "")
console.log("[INFO] Remote command center host is set to " + cc_host)
cc_api_host = host.replace(/\/$/, "") + '/api'
cc_ws_host = cc_api_host.replace(/^http/, "ws") + '/ws'
console.log("[INFO] Remote command center API host is set to " + cc_api_host)
console.log("[INFO] Remote command center WS host is set to " + cc_ws_host)
return true
} else {
return false
Expand Down Expand Up @@ -78,28 +87,39 @@ function check_args() {
}
check_args()

// Create web socket
var ws = new WebSocket(cc_ws_host + '/status')

ws.on('message', function incoming(data) {
console.log("WS RECVD: " + data);
});

// Create chat bot
var connector = new builder.ChatConnector(credentials.credentials)
var bot = new builder.UniversalBot(connector)
server.post('/api/messages', connector.listen())
var chat_connector = new builder.ChatConnector(credentials.chat_credentials)
var chat_bot = new builder.UniversalBot(chat_connector)
server.post('/api/messages', chat_connector.listen())
var call_connector = new calling.CallConnector(credentials.call_credentials)
var call_bot = new calling.UniversalCallBot(call_connector);
server.post('/api/calls', call_connector.listen());

//=========================================================
// Bots Dialogs
//=========================================================

var addresses = {}
var chat_addresses = {}

bot.dialog('/', [
chat_bot.dialog('/', [
function (session) {
console.log(util.inspect(session.message.address))
var address = session.message.address
addresses[address.channelId] = {channelId: address.channelId, bot: {id: address.bot.id, name: address.bot.name}, serviceUrl: address.serviceUrl, useAuth: address.useAuth}
chat_addresses[address.channelId] = {channelId: address.channelId, bot: {id: address.bot.id, name: address.bot.name}, serviceUrl: address.serviceUrl, useAuth: address.useAuth}
request.post({
headers: {'content-type' : 'application/x-www-form-urlencoded'},
form: { interface: session.message.address.channelId, username: session.message.address.user.id, text_input: session.message.text },
url: cc_host + '/api/infer',
url: cc_api_host + '/infer',
form: { interface: session.message.address.channelId, username: session.message.address.user.id, speech_input: session.message.text }
}, function(error, response, body){
address = addresses[session.message.address.channelId]
address = chat_addresses[session.message.address.channelId]
address['user'] = { id: session.message.address.user.id }
if (error) {
text = "Error occured '" + error.code + "'!!! Is command center running?"
Expand All @@ -112,7 +132,7 @@ bot.dialog('/', [
if ( result ) {
request.post({
headers: {'content-type' : 'application/x-www-form-urlencoded'},
url: cc_host + '/api/add_interface',
url: cc_api_host + '/add_interface',
form: { interface: session.message.address.channelId, token: result[1], username: session.message.address.user.id }
}, function(error, response, body){
if (error) {
Expand All @@ -127,7 +147,7 @@ bot.dialog('/', [
text = response.statusCode + " " + response.statusMessage + " received. Go through the logs and figure. Otherwise create an issue on github with logs attached."
}
var reply = new builder.Message().address(address).text(text)
bot.send(reply)
chat_bot.send(reply)
})
return
} else {
Expand All @@ -139,7 +159,15 @@ bot.dialog('/', [
text = response.statusCode + " " + response.statusMessage + " received. Go through the logs and figure. Otherwise create an issue on github with logs attached."
}
var reply = new builder.Message().address(address).text(text)
bot.send(reply)
chat_bot.send(reply)
})
}
])


call_bot.dialog('/', [
function (session) {
session.send('Hey there! How can I help you?');
console.log(util.inspect(session.message))
}
])
3 changes: 2 additions & 1 deletion lucida/botframework-interface/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"botbuilder-calling": "^3.0.1",
"request": "^2.81.0",
"restify": "^4.3.0",
"url": "^0.11.0"
"url": "^0.11.0",
"ws": "^3.0.0"
}
}
7 changes: 6 additions & 1 deletion lucida/botframework-interface/start_interface.sh
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,12 @@ rm -f phantom.out

cat << EOF > credentials.js
// DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
exports.credentials = {
exports.chat_credentials = {
appId: '$BFW_APPID',
appPassword: '$BFW_APPPWD'
}
exports.call_credentials = {
callbackUrl: '$BFW_HOST/api/calls',
appId: '$BFW_APPID',
appPassword: '$BFW_APPPWD'
}
Expand Down
65 changes: 13 additions & 52 deletions lucida/commandcenter/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,27 @@
from __future__ import division
from __future__ import unicode_literals

import sys, glob, os
sys.path.insert(0, glob.glob(os.path.abspath(os.path.dirname(__file__)) +
'/../../tools/thrift-0.9.3/lib/py/build/lib*')[0])

from controllers import *
from controllers.FlaskApp import app
from controllers import WebServer
from controllers.Parser import cmd_port
from flask import *
from threading import Thread
import logging
import os

def main():
cmd_host = '0.0.0.0'
logging.basicConfig(level=logging.DEBUG, format="%(levelname)8s %(asctime)s %(message)s ")
WebServer.tornado.options.parse_command_line()

# Initialize the Flask app with the template folder address.
app = Flask(__name__, template_folder='templates')

# app.config.from_object('config')
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB due to MongoDB

# Register the controllers.
app.register_blueprint(Main.main)
app.register_blueprint(User.user)
app.register_blueprint(Create.create)
app.register_blueprint(Learn.learn)
app.register_blueprint(Infer.infer)

# Session.
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'

def flask_listener():

# For https (with ASR capability)
if os.environ.get('SECURE_HOST'):
print 'Starting secure flask'
app.run(host='0.0.0.0', port=3000, debug=True, use_reloader=False,
threaded=True, ssl_context=('certs/server.crt', 'certs/server.key'))
# For http (without ASR capability)
else:
print 'Starting non-secure flask'
app.run(host='0.0.0.0', port=3000, debug=True, use_reloader=False,
threaded=True)

def web_socket_listener():
print 'Start web socket at ' + str(cmd_port)
logging.basicConfig(level=logging.DEBUG,
format="%(levelname)8s %(asctime)s %(message)s ")
logging.debug('Starting up server')
WebSocket.tornado.options.parse_command_line()

# For wss (with ASR capability)
if os.environ.get('SECURE_HOST'):
print 'Starting secure web socket'
WebSocket.Application().listen(cmd_port, ssl_options={
logging.info('Spinning up web server at https://' + str(cmd_host) + ':' + str(cmd_port))
WebServer.Application(app).listen(cmd_port, address=str(cmd_host), ssl_options={
"certfile":"certs/server.crt",
"keyfile":"certs/server.key"})
# For ws (without ASR capability)
else:
print 'Starting non-secure web socket'
WebSocket.Application().listen(cmd_port)
logging.info('Spinning up web server at http://' + str(cmd_host) + ':' + str(cmd_port))
WebServer.Application(app).listen(cmd_port, address=str(cmd_host))

WebSocket.tornado.ioloop.IOLoop.instance().start()
WebServer.tornado.ioloop.IOLoop.instance().start()

if __name__ == '__main__':
Thread(target = flask_listener).start()
web_socket_listener()
main()
7 changes: 5 additions & 2 deletions lucida/commandcenter/controllers/Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ def username_exists(self, username):

#Returns true if the username already exists.
def get_username(self, interface, interface_uid):
interface += "_interface"
row = self.users.find_one({interface: interface_uid});
if interface and interface != "web":
interface += "_interface"
row = self.users.find_one({interface: interface_uid})
else:
row = self.users.find_one({'username': interface_uid})
if not row is None:
return row['username']
return None
Expand Down
33 changes: 33 additions & 0 deletions lucida/commandcenter/controllers/FlaskApp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import sys, glob, os
sys.path.insert(0, glob.glob(os.path.abspath(os.path.dirname(__file__)) +
'/../../../tools/thrift-0.9.3/lib/py/build/lib*')[0])

from . import Main
from . import User
from . import Create
from . import Learn
from . import Infer
from .Parser import cmd_port
from flask import *
import logging


# Initialize the Flask app with the template folder address.
app = Flask(__name__, template_folder='../templates')

# app.config.from_object('config')
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16 MB due to MongoDB

# Register the controllers.
app.register_blueprint(Main.main)
app.register_blueprint(User.user)
app.register_blueprint(Create.create)
app.register_blueprint(Learn.learn)
app.register_blueprint(Infer.infer)

# Session.
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
2 changes: 1 addition & 1 deletion lucida/commandcenter/controllers/ThriftClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def infer(self, LUCID, workflow_name, text_data, image_data):

i = 0
for x in resultText:
resultText[i] = [unicode(resultText)] # Text information must be unicode'd and array'd to be properly passed. IMAGE DATA DOES NOT HAVE THIS DONE TO IT.
resultText[i] = [unicode(x)] # Text information must be unicode'd and array'd to be properly passed. IMAGE DATA DOES NOT HAVE THIS DONE TO IT.
i+= 1

# Processes the current workflow state, and in the process finds if this is the final stage or if next stage exists.
Expand Down
Loading