summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/needs_auth/BingCreateImages.py
diff options
context:
space:
mode:
authorkqlio67 <kqlio67@users.noreply.github.com>2024-11-06 17:39:14 +0100
committerkqlio67 <kqlio67@users.noreply.github.com>2024-11-06 17:39:14 +0100
commit6dd378d2aca256f45ff7b2fd23c59497aad82045 (patch)
treec07a20b0e2e3c1643065cef96910ae8acc84a5eb /g4f/Provider/needs_auth/BingCreateImages.py
parentUpdate (g4f/Provider/not_working/) (diff)
downloadgpt4free-6dd378d2aca256f45ff7b2fd23c59497aad82045.tar
gpt4free-6dd378d2aca256f45ff7b2fd23c59497aad82045.tar.gz
gpt4free-6dd378d2aca256f45ff7b2fd23c59497aad82045.tar.bz2
gpt4free-6dd378d2aca256f45ff7b2fd23c59497aad82045.tar.lz
gpt4free-6dd378d2aca256f45ff7b2fd23c59497aad82045.tar.xz
gpt4free-6dd378d2aca256f45ff7b2fd23c59497aad82045.tar.zst
gpt4free-6dd378d2aca256f45ff7b2fd23c59497aad82045.zip
Diffstat (limited to 'g4f/Provider/needs_auth/BingCreateImages.py')
-rw-r--r--g4f/Provider/needs_auth/BingCreateImages.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/g4f/Provider/needs_auth/BingCreateImages.py b/g4f/Provider/needs_auth/BingCreateImages.py
new file mode 100644
index 00000000..80984d40
--- /dev/null
+++ b/g4f/Provider/needs_auth/BingCreateImages.py
@@ -0,0 +1,54 @@
+from __future__ import annotations
+
+from ...cookies import get_cookies
+from ...image import ImageResponse
+from ...errors import MissingAuthError
+from ...typing import AsyncResult, Messages, Cookies
+from ..base_provider import AsyncGeneratorProvider, ProviderModelMixin
+from ..bing.create_images import create_images, create_session
+
+class BingCreateImages(AsyncGeneratorProvider, ProviderModelMixin):
+ label = "Microsoft Designer in Bing"
+ parent = "Bing"
+ url = "https://www.bing.com/images/create"
+ working = True
+ needs_auth = True
+ image_models = ["dall-e"]
+
+ def __init__(self, cookies: Cookies = None, proxy: str = None, api_key: str = None) -> None:
+ if api_key is not None:
+ if cookies is None:
+ cookies = {}
+ cookies["_U"] = api_key
+ self.cookies = cookies
+ self.proxy = proxy
+
+ @classmethod
+ async def create_async_generator(
+ cls,
+ model: str,
+ messages: Messages,
+ api_key: str = None,
+ cookies: Cookies = None,
+ proxy: str = None,
+ **kwargs
+ ) -> AsyncResult:
+ session = BingCreateImages(cookies, proxy, api_key)
+ yield await session.generate(messages[-1]["content"])
+
+ async def generate(self, prompt: str) -> ImageResponse:
+ """
+ Asynchronously creates a markdown formatted string with images based on the prompt.
+
+ Args:
+ prompt (str): Prompt to generate images.
+
+ Returns:
+ str: Markdown formatted string with images.
+ """
+ cookies = self.cookies or get_cookies(".bing.com", False)
+ if cookies is None or "_U" not in cookies:
+ raise MissingAuthError('Missing "_U" cookie')
+ async with create_session(cookies, self.proxy) as session:
+ images = await create_images(session, prompt)
+ return ImageResponse(images, prompt, {"preview": "{image}?w=200&h=200"} if len(images) > 1 else {}) \ No newline at end of file