-
Notifications
You must be signed in to change notification settings - Fork 0
/
Max_assistant.txt
74 lines (63 loc) · 2.44 KB
/
Max_assistant.txt
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
import openai
import pyttsx3
import speech_recognition as sr
import time
#openAi api key
openai.api_key = "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"
#instalize the text to speech engine
engin = pyttsx3.init()
def transcribe_audio_to_text(filename):
recognizer = sr.Recognizer()
with sr.Audiofile(filename) as source:
audio = recognizer.record(source)
try:
return recognizer.recogniz_google(audio)
except:
print("an unknown error occured")
def generate_response(prompt):
response =openai.Completion.create(
engin="text-davinci-003",
prompt=prompt,
max_tokens=4000,
n=1,
stop=None,
temperature=0.5,
)
return response["choices"][0]["text"]
def speak_text(text):
engin.say(text)
engin.runAndWait()
def main():
while True:
#wait for user to say "max"
print("Say 'Max' to start recording your question..")
with sr.Microphone() as source:
recognizer = sr.Recognizer()
audio = recognizer.listen(source, timeout=5, phrase_time_limit=5)
try:
transcription = recognizer.recogniz_google(audio)
print("You said:", text)
if transcription.lower() == "max":
#Record audio
filename = "input.wav"
print("Say your question...")
with sr.Microphone()as source:
recognizer = sr.Recognizer()
source.pause_threshold = 1
audio = recognizer.listen(source, pharse_time_limit=None, timeout=None)
with open(filename, "wb") as f:
f.write(audio.get_wav_data())
# TranscribeAudio to text
text = transcribe_audio_to_text(filename)
if text:
print(f"You said: {text}")
#generate response using gpt-3
response = generate_response(text)
print(f"GPT-3say: {response}")
#read response using text_to_speech
speak_text(response)
except Exception as e:
print("An error 404 occurred:{}".format(e))
break
if __name__=="__main__":
main()