diff options
author | Tekky <98614666+xtekky@users.noreply.github.com> | 2023-10-22 23:55:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-22 23:55:32 +0200 |
commit | 955fb4bbaa3c31787bbe699aa913f22ec3a4a639 (patch) | |
tree | c298e3feb1110b047a74cdb224024b412712323a /g4f/Provider/needs_auth | |
parent | Merge pull request #1122 from Luneye/patch-2 (diff) | |
parent | Debug logging support (diff) | |
download | gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.tar gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.tar.gz gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.tar.bz2 gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.tar.lz gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.tar.xz gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.tar.zst gpt4free-955fb4bbaa3c31787bbe699aa913f22ec3a4a639.zip |
Diffstat (limited to 'g4f/Provider/needs_auth')
-rw-r--r-- | g4f/Provider/needs_auth/OpenaiChat.py | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/g4f/Provider/needs_auth/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py index b4b4a670..14619958 100644 --- a/g4f/Provider/needs_auth/OpenaiChat.py +++ b/g4f/Provider/needs_auth/OpenaiChat.py @@ -3,7 +3,7 @@ from __future__ import annotations import uuid, json, time from ..base_provider import AsyncGeneratorProvider -from ..helper import get_browser, get_cookies, format_prompt +from ..helper import get_browser, get_cookies, format_prompt, get_event_loop from ...typing import AsyncResult, Messages from ...requests import StreamSession @@ -73,26 +73,33 @@ class OpenaiChat(AsyncGeneratorProvider): last_message = new_message @classmethod - def browse_access_token(cls) -> str: - try: - from selenium.webdriver.common.by import By - from selenium.webdriver.support.ui import WebDriverWait - from selenium.webdriver.support import expected_conditions as EC + async def browse_access_token(cls) -> str: + def browse() -> str: + try: + from selenium.webdriver.common.by import By + from selenium.webdriver.support.ui import WebDriverWait + from selenium.webdriver.support import expected_conditions as EC - driver = get_browser() - except ImportError: - return + driver = get_browser() + except ImportError: + return - driver.get(f"{cls.url}/") - try: - WebDriverWait(driver, 1200).until( - EC.presence_of_element_located((By.ID, "prompt-textarea")) - ) - javascript = "return (await (await fetch('/api/auth/session')).json())['accessToken']" - return driver.execute_script(javascript) - finally: - time.sleep(1) - driver.quit() + driver.get(f"{cls.url}/") + try: + WebDriverWait(driver, 1200).until( + EC.presence_of_element_located((By.ID, "prompt-textarea")) + ) + javascript = "return (await (await fetch('/api/auth/session')).json())['accessToken']" + return driver.execute_script(javascript) + finally: + driver.close() + time.sleep(0.1) + driver.quit() + loop = get_event_loop() + return await loop.run_in_executor( + None, + browse + ) @classmethod async def fetch_access_token(cls, cookies: dict, proxies: dict = None) -> str: @@ -110,7 +117,7 @@ class OpenaiChat(AsyncGeneratorProvider): if cookies: cls._access_token = await cls.fetch_access_token(cookies, proxies) if not cls._access_token: - cls._access_token = cls.browse_access_token() + cls._access_token = await cls.browse_access_token() if not cls._access_token: raise RuntimeError("Read access token failed") return cls._access_token |