0

我想使用 DialoGPT 使用麦克风和扬声器与我交谈。

但是,在我到达那里之前,我想首先使用 pyttsx3 以某种方式将此聊天机器人连接到扬声器。我能够正确使用 pyttsx3 使用预先插入的文本(即“hello world”)从我的计算机扬声器中产生声音。

我还可以使用 DialoGPT 使用终端进行对话。

这就是将两者联系起来的第一个问题。我已经粘贴了我当前的代码,想知道是否有人可以为我提供一些帮助。

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))
4

1 回答 1

0

解决了~

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()
于 2021-08-02T17:10:35.373 回答