-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatbot.py
150 lines (98 loc) · 2.79 KB
/
chatbot.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from chat_app import*
import sys,time
from socket import socket
import pyttsx3
import datetime
import speech_recognition as sr
from gtts import gTTS # recognize audio
import pyaudio
import regex as re
# In[2]:
from snap7.client import Client as SnapClient
from snap7.types import areas
from snap7.util import *
from opcua import Server, Client, ua, uamethod
from plc_utils import read_data, write_data
# In[3]:
engine = pyttsx3.init()
# In[4]:
global plc
plc=SnapClient()
plc_ip="192.168.0.1"
plc.connect(plc_ip, 0, 1)
plc.get_connected()
plc.get_cpu_state()
engine.say("Client Connected")
engine.runAndWait()
# In[5]:
def voice_data():
r=sr.Recognizer()
with sr.Microphone() as source:
print('this is where you start talking...')
r.adjust_for_ambient_noise(source)
audio =r.listen(source)
data=''
try:
data=r.recognize_google(audio)
except sr.UnknownValueError:#check unknown error
print('i dont understand'
'google speech recognition does not recognize text')
except sr.RequestError as e:
print('SORRY, THE SERVICE IS DOWN'+e)
return data
# In[6]:
def conversation():
print("Talk to me.")
while True:
keywords=['ph',"setpoint","set","point","value"]
text=input("You: ")
#text=voice_data()
if any(i in text for i in keywords ):
print("keyword found")
return text
break
print(chatbot_response(text))
engine.say(chatbot_response(text))
engine.runAndWait()
# In[7]:
def get_ph():
message=conversation()
print(message)
values=re.findall('\d+', message )
if len(values)==0:
while True:
print("didnt get that.what setpoint do you want?")
message=conversation()
values=re.findall('\d+', message )
if len(values)>0:
ph_value=int(values[0])
break
else:
ph_value=values[0]
return ph_value
# In[8]:
def set_PH():
#init_plc()
setpoint=get_ph()
#write_data(plc, 'DB3.DBD0', setpoint)
print('INFO:setpoint set to {}'.format(setpoint))
engine.say('setpoint set to {}'.format(setpoint))
engine.runAndWait()
print('INFO:starting process')
engine.say('starting process')
engine.runAndWait()
write_data(plc, 'M0.3', False)#set stop to False
write_data(plc, 'M0.2', True)#set start to True
# In[9]:
def stop_process():
stop_words=['stop','end','halt']
text=input("You: ")
#text=voice_data()
if any(i in text for i in stop_words ):
print("stop word found")
write_data(plc, 'M0.3', True)#stop to true
write_data(plc, 'M0.2', False)#start to false
# In[ ]: