-
Notifications
You must be signed in to change notification settings - Fork 3
/
python_ai.py
243 lines (185 loc) · 10.3 KB
/
python_ai.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import *
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import re
from selenium.webdriver.chrome.service import Service as ChromeService
class ChatBot:
def __init__(self, url: str, view: bool = False, debug: bool = True, incognito:bool=True, executable_path: str = None, lang=""):
"""The default classe for the chatbots
Args:
url (str): the url of the website to scrape
view (bool, optional): start a visible instance of the browser. Defaults to False.
debug (bool, optional): prints log messages. Defaults to True.
incognito (bool, optional): start the browser in incognito mode. Defaults to True.
executable_path (str, optional): the executable path of the ChromDriver. Defaults to "chromedriver.exe".
lang (str, optional): experimental. Defaults to "".
"""
self.view = view
self.debug = debug
self.name = 'Chatbot Class'
self.lang = lang
self.langs = {"fr": ' Répond en Francais',
"en": '[Answer in English]', "": "", None: "", "None": ""}
self.role = ""
chrome_options = Options()
if not self.view:
print("headless")
chrome_options.add_argument("--headless=new")
else:
chrome_options.add_experimental_option("detach", True)
if incognito:
chrome_options.add_argument('--incognito')
if executable_path:
service = ChromeService(executable_path=executable_path)
else:
service = None
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--start-maximized")
self.driver = webdriver.Chrome(options=chrome_options, service=service)
self.driver.get(url)
def promptify(self, prompt):
return self.role + " " + prompt + " " + self.langs[self.lang]
def close(self):
self.driver.quit()
def ask(self):
pass
class youChat(ChatBot):
def __init__(self, *args, **kwargs):
super().__init__("https://you.com/search?q=who+are+you&tbm=youchat&cfr=chat", view=True, *args, **kwargs)
self.name = 'YouChat'
time.sleep(1)
def ask(self, prompt: str = 'Who are you ?'):
self.driver.find_element(By.ID, 'search-input-textarea').send_keys(prompt)
self.driver.find_element(By.ID, 'search-input-textarea').send_keys(Keys.RETURN)
time.sleep(3)
stop = self.driver.page_source
while "Stop generating" in stop:
time.sleep(1)
stop = self.driver.page_source
return self.driver.find_elements(By.XPATH, '/html/body/div/div/div/div[3]/div[2]/section/main/div/div[1]/div[2]/div/div/li/div[1]/div')[-1].text
class CodeAi(ChatBot):
def __init__(self, *args, **kwargs):
super().__init__("https://codepal.ai", *args, **kwargs)
self.driver.find_element(By.XPATH, '//*[@id="termsfeed-com---nb"]/div/div[3]/button[2]').click()
def set_language(self, langue='python'):
self.driver.find_element(By.ID, "select2-select-language-container").click()
self.driver.find_element(By.XPATH, "/html/body/span/span/span[1]/input").send_keys(langue)
time.sleep(0.5)
self.driver.find_element(By.ID, 'select2-select-language-results').click()
def ask(self, prompt=""):
if prompt not in ["", " ", None]:
self.driver.find_element(By.ID, 'input-code-description').clear()
self.driver.find_element(By.ID, 'input-code-description').send_keys(prompt)
self.driver.find_element(By.ID, "btn-generate-code").click()
time.sleep(10)
return self.driver.find_element(By.ID, "state-success").text
class ChatAi(ChatBot):
def __init__(self, *args, **kwargs):
super().__init__("https://www.aichatting.net/", *args, **kwargs)
self.name = "Chat"
def ask(self, promt):
self.driver.find_element(By.XPATH, '//*[@id="scrollContainer"]/div[2]/div/div[1]/input').send_keys(promt)
self.driver.find_element(By.XPATH, '//*[@id="scrollContainer"]/div[2]/div/div[1]/input').send_keys(Keys.RETURN)
time.sleep(3)
res = len(self.driver.find_elements(By.CLASS_NAME, 'MessageItem_msg__C8oWx')[-1].text)
time.sleep(1)
while res < len(self.driver.find_elements(By.CLASS_NAME, 'MessageItem_msg__C8oWx')[-1].text) and self.driver.find_elements(By.CLASS_NAME, 'MessageItem_msg__C8oWx')[-1].text != promt:
time.sleep(4)
res = len(self.driver.find_elements(By.CLASS_NAME, 'MessageItem_msg__C8oWx')[-1].text)
return self.driver.find_elements(By.CLASS_NAME, 'MessageItem_msg__C8oWx')[-1].text
class LLMA(ChatBot):
def __init__(self, *args, **kwargs):
super().__init__("https://huggingface-projects-llama-2-7b-chat.hf.space/?__theme=dark", *args, **kwargs)
self.name = 'LLMA'
time.sleep(1)
def undo(self):
self.driver.find_element((By.ID, 'component-10')).click()
def retry(self):
self.driver.find_element(By.ID, "component-9").click()
time.sleep(3)
res = len(self.driver.find_elements(By.XPATH, '//div[@data-testid="bot"]')[-1].text)
time.sleep(1)
while res < len(self.driver.find_elements(By.XPATH, '//div[@data-testid="bot"]')[-1].text):
time.sleep(4)
res = len(self.driver.find_elements(By.XPATH, '//div[@data-testid="bot"]')[-1].text)
return self.driver.find_elements(By.XPATH, '//div[@data-testid="bot"]')[-1].text
def get_respnces(self):
return [i.text for i in self.driver.find_elements(By.XPATH, '//div[@data-testid="bot"]')]
def ask(self, prompt: str = 'Who are you ?'):
self.driver.find_element(By.XPATH, '//*[@id="component-11"]/label/textarea').send_keys(prompt)
self.driver.find_element(By.XPATH, '//*[@id="component-11"]/label/textarea').send_keys(Keys.RETURN)
time.sleep(10)
res = len(self.driver.find_elements(By.XPATH, '//*[@id="component-8"]/div[2]/div[2]/div/div[2]/div')[-1].text)
time.sleep(1)
while res < len(self.driver.find_elements(By.XPATH, '//*[@id="component-8"]/div[2]/div[2]/div/div[2]/div')[-1].text):
time.sleep(5)
res = res = len(self.driver.find_elements(By.XPATH, '//*[@id="component-8"]/div[2]/div[2]/div/div[2]/div')[-1].text)
res = res = self.driver.find_elements(By.XPATH, '//*[@id="component-8"]/div[2]/div[2]/div/div[2]/div')[-1].text
return res
class FreeGPT(ChatBot):
def __init__(self, *args, **kwargs):
super().__init__("https://chataigpt.org/", *args, **kwargs)
self.name = 'freeGPT'
time.sleep(1)
self.driver.find_element(By.ID, "ez-accept-all").click()
el = self.driver.find_element(By.XPATH, '/html/body/div[2]')
self.driver.execute_script("arguments[0].remove()", el)
def ask(self, prompt: str = 'Who are you ?'):
self.driver.find_element(By.XPATH, '//*[@id="post-10"]/div/div/div[6]/div/div/div/div/div/div[3]/textarea').send_keys(prompt)
self.driver.find_element(By.XPATH, '//*[@id="post-10"]/div/div/div[6]/div/div/div/div/div/div[3]/textarea').send_keys(Keys.RETURN)
time.sleep(5)
res = len(self.driver.find_elements(By.CLASS_NAME, 'wpaicg-ai-message')[-1].text)
time.sleep(1)
while res < len(self.driver.find_elements(By.CLASS_NAME, 'wpaicg-ai-message')[-1].text):
time.sleep(5)
res = res = len(self.driver.find_elements(By.CLASS_NAME, 'wpaicg-ai-message')[-1].text)
res = res = self.driver.find_elements(By.CLASS_NAME, 'wpaicg-ai-message')[-1].text
return res
class EChat(ChatBot):
def __init__(self, *args, **kwargs):
super().__init__("https://echatgpt.org/chat", *args, **kwargs)
self.name = 'eChat'
time.sleep(1)
self.driver.find_element(By.XPATH, "/html/body/div[4]/div[2]/div[1]/div[2]/div[2]/button[1]").click()
def ask(self, prompt: str = 'Who are you ?'):
self.driver.find_element(By.CLASS_NAME, 'chatview__textarea-message').send_keys(prompt)
self.driver.find_element(By.CLASS_NAME, 'chatview__textarea-message').send_keys(Keys.RETURN)
time.sleep(10)
res = len(self.driver.find_elements(By.CLASS_NAME, 'flex-row-reverse')[-1].text)
time.sleep(1)
while res < len(self.driver.find_elements(By.CLASS_NAME, 'flex-row-reverse')[-1].text):
time.sleep(5)
res = res = len(self.driver.find_elements(
By.CLASS_NAME, 'flex-row-reverse')[-1].text)
res = res = self.driver.find_elements(
By.CLASS_NAME, 'flex-row-reverse')[-1].text
return res
class MyGPT(ChatBot):
def __init__(self, *args, **kwargs):
super().__init__("https://www.mychatbotgpt.com/", *args, **kwargs)
self.name = 'MyGPT'
time.sleep(1)
def ask(self, prompt: str = 'Who are you ?'):
self.driver.find_element(By.XPATH, '//*[@id="post-10"]/div/div/div[6]/div/div/div/div/div/div[3]/textarea').send_keys(prompt)
self.driver.find_element(By.XPATH, '//*[@id="post-10"]/div/div/div[6]/div/div/div/div/div/div[3]/textarea').send_keys(Keys.RETURN)
time.sleep(5)
res = len(self.driver.find_elements(By.CLASS_NAME, 'wpaicg-ai-message')[-1].text)
time.sleep(1)
while res < len(self.driver.find_elements(By.CLASS_NAME, 'wpaicg-ai-message')[-1].text):
time.sleep(5)
res = res = len(self.driver.find_elements(By.CLASS_NAME, 'wpaicg-ai-message')[-1].text)
res = res = self.driver.find_elements(By.CLASS_NAME, 'wpaicg-ai-message')[-1].text
return res
if __name__ == '__main__':
chat = EChat()
command = input(">")
while command not in ("q", "quit", "exit"):
print(chat.ask(command))
command = input(">")
print("Bye")
chat.close()