2024-02-09 08:24:15 -05:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import os
|
2024-03-10 21:41:59 -04:00
|
|
|
from typing import Iterator, Union
|
2024-02-09 08:24:15 -05:00
|
|
|
|
|
|
|
from ..cookies import get_cookies
|
|
|
|
from ..image import ImageResponse
|
|
|
|
from ..errors import MissingRequirementsError, MissingAuthError
|
2024-04-10 20:40:30 -04:00
|
|
|
from ..typing import AsyncResult, Messages, Cookies
|
|
|
|
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
|
2024-02-12 05:41:27 -05:00
|
|
|
from .bing.create_images import create_images, create_session, get_cookies_from_browser
|
2024-02-09 08:24:15 -05:00
|
|
|
|
2024-04-10 20:40:30 -04:00
|
|
|
class BingCreateImages(AsyncGeneratorProvider, ProviderModelMixin):
|
2024-04-12 14:58:40 -04:00
|
|
|
label = "Microsoft Designer"
|
2024-04-10 20:40:30 -04:00
|
|
|
url = "https://www.bing.com/images/create"
|
|
|
|
working = True
|
2024-04-20 04:43:53 -04:00
|
|
|
needs_auth = True
|
2024-02-09 08:24:15 -05:00
|
|
|
|
2024-03-13 00:27:54 -04:00
|
|
|
def __init__(self, cookies: Cookies = None, proxy: str = None) -> None:
|
|
|
|
self.cookies: Cookies = cookies
|
|
|
|
self.proxy: str = proxy
|
2024-02-09 08:24:15 -05:00
|
|
|
|
2024-04-10 20:40:30 -04:00
|
|
|
@classmethod
|
|
|
|
async def create_async_generator(
|
|
|
|
cls,
|
|
|
|
model: str,
|
|
|
|
messages: Messages,
|
|
|
|
api_key: str = None,
|
|
|
|
cookies: Cookies = None,
|
|
|
|
proxy: str = None,
|
|
|
|
**kwargs
|
|
|
|
) -> AsyncResult:
|
|
|
|
if api_key is not None:
|
|
|
|
cookies = {"_U": api_key}
|
|
|
|
session = BingCreateImages(cookies, proxy)
|
|
|
|
yield await session.create_async(messages[-1]["content"])
|
|
|
|
|
2024-03-10 21:41:59 -04:00
|
|
|
def create(self, prompt: str) -> Iterator[Union[ImageResponse, str]]:
|
2024-02-09 08:24:15 -05:00
|
|
|
"""
|
|
|
|
Generator for creating imagecompletion based on a prompt.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
prompt (str): Prompt to generate images.
|
|
|
|
|
|
|
|
Yields:
|
|
|
|
Generator[str, None, None]: The final output as markdown formatted string with images.
|
|
|
|
"""
|
|
|
|
cookies = self.cookies or get_cookies(".bing.com", False)
|
|
|
|
if "_U" not in cookies:
|
|
|
|
login_url = os.environ.get("G4F_LOGIN_URL")
|
|
|
|
if login_url:
|
|
|
|
yield f"Please login: [Bing]({login_url})\n\n"
|
|
|
|
try:
|
|
|
|
self.cookies = get_cookies_from_browser(self.proxy)
|
|
|
|
except MissingRequirementsError as e:
|
|
|
|
raise MissingAuthError(f'Missing "_U" cookie. {e}')
|
|
|
|
yield asyncio.run(self.create_async(prompt))
|
|
|
|
|
|
|
|
async def create_async(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 "_U" not in cookies:
|
|
|
|
raise MissingAuthError('Missing "_U" cookie')
|
2024-02-09 12:11:35 -05:00
|
|
|
proxy = self.proxy or os.environ.get("G4F_PROXY")
|
2024-02-09 08:24:15 -05:00
|
|
|
async with create_session(cookies, proxy) as session:
|
2024-02-09 12:11:35 -05:00
|
|
|
images = await create_images(session, prompt, proxy)
|
2024-02-12 05:41:27 -05:00
|
|
|
return ImageResponse(images, prompt, {"preview": "{image}?w=200&h=200"} if len(images) > 1 else {})
|