def chat(args): with tf.Session() as sess: # Create model and load parameters. args.batch_size = 1 # We decode one sentence at a time. model = create_model(sess, args) # Load vocabularies. vocab_path = os.path.join(args.data_dir, "vocab%d.in" % args.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) # Decode from standard input. sys.stdout.write("> ") sys.stdout.flush() sentence = sys.stdin.readline() for sent in sentence: predicted_sentence = get_predicted_sentence( args, sentence, vocab, rev_vocab, model, sess) while sentence: predicted_sentence = get_predicted_sentence( args, sentence, vocab, rev_vocab, model, sess) # print(predicted_sentence) if isinstance(predicted_sentence, list): for sent in predicted_sentence: print(" (%s) -> %s" % (sent['prob'], sent['dec_inp'])) else: print(sentence, ' -> ', predicted_sentence) sys.stdout.write("> ") sys.stdout.flush() sentence = sys.stdin.readline()
def _get_predicted_sentence(self, sentence): return get_predicted_sentence( self.args, sentence, self.vocab, self.rev_vocab, self.model, self.session, )
def gen_response_debug(self, sent, args=None): sent = " ".join([w.lower() for w in jieba.cut(sent) if w not in [' ']]) raw = get_predicted_sentence(args, sent, self.vocab, self.rev_vocab, self.model, self.sess, debug=False, return_raw=True) return raw
def my_predict(): def _get_test_dataset(): with open(TEST_DATASET_PATH) as test_fh: test_sentences = [s.strip() for s in test_fh.readlines()] # print test_sentences # print '///////////////////////' return test_sentences results_filename = '_'.join([ '1results_left', str(FLAGS.num_layers), str(FLAGS.size), str(FLAGS.vocab_size) ]) results_path = os.path.join(FLAGS.results_dir, results_filename) with tf.Session() as sess, open(results_path, 'w') as results_fh: model = create_model(sess, forward_only=True) model.batch_size = 1 vocab_path = os.path.join(FLAGS.data_dir, "vocab%d.in" % FLAGS.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) test_dataset = _get_test_dataset() i = 0 j = 0 allright = 0 for sentence in test_dataset: if i % 2 == 0: predicted_sentence = get_predicted_sentence( sentence, vocab, rev_vocab, model, sess) print(predicted_sentence, ' -> ', sentence) end = sentence if i % 2 == 1: sentence = sentence + ' ' + end pre_sentence = predicted_sentence + ' ' + end if sentence == pre_sentence: allright += 1 print('^ is allright' + '\n') results_fh.write(sentence + '\n' + predicted_sentence + ' ' + end + '\n') else: print('Error~right is %s' % sentence) results_fh.write(sentence + '\n' + predicted_sentence + ' ' + end + '\n') i = i + 1 print 'traj=', i / 2, ',allright=', allright, ',accuracy=', allright * 1.0 / ( i * 1.0 / 2) results_fh.write('traj=%d,allright=%d,accuracy=%f' % (i / 2, allright, allright * 1.0 / (i * 1.0 / 2)))
def gen_response(self, sent, max_cand=100): sent = " ".join([w.lower() for w in jieba.cut(sent) if w not in [' ']]) print("================Received Response=========") print(sent) country = [ '美金 (USD) 美金 (USD)', '港幣 (HKD) 港幣 (HKD)', '英鎊 (GBP) 英鎊 (GBP)', '澳幣 (AUD) 澳幣 (AUD)', '加拿大幣 (CAD) 加拿大幣 (CAD)', '新加坡幣 (SGD) 新加坡幣 (SGD)', '瑞士法郎 (CHF) 瑞士法郎 (CHF)', '日圓 (JPY) 日圓 (JPY)', '南非幣 (ZAR) 南非幣 (ZAR)', '瑞典幣 (SEK) 瑞典幣 (SEK)', '紐元 (NZD) 紐元 (NZD)', '泰幣 (THB) 泰幣 (THB)', '菲國比索 (PHP) 菲國比索 (PHP)', '印尼幣 (IDR) 印尼幣 (IDR)', '歐元 (EUR) 歐元 (EUR)', '韓元 (KRW) 韓元 (KRW)', '越南盾 (VND) 越南盾 (VND)', '馬來幣 (MYR) 馬來幣 (MYR)', '人民幣 (CNY) 人民幣 (CNY)' ] if "匯款" in sent: return "需要匯多少錢(台幣)到哪個地區?" #sent_split = sent.split(' ') amount = [float(s) for s in sent.split() if s.isdigit()] sent_split = [s for s in sent.split() if not s.isdigit()] for sctr in sent_split: for idx, ctr in enumerate(country): if sctr in ctr: return "Ok! 今日的牌告匯率為" + str( currency.iloc[idx][1] ) + str(sctr) + "-台幣,換算約為: " + str( round(amount[0] / float(currency.iloc[idx][1]), 3)) + str(sctr) + "。每筆交易我們收取$150元手續費。您同意此筆交易進行嗎?" print("================Received End=========") # if self.debug: return sent raw = get_predicted_sentence(self.args, sent, self.vocab, self.rev_vocab, self.model, self.sess, debug=False) # find bests candidates cands = sorted(raw, key=lambda v: v['prob'], reverse=True)[:max_cand] if max_cand == -1: # return all cands for debug cands = [ (r['prob'], ' '.join([w for w in r['dec_inp'].split() if w[0] != '_'])) for r in cands ] return cands else: cands = [[w for w in r['dec_inp'].split() if w[0] != '_'] for r in cands] return ' '.join(choice(cands)) or 'No comment'
def predict(args, debug=False, parent=0): '''Тестирует модель на вопросы, записанные в файле test_dataset.txt и записывает результаты в файл ''' def _get_test_dataset(): with open(args.test_dataset_path) as test_fh: test_sentences = [s.strip() for s in test_fh.readlines()] return test_sentences results_filename = '_'.join([ 'results', str(args.num_layers), str(args.size), str(args.vocab_size) ]) results_path = os.path.join(args.results_dir, results_filename + '.txt') with tf.Session() as sess, open(results_path, 'w') as results_fh: # Создание модели и загрузка параметров. args.batch_size = 1 model = create_model(sess, args) # Загрузка словарей vocab_path = os.path.join(args.data_dir, "vocab%d.in" % args.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) test_dataset = _get_test_dataset() for sentence in test_dataset: # Get token-ids for the input sentence. predicted_sentence = get_predicted_sentence(args, sentence, vocab, rev_vocab, model, sess, debug=debug) if isinstance(predicted_sentence, list): print("%s : (%s)" % (sentence, datetime.now())) results_fh.write("%s : (%s)\n" % (sentence, datetime.now())) for sent in predicted_sentence: print(" (%s) -> %s" % (sent['prob'], sent['dec_inp'])) results_fh.write(" (%f) -> %s\n" % (sent['prob'], sent['dec_inp'])) else: print(sentence, ' -> ', predicted_sentence) results_fh.write("%s -> %s\n" % (sentence, predicted_sentence)) results_fh.close() print("Результаты теста записаны в %s" % results_path) if parent: parent.clearWidget()
def gen_response(self, sent, max_cand=100): sent = " ".join([w.lower() for w in jieba.cut(sent) if w not in [' ']]) # if self.debug: return sent raw = get_predicted_sentence(self.args, sent, self.vocab, self.rev_vocab, self.model, self.sess, debug=False) # find bests candidates cands = sorted(raw, key=lambda v: v['prob'], reverse=True)[:max_cand] if max_cand == -1: # return all cands for debug cands = [(r['prob'], ' '.join([w for w in r['dec_inp'].split() if w[0] != '_'])) for r in cands] return cands else: cands = [[w for w in r['dec_inp'].split() if w[0] != '_'] for r in cands] return ' '.join(choice(cands)) or 'No comment'
def predict(args, debug=False): def _get_test_dataset(): with open(args.test_dataset_path) as test_fh: test_sentences = [s.strip() for s in test_fh.readlines()] return test_sentences results_filename = '_'.join([ 'results', str(args.num_layers), str(args.size), str(args.vocab_size) ]) results_path = os.path.join(args.results_dir, results_filename + '.txt') with tf.Session() as sess, open(results_path, 'w') as results_fh: # Create model and load parameters. args.batch_size = 1 model = create_model(sess, args) # Load vocabularies. vocab_path = os.path.join(args.data_dir, "vocab%d.in" % args.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) test_dataset = _get_test_dataset() for sentence in test_dataset: # Get token-ids for the input sentence. predicted_sentence = get_predicted_sentence(args, sentence, vocab, rev_vocab, model, sess, debug=debug) if isinstance(predicted_sentence, list): print("%s : (%s)" % (sentence, datetime.now())) results_fh.write("%s : (%s)\n" % (sentence, datetime.now())) for sent in predicted_sentence: print(" (%s) -> %s" % (sent['prob'], sent['dec_inp'])) results_fh.write(" (%f) -> %s\n" % (sent['prob'], sent['dec_inp'])) else: print(sentence, ' -> ', predicted_sentence) results_fh.write("%s -> %s\n" % (sentence, predicted_sentence)) # break results_fh.close() print("results written in %s" % results_path) sent['dec_inp']
def chat(args): if args.gpu_usage == 0: #config = tf.ConfigProto(device_count = {'GPU': 0}) os.environ['CUDA_VISIBLE_DEVICES'] = '' gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_usage) config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(config=config) as sess: # Create model and load parameters. args.batch_size = 1 # We decode one sentence at a time. model = create_model(sess, args) # Load vocabularies. vocab_path = os.path.join(args.data_dir, "vocab%d.in" % args.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) # Decode from standard input. sys.stdout.write("> ") sys.stdout.flush() sentence = sys.stdin.readline() if len(sentence.split(' ')) < 2: sentence = sentence_split(sentence) while sentence: predicted_sentence = get_predicted_sentence( args, sentence, vocab, rev_vocab, model, sess) # print(predicted_sentence) if isinstance(predicted_sentence, list): for sent in predicted_sentence: print("%s: %s" % ('chatbot', sentence_combine(sent['dec_inp']))) # for sent in predicted_sentence: # print(" (%s) -> %s" % (sent['prob'], sent['dec_inp'])) else: print(sentence, ' -> ', predicted_sentence) sys.stdout.write("> ") sys.stdout.flush() sentence = sys.stdin.readline() if len(sentence.split(' ')) < 2: sentence = sentence_split(sentence)
def chat(args): with tf.Session() as sess: # Create model and load parameters. model = create_model(sess, args) model.batch_size = 1 # We decode one sentence at a time. # Load vocabularies. vocab_path = os.path.join(args.data_dir, "vocab%d.in" % args.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) # Decode from standard input. sys.stdout.write("> ") sys.stdout.flush() sentence = sys.stdin.readline() while sentence: predicted_sentence = get_predicted_sentence(args, sentence, vocab, rev_vocab, model, sess) print(predicted_sentence) print("> ") sys.stdout.flush() sentence = sys.stdin.readline()
def predict(args, debug=False): def _get_test_dataset(): with open(args.test_dataset_path) as test_fh: test_sentences = [s.strip() for s in test_fh.readlines()] return test_sentences results_filename = '_'.join(['results', str(args.num_layers), str(args.size), str(args.vocab_size)]) results_path = os.path.join(args.results_dir, results_filename+'.txt') with tf.Session() as sess, open(results_path, 'w') as results_fh: # Create model and load parameters. args.batch_size = 1 model = create_model(sess, args) # Load vocabularies. vocab_path = os.path.join(args.data_dir, "vocab%d.in" % args.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) test_dataset = _get_test_dataset() for sentence in test_dataset: # Get token-ids for the input sentence. predicted_sentence = get_predicted_sentence(args, sentence, vocab, rev_vocab, model, sess, debug=debug) if isinstance(predicted_sentence, list): print("%s : (%s)" % (sentence, datetime.now())) results_fh.write("%s : (%s)\n" % (sentence, datetime.now())) for sent in predicted_sentence: print(" (%s) -> %s" % (sent['prob'], sent['dec_inp'])) results_fh.write(" (%f) -> %s\n" % (sent['prob'], sent['dec_inp'])) else: print(sentence, ' -> ', predicted_sentence) results_fh.write("%s -> %s\n" % (sentence, predicted_sentence)) # break results_fh.close() print("results written in %s" % results_path)
def predict(args, debug=False): def _get_test_dataset(): with open(args.test_dataset_path) as test_fh: test_sentences = [s.strip() for s in test_fh.readlines()] return test_sentences def sentence_split(sentence): seg_list = jieba.cut(sentence, cut_all=False) sentence = ' '.join((' '.join(seg_list)).split()) return sentence results_filename = '_'.join([ 'results', str(args.num_layers), str(args.size), str(args.vocab_size) ]) results_path = os.path.join(args.results_dir, results_filename + '.txt') # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_usage) # with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: if args.gpu_usage == 0: #config = tf.ConfigProto(device_count = {'GPU': 0}) os.environ['CUDA_VISIBLE_DEVICES'] = '' gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_usage) config = tf.ConfigProto(gpu_options=gpu_options) with tf.Session(config=config) as sess, open(results_path, 'w') as results_fh: # Create model and load parameters. args.batch_size = 1 model = create_model(sess, args) # Load vocabularies. vocab_path = os.path.join(args.data_dir, "vocab%d.in" % args.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) test_dataset = _get_test_dataset() for sentence in test_dataset: # Get token-ids for the input sentence. sentence = sentence_split(sentence) predicted_sentence = get_predicted_sentence(args, sentence, vocab, rev_vocab, model, sess, debug=debug) if isinstance(predicted_sentence, list): print("input: %s (%s)" % (sentence, datetime.now())) results_fh.write("input: %s (%s)\n" % (sentence, datetime.now())) for sent in predicted_sentence: print("chatbot: %s (%s)" % (sent['dec_inp'], sent['prob'])) results_fh.write("chatbot: %s (%f)\n" % (sent['dec_inp'], sent['prob'])) else: print(sentence, ' -> ', predicted_sentence) results_fh.write("%s -> %s\n" % (sentence, predicted_sentence)) results_fh.close() print("results written in %s" % results_path)
def fight(args, args1): if args.gpu_usage == 0: #config = tf.ConfigProto(device_count = {'GPU': 0}) os.environ['CUDA_VISIBLE_DEVICES'] = '' gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_usage) config = tf.ConfigProto(gpu_options=gpu_options) #create two graphs to build nested sessions model_graph = tf.Graph() adv_graph = tf.Graph() adv_sess = tf.Session(graph=adv_graph, config=config) sess = tf.Session(graph=model_graph, config=config) total_sent = [] total_sent = open('%s/random_sent.txt' % args.work_root, 'r').readlines() total_sent_len = len(total_sent) chatbot_A = 'Chatbot_A' chatbot_B = 'Chatbot_B' fight_tims = 5 Sleep_or_not = True #False # model_name if args.model_name[0] == 'g': chatbot_A = 'Gossiping_Bot' elif args.model_name[0] == 'w': chatbot_A = 'WomenTalk_Bot' # model_2_name if args.model_2_name[0] == 'g': chatbot_B = 'Gossiping_Bot' elif args.model_2_name[0] == 'w': chatbot_B = 'WomenTalk_Bot' # # gpu_options test # gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=args.gpu_usage) # config=tf.ConfigProto(gpu_options=gpu_options) ## 1st session with sess.as_default(): # Create model and load parameters. with model_graph.as_default(): args.batch_size = 1 # We decode one sentence at a time. model = create_model(sess, args) # Load vocabularies. vocab_path = os.path.join(args.data_dir, "vocab%d.in" % args.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) ## 2nd session with adv_sess.as_default(): with adv_graph.as_default(): # Create model and load parameters. args1.batch_size = 1 # We decode one sentence at a time. model_r = create_model(adv_sess, args1) # Load vocabularies. vocab_path_r = os.path.join(args1.data_dir, "vocab%d.in" % args1.vocab_size) vocab_r, rev_vocab_r = data_utils.initialize_vocabulary( vocab_path_r) # Decode from standard input. print('\n') sys.stdout.write("> ") sys.stdout.flush() sentence = sys.stdin.readline() ## make sure jieba split sentence = sentence_split(sentence) while sentence: if sentence == 'random': sentence = total_sent[random.randint( 0, total_sent_len)] sentence = sentence_split(sentence) print('>> ', sentence) elif sentence == 'exit': print('\nBYE BYE ~ \n') break for turns in range(0, fight_tims): # ChatbotA predicted_sentence = get_predicted_sentence( args, sentence, vocab, rev_vocab, model, sess) str1 = predicted_sentence[0]['dec_inp'] str1 = sentence_split(str1) ## make sure jieba split # ChatbotB predicted_sentence1 = get_predicted_sentence( args1, str1, vocab_r, rev_vocab_r, model_r, adv_sess) # print(predicted_sentence) # ChatbotA if isinstance(predicted_sentence, list): for sent in predicted_sentence: # random_sleep(Sleep_or_not) print("%s: %s" % (chatbot_A, sentence_combine(sent['dec_inp']))) # ChatbotB if isinstance(predicted_sentence1, list): for sent in predicted_sentence1: random_sleep(Sleep_or_not) print("%s: %s" % (chatbot_B, sentence_combine(sent['dec_inp']))) sentence = predicted_sentence1[0]['dec_inp'] sentence = sentence_split( sentence) ## make sure jieba split print('\n') sys.stdout.write("> ") sys.stdout.flush() sentence = sys.stdin.readline() sentence = sentence_split( sentence) ## make sure jieba split
async def service_callback(): async with websockets.connect('ws://localhost:9090') as websocket: # advertise the service await websocket.send("{ \"op\": \"advertise_service\",\ \"type\": \"roboy_communication_cognition/GenerateAnswer\",\ \"service\": \"/roboy/cognition/generative_nlp/answer\"\ }") i = 1 # counter for the service request IDs with tf.Session() as sess: # Create model and load parameters. logging.info("Loading the model") args = params_setup() args.batch_size = 1 # We decode one sentence at a time. model = create_model(sess, args) # Load vocabularies. vocab_path = os.path.join(args.data_dir, "vocab%d.in" % args.vocab_size) vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path) logging.info( "Service /roboy/cognition/generative_nlp/answer is ready") # wait for the service request, generate the answer, and send it back while True: try: request = await websocket.recv() sentence = json.loads(request)["args"]["text_input"] model_response = get_predicted_sentence( args, sentence, vocab, rev_vocab, model, sess) srv_response = {} answer = {} if isinstance(model_response, list): text = model_response[0]['dec_inp'] else: text = model_response['dec_inp'] answer["text_output"] = ''.join([ i if ord(i) < 128 else '' for i in text ]) # strip down unicode srv_response["values"] = answer srv_response["op"] = "service_response" srv_response[ "id"] = "service_request:/roboy/cognition/generative_nlp/answer:" + str( i) srv_response["result"] = True srv_response[ "service"] = "/roboy/cognition/generative_nlp/answer" i += 1 await websocket.send(json.dumps(srv_response)) except Exception as e: logging.exception( "Oopsie! Got an exception in generative_nlp")