File size: 991 Bytes
ba7714e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import speech_recognition as sr
import pyttsx3
engine = pyttsx3.init()
listener =sr.Recognizer()
openai.api_key = "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"

while True:
    with sr.Microphone() as source:
        print("speak now...")
        voice = listener.listen(source)
        data = listener.recognize_google(voice)
        model = "text-davinci-003"

        if "exit" in data:
            break

    completion = openai.Completion.create(model ="text-davinci-002",
      prompt = data,
      max_tokens = 1024,
      temperature = 0.5,
      n = 1,
      stop = None)
    response = completion.choices[0].text
    choice = int(input("press 1 to print the response or press 2 to print and hear the response: "))

    if choice == 1:
        print(response)
    else:
        print(response)
        engine.say(response)
        engine.runAndWait()

    repeat = input("do you want to ask more questions?: ")
    if repeat in ["no","No","NO"]:
        break