-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat.py
72 lines (64 loc) · 2.46 KB
/
chat.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
import requests
from modules import *
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--no-sandbox")
driver = webdriver.Chrome("./drivers/chromedriver.exe",
chrome_options=chrome_options)
prefs = {"profile.managed_default_content_settings.images":2}
chrome_options.add_experimental_option("prefs", prefs)
url = "http://3.87.126.120:7000/response?question="
def chat_response(query="", initialise=False):
if initialise:
time.sleep(5)
res = driver.find_elements_by_xpath(
'/html/body/form/table/tbody/tr[3]/td[2]')[0].text
return res
else:
inp = driver.find_element_by_name('ENTRY')
inp.send_keys(query)
inp.send_keys(Keys.ENTER)
time.sleep(3)
res = driver.find_elements_by_xpath(
'/html/body/form/table/tbody/tr[3]/td[2]')[0].text
if "elbot" in res.lower():
res = res[:res.find("Elbot")] + "VVS" + res[res.find("Elbot")+5:]
return res
def master_chat():
print("Preparing Chat Engine : ")
driver.get("http://elbot-e.artificial-solutions.com/cgi-bin/elbot.cgi")
# driver.find_element_by_class_name("pb-widget__description__chat-now__button").click()
time.sleep(5)
print(chat_response(initialise=True))
system("cls")
print("I am ready..")
r = sr.Recognizer()
while 1:
with sr.Microphone() as source:
print("Say something")
audio = r.listen(source, timeout=3)
print("Times Up !!")
try:
text = r.recognize_google(audio)
except:
continue
print("Text : " + text)
if "exit" in text:
return
else:
payload = url + text
response = requests.get(payload)
try:
response = json.loads(
str(response.__dict__['_content'].decode("utf-8")))
if response['score'] > 0.5:
print("From Model : ", end=" ")
print(response['answer'], end=" ")
print(response['score'])
else:
print("From Web : ", end=" ")
print(chat_response(query=text))
except json.decoder.JSONDecodeError:
print(str(response.__dict__['_content'].decode("utf-8")))
if __name__ == "__main__":
master_chat()