Jarvis / app.py
ILLICITV1's picture
Update app.py
8e856e0
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()