Read cookies from config in Gemini

Fixes for OpenaiChat Provider
This commit is contained in:
Heiner Lohaus 2024-02-09 18:11:35 +01:00
parent 2054acce0a
commit af204c31cb
5 changed files with 14 additions and 9 deletions

View File

@ -88,7 +88,7 @@ class CreateImagesBing:
cookies = self.cookies or get_cookies(".bing.com", False) cookies = self.cookies or get_cookies(".bing.com", False)
if "_U" not in cookies: if "_U" not in cookies:
raise MissingAuthError('Missing "_U" cookie') 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: 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"}) return ImageResponse(images, prompt, {"preview": "{image}?w=200&h=200"})

View File

@ -66,6 +66,8 @@ class Gemini(AsyncGeneratorProvider):
prompt = format_prompt(messages) prompt = format_prompt(messages)
if not cookies: if not cookies:
cookies = get_cookies(".google.com", False, True)
if "__Secure-1PSID" not in cookies or "__Secure-1PSIDCC" not in cookies:
driver = None driver = None
try: try:
driver = get_browser(proxy=proxy) driver = get_browser(proxy=proxy)
@ -88,8 +90,6 @@ class Gemini(AsyncGeneratorProvider):
if driver: if driver:
driver.close() driver.close()
if not cookies:
cookies = get_cookies(".google.com", False)
if "__Secure-1PSID" not in cookies: if "__Secure-1PSID" not in cookies:
raise MissingAuthError('Missing "__Secure-1PSID" cookie') raise MissingAuthError('Missing "__Secure-1PSID" cookie')
@ -101,6 +101,7 @@ class Gemini(AsyncGeneratorProvider):
) as session: ) as session:
async with session.get(cls.url, proxy=proxy) as response: async with session.get(cls.url, proxy=proxy) as response:
text = await response.text() text = await response.text()
open("test.html", "w").write(text)
match = re.search(r'SNlM0e\":\"(.*?)\"', text) match = re.search(r'SNlM0e\":\"(.*?)\"', text)
if match: if match:
snlm0e = match.group(1) snlm0e = match.group(1)

View File

@ -38,6 +38,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
supports_gpt_4 = True supports_gpt_4 = True
default_model = None default_model = None
models = ["gpt-3.5-turbo", "gpt-4", "gpt-4-gizmo"] models = ["gpt-3.5-turbo", "gpt-4", "gpt-4-gizmo"]
model_aliases = {"text-davinci-002-render-sha": "gpt-3.5-turbo"}
_cookies: dict = {} _cookies: dict = {}
@classmethod @classmethod
@ -403,7 +404,7 @@ class OpenaiChat(AsyncGeneratorProvider, ProviderModelMixin):
if "message_type" not in line["message"]["metadata"]: if "message_type" not in line["message"]["metadata"]:
continue continue
try: 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: if image_response:
yield image_response yield image_response
except Exception as e: except Exception as e:

View File

@ -27,7 +27,7 @@ _cookies: Dict[str, Cookies] = {}
if has_browser_cookie3 and os.environ.get('DBUS_SESSION_BUS_ADDRESS') == "/dev/null": if has_browser_cookie3 and os.environ.get('DBUS_SESSION_BUS_ADDRESS') == "/dev/null":
_LinuxPasswordManager.get_password = lambda a, b: b"secret" _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. 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: if domain_name in _cookies:
return _cookies[domain_name] 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 _cookies[domain_name] = cookies
return cookies return cookies
@ -50,7 +50,7 @@ def set_cookies(domain_name: str, cookies: Cookies = None) -> None:
elif domain_name in _cookies: elif domain_name in _cookies:
_cookies.pop(domain_name) _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. 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: for cookie in cookie_jar:
if cookie.name not in cookies: if cookie.name not in cookies:
cookies[cookie.name] = cookie.value cookies[cookie.name] = cookie.value
if single_browser and len(cookie_jar):
break
except BrowserCookieError: except BrowserCookieError:
pass pass
except Exception as e: except Exception as e:

View File

@ -65,7 +65,8 @@ def get_browser(
options=options, options=options,
user_data_dir=user_data_dir, user_data_dir=user_data_dir,
driver_executable_path=driver, driver_executable_path=driver,
headless=headless headless=headless,
patcher_force_close=True
) )
def get_driver_cookies(driver: WebDriver) -> Cookies: def get_driver_cookies(driver: WebDriver) -> Cookies: