Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8a80e0

Browse files
committed
Updated driver so it accepts the new modal. Bumped the library version.
1 parent ccd037f commit d8a80e0

File tree

3 files changed

+54
-30
lines changed

3 files changed

+54
-30
lines changed

gpt4_openai/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
4242
raise ValueError("You have reached the maximum number of requests per hour ! Help me to Improve. Abusing this tool is at your own risk")
4343
else:
4444
sleep(2)
45-
data = self.chatbot.send_message(prompt)
45+
data = self.chatbot.send_message(prompt, model='gpt4')
4646
#print(data)
4747
response = data["message"]
4848
self.conversation = data["conversation_id"]

gpt4_openai/driver.py

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
'//div[starts-with(@class, "markdown prose w-full break-words")]',
2727
)
2828
chatgpt_alert = (By.XPATH, '//div[@role="alert"]')
29+
chatgpt_dialog = (By.XPATH, '//div[@role="dialog"]')
2930
chatgpt_intro = (By.ID, 'headlessui-portal-root')
3031
chatgpt_login_btn = (By.XPATH, '//button[text()="Log in"]')
3132
chatgpt_login_h1 = (By.XPATH, '//h1[text()="Welcome back"]')
@@ -80,6 +81,7 @@ def __init__(
8081
:param chrome_args: The arguments to pass to the browser
8182
:param moderation: Whether to enable message moderation
8283
:param verbose: Whether to enable verbose logging
84+
:param headless: Whether to run the browser in headless mode
8385
'''
8486
self.__init_logger(verbose)
8587

@@ -149,7 +151,7 @@ def __init_logger(self, verbose: bool) -> None:
149151
:param verbose: Whether to enable verbose logging
150152
'''
151153
self.logger = logging.getLogger('pyChatGPT')
152-
self.logger.setLevel(logging.INFO)
154+
self.logger.setLevel(logging.DEBUG)
153155
if verbose:
154156
formatter = logging.Formatter('[%(funcName)s] %(message)s')
155157
stream_handler = logging.StreamHandler()
@@ -377,19 +379,39 @@ def __check_blocking_elements(self) -> None:
377379
Check for blocking elements and dismiss them
378380
'''
379381
self.logger.debug('Looking for blocking elements...')
382+
380383
try:
381-
intro = WebDriverWait(self.driver, 3).until(
382-
EC.presence_of_element_located(chatgpt_intro)
384+
# FInd a button to dismiss the dialog with class="btn relative btn-primary" inside the div[@role="dialog"]
385+
btn_to_dismiss = WebDriverWait(self.driver, 5).until(
386+
EC.presence_of_element_located((By.XPATH, '//div[@role="dialog"]//button[@class="btn relative btn-primary"]'))
383387
)
384-
self.logger.debug('Dismissing intro...')
385-
self.driver.execute_script('arguments[0].remove()', intro)
386-
except SeleniumExceptions.TimeoutException:
388+
if btn_to_dismiss:
389+
self.logger.debug('Dismissing dialog...')
390+
self.driver.execute_script('arguments[0].click()', btn_to_dismiss)
391+
except:
392+
pass
393+
394+
try:
395+
# for 3 times
396+
i = 0
397+
while i<=2:
398+
self.__sleep(0.4)
399+
if i !=2:
400+
#get the button with class="btn relative btn-neutral ml-auto"
401+
btn = WebDriverWait(self.driver, 5).until(
402+
EC.presence_of_element_located((By.XPATH, '//button[@class="btn relative btn-neutral ml-auto"]'))
403+
)
404+
else:
405+
#get the button with class="btn relative btn-primary ml-auto"
406+
btn = WebDriverWait(self.driver, 5).until(
407+
EC.presence_of_element_located((By.XPATH, '//button[@class="btn relative btn-primary ml-auto"]'))
408+
)
409+
if btn:
410+
self.logger.debug('Dismissing dialog...')
411+
self.driver.execute_script('arguments[0].click()', btn)
412+
i+=1
413+
except:
387414
pass
388-
389-
alerts = self.driver.find_elements(*chatgpt_alert)
390-
if alerts:
391-
self.logger.debug('Dismissing alert...')
392-
self.driver.execute_script('arguments[0].remove()', alerts[0])
393415

394416
def __stream_message(self):
395417
prev_content = ''
@@ -409,15 +431,15 @@ def __stream_message(self):
409431
if not result_streaming:
410432
break
411433

412-
def send_message(self, message: str, stream: bool = False) -> dict:
434+
def send_message(self, message: str, stream: bool = False, model: str = "default") -> dict:
413435
'''
414436
Send a message to ChatGPT\n
415437
:param message: Message to send
416438
:return: Dictionary with keys `message` and `conversation_id`
417439
'''
418440
self.logger.debug('Ensuring Cloudflare cookies...')
419441
self.__ensure_cf()
420-
self.__check_blocking_elements()
442+
#self.__check_blocking_elements()
421443

422444
# Wait for page to load
423445
try:
@@ -429,17 +451,19 @@ def send_message(self, message: str, stream: bool = False) -> dict:
429451

430452
# If we have paid access, we should select GPT4 model
431453
try:
432-
self.logger.debug('Trying to select model...')
433-
WebDriverWait(self.driver, 3).until(
434-
EC.presence_of_element_located(model_selector)
435-
).click()
436-
self.logger.debug('Paid access detected, selecting GPT4 model...')
437-
self.__sleep(1.0)
438-
WebDriverWait(self.driver, 3).until(
439-
EC.presence_of_element_located(gpt4_selector)
440-
).click()
441-
self.logger.debug('GPT4 model selected')
454+
if model == "gpt4":
455+
self.logger.debug('Trying to select model...')
456+
WebDriverWait(self.driver, 3).until(
457+
EC.presence_of_element_located(model_selector)
458+
).click()
459+
self.logger.debug('Paid access detected, selecting GPT4 model...')
460+
self.__sleep(1.0)
461+
WebDriverWait(self.driver, 3).until(
462+
EC.presence_of_element_located(gpt4_selector)
463+
).click()
464+
self.logger.debug('GPT4 model selected')
442465
except SeleniumExceptions.TimeoutException:
466+
print(">>> WARNING <<<\n>> You don't have paid access to GPT4, using default model...")
443467
self.logger.debug('Paid access not detected, using default model...')
444468

445469
self.logger.debug('Sending message...')
@@ -489,19 +513,19 @@ def send_message(self, message: str, stream: bool = False) -> dict:
489513
content = markdownify(response.get_attribute('innerHTML')).replace(
490514
'Copy code`', '`'
491515
)
516+
492517
pattern = re.compile(
493518
r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'
494519
)
495520
matches = pattern.search(self.driver.current_url)
496521
if not matches:
497522
self.driver.refresh()
498-
self.__sleep(1)
499-
self.__check_blocking_elements()
500-
self.__sleep(1)
523+
self.__sleep(2)
524+
#self.__check_blocking_elements()
501525
self.driver.execute_script("arguments[0].click();", WebDriverWait(self.driver, 10).until(
502526
EC.element_to_be_clickable(chatgpt_chats_list_first_node)
503527
))
504-
self.__sleep(1)
528+
self.__sleep(2)
505529
#print(self.driver.current_url)
506530
matches = pattern.search(self.driver.current_url)
507531

@@ -568,4 +592,4 @@ def refresh_chat_page(self) -> None:
568592

569593
self.driver.get(chatgpt_chat_url)
570594
self.__check_capacity(chatgpt_chat_url)
571-
self.__check_blocking_elements()
595+
self.__check_blocking_elements()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="gpt4-openai-api",
5-
version="0.1.0",
5+
version="0.2.0",
66
description="Python package for unofficial GPT-4 API access via chat.openai.com using Selenium browser",
77
author="Erol444",
88
author_email="[email protected]",

0 commit comments

Comments
 (0)