diff options
Diffstat (limited to '')
-rw-r--r-- | g4f/Provider/BingCreateImages.py (renamed from g4f/Provider/CreateImagesBing.py) | 46 |
1 files changed, 4 insertions, 42 deletions
diff --git a/g4f/Provider/CreateImagesBing.py b/g4f/Provider/BingCreateImages.py index 59eafe0c..901b7787 100644 --- a/g4f/Provider/CreateImagesBing.py +++ b/g4f/Provider/BingCreateImages.py @@ -1,60 +1,22 @@ from __future__ import annotations import asyncio -import time import os from typing import Generator from ..cookies import get_cookies -from ..webdriver import WebDriver, get_driver_cookies, get_browser from ..image import ImageResponse from ..errors import MissingRequirementsError, MissingAuthError -from .bing.create_images import BING_URL, create_images, create_session +from .bing.create_images import create_images, create_session, get_cookies_from_browser -BING_URL = "https://www.bing.com" -TIMEOUT_LOGIN = 1200 - -def wait_for_login(driver: WebDriver, timeout: int = TIMEOUT_LOGIN) -> None: - """ - Waits for the user to log in within a given timeout period. - - Args: - driver (WebDriver): Webdriver for browser automation. - timeout (int): Maximum waiting time in seconds. - - Raises: - RuntimeError: If the login process exceeds the timeout. - """ - driver.get(f"{BING_URL}/") - start_time = time.time() - while not driver.get_cookie("_U"): - if time.time() - start_time > timeout: - raise RuntimeError("Timeout error") - time.sleep(0.5) - -def get_cookies_from_browser(proxy: str = None) -> dict[str, str]: - """ - Retrieves cookies from the browser using webdriver. - - Args: - proxy (str, optional): Proxy configuration. - - Returns: - dict[str, str]: Retrieved cookies. - """ - with get_browser(proxy=proxy) as driver: - wait_for_login(driver) - time.sleep(1) - return get_driver_cookies(driver) - -class CreateImagesBing: +class BingCreateImages: """A class for creating images using Bing.""" def __init__(self, cookies: dict[str, str] = {}, proxy: str = None) -> None: self.cookies = cookies self.proxy = proxy - def create_completion(self, prompt: str) -> Generator[ImageResponse, None, None]: + def create(self, prompt: str) -> Generator[ImageResponse, None, None]: """ Generator for creating imagecompletion based on a prompt. @@ -91,4 +53,4 @@ class CreateImagesBing: proxy = self.proxy or os.environ.get("G4F_PROXY") async with create_session(cookies, proxy) as session: images = await create_images(session, prompt, proxy) - return ImageResponse(images, prompt, {"preview": "{image}?w=200&h=200"})
\ No newline at end of file + return ImageResponse(images, prompt, {"preview": "{image}?w=200&h=200"} if len(images) > 1 else {})
\ No newline at end of file |