Exemplo n.º 1
0
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():
    dialogue_mgr = DialogueManager(RESOURCE_PATH)

    print("I'm ready!")

    while True:
        query = input()
        answer = dialogue_mgr.generate_answer(query)
        print(answer)
Exemplo n.º 3
0
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)
Exemplo n.º 4
0
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)))
Exemplo n.º 5
0
def get_bot_response():
    userText = request.args.get('msg')
    dialogue_manager = DialogueManager(RESOURCE_PATH)
    return dialogue_manager.generate_answer(userText)
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))