-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
102 lines (72 loc) · 2.42 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import argparse
import os
import openai
import subprocess
import speech_recognition as sr
LANGUAGES = {
"en": "english",
"zh-hans": "chinese"
}
def transcribe_speech(language: str) -> str:
"""
Records audio from the microphone and transcribes it into text.
Args:
language: A string indicating the language of the speech to be transcribed.
Returns:
A string containing the transcribed text.
Raises:
sr.RequestError: If there is an error with the API request.
sr.UnknownValueError: If the speech could not be transcribed.
"""
print("\n\nListening...")
recognizer = sr.Recognizer()
mic = sr.Microphone()
with mic as source:
recognizer.adjust_for_ambient_noise(source)
audio = recognizer.listen(source)
print(audio)
transcript = recognizer.recognize_whisper(audio, language=language)
print(transcript)
return transcript
def send_request(language: str, words: str) -> None:
"""
Sends a request to the OpenAI API and speaks out the response.
Args:
language: A string indicating the language of the chat message.
words: A string containing the chat message to be sent.
Returns:
None.
Raises:
openai.error.OpenAIError: If there is an error with the OpenAI API request.
"""
openai.api_key = os.getenv("OPENAI_API_KEY")
completion = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0301",
messages=[
{
"role": "user",
"content": words
}
]
)
answer = completion.choices[0].message["content"]
print(answer)
out = answer.replace('\n', " ")
cmd_str = f"say {'--voice Tingting' if language == 'chinese' else ''} \"{out}\""
subprocess.call(cmd_str, shell=True)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--language",
type=str,
choices=sorted(LANGUAGES.keys()),
default="en",
metavar="LANGUAGE",
help="language to talk, available: {%(choices)s}",
)
options = parser.parse_args()
language = LANGUAGES[options.language]
print(language)
while True:
input_words = transcribe_speech(language)
send_request(language, input_words)