From af204c31cb047436f4e1c36199e15692eb271f20 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Fri, 9 Feb 2024 18:11:35 +0100 Subject: Read cookies from config in Gemini Fixes for OpenaiChat Provider --- g4f/Provider/CreateImagesBing.py | 4 ++-- g4f/Provider/needs_auth/Gemini.py | 5 +++-- g4f/Provider/needs_auth/OpenaiChat.py | 3 ++- g4f/cookies.py | 8 +++++--- g4f/webdriver.py | 3 ++- 5 files changed, 14 insertions(+), 9 deletions(-) (limited to 'g4f') diff --git a/g4f/Provider/CreateImagesBing.py b/g4f/Provider/CreateImagesBing.py index 4d045188..59eafe0c 100644 --- a/g4f/Provider/CreateImagesBing.py +++ b/g4f/Provider/CreateImagesBing.py @@ -88,7 +88,7 @@ class CreateImagesBing: cookies = self.cookies or get_cookies(".bing.com", False) if "_U" not in cookies: raise MissingAuthError('Missing "_U" cookie') - proxy = os.environ.get("G4F_PROXY") + proxy = self.proxy or os.environ.get("G4F_PROXY") async with create_session(cookies, proxy) as session: - images = await create_images(session, prompt, self.proxy) + images = await create_images(session, prompt, proxy) return ImageResponse(images, prompt, {"preview": "{image}?w=200&h=200"}) \ No newline at end of file diff --git a/g4f/Provider/needs_auth/Gemini.py b/g4f/Provider/needs_auth/Gemini.py index 402fc02f..cf739a22 100644 --- a/g4f/Provider/needs_auth/Gemini.py +++ b/g4f/Provider/needs_auth/Gemini.py @@ -66,6 +66,8 @@ class Gemini(AsyncGeneratorProvider): prompt = format_prompt(messages) if not cookies: + cookies = get_cookies(".google.com", False, True) + if "__Secure-1PSID" not in cookies or "__Secure-1PSIDCC" not in cookies: driver = None try: driver = get_browser(proxy=proxy) @@ -88,8 +90,6 @@ class Gemini(AsyncGeneratorProvider): if driver: driver.close() - if not cookies: - cookies = get_cookies(".google.com", False) if "__Secure-1PSID" not in cookies: raise MissingAuthError('Missing "__Secure-1PSID" cookie') @@ -101,6 +101,7 @@ class Gemini(AsyncGeneratorProvider): ) as session: async with session.get(cls.url, proxy=proxy) as response: text = await response.text() + open("test.html", "w").write(text) match = re.search(r'SNlM0e\":\"(.*?)\"', text) if match: snlm0e = match.group(1) diff --git a/g4f/Provider/needs_auth/OpenaiChat.py b/g4f/Provider/needs_auth/OpenaiChat.py index 7c707b5e..b1b267fd 100644 --- a/g4f/Provider/needs_auth/OpenaiChat.py +++ b/g4f/Provider/needs_auth/OpenaiChat.py @@ -38,6 +38,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): supports_gpt_4 = True default_model = None models = ["gpt-3.5-turbo", "gpt-4", "gpt-4-gizmo"] + model_aliases = {"text-davinci-002-render-sha": "gpt-3.5-turbo"} _cookies: dict = {} @classmethod @@ -403,7 +404,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin): if "message_type" not in line["message"]["metadata"]: continue try: - image_response = await cls.get_generated_image(session, headers, line) + image_response = await cls.get_generated_image(session, auth_headers, line) if image_response: yield image_response except Exception as e: diff --git a/g4f/cookies.py b/g4f/cookies.py index b5c869e7..af0e6192 100644 --- a/g4f/cookies.py +++ b/g4f/cookies.py @@ -27,7 +27,7 @@ _cookies: Dict[str, Cookies] = {} if has_browser_cookie3 and os.environ.get('DBUS_SESSION_BUS_ADDRESS') == "/dev/null": _LinuxPasswordManager.get_password = lambda a, b: b"secret" -def get_cookies(domain_name: str = '', raise_requirements_error: bool = True) -> Dict[str, str]: +def get_cookies(domain_name: str = '', raise_requirements_error: bool = True, single_browser: bool = False) -> Dict[str, str]: """ Load cookies for a given domain from all supported browsers and cache the results. @@ -40,7 +40,7 @@ def get_cookies(domain_name: str = '', raise_requirements_error: bool = True) -> if domain_name in _cookies: return _cookies[domain_name] - cookies = load_cookies_from_browsers(domain_name, raise_requirements_error) + cookies = load_cookies_from_browsers(domain_name, raise_requirements_error, single_browser) _cookies[domain_name] = cookies return cookies @@ -50,7 +50,7 @@ def set_cookies(domain_name: str, cookies: Cookies = None) -> None: elif domain_name in _cookies: _cookies.pop(domain_name) -def load_cookies_from_browsers(domain_name: str, raise_requirements_error: bool = True) -> Cookies: +def load_cookies_from_browsers(domain_name: str, raise_requirements_error: bool = True, single_browser: bool = False) -> Cookies: """ Helper function to load cookies from various browsers. @@ -73,6 +73,8 @@ def load_cookies_from_browsers(domain_name: str, raise_requirements_error: bool for cookie in cookie_jar: if cookie.name not in cookies: cookies[cookie.name] = cookie.value + if single_browser and len(cookie_jar): + break except BrowserCookieError: pass except Exception as e: diff --git a/g4f/webdriver.py b/g4f/webdriver.py index b54fae15..d9028a63 100644 --- a/g4f/webdriver.py +++ b/g4f/webdriver.py @@ -65,7 +65,8 @@ def get_browser( options=options, user_data_dir=user_data_dir, driver_executable_path=driver, - headless=headless + headless=headless, + patcher_force_close=True ) def get_driver_cookies(driver: WebDriver) -> Cookies: -- cgit v1.2.3 From 33b247305c84ee6917ed8d3d2a10873646379730 Mon Sep 17 00:00:00 2001 From: H Lohaus Date: Fri, 9 Feb 2024 18:47:57 +0100 Subject: Update Gemini.py --- g4f/Provider/needs_auth/Gemini.py | 1 - 1 file changed, 1 deletion(-) (limited to 'g4f') diff --git a/g4f/Provider/needs_auth/Gemini.py b/g4f/Provider/needs_auth/Gemini.py index cf739a22..32510505 100644 --- a/g4f/Provider/needs_auth/Gemini.py +++ b/g4f/Provider/needs_auth/Gemini.py @@ -101,7 +101,6 @@ class Gemini(AsyncGeneratorProvider): ) as session: async with session.get(cls.url, proxy=proxy) as response: text = await response.text() - open("test.html", "w").write(text) match = re.search(r'SNlM0e\":\"(.*?)\"', text) if match: snlm0e = match.group(1) -- cgit v1.2.3