树莓派Linux实现ChatGPT语音交互(语音识别,TTS)
import speech_recognition as sr
from gtts import gTTS
from os import system
from time import sleep
from random_user_agent.params import SoftwareName, HardwareType
from random_user_agent.user_agent import UserAgent
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
# 初始化语音识别和文本转语音对象
recognizer = sr.Recognizer()
chrome_options = Options()
chrome_options.add_argument("--headless")
user_agent = UserAgent(software_names=[SoftwareName.CHROME], hardware_type=HardwareType.COMPUTER)
def get_audio():
# 获取音频输入
with sr.Microphone() as source:
print("请开始说话...")
audio = recognizer.listen(source)
try:
# 将音频转换为文本
text = recognizer.recognize_google(audio, language="zh-CN")
print(f"你说: {text}")
return text
except sr.UnknownValueError:
print("无法理解这个语音")
return None
except sr.RequestError as e:
print(f"识别服务出错; {e}")
return None
def say(text):
# 将文本转换为语音并播放
tts = gTTS(text=text, lang="zh-cn")
tts.save("output.mp3")
system("mpg321 output.mp3")
def interact_with_chatgpt(text):
# 与ChatGPT交流
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://chat.openai.com/auth/login")
sleep(5)
driver.execute_script("document.querySelector('[data-testid=\"login-email\"]').click()")
sleep(2)
driver.find_element(By.XPATH, '//*[@id="__next"]/div/main/div/div[2]/div/div/div[2]/div/div/input').send_keys(text)
sleep(2)
driver.execute_script("document.querySelector('[data-testid=\"send-button\"]').click()")
sleep(5)
response = driver.find_elements(By.XPATH, '//*[@data-testid="message-text-content"]')[-1].text
driver.quit()
return response
def main():
while True:
text = get_audio()
if text:
response = interact_with_chatgpt(text)
say(response)
sleep(1)
if __name__ == "__main__":
main()
这段代码实现了与ChatGPT的语音交云功能。首先,它使用speech_recognition
库来识别语音并将其转换为文本。然后,它使用gTTS
库将文本转换为语音,并使用mpg321
工具播放生成的音频文件。最后,它使用Selenium自动化工具与ChatGPT进行交云,并获取回应,然后再次使用gTTS
和mpg321
将回应转换为语音并播放。这个例子展示了如何将语音识别和文本到语音技术应用到实际的交云机器人中。
评论已关闭