0

I want to use DialoGPT to have a conversation with me using a microphone and speaker.

However, before I get there, I want to begin by somehow connecting this chat bot to a speaker using pyttsx3. I am able to properly use pyttsx3 to produce sound out of my computer speakers using pre inserted text (i.e. "hello world").

I am also able to use DialoGPT to have a conversation using the terminal.

Therein lies the first issue of connecting the two. I have pasted my current code and was wondering if someone could offer me some assistance.

from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import pyttsx3
engine = pyttsx3.init()

# Set properties before adding
# Things to say

# Sets speed percent
# Can be more than 100
engine.setProperty('rate',190)
# Set volume 0-1
engine.setProperty('volume', 0.7)
# Set ID of voice
voice_id = "com.apple.speech.synthesis.voice.karen"

# Use female voice
engine.setProperty('voice', voice_id)


tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")

# Let's chat for 5 lines
for step in range(5):
    # encode the new user input, add the eos_token and return a tensor in Pytorch
    new_user_input_ids = tokenizer.encode(input(">> User:") + tokenizer.eos_token, return_tensors='pt')

    # append the new user input tokens to the chat history
    bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if step > 0 else new_user_input_ids

    # generated a response while limiting the total chat history to 1000 tokens,
    chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)

    # pretty print last ouput tokens from bot
    engine.say("DialoGPT: {}").format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True))
eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

0

Solved~

import pyttsx3
engine = pyttsx3.init()


# Set properties before adding
# Things to say

# Sets speed percent
# Can be more than 100
engine.setProperty('rate',190)
# Set volume 0-1
engine.setProperty('volume', 0.7)
# Set ID of voice
voice_id = "com.apple.speech.synthesis.voice.karen"

# Use female voice
engine.setProperty('voice', voice_id)



from transformers import AutoModelForCausalLM, AutoTokenizer
import torch



tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")

# Let's chat for 3 lines
for step in range(3):
    # encode the new user input, add the eos_token and return a tensor in Pytorch
    new_user_input_ids = tokenizer.encode(input("User:") + tokenizer.eos_token, return_tensors='pt')

    # append the new user input tokens to the chat history
    bot_input_ids = torch.cat([chat_history_ids, new_user_input_ids], dim=-1) if step > 0 else new_user_input_ids

    # generated a response while limiting the total chat history to 1000 tokens,
    chat_history_ids = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)

    # pretty print last ouput tokens from bot
    str = ("DialoGPT: {}".format(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)))

    engine.say(tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True))
    print(str)
    engine.runAndWait()