2024-02-09 16:24:15 +03:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2024-11-06 19:39:14 +03:00
|
|
|
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
|
2024-02-09 16:24:15 +03:00
|
|
|
|
2024-04-11 03:40:30 +03:00
|
|
|
class BingCreateImages(AsyncGeneratorProvider, ProviderModelMixin):
|
2024-05-19 00:13:57 +03:00
|
|
|
label = "Microsoft Designer in Bing"
|
2024-04-22 02:27:48 +03:00
|
|
|
parent = "Bing"
|
2024-04-11 03:40:30 +03:00
|
|
|
url = "https://www.bing.com/images/create"
|
|
|
|
working = True
|
2024-04-20 11:43:53 +03:00
|
|
|
needs_auth = True
|
2024-04-21 16:15:55 +03:00
|
|
|
image_models = ["dall-e"]
|
2024-02-09 16:24:15 +03:00
|
|
|
|
2024-04-23 18:34:42 +03:00
|
|
|
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
|
2024-02-09 16:24:15 +03:00
|
|
|
|
2024-04-11 03:40:30 +03:00
|
|
|
@classmethod
|
|
|
|
async def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
api_key: str = None,
|
|
|
|
cookies: Cookies = None,
|
|
|
|
proxy: str = None,
|
|
|
|
**kwargs
|
|
|
|
) -> AsyncResult:
|
2024-04-23 18:34:42 +03:00
|
|
|
session = BingCreateImages(cookies, proxy, api_key)
|
2024-05-19 00:13:57 +03:00
|
|
|
yield await session.generate(messages[-1]["content"])
|
2024-02-09 16:24:15 +03:00
|
|
|
|
2024-05-19 00:13:57 +03:00
|
|
|
async def generate(self, prompt: str) -> ImageResponse:
|
2024-02-09 16:24:15 +03:00
|
|
|
"""
|
|
|
|
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)
|
2024-05-07 08:37:23 +03:00
|
|
|
if cookies is None or "_U" not in cookies:
|
2024-02-09 16:24:15 +03:00
|
|
|
raise MissingAuthError('Missing "_U" cookie')
|
2024-05-19 00:13:57 +03:00
|
|
|
async with create_session(cookies, self.proxy) as session:
|
|
|
|
images = await create_images(session, prompt)
|
2024-02-12 13:41:27 +03:00
|
|
|
return ImageResponse(images, prompt, {"preview": "{image}?w=200&h=200"} if len(images) > 1 else {})
|