Jann/app.py
def model_reply():
    """Flask route to respond to inference request."""

    # Initialize a blank message and blank response
    message = None
    resp = None
    return_all_samples = False

    # Log the request
    tf.logging.info(request)

    # If msg is set in the request arguments, this is direct to URL
    if 'msg' in request.args:
        message = request.args.get('msg')
        tf.logging.info('message from msg argument: {}'.format(message))
    else:
        if 'msgs' in request.args:
            message = request.args.get('msgs')
            tf.logging.info('message from msgs argument: {}'.format(message))
            return_all_samples = True
        else:
            # This is a dialogflow request, follow the Dialogflow protocol
            data_json = request.get_json(silent=False, force=True)
            tf.logging.debug('JSON Data: {}'.format(data_json))
            try:
                message = data_json["queryResult"]["queryText"]
            except TypeError as e:
                tf.logging.debug(e)

    # If the message exists, then use it to generate an inference
    if message:
        try:
            nns, distances = gen_model_use.inference(message)
            # print all the returned responses, and distance to input
            for nn, distance in zip(nns, distances):
                tf.logging.info('d: {}, {}'.format(
                    distance,
                    unique_strings[nn].split('\t')))  # args.delimiter

            if return_all_samples:
                # Return all the closest N neighbors
                neighbor_sample = nns[:sample_from_n_neighbors]
            else:
                # Sample from the closest N neighbors
                neighbor_sample = [random.choice(nns[:sample_from_n_neighbors])]

            # handle both the pairs and no pairs cases
            if not opts.no_pairs:
                # neighbor is index 0 and the response is index 1
                gen_resp = '\t'.join([unique_strings[idx].split('\t')[1]
                                    for idx in neighbor_sample])
            else:
                # otherwise, just take the first line
                gen_resp = '\t'.join([unique_strings[idx].split('\t')[0]
                                      for idx in neighbor_sample])

            # Build the response as the fulfillment text
            resp = {'fulfillmentText': gen_resp}

        except Exception as error:
            tf.logging.error('Generative model response error', error)
            resp = {'fulfillmentText': 'None'}
    else:
        tf.logging.error('No message error')
        resp = {'fulfillmentText': 'None'}

    # return the response in a json object
    return jsonify(resp)
Flask route to respond to inference request.
Flask route to respond to inference request.
Flask route to respond to inference request .
def model_reply():
    message = None
    resp = None
    return_all_samples = False
    tf.logging.info(request)
    if 'msg' in request.args:
        message = request.args.get('msg')
        tf.logging.info('message from msg argument: {}'.format(message))
    else:
        if 'msgs' in request.args:
            message = request.args.get('msgs')
            tf.logging.info('message from msgs argument: {}'.format(message))
            return_all_samples = True
        else:
            data_json = request.get_json(silent=False, force=True)
            tf.logging.debug('JSON Data: {}'.format(data_json))
            try:
                message = data_json["queryResult"]["queryText"]
            except TypeError as e:
                tf.logging.debug(e)
    if message:
        try:
            nns, distances = gen_model_use.inference(message)
            for nn, distance in zip(nns, distances):
                tf.logging.info('d: {}, {}'.format(
                    distance,
                    unique_strings[nn].split('\t')))  
            if return_all_samples:
                neighbor_sample = nns[:sample_from_n_neighbors]
            else:
                neighbor_sample = [random.choice(nns[:sample_from_n_neighbors])]
            if not opts.no_pairs:
                gen_resp = '\t'.join([unique_strings[idx].split('\t')[1]
                                    for idx in neighbor_sample])
            else:
                gen_resp = '\t'.join([unique_strings[idx].split('\t')[0]
                                      for idx in neighbor_sample])
            resp = {'fulfillmentText': gen_resp}
        except Exception as error:
            tf.logging.error('Generative model response error', error)
            resp = {'fulfillmentText': 'None'}
    else:
        tf.logging.error('No message error')
        resp = {'fulfillmentText': 'None'}
    return jsonify(resp)
def model_reply ( ) : message = None resp = None return_all_samples = False tf . logging . info ( request ) if 'msg' in request . args : message = request . args . get ( 'msg' ) tf . logging . info ( 'message from msg argument: {}' . format ( message ) ) else : if 'msgs' in request . args : message = request . args . get ( 'msgs' ) tf . logging . info ( 'message from msgs argument: {}' . format ( message ) ) return_all_samples = True else : data_json = request . get_json ( silent = False , force = True ) tf . logging . debug ( 'JSON Data: {}' . format ( data_json ) ) try : message = data_json [ "queryResult" ] [ "queryText" ] except TypeError as e : tf . logging . debug ( e ) if message : try : nns , distances = gen_model_use . inference ( message ) for nn , distance in zip ( nns , distances ) : tf . logging . info ( 'd: {}, {}' . format ( distance , unique_strings [ nn ] . split ( '\t' ) ) ) if return_all_samples : neighbor_sample = nns [ : sample_from_n_neighbors ] else : neighbor_sample = [ random . choice ( nns [ : sample_from_n_neighbors ] ) ] if not opts . no_pairs : gen_resp = '\t' . join ( [ unique_strings [ idx ] . split ( '\t' ) [ 1 ] for idx in neighbor_sample ] ) else : gen_resp = '\t' . join ( [ unique_strings [ idx ] . split ( '\t' ) [ 0 ] for idx in neighbor_sample ] ) resp = { 'fulfillmentText' : gen_resp } except Exception as error : tf . logging . error ( 'Generative model response error' , error ) resp = { 'fulfillmentText' : 'None' } else : tf . logging . error ( 'No message error' ) resp = { 'fulfillmentText' : 'None' } return jsonify ( resp )
Flask route to respond to inference request.
Flask route to respond to inference request .
"""Flask route to respond to inference request.""" # Initialize a blank message and blank response # Log the request # If msg is set in the request arguments, this is direct to URL # This is a dialogflow request, follow the Dialogflow protocol # If the message exists, then use it to generate an inference # print all the returned responses, and distance to input # args.delimiter # Return all the closest N neighbors # Sample from the closest N neighbors # handle both the pairs and no pairs cases # neighbor is index 0 and the response is index 1 # otherwise, just take the first line # Build the response as the fulfillment text # return the response in a json object