def infer_route(): options = {} if os.environ.get('ASR_ADDR_PORT'): options['asr_addr_port'] = os.environ.get('ASR_ADDR_PORT') else: options['asr_addr_port'] = 'ws://*****:*****@@@@@@@@@@', speech_input image_input = [upload_file.read()] if upload_file else None lucida_id = session['username'] # Check if context is saved for Lucida user # If not, classify query, otherwise restore session if lucida_id not in Config.SESSION: services_needed = \ query_classifier.predict(speech_input, upload_file) speech_input = [speech_input] else: services_needed = Config.SESSION[lucida_id]['graph'] Config.SESSION[lucida_id]['data']['text'].append( speech_input) speech_input = Config.SESSION[lucida_id]['data']['text'] options['result'] = thrift_client.infer(lucida_id, \ services_needed, speech_input, image_input) log('Result ' + options['result']) # Check if Calendar service is needed. # If so, JavaScript needs to receive the parsed dates. if services_needed.has_service('CA'): options['dates'] = options['result'] options['result'] = None return render_template('infer.html', **options) else: raise RuntimeError('Did you click the Ask button?') except Exception as e: log(e) if str(e) == 'TSocket read 0 bytes': e = 'Back-end service encountered a problem' options['error'] = e return render_template('infer.html', **options) # Display. return render_template('infer.html', **options)
def generic_infer_route(form, upload_file): options = {} if os.environ.get('ASR_ADDR_PORT'): options['asr_addr_port'] = os.environ.get('ASR_ADDR_PORT') else: options['asr_addr_port'] = 'ws://localhost:' + port_dic["cmd_port"] try: # Deal with POST requests. if request.method == 'POST': if not upload_file is None and upload_file.filename != '': check_image_extension(upload_file) # Classify the query. speech_input = form['speech_input'] if 'speech_input' in form \ else '' print 'Query: ', speech_input image_input = [upload_file.read()] if upload_file else None lucida_id = session['username'] # Check if context is saved for Lucida user # If not, classify query, otherwise restore session if lucida_id not in Config.SESSION: services_needed = query_classifier.predict( speech_input, upload_file) speech_input = [speech_input] else: services_needed = Config.SESSION[lucida_id]['graph'] Config.SESSION[lucida_id]['data']['text'].append(speech_input) speech_input = Config.SESSION[lucida_id]['data']['text'] node = services_needed.get_node(0) try: options['result'] = thrift_client.infer( lucida_id, node.service_name, speech_input, image_input) except Exception as ex: print "Exception raised while trying to infer", ex.message options['error'] = str(ex) raise if 'result' in options: log('Result ' + options['result']) # Check if Calendar service is needed. # If so, JavaScript needs to receive the parsed dates. if services_needed.has_service('CAWF'): options['dates'] = options['result'] options['result'] = None else: options['error'] = "Result was empty." except Exception as e: log(e) options['errno'] = "Unknown" options['error'] = str(e.message) if 'code' in e and re.match("^4\d\d$", str(e.code)): options['errno'] = e.code if str(e) == 'TSocket read 0 bytes': options['error'] = 'Back-end service encountered a problem' if str(e).startswith('Could not connect to'): options['error'] = 'Back-end service is not running' return options
def generic_infer_route(form, upload_file): options = {} if os.environ.get('ASR_ADDR_PORT'): options['asr_addr_port'] = os.environ.get('ASR_ADDR_PORT') else: options['asr_addr_port'] = 'ws://*****:*****@@@@@@@@@@', speech_input image_input = [upload_file.read()] if upload_file else None lucida_id = session['username'] # Check if context is saved for Lucida user # If not, classify query, otherwise restore session if lucida_id not in Config.SESSION: services_needed = query_classifier.predict(speech_input, upload_file) speech_input = [speech_input] else: services_needed = Config.SESSION[lucida_id]['graph'] Config.SESSION[lucida_id]['data']['text'].append(speech_input) speech_input = Config.SESSION[lucida_id]['data']['text'] node = services_needed.get_node(0) options['result'] = thrift_client.infer(lucida_id, node.service_name, speech_input, image_input) log('Result ' + options['result']) # Check if Calendar service is needed. # If so, JavaScript needs to receive the parsed dates. if services_needed.has_service('CA'): options['dates'] = options['result'] options['result'] = None except Exception as e: log(e) options['errno'] = 500 options['error'] = str(e) if 'code' in e and re.match("^4\d\d$", str(e.code)): options['errno'] = e.code if str(e) == 'TSocket read 0 bytes': options['error'] = 'Back-end service encountered a problem' if str(e).startswith('Could not connect to'): options['error'] = 'Back-end service is not running' return options
def infer_route(): options = {} if os.environ.get('ASR_ADDR_PORT'): options['asr_addr_port'] = os.environ.get('ASR_ADDR_PORT') else: options['asr_addr_port'] = 'ws://*****:*****@@@@@@@@@@', form['speech_input'] # Classify the query. services_needed = \ query_classifier.predict(form['speech_input'], upload_file) options['result'] = thrift_client.infer( session['username'], services_needed, form['speech_input'], upload_file.read()) log('Result ' + options['result']) # Check if Calendar service is needed. # If so, JavaScript needs to receive the parsed dates. if services_needed.has_service('CA'): options['dates'] = options['result'] options['result'] = None return render_template('infer.html', **options) else: raise RuntimeError('Did you click the Ask button?') except Exception as e: log(e) if str(e) == 'TSocket read 0 bytes': e = 'Back-end service encountered a problem' options['error'] = e return render_template('infer.html', **options) # Display. return render_template('infer.html', **options)
def infer_route(): options = {} if os.environ.get("ASR_ADDR_PORT"): options["asr_addr_port"] = os.environ.get("ASR_ADDR_PORT") else: options["asr_addr_port"] = "ws://*****:*****@@@@@@@@@@", form["speech_input"] # Classify the query. services_needed = query_classifier.predict(form["speech_input"], upload_file) options["result"] = thrift_client.infer( session["username"], services_needed, form["speech_input"], upload_file.read() ) log("Result " + options["result"]) # Check if Calendar service is needed. # If so, JavaScript needs to receive the parsed dates. if services_needed.has_service("CA"): options["dates"] = options["result"] options["result"] = None return render_template("infer.html", **options) else: raise RuntimeError("Did you click the Ask button?") except Exception as e: log(e) if str(e) == "TSocket read 0 bytes": e = "Back-end service encountered a problem" options["error"] = e return render_template("infer.html", **options) # Display. return render_template("infer.html", **options)
def learn_route(): options = {} username = session['username'] try: form = request.form # Deal with POST requests. if request.method == 'POST': # If the request does not contain an "op" field. if not 'op' in request.form: raise RuntimeError('Did you click the button?') # Add image knowledge. elif form['op'] == 'add_image': image_type = 'image' label = form['label'] # Check the uploaded image. upload_file = request.files['file'] if upload_file.filename == '': raise RuntimeError('Empty file is not allowed') check_image_extension(upload_file) # Check the label of the image. check_text_input(label) # Check whether the user can add one more image. database.check_add_image(username) # Generate the id. image_data = upload_file.read() image_id = hashlib.md5( username + str(datetime.datetime.now())).hexdigest() # Send the image to IMM. upload_file.close() thrift_client.learn_image(username, image_type, image_data, image_id) # Add the image into the database. database.add_image(username, image_data, label, image_id) # Delete image knowledge. elif form['op'] == 'delete_image': image_type = 'unlearn' image_id = form['image_id'] # Send the unlearn request to IMM. thrift_client.learn_image(username, image_type, '', image_id) # Delete the image from the database. database.delete_image(username, image_id) # Add text knowledge. elif form['op'] == 'add_text' or form['op'] == 'add_url': text_type = 'text' if form['op'] == 'add_text' else 'url' text_data = form['knowledge'] # Check the text knowledge. check_text_input(text_data) # Check whether the user can add one more piece of text. database.check_add_text(username) # Generate the id. text_id = hashlib.md5( username + text_data + str(datetime.datetime.now())).hexdigest() # Send the text to QA. thrift_client.learn_text(username, text_type, text_data, text_id) # Add the text knowledge into the database. database.add_text(username, text_type, text_data, text_id) # Delete text knowledge. elif form['op'] == 'delete_text': text_type = 'unlearn' text_id = form['text_id'] # Send the unlearn request to QA. thrift_client.learn_text(username, text_type, '', text_id) # Delete the text from into the database. database.delete_text(username, text_id) else: raise RuntimeError('Did you click the button?') except Exception as e: log(e) if str(e) == 'TSocket read 0 bytes': e = 'Back-end service encountered a problem' options['error'] = e try: # Retrieve knowledge. options['pictures'] = database.get_images(username) options['text'] = database.get_text(username) except Exception as e: log(e) options['error'] = e return render_template('learn.html', **options)
def generic_learn_route(op, form, upload_file): options = {} username = session['username'] try: # Add image knowledge. if op == 'add_image': image_type = 'image' label = form['label'] # Check the uploaded image. if upload_file.filename == '': raise RuntimeError('Empty file is not allowed') check_image_extension(upload_file) # Check the label of the image. check_text_input(label) # Check whether the user can add one more image. database.check_add_image(username) # Generate the id. image_data = upload_file.read() image_id = hashlib.md5(username + str(datetime.datetime.now())).hexdigest() # Send the image to IMM. upload_file.close() thrift_client.learn_image(username, image_type, image_data, image_id) # Add the image into the database. database.add_image(username, image_data, label, image_id) # Delete image knowledge. elif op == 'delete_image': image_type = 'unlearn' image_id = form['image_id'] # Send the unlearn request to IMM. thrift_client.learn_image(username, image_type, '', image_id) # Delete the image from the database. database.delete_image(username, image_id) # Add text knowledge. elif op == 'add_text' or op == 'add_url': text_type = 'text' if op == 'add_text' else 'url' text_data = form['knowledge'] # Check the text knowledge. check_text_input(text_data) # Check whether the user can add one more piece of text. database.check_add_text(username) # Generate the id. text_id = hashlib.md5(username + text_data + str(datetime.datetime.now())).hexdigest() # Send the text to QA. thrift_client.learn_text(username, text_type, text_data, text_id) # Add the text knowledge into the database. database.add_text(username, text_type, text_data, text_id) # Delete text knowledge. elif op == 'delete_text': text_type = 'unlearn' text_id = form['text_id'] # Send the unlearn request to QA. thrift_client.learn_text(username, text_type, '', text_id) # Delete the text from into the database. database.delete_text(username, text_id) else: raise RuntimeError('Did you click the button?') except Exception as e: log(e) options['errno'] = 500 options['error'] = str(e) if 'code' in e and re.match("^4\d\d$", str(e.code)): options['errno'] = e.code if str(e) == 'TSocket read 0 bytes': options['error'] = 'Back-end service encountered a problem' if str(e).startswith('Could not connect to'): options['error'] = 'Back-end service is not running' return options
def learn_route(): options = {} username = session['username'] try: form = request.form # Deal with POST requests. if request.method == 'POST': # If the request does not contain an "op" field. if not 'op' in request.form: raise RuntimeError('Did you click the button?') # Add image knowledge. elif form['op'] == 'add_image': image_type = 'image' label = form['label'] # Check the uploaded image. upload_file = request.files['file'] if upload_file.filename == '': raise RuntimeError('Empty file is not allowed') check_image_extension(upload_file) # Check the label of the image. check_text_input(label) # Check whether the user can add one more image. database.check_add_image(username) # Send the image to IMM. image_data = upload_file.read() upload_file.close() thrift_client.learn_image(username, image_type, image_data, label) # Add the image into the database. database.add_image(username, image_data, label) # Delete image knowledge. elif form['op'] == 'delete_image': image_type = 'unlearn' label = form['label'] # Send the unlearn request to IMM. thrift_client.learn_image(username, image_type, '', label) # Delete the image from the database. database.delete_image(username, label) # Add text knowledge. elif form['op'] == 'add_text' or form['op'] == 'add_url': text_type = 'text' if form['op'] == 'add_text' else 'url' text_data = form['knowledge'] # Check the text knowledge. check_text_input(text_data) # Check whether the user can add one more piece of text. database.check_add_text(username) # Generate the id. text_id = hashlib.md5(username + text_data + str(datetime.datetime.now())).hexdigest() # Send the text to QA. thrift_client.learn_text(username, text_type, text_data, text_id) # Add the text knowledge into the database. database.add_text(username, text_type, text_data, text_id) # Delete text knowledge. elif form['op'] == 'delete_text': text_type = 'unlearn' text_id = form['text_id'] # Send the unlearn request to QA. thrift_client.learn_text(username, text_type, '', text_id) # Delete the text from into the database. database.delete_text(username, text_id) else: raise RuntimeError('Did you click the button?') except Exception as e: log(e) if str(e) == 'TSocket read 0 bytes': e = 'Back-end service encountered a problem' options['error'] = e try: # Retrieve knowledge. options['pictures'] = database.get_images(username) options['text'] = database.get_text(username) except Exception as e: log(e) options['error'] = e return render_template('learn.html', **options)