forked from NicolasCort/ChatGPT_Voice_Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChat_bot_es.py
80 lines (69 loc) · 2.59 KB
/
Chat_bot_es.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import openai
import speech_recognition as sr
import pyttsx3
import time
# Initialize OpenAI API
openai.api_key = ""
# Initialize the text to speech engine
engine=pyttsx3.init()
def transcribe_audio_to_test(filename):
recogizer=sr.Recognizer()
with sr.AudioFile(filename)as source:
audio=recogizer.record(source)
try:
return recogizer.recognize_google(audio, language="es")
except:
print("No se que ha pasao")
def generate_response(prompt):
response= openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=4000,
n=1,
stop=None,
temperature=0.5,
)
return response ["choices"][0]["text"]
# Set Spanish voice for text-to-speech engine
voices = engine.getProperty('voices')
spanish_voice = None
for voice in voices:
if "spanish" in voice.languages:
spanish_voice = voice.id
if spanish_voice is not None:
engine.setProperty('voice', spanish_voice)
def speak_text(text):
engine.say(text)
engine.runAndWait()
def main():
while True:
#Waith for user say "genius"
print("Di 'Hola' para empezar a grabar")
with sr.Microphone() as source:
recognizer=sr.Recognizer()
audio=recognizer.listen(source)
try:
transcription = recognizer.recognize_google(audio, language="es")
if transcription.lower()=="hola":
#record audio
filename ="input.wav"
print("Dime que quieres mozo")
with sr.Microphone() as source:
recognizer=sr.Recognizer()
source.pause_threshold=1
audio=recognizer.listen(source,phrase_time_limit=None,timeout=None)
with open(filename,"wb")as f:
f.write(audio.get_wav_data())
#transcript audio to test
text=transcribe_audio_to_test(filename)
if text:
print(f"Yo {text}")
#Generate the response
response = generate_response(text)
print(f"El bot ese dice: {response}")
#read resopnse using GPT3
speak_text(response)
except Exception as e:
print("Ahhhhhh erroor : {}".format(e))
if __name__=="__main__":
main()