from hugchat import hugchat
from hugchat.login import Login
import speech_recognition as sr
from gtts import gTTS
from playsound import playsound
import pygame
import os
r = sr.Recognizer()
email='hope69034@gmail.com'
passwd='!Ing12924103'
sign = Login(email, passwd)
cookies = sign.login()
sign.saveCookies()
chatbot = hugchat.ChatBot(cookies=cookies.get_dict())
global count
count = 1
global file_name
file_name='./sound/sample.mp3'
def filename_change():
global file_name
global count
while os.path.exists(file_name):
count += 1
file_name = f'./sound/sample{count}.mp3'
def chatchat(a):
try:
output=chatbot.chat(a)
output = output.split('.')
output = output[0] +'.'
print('Bot:',output)
tts_en=gTTS(text=output, lang='en')
filename_change()
tts_en.save(file_name)
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load(file_name)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
pygame.mixer.music.stop()
pygame.mixer.quit()
pygame.quit()
except:
chatchat(a)
def api():
text = r.recognize_vosk(audio, language='en-US')
print('User:',text[14:-3])
chatchat(text)
while True:
with sr.Microphone() as source:
print('<<<<<<<<<<<<<<<< say someting >>>>>>>>>>>>>>>>')
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load('./say.mp3')
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
pygame.mixer.music.stop()
pygame.mixer.quit()
pygame.quit()
audio = r.listen(source)
try:
api()
except sr.UnknownValueError:
print('인식실패')
except sr.RequestError:
print('요청실패: {0}'.format(e))
잘 봤습니다. 좋은 글 감사합니다.