-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
88 lines (54 loc) · 2.26 KB
/
api.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
# chatgpt
import os
import openai
from dotenv import load_dotenv
from pathlib import Path
import pyttsx3
import speech_recognition as sr
import time
load_dotenv(Path(".\.env"))
openai.api_key = os.getenv("SECRET_KEY")
class API:
def __init__(self) -> None:
self.engine = pyttsx3.init()
def chatgpt_input(self, userinput='Enter prompt'):
completion = openai.ChatCompletion.create(model="gpt-3.5-turbo",
messages=[{"role": "user",
"content":userinput }])
return completion
def TextToSpeech(self,answer):
self.engine.say(answer)
self.engine.runAndWait()
# time.sleep(2)
# self.engine.stop()
def SpeechToText(self):
MyText ="something went wrong"
try:
# use the microphone as source for input.
with sr.Microphone() as source2:
r = sr.Recognizer()
# wait for a second to let the recognizer
# adjust the energy threshold based on
# the surrounding noise level
r.adjust_for_ambient_noise(source2)
#listens for the user's input
print("Try and say something")
audio2 = r.listen(source2)
# Using google to recognize audio
MyText = r.recognize_google(audio2)
MyText = MyText.lower()
print("Did you say ",MyText)
#return MyText
# SpeechToText(MyText)
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
return
except sr.UnknownValueError:
print("unknown error occurred")
return
self.TextToSpeech(MyText)
# Loop infinitely for user to
# speak
if __name__ == "__main__":
api = API()
api.SpeechToText()