def main(): args = parse_args() token = args.token if not token: if not "TELEGRAM_TOKEN" in os.environ: print("Please, set bot token through --token or TELEGRAM_TOKEN env variable") return token = os.environ["TELEGRAM_TOKEN"] advanced_manager = DialogueManager(RESOURCE_PATH) advanced_manager.create_chitchat_bot() bot = BotHandler(token, advanced_manager) print("Ready to talk!") offset = 0 while True: updates = bot.get_updates(offset=offset) for update in updates: print("An update received.") if "message" in update: chat_id = update["message"]["chat"]["id"] if "text" in update["message"]: text = update["message"]["text"] if is_unicode(text): print("Update content: {}".format(update)) bot.send_message(chat_id, bot.get_answer(update["message"]["text"])) else: bot.send_message(chat_id, "Hmm, you are sending some weird characters to me...") offset = max(offset, update['update_id'] + 1) time.sleep(1)
def main(): dialogue_mgr = DialogueManager(RESOURCE_PATH) print("I'm ready!") while True: query = input() answer = dialogue_mgr.generate_answer(query) print(answer)
def main(): args = parse_args() token = args.token if not token: if not "TELEGRAM_TOKEN" in os.environ: print( "Please, set bot token through --token or TELEGRAM_TOKEN env variable" ) return token = os.environ["TELEGRAM_TOKEN"] paths = dict({ 'INTENT_RECOGNIZER': "intent_recognizer.pkl", 'TFIDF_VECTORIZER': "TFIDF_VECTORIZER.pkl", 'TAG_CLASSIFIER': "tag_classifier.pkl", 'WORD_EMBEDDINGS': "GoogleNews-0.5Mvectors300.bin", 'THREAD_EMBEDDINGS_FOLDER': "thread_embeddings_by_tags" }) ################################################################# # Your task is to complete dialogue_manager.py and use your # advanced DialogueManager instead of SimpleDialogueManager. # This is the point where you plug it into the Telegram bot. # Do not forget to import all needed dependencies when you do so. #simple_manager = SimpleDialogueManager() advanced_manager = DialogueManager(paths) advanced_manager.create_chitchat_bot() bot = BotHandler(token, advanced_manager) ############################################################### print("Ready to talk!") offset = 0 while True: updates = bot.get_updates(offset=offset) for update in updates: print("An update received.") if "message" in update: chat_id = update["message"]["chat"]["id"] if "text" in update["message"]: text = update["message"]["text"] if is_unicode(text): print("Update content: {}".format(update)) bot.send_message( chat_id, bot.get_answer(update["message"]["text"])) else: bot.send_message( chat_id, "Hmm, you are sending some weird characters to me..." ) offset = max(offset, update['update_id'] + 1) time.sleep(1)
def post(self): data_json = request.get_json() print(data_json) r = data_json['request'] language = data_json['language'] DM = DialogueManager() if language == 'en': value = DM.intentClassifierEN(r) else: value = DM.intentClassifierFR(r) return jsonify({"intent": value})
def main(): paths = RESOURCE_PATH bot = DialogueManager(paths) print("Ready to talk!") while True: question = input('>>>:') if is_unicode(question): answer = bot.generate_answer(question) else: answer = "Hmm, you are sending some weird characters to me..." print('Bot:', answer)
def interactive_main(args): """Main function for the interactive mode.""" dialogue_manager = DialogueManager() print( 'Welcome to the interactive mode, here you can chitchat with the bot. To quit the program, type \'exit\' or just press ENTER. Have fun!' ) while True: question = input('Q: ') if question == '' or question == 'exit': break print('A: {}'.format(dialogue_manager.generate_answer(question)))
def echo(bot,bot1): ''' Function to actually fetch messages from user and send back reply. Bot Token is reqired to run this script''' # Request updates after the last update_id global update_id for update in bot.get_updates(offset=update_id, timeout=10): update_id = update.update_id + 1 try: chat_id = update.message.chat.id dialogue_manager = DialogueManager(RESOURCE_PATH) #prediction call here if update.message.voice : file_id = bot1.get_file(update.message.voice.file_id) file_info=bot1.get_file(update.message.voice.file_id) downloaded_file = bot1.download_file(file_info.file_path) with open('new_file.ogg', 'wb') as new_file: new_file.write(downloaded_file) src_filename = 'new_file.ogg' dest_filename = 'output.wav' process = subprocess.run(['ffmpeg','-y' ,'-i', src_filename, dest_filename]) if process.returncode != 0: raise Exception("Something went wrong") print ("\n Request") tp.audio_text() msg="" with open('Results/output.txt', 'r') as reader: msg+=reader.read() answer=dialogue_manager.generate_answer(msg) answer = "You said :" + msg + "\n" + answer #log(update.message.voice.file_id) log(update.message) else: log(update.message) msg = update.message.text answer=dialogue_manager.generate_answer(msg) f1 = open("Results/data.txt", "a") f1.write(msg) f1.close() #voice() #s = voice(text_data) + ".mp3" bot.send_message(chat_id=chat_id, text = answer ) #bot.send_audio(chat_id=chat_id, audio=open(s, 'rb')) #bot.send_message(chat_id=chat_id, text = "\U0001F973 " + voice(text_data)) except Exception as e: print ("ERROR - CHAT ID-", e)
def main(): args = parse_args() token = args.token if not token: if not "TELEGRAM_TOKEN" in os.environ: print( "Please, set bot token through --token or TELEGRAM_TOKEN env variable" ) return token = os.environ["TELEGRAM_TOKEN"] ################################################################# # Your task is to complete dialogue_manager.py and use your # advanced DialogueManager instead of SimpleDialogueManager. # This is the point where you plug it into the Telegram bot. # Do not forget to import all needed dependencies when you do so. #simple_manager = SimpleDialogueManager() #bot = BotHandler(token, simple_manager) dialogue_manager = DialogueManager(RESOURCE_PATH) dialogue_manager.create_chitchat_bot() bot = BotHandler(token, dialogue_manager) ############################################################### print("Ready to talk!") offset = 0 while True: updates = bot.get_updates(offset=offset) for update in updates: print("An update received.") if "message" in update: chat_id = update["message"]["chat"]["id"] if "text" in update["message"]: text = update["message"]["text"] if is_unicode(text): print("Update content: {}".format(update)) bot.send_message( chat_id, bot.get_answer(update["message"]["text"])) else: bot.send_message( chat_id, "Hmm, you are sending some weird characters to me..." ) offset = max(offset, update['update_id'] + 1) time.sleep(1)
def main(): token = '1547700302:AAERs9g0D40N-VvW0TIh5jvJc1EujMjC8nc' if not token: if not "TELEGRAM_TOKEN" in os.environ: print( "Please, set bot token through --token or TELEGRAM_TOKEN env variable" ) return token = os.environ["TELEGRAM_TOKEN"] # This is the point where you plug it into the Telegram bot. # Do not forget to import all needed dependencies when you do so. dialogue_manager = DialogueManager(RESOURCE_PATH) dialogue_manager.create_chitchat_bot() bot = BotHandler(token, dialogue_manager) print("Ready to talk!") offset = 0 while True: updates = bot.get_updates(offset=offset) for update in updates: print("An update received.") if "message" in update: chat_id = update["message"]["chat"]["id"] if "text" in update["message"]: text = update["message"]["text"] text = text.encode('ascii', 'ignore').decode('ascii') if is_unicode(text): print("Update content: {}".format(update)) bot.send_message(chat_id, bot.get_answer(text)) else: bot.send_message( chat_id, "Hmm, you are sending some weird characters to me..." ) if "voice" in update["message"]: file_id = update["message"]["voice"]["file_id"] text = voice_recognition.get(update["message"], token) print(f"Did u said '{text}'") bot.send_message(chat_id, f"Did u said '{text}'") print("Update content: {}".format(update)) bot.send_message(chat_id, bot.get_answer(text)) offset = max(offset, update['update_id'] + 1) time.sleep(1)
def __init__(self): pygame.init() self.SCREEN_WIDTH = 500 self.SCREEN_HEIGHT = 600 self.CAPTION = 'dialogue_test' self.FPS = 60 self.screen = pygame.display.set_mode(( self.SCREEN_WIDTH, self.SCREEN_HEIGHT, )) pygame.display.set_caption(self.CAPTION) self.clock = pygame.time.Clock() self.dialogue_manager = DialogueManager() logger.log_neutral("Activated") self.dialogue_manager.activate() self.dialogue_manager.set_active_dialogue("person1")
def main(): token = "959893539:AAEKPBviifT1oYt85_xW6G30ITF1_Xy8nQw" print(token) if not token: if not "TELEGRAM_TOKEN" in os.environ: print( "Please, set bot token through --token or TELEGRAM_TOKEN env variable" ) return token = os.environ["TELEGRAM_TOKEN"] # This is the point where you plug it into the Telegram bot. # Do not forget to import all needed dependencies when you do so. dialogue_manager = DialogueManager() dialogue_manager.create_chitchat_bot() bot = BotHandler(token, dialogue_manager) print("Ready to talk!") offset = 0 while True: updates = bot.get_updates(offset=offset) for update in updates: print("An update received.") if "message" in update: chat_id = update["message"]["chat"]["id"] if "text" in update["message"]: text = update["message"]["text"] if is_unicode(text): print("Update content: {}".format(update)) bot.send_message( chat_id, bot.get_answer(update["message"]["text"])) else: bot.send_message( chat_id, "Hmm, you are sending some weird characters to me..." ) offset = max(offset, update['update_id'] + 1) time.sleep(1)
def main(): """ The main function of the conversation service """ args = parse_args() token = args.token dialogue = args.dialogue if not token: if 'TELEGRAM_TOKEN' not in os.environ: print('Please, set bot token through ' '--token or TELEGRAM_TOKEN env variable') return token = os.environ['TELEGRAM_TOKEN'] ################################################################# # Your task is to complete dialogue_manager.py and use your # advanced DialogueManager instead of SimpleDialogueManager. # This is the point where you plug it into the Telegram bot. # Do not forget to import all needed dependencies when you do so. if dialogue == 'simple': manager = SimpleDialogueManager() elif dialogue == 'advanced': manager = DialogueManager(RESOURCE_PATH) bot = BotHandler(token, manager) ############################################################### print('Ready to talk!') offset = 0 while True: updates = bot.get_updates(offset=offset) for update in updates: print('An update received.') if 'message' in update: chat_id = update['message']['chat']['id'] if 'text' in update['message']: text = update['message']['text'] if is_unicode(text): print('Update content: {}'.format(update)) bot.send_message( chat_id, bot.get_answer(update['message']['text'])) else: msg = ('Hmm, you are sending some weird characters to ' 'me...') bot.send_message(chat_id, msg) offset = max(offset, update['update_id'] + 1) time.sleep(1)
def main(): args = parse_args() token = args.token if not token: if not "TELEGRAM_TOKEN" in os.environ: print( "Please, set bot token through --token or TELEGRAM_TOKEN env variable" ) return token = os.environ["TELEGRAM_TOKEN"] ################################################################# from dialogue_manager import DialogueManager RESOURCE_PATH = { 'INTENT_RECOGNIZER': 'intent_recognizer.pkl', 'TAG_CLASSIFIER': 'tag_classifier.pkl', 'TFIDF_VECTORIZER': 'tfidf_vectorizer.pkl', 'THREAD_EMBEDDINGS_FOLDER': 'thread_embeddings_by_tags', 'WORD_EMBEDDINGS': 'data/word_embeddings.tsv' } dialogue_manager = DialogueManager(RESOURCE_PATH) bot = BotHandler(token, dialogue_manager) ############################################################### print("Ready to talk!") offset = 0 while True: updates = bot.get_updates(offset=offset) for update in updates: print("An update received.") if "message" in update: chat_id = update["message"]["chat"]["id"] if "text" in update["message"]: text = update["message"]["text"] if is_unicode(text): print("Update content: {}".format(update)) bot.send_message( chat_id, bot.get_answer(update["message"]["text"])) else: bot.send_message( chat_id, "Hmm, you are sending some weird characters to me..." ) offset = max(offset, update['update_id'] + 1) time.sleep(1)
def main(): pipeline = SpeechPipeline( PyAudioInput(frame_width=20, sample_rate=16000, exception_on_overflow=False), [ VoiceActivityDetector(), WakewordTrigger(pre_emphasis=0.97, model_dir="tflite"), GoogleSpeechRecognizer(GOOGLE_CREDS), ActivationTimeout(), ], ) dialogue_manager = DialogueManager( "tflite", "distilbert-base-cased-distilled-squad") manager = TextToSpeechManager( TextToSpeechClient(KEY_ID, KEY_SECRET), PyAudioOutput(), ) @pipeline.event def on_activate(context): print(context.is_active) @pipeline.event def on_recognize(context): pipeline.pause() answer = dialogue_manager(context.transcript) manager.synthesize(answer, "text", "demo-male") pipeline.resume() @pipeline.event def on_deactivate(context): print(context.is_active) manager.synthesize(dialogue_manager.greet(), "text", "demo-male") pipeline.start() pipeline.run()
def main(): config = configparser.ConfigParser() config.read('settings_secret.ini') token = config['TELEGRAM']['TOKEN'] if not token: print("Please, set bot token through --token or TELEGRAM_TOKEN env variable") return manager = DialogueManager(config) bot = BotHandler(token, manager) print("Ready to talk!") offset = 0 while True: updates = bot.get_updates(offset=offset) for update in updates: print("An update received.") if "message" in update: chat_id = update["message"]["chat"]["id"] if "text" in update["message"]: text = update["message"]["text"] # if text is correctly encoded in unicode if is_unicode(text): print("Update content: {}".format(update)) answer = bot.get_answer(update["message"]["text"]) print('answer', answer) if 'png' in answer: # means the answer is weather forecast. Send text and image name separately. bot.send_message(chat_id, answer.split(';')[0]) bot.send_message(chat_id, answer.split(';')[1]) else: bot.send_message(chat_id, bot.get_answer(update["message"]["text"])) else: # At first try to drop non-unicode characters. If no english words are left - send fixed answer. text = ''.join([i if ord(i) < 128 else ' ' for i in text]) if re.findall('\w+', text) == []: bot.send_message(chat_id, "Hmm, you are sending some weird characters to me... I can speak only English.") else: answer = bot.get_answer(text) print('answer', answer) if 'png' in answer: # means the answer is weather forecast. Send text and image name separately. bot.send_message(chat_id, answer.split(';')[0]) bot.send_message(chat_id, answer.split(';')[1]) #bot.send_message(chat_id, "Hmm, you are sending some weird characters to me...") offset = max(offset, update['update_id'] + 1) time.sleep(1)
def main(): args = parse_args() token = args.token if not token: if not "TELEGRAM_TOKEN" in os.environ: print( "Please, set bot token through --token or TELEGRAM_TOKEN env variable" ) return token = os.environ["TELEGRAM_TOKEN"] ################################################################# # triggers the reload of all the modules #reload() # triggers the load of all the modules load() #dialogue_manager = SimpleDialogueManager() dialogue_manager = DialogueManager() bot = BotHandler(token, dialogue_manager) ############################################################### print("Ready to talk!") offset = 0 while True: print("INSIDE TRUE") updates = bot.get_updates(offset=offset) print("updates %s" % (updates)) for update in updates: print("An update received.") if "message" in update: chat_id = update["message"]["chat"]["id"] if "text" in update["message"]: text = update["message"]["text"] print("text is %s" % (text)) if is_unicode(text): print("Update content: {}".format(update)) bot.send_message( chat_id, bot.get_answer(update["message"]["text"])) else: bot.send_message( chat_id, "Hmm, you are sending some weird characters to me..." ) offset = max(offset, update['update_id'] + 1) time.sleep(1)
def main(): args = parse_args() token = args.token if not token and "TELEGRAM_TOKEN" in os.environ: token = os.environ["TELEGRAM_TOKEN"] if not token: bot_logger.error( "Please, set bot token through --token or TELEGRAM_TOKEN env variable" ) return master = args.master if not token and "TELEGRAM_MASTER" in os.environ: master = os.environ["TELEGRAM_MASTER"] if not master: bot_logger.warning( "The Bot hasn't master." "To use master commands," "put your username through --master or TELEGRAM_MASTER env variable." ) dialogue_manager = DialogueManager(RESOURCE_PATH) bot = BotHandler(token, dialogue_manager, master_name=master) bot_logger.info("Ready to talk!") offset = 0 while True: time.sleep(1) updates = bot.get_updates(offset=offset) for update in updates: bot_logger.info("Update content: {}".format(update)) offset = max(offset, update["update_id"] + 1) if update.get("message", {}).get("text", None): chat_id = update["message"]["chat"]["id"] text = update["message"]["text"] answer = bot.get_answer(text) bot_logger.info("Answer: {}".format(answer)) bot.send_message(chat_id, answer)
class Game: def __init__(self): pygame.init() self.SCREEN_WIDTH = 500 self.SCREEN_HEIGHT = 600 self.CAPTION = 'dialogue_test' self.FPS = 60 self.screen = pygame.display.set_mode(( self.SCREEN_WIDTH, self.SCREEN_HEIGHT, )) pygame.display.set_caption(self.CAPTION) self.clock = pygame.time.Clock() self.dialogue_manager = DialogueManager() logger.log_neutral("Activated") self.dialogue_manager.activate() self.dialogue_manager.set_active_dialogue("person1") def event_handler(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.MOUSEBUTTONDOWN: self.dialogue_manager.trigger_dialogue() def key_handler(self): pass def graphics_handler(self): self.screen.fill(pygame.Color('gold')) # draw stuff here pygame.display.update() self.clock.tick(self.FPS) def run(self): while True: self.event_handler() self.key_handler() self.graphics_handler()
def main(): args = parse_args() token = '1383778233:AAH_pAE0rkD4gigtAbyeqLMasU43kcyNsGQ' if not token: if not "TELEGRAM_TOKEN" in os.environ: print( "Please, set bot token through --token or TELEGRAM_TOKEN env variable" ) return token = os.environ["TELEGRAM_TOKEN"] dialogue_manager = DialogueManager(RESOURCE_PATH) bot = BotHandler(token, dialogue_manager) ############################################################### print("Ready to talk!") offset = 0 while True: updates = bot.get_updates(offset=offset) for update in updates: print("An update received.") if "message" in update: chat_id = update["message"]["chat"]["id"] if "text" in update["message"]: text = update["message"]["text"] if is_unicode(text): print("Update content: {}".format(update)) bot.send_message( chat_id, bot.get_answer(update["message"]["text"])) else: bot.send_message( chat_id, "Hmm, you are sending some weird shit that makes no sense..." ) offset = max(offset, update['update_id'] + 1) time.sleep(2)
def set_global_p2p_state(state, p2p_state): global mob for member in mob.users: if member.today_status() in state: dialogues[member.id].p2p_state = p2p_state def full_time(t: datetime): return t.minute > 58 try: mob.load('mob_swap') for member in mob.users: dm = DialogueManager(member.id, mob) dm.p2p_state = 'IDLE' dialogues.update({member.id: dm}) except FileNotFoundError: print('WARNING: No mob swapfile detected') # Swap does not exist while True: # Check for updates updates = [] try: updates = bot.get_updates(upd_offset)['result'] except KeyboardInterrupt: mob.swap('mob_swap') mob.broadcast( 'Бот упал и не встанет, пока вы не попросите об этом @aryavorskiy') exit()
def get_bot_response(): userText = request.args.get('msg') dialogue_manager = DialogueManager(RESOURCE_PATH) return dialogue_manager.generate_answer(userText)
from dialogue_manager import DialogueManager ips_file = os.path.join('.', 'white_list.txt') checkIpFlag = False def is_unicode(text): return len(text) == len(text.encode()) class SimpleDialogueManager(object): def generate_answer(self, question): return "Hello, pal" dialogue_manager = DialogueManager() class ClientServerProtocol(asyncio.Protocol): def __init__(self): super().__init__() self.ips = self.readIps() def readIps(self): with open(ips_file, 'r') as f: result = f.readlines() result = [l.strip() for l in result if len(l.strip()) != 0] return result
def main(): dialogue_manager = DialogueManager(RESOURCE_PATH) bot = BotHandler(dialogue_manager) print('\nCreating and Training Chatbot, this will take about 4 minutes, please wait...\n') dialogue_manager.create_chitchat_bot() print("Ready to talk!") host = '' # Get local machine name # "http://chat-chat.1d35.starter-us-east-1.openshiftapps.com" port = 8080 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind((host, port)) sock.listen(1) print('host: ', host) def before(value, a): # Find first part and return slice before it. pos_a = value.find(a) if pos_a == -1: return "" return value[0:pos_a] def after(value, a): # Find and validate first part. pos_a = value.rfind(a) if pos_a == -1: return "" # Returns chars after the found string. adjusted_pos_a = pos_a + len(a) if adjusted_pos_a >= len(value): return "" return value[adjusted_pos_a:] def parse_data(data): # old version (brute force): # question = before(after(data, '"text":"'), '"') # from_id = before(after(data, '"recipient":{"id":"'), '"') # conversation_id = before(after(data, '"conversation":{"id":"'), '"') # recipient_id = before(after(data, '"from":{"id":"'), '"') # reply_to_id = before(after(data, ',"id":"'), '"') # question = data['text'] # from_id = data['recipient']['id'] # conversation_id = data['conversation']['id'] # recipient_id = data['from']['id'] # reply_to_id = data['id'] print('entre a -> parse_data()') question = data.get('text', '') from_id = data.get('recipient', {}).get('id', '') conversation_id = data.get('conversation', {}).get('id', '') recipient_id = data.get('from', {}).get('id', '') reply_to_id = data.get('id', '') return question, from_id, conversation_id, recipient_id, reply_to_id def handler(c, a, bot): while True: try: data = c.recv(1024) print(data) if data: data_str = str(data, 'utf-8') (request_header, request_body) = data_str.split("\r\n\r\n") request_body = json.loads(request_body) request_type = request_body.get('type', '') print('request type ->', request_type) if request_type == 'message': print('voy a -> parse_data()') question, from_id, conversation_id, recipient_id, reply_to_id = parse_data( request_body) print('regrese de -> parse_data()') print('question ->', question) print('voy a -> bot.get_answer()') text = bot.get_answer(question) # text = 'respuesta de prueba' print('ya regrese de -> bot.get_answer()') print('text ->', text) if text == '': text = "Hmm, you are sending some weird characters to me..." print('\nquestion:', question, '\nanswer:', text) body = {'type': 'message', 'from': {'id': from_id, 'name': 'Walter bot'}, 'conversation': {'id': conversation_id, 'name': 'Walter conversation'}, 'recipient': {'id': recipient_id, 'name': 'Walter user'}, 'text': text, 'replyToId': reply_to_id} body_json = json.dumps(body) # c.send(bytes(text, "utf8")) base_url = request_body.get('serviceUrl', '') try: api_url = urljoin( base_url, '/v3/conversations/' + conversation_id + '/activities/' + reply_to_id) requests.post(api_url, data=body_json, headers={ 'Authorization': 'Bearer', 'Content-Type': 'application/json'}) # c.send(bytes('HTTP/1.1 200 OK', 'utf-8')) # print('sending body...', bytes(body_json, 'utf-8')) # c.send(bytes(body_json, 'utf-8')) # finally: # pass except: print('something wrong with the post!') else: # print('no data') break except: break c.close() while True: c, a = sock.accept() c.send(bytes('HTTP/1.1 200 OK', 'utf-8')) cThread = threading.Thread(target=handler, args=(c, a, bot)) cThread.daemon = True cThread.start() print('connection: ', c) print('argument: ', a)
def test_nlg(): dialogue_manager = DialogueManager(suspect_identity="guilty") message = "What were you doing last night?" response = dialogue_manager.test_single_respond(message, "extracted_info") print(response) '''
def test_dialogue_manager(): dialogue_manager = DialogueManager(suspect_identity="guilty") message = "What were you doing last night?" print(dialogue_manager.strategize(message)) '''
def main(): args = parse_args() token = args.token if not token: if not "TELEGRAM_TOKEN" in os.environ: print( "Please, set bot token through --token or TELEGRAM_TOKEN env variable" ) return token = os.environ["TELEGRAM_TOKEN"] # ResNeXT is default model use_resnext = True dialogue_manager = DialogueManager(RESOURCE_PATH, use_resnext) bot = BotHandler(token, dialogue_manager) # managing bot chat print("Ready to talk!") offset = 0 while True: updates = bot.get_updates(offset=offset) for update in updates: print("An update received.") print(update) if "message" in update: chat_id = update["message"]["chat"]["id"] # change models or ask for photo if "text" in update["message"]: text = update["message"]['text'].lower() if (text == 'use inception') & (use_resnext == True): use_resnext = False dialogue_manager = DialogueManager( RESOURCE_PATH, use_resnext) bot = BotHandler(token, dialogue_manager) bot.send_message(chat_id, 'Changed to Inception') elif (text == 'use inception') & (use_resnext == False): bot.send_message(chat_id, 'Inception is current model') elif (text == 'use resnext') & (use_resnext == True): bot.send_message(chat_id, 'ResNext is current model') elif (text == 'use resnext') & (use_resnext == False): use_resnext = True dialogue_manager = DialogueManager( RESOURCE_PATH, use_resnext) bot = BotHandler(token, dialogue_manager) bot.send_message(chat_id, 'Changed to ResNext') else: bot.send_message( chat_id, "Send me photo to predict Instagram hashtags!") bot.send_message( chat_id, "If you want to change model, type 'use inception' or 'use resnext'" ) # processing photo logic if "photo" in update["message"]: photo_saved_path = bot.download_photo(update) answer = bot.get_answer(photo_saved_path) """ prediction_text looks like: Top5 tags with numbers of posts: food (379.8m), nature (500.7m), christmas (146.4m), dessert (52.8m), cake (81m) convenient_tag_string looks like: #food #nature #christmas #dessert #cake """ prediction_text, convenient_tag_string = answer with open(RESOURCE_PATH['LOGGER_FILE_PATH'], 'a') as f: # for full investigation json.dump( { 'photo_saved_path': photo_saved_path, 'answer': answer, "update": update }, f) with open(RESOURCE_PATH['SHORT_LOGGER_FILE_PATH'], 'a') as f: # for easy investigation json.dump( { 'photo_saved_path': photo_saved_path, 'answer': answer }, f) bot.send_message(chat_id, prediction_text) bot.send_message(chat_id, convenient_tag_string) offset = max(offset, update['update_id'] + 1) time.sleep(1)
def main(): # The first thing to do is to distinguish programming-related questions from general ones. # It would also be good to predict which programming language a particular question refers to in order to speed up question search by a factor of 10. # Load 200k examples of each class (dialogue and programming-related) and preprocess the text dialogue_df = prepare_data('data/dialogues.tsv', 200000) stackoverflow_df = prepare_data('data/tagged_posts.tsv', 200000) # Prepare data for binary classification (programming-related or not) on TF-IDF representations of texts. X = np.concatenate( [dialogue_df['text'].values, stackoverflow_df['title'].values]) y = ['dialogue'] * dialogue_df.shape[0] + ['stackoverflow' ] * stackoverflow_df.shape[0] intent_recog = intent_classifier(X, y) # Dump the classifier to use it in the running bot pickle.dump(intent_recog, open(RESOURCE_PATH['INTENT_RECOGNIZER'], 'wb')) # Prepare data for multiclass classification (10 possible languages) on TF-IDF representations of texts. X = stackoverflow_df['title'].values y = stackoverflow_df['tag'].values tagger = language_classifier(X, y) # Dump the classifier to use it in the running bot pickle.dump(tagger, open(RESOURCE_PATH['TAG_CLASSIFIER'], 'wb')) # To find a relevant answer on a question we will use vector representations to calculate similarity between the question and existing threads. # Load embeddings that were trained in supervised mode for duplicates detection on the same corpus that is used in search (StackOverflow posts). starspace_embeddings, embeddings_dim = load_embeddings( 'starspace_embeddings.tsv') # Load all the entire set of StackOverflow posts (2,171,575 samples) posts_df = pd.read_csv('data/tagged_posts.tsv', sep='\t') # For each tag, create two data structures that will serve as online search index: tag_post_ids and tag_vectors os.makedirs(RESOURCE_PATH['THREAD_EMBEDDINGS_FOLDER'], exist_ok=True) for tag, count in counts_by_tag.items(): tag_posts = posts_df[ posts_df['tag'] == tag] # filter out all posts about other programming languages tag_post_ids = tag_posts[ 'post_id'].values # list of post_ids needed to show the title and link to the thread tag_vectors = np.zeros( (count, embeddings_dim), dtype=np.float32 ) # matrix where embeddings for each answer are stored for i, title in enumerate(tag_posts['title']): tag_vectors[i, :] = question_to_vec(title, starspace_embeddings, embeddings_dim) # Dump post ids and vectors to a file. filename = os.path.join(RESOURCE_PATH['THREAD_EMBEDDINGS_FOLDER'], os.path.normpath('%s.pkl' % tag)) pickle.dump((tag_post_ids, tag_vectors), open(filename, 'wb')) dialogue_manager = DialogueManager(RESOURCE_PATH) questions = [ "Hey", "How are you doing?", "What's your hobby?", "How to loop in python?", "How to delete rows in pandas?", "python3 re", "What is AI?", ] for question in questions: answer = dialogue_manager.generate_answer(question) print('Q: %s\nA: %s \n' % (question, answer))
from flask import Flask, render_template, request from dialogue_manager import DialogueManager from utils import RESOURCE_PATH app = Flask(__name__) @app.route("/") def home(): return render_template("index.html") paths = RESOURCE_PATH dialogue = DialogueManager(paths) @app.route("/get") def get_bot_response(): user_question = request.args.get('msg') return dialogue.generate_answer(user_question) if __name__ == "__main__": app.run()
sys.path.append(os.path.abspath("../")) # python only # from dialogue_manager_python import DialogueManager # all tags from dialogue_manager import DialogueManager from config import * from utils import * # DB_NAME = '../StackOverflow_python.db' # DB_NAME = '../StackOverflow.db' DB_NAME = '../StackOverflow_newline_score.db' # load model class dm = DialogueManager(DATA_PATH, MODEL_PATH) app = Flask(__name__) class SubmissionForm(Form): question_form = TextAreaField( '', [validators.DataRequired(), validators.length(min=1)]) @app.route('/') def index(): form = SubmissionForm(request.form) return render_template('main.html', form=form)