-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoicebot_without_GUI.py
More file actions
51 lines (41 loc) · 1.26 KB
/
voicebot_without_GUI.py
File metadata and controls
51 lines (41 loc) · 1.26 KB
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
import openai
import speech_recognition as sr
import pyttsx3
# set up OpenAI API credentials
openai.api_key = "Your_API_Key"
# initialize text-to-speech engine
engine = pyttsx3.init()
# define function to capture user's voice input
def get_voice_input():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Speak:")
audio = r.listen(source)
try:
text_input = r.recognize_google(audio)
print("You said: ", text_input)
return text_input
except:
print("Could not recognize input.")
return ""
# define function to generate text response using OpenAI API
def generate_response(input_text):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=input_text,
max_tokens=60,
n=1,
stop=None,
temperature=0.5,
)
return response.choices[0].text.strip()
# define function to convert text response to speech
def generate_speech_response(text_response):
engine.say(text_response)
engine.runAndWait()
# main loop to continuously listen for user input and generate responses
while True:
user_input = get_voice_input()
if user_input:
response = generate_response(user_input)
generate_speech_response(response)