-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_with_file.py
113 lines (87 loc) · 3.17 KB
/
test_with_file.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import openai
import pyttsx3 as tts
import speech_recognition as sr
import config
import paho.mqtt.client as mqtt
import socket
import time
class AiBot:
def __init__(self):
hostname=socket.gethostname()
self.IPAddr=socket.gethostbyname(hostname)
self._engine = tts.init()
self._openAiKey = config.configDict["key"]
self.mqtt_client = mqtt.Client()
self.mqtt_client.on_connect = self.on_connect
self.mqtt_client.connect(self.IPAddr, 1883, 60) #
voices = self._engine.getProperty('voices')
self._engine.setProperty('voice',voices[config.configDict["voice"]].id) # 10 für dietpi; 0 für test rechner
openai.api_key = self._openAiKey
openai.api_base = "https://api.openai.com/v1"
def gererateRespose(self, prompt):
if not("?" in prompt):
prompt = prompt + "?"
prompt = " Beantworte auf Deutsch:" + prompt
print(prompt)
response = openai.Completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=3500,
n=1,
stop=None,
temperature=0.1,
)
self._termianl_answer(response= response.choices[0].text)
self._answer(response= response.choices[0].text)
def LED(self, prompt):
print(prompt)
if ("licht" in prompt and ("ein" in prompt or "an" in prompt)) :
self.mqtt_client.publish('LED', payload=1 , qos=0, retain=False)
self._answer("LED ein")
self._termianl_answer("LED ein")
if ("licht" in prompt and "aus" in prompt):
self.mqtt_client.publish('LED', payload=0 , qos=0, retain=False)
self._answer("LED aus")
self._termianl_answer("LED aus")
def _answer(self, response):
self._engine.say(""+ response)
self._engine.runAndWait()
def on_connect(self,client, userdata, flags, rc):
print(f"Connected with result code {rc}")
def _termianl_answer(self, response):
print(response)
myAI = AiBot()
while True:
#with sr.Microphone(device_index=config.configDict["device"]) as source
print("listening: ")
listner = sr.Recognizer()
audio = sr.AudioFile("LED_EIN.wav")
with audio as source:
audio = listner.record(source)
try:
text = listner.recognize_google(audio, language= 'de-at')#, show_all=True
except sr.exceptions.UnknownValueError:
pass
print(text)
#text = input()
# if ("Athena" in text):
if ("led" in text.lower()):
myAI.LED(text.lower())
else:
myAI.gererateRespose(text)
# else:
audio = sr.AudioFile("LED_AUS.wav")
time.sleep(3)
with audio as source:
audio = listner.record(source)
try:
text = listner.recognize_google(audio, language= 'de-at')#, show_all=True
except sr.exceptions.UnknownValueError:
pass
if ("led" in text.lower()):
myAI.LED(text.lower())
else:
myAI.gererateRespose(text)
# else:
print(text)
time.sleep(3)