Spaces:
Runtime error
Runtime error
File size: 1,489 Bytes
d2da3ad |
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 |
import openai
import pyttsx3
import speech_recognition as sr
# Set up OpenAI API
openai.api_key = "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"
# Set up Text-to-Speech engine
engine = pyttsx3.init()
# Set up Speech Recognition engine
r = sr.Recognizer()
# Set up Microphone
mic = sr.Microphone()
# Define function to convert text to speech
def speak(text):
engine.say(text)
engine.runAndWait()
# Define function to recognize speech
def recognize_speech():
with mic as source:
r.adjust_for_ambient_noise(source)
audio = r.listen(source)
try:
text = r.recognize_google(audio)
return text
except:
return ""
# Define function to get answer from OpenAI API
def get_answer(question):
response = openai.Completion.create(
engine="text-davinci-003",
prompt=f"Q: {question}\nA:",
temperature=0.5,
max_tokens=50,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
answer = response.choices[0].text.strip()
return answer
# Define main function
def main():
# Greet the user
speak("Hello! I'm your personal assistant. How can I help you today?")
while True:
# Listen for user's question
speak("Please ask me a question.")
question = recognize_speech()
# Get answer from OpenAI API
answer = get_answer(question)
# Speak the answer
speak(answer)
if __name__ == '__main__':
main() |