Merge pull request #1498 from hlohaus/sort

Improve CreateImagesBing
This commit is contained in:
H Lohaus 2024-01-22 04:33:19 +01:00 committed by GitHub
commit efbcc04d8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 68 additions and 83 deletions

View File

@ -4,9 +4,9 @@ import json, uuid
from aiohttp import ClientSession from aiohttp import ClientSession
from ...typing import AsyncResult, Messages from ..typing import AsyncResult, Messages
from ..base_provider import AsyncGeneratorProvider from .base_provider import AsyncGeneratorProvider
from ..helper import format_prompt, get_cookies from .helper import format_prompt, get_cookies
map = { map = {
"openchat/openchat_3.5": "openchat/openchat-3.5-1210", "openchat/openchat_3.5": "openchat/openchat-3.5-1210",

View File

@ -11,11 +11,8 @@ from .selenium import *
from .Aura import Aura from .Aura import Aura
from .AiAsk import AiAsk from .AiAsk import AiAsk
from .Aichat import Aichat
from .AiChatOnline import AiChatOnline from .AiChatOnline import AiChatOnline
from .AItianhu import AItianhu from .AItianhu import AItianhu
from .AItianhuSpace import AItianhuSpace
from .Berlin import Berlin
from .Bing import Bing from .Bing import Bing
from .ChatAnywhere import ChatAnywhere from .ChatAnywhere import ChatAnywhere
from .ChatBase import ChatBase from .ChatBase import ChatBase
@ -43,20 +40,16 @@ from .GptGo import GptGo
from .GptGod import GptGod from .GptGod import GptGod
from .GptTalkRu import GptTalkRu from .GptTalkRu import GptTalkRu
from .Hashnode import Hashnode from .Hashnode import Hashnode
from .HuggingChat import HuggingChat
from .Koala import Koala from .Koala import Koala
from .Liaobots import Liaobots from .Liaobots import Liaobots
from .Llama2 import Llama2 from .Llama2 import Llama2
from .MyShell import MyShell
from .OnlineGpt import OnlineGpt from .OnlineGpt import OnlineGpt
from .Opchatgpts import Opchatgpts
from .PerplexityAi import PerplexityAi
from .Phind import Phind from .Phind import Phind
from .Pi import Pi from .Pi import Pi
from .TalkAi import TalkAi
from .Vercel import Vercel from .Vercel import Vercel
from .Ylokh import Ylokh from .Ylokh import Ylokh
from .You import You from .You import You
from .Yqcloud import Yqcloud
from .Bestim import Bestim from .Bestim import Bestim
import sys import sys

View File

@ -13,10 +13,10 @@ from urllib.parse import quote
from typing import Generator, List, Dict from typing import Generator, List, Dict
from ..create_images import CreateImagesProvider from ..create_images import CreateImagesProvider
from ..helper import get_cookies, get_event_loop from ..helper import get_cookies
from ...webdriver import WebDriver, get_driver_cookies, get_browser from ...webdriver import WebDriver, get_driver_cookies, get_browser
from ...base_provider import ProviderType from ...base_provider import ProviderType
from ...image import format_images_markdown from ...image import ImageResponse
BING_URL = "https://www.bing.com" BING_URL = "https://www.bing.com"
TIMEOUT_LOGIN = 1200 TIMEOUT_LOGIN = 1200
@ -161,23 +161,7 @@ def read_images(html_content: str) -> List[str]:
raise RuntimeError("No images found") raise RuntimeError("No images found")
return images return images
async def create_images_markdown(cookies: Dict[str, str], prompt: str, proxy: str = None) -> str: def get_cookies_from_browser(proxy: str = None) -> dict[str, str]:
"""
Creates markdown formatted string with images based on the prompt.
Args:
cookies (Dict[str, str]): Cookies to be used for the session.
prompt (str): Prompt to generate images.
proxy (str, optional): Proxy configuration.
Returns:
str: Markdown formatted string with images.
"""
async with create_session(cookies) as session:
images = await create_images(session, prompt, proxy)
return format_images_markdown(images, prompt)
def get_cookies_from_browser(proxy: str = None) -> Dict[str, str]:
""" """
Retrieves cookies from the browser using webdriver. Retrieves cookies from the browser using webdriver.
@ -185,7 +169,7 @@ def get_cookies_from_browser(proxy: str = None) -> Dict[str, str]:
proxy (str, optional): Proxy configuration. proxy (str, optional): Proxy configuration.
Returns: Returns:
Dict[str, str]: Retrieved cookies. dict[str, str]: Retrieved cookies.
""" """
with get_browser(proxy=proxy) as driver: with get_browser(proxy=proxy) as driver:
wait_for_login(driver) wait_for_login(driver)
@ -195,47 +179,46 @@ def get_cookies_from_browser(proxy: str = None) -> Dict[str, str]:
class CreateImagesBing: class CreateImagesBing:
"""A class for creating images using Bing.""" """A class for creating images using Bing."""
_cookies: Dict[str, str] = {} def __init__(self, cookies: dict[str, str] = {}, proxy: str = None) -> None:
self.cookies = cookies
self.proxy = proxy
@classmethod def create_completion(self, prompt: str) -> Generator[ImageResponse, None, None]:
def create_completion(cls, prompt: str, cookies: Dict[str, str] = None, proxy: str = None) -> Generator[str, None, None]:
""" """
Generator for creating imagecompletion based on a prompt. Generator for creating imagecompletion based on a prompt.
Args: Args:
prompt (str): Prompt to generate images. prompt (str): Prompt to generate images.
cookies (Dict[str, str], optional): Cookies for the session. If None, cookies are retrieved automatically.
proxy (str, optional): Proxy configuration.
Yields: Yields:
Generator[str, None, None]: The final output as markdown formatted string with images. Generator[str, None, None]: The final output as markdown formatted string with images.
""" """
loop = get_event_loop() cookies = self.cookies or get_cookies(".bing.com")
cookies = cookies or cls._cookies or get_cookies(".bing.com")
if "_U" not in cookies: if "_U" not in cookies:
login_url = os.environ.get("G4F_LOGIN_URL") login_url = os.environ.get("G4F_LOGIN_URL")
if login_url: if login_url:
yield f"Please login: [Bing]({login_url})\n\n" yield f"Please login: [Bing]({login_url})\n\n"
cls._cookies = cookies = get_cookies_from_browser(proxy) self.cookies = get_cookies_from_browser(self.proxy)
yield loop.run_until_complete(create_images_markdown(cookies, prompt, proxy)) yield asyncio.run(self.create_async(prompt))
@classmethod async def create_async(self, prompt: str) -> ImageResponse:
async def create_async(cls, prompt: str, cookies: Dict[str, str] = None, proxy: str = None) -> str:
""" """
Asynchronously creates a markdown formatted string with images based on the prompt. Asynchronously creates a markdown formatted string with images based on the prompt.
Args: Args:
prompt (str): Prompt to generate images. prompt (str): Prompt to generate images.
cookies (Dict[str, str], optional): Cookies for the session. If None, cookies are retrieved automatically.
proxy (str, optional): Proxy configuration.
Returns: Returns:
str: Markdown formatted string with images. str: Markdown formatted string with images.
""" """
cookies = cookies or cls._cookies or get_cookies(".bing.com") cookies = self.cookies or get_cookies(".bing.com")
if "_U" not in cookies: if "_U" not in cookies:
cls._cookies = cookies = get_cookies_from_browser(proxy) raise RuntimeError('"_U" cookie is missing')
return await create_images_markdown(cookies, prompt, proxy) async with create_session(cookies) as session:
images = await create_images(session, prompt, self.proxy)
return ImageResponse(images, prompt)
service = CreateImagesBing()
def patch_provider(provider: ProviderType) -> CreateImagesProvider: def patch_provider(provider: ProviderType) -> CreateImagesProvider:
""" """
@ -247,4 +230,8 @@ def patch_provider(provider: ProviderType) -> CreateImagesProvider:
Returns: Returns:
CreateImagesProvider: The patched provider with image creation capabilities. CreateImagesProvider: The patched provider with image creation capabilities.
""" """
return CreateImagesProvider(provider, CreateImagesBing.create_completion, CreateImagesBing.create_async) return CreateImagesProvider(
provider,
service.create_completion,
service.create_async
)

View File

@ -1,11 +1,9 @@
from __future__ import annotations from __future__ import annotations
from aiohttp import ClientSession from ...typing import Messages
from ..base_provider import AsyncProvider, format_prompt
from ..typing import Messages from ..helper import get_cookies
from .base_provider import AsyncProvider, format_prompt from ...requests import StreamSession
from .helper import get_cookies
from ..requests import StreamSession
class Aichat(AsyncProvider): class Aichat(AsyncProvider):
url = "https://chat-gpt.org/chat" url = "https://chat-gpt.org/chat"

View File

@ -5,9 +5,9 @@ import uuid
import json import json
from aiohttp import ClientSession from aiohttp import ClientSession
from ..typing import AsyncResult, Messages from ...typing import AsyncResult, Messages
from .base_provider import AsyncGeneratorProvider from ..base_provider import AsyncGeneratorProvider
from .helper import format_prompt from ..helper import format_prompt
class Berlin(AsyncGeneratorProvider): class Berlin(AsyncGeneratorProvider):

View File

@ -3,9 +3,9 @@ from __future__ import annotations
import random, string, json import random, string, json
from aiohttp import ClientSession from aiohttp import ClientSession
from ..typing import Messages, AsyncResult from ...typing import Messages, AsyncResult
from .base_provider import AsyncGeneratorProvider from ..base_provider import AsyncGeneratorProvider
from .helper import get_random_string from ..helper import get_random_string
class Opchatgpts(AsyncGeneratorProvider): class Opchatgpts(AsyncGeneratorProvider):
url = "https://opchatgpts.net" url = "https://opchatgpts.net"

View File

@ -1,10 +1,10 @@
from __future__ import annotations from __future__ import annotations
import random import random
from ..requests import StreamSession from ...requests import StreamSession
from ..typing import AsyncResult, Messages from ...typing import AsyncResult, Messages
from .base_provider import AsyncGeneratorProvider, format_prompt from ..base_provider import AsyncGeneratorProvider, format_prompt
class Yqcloud(AsyncGeneratorProvider): class Yqcloud(AsyncGeneratorProvider):

View File

@ -19,3 +19,7 @@ from .Aibn import Aibn
from .Ails import Ails from .Ails import Ails
from .ChatgptDuo import ChatgptDuo from .ChatgptDuo import ChatgptDuo
from .Cromicle import Cromicle from .Cromicle import Cromicle
from .Opchatgpts import Opchatgpts
from .Yqcloud import Yqcloud
from .Aichat import Aichat
from .Berlin import Berlin

View File

@ -2,7 +2,6 @@ from .Bard import Bard
from .Raycast import Raycast from .Raycast import Raycast
from .Theb import Theb from .Theb import Theb
from .ThebApi import ThebApi from .ThebApi import ThebApi
from .HuggingChat import HuggingChat
from .OpenaiChat import OpenaiChat from .OpenaiChat import OpenaiChat
from .OpenAssistant import OpenAssistant from .OpenAssistant import OpenAssistant
from .Poe import Poe from .Poe import Poe

View File

@ -3,11 +3,11 @@ from __future__ import annotations
import time import time
import random import random
from ..typing import CreateResult, Messages from ...typing import CreateResult, Messages
from .base_provider import AbstractProvider from ..base_provider import AbstractProvider
from .helper import format_prompt, get_random_string from ..helper import format_prompt, get_random_string
from ..webdriver import WebDriver, WebDriverSession from ...webdriver import WebDriver, WebDriverSession
from .. import debug from ... import debug
class AItianhuSpace(AbstractProvider): class AItianhuSpace(AbstractProvider):
url = "https://chat3.aiyunos.top/" url = "https://chat3.aiyunos.top/"

View File

@ -2,10 +2,10 @@ from __future__ import annotations
import time, json import time, json
from ..typing import CreateResult, Messages from ...typing import CreateResult, Messages
from .base_provider import AbstractProvider from ..base_provider import AbstractProvider
from .helper import format_prompt from ..helper import format_prompt
from ..webdriver import WebDriver, WebDriverSession, bypass_cloudflare from ...webdriver import WebDriver, WebDriverSession, bypass_cloudflare
class MyShell(AbstractProvider): class MyShell(AbstractProvider):
url = "https://app.myshell.ai/chat" url = "https://app.myshell.ai/chat"

View File

@ -6,10 +6,10 @@ from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.keys import Keys
from ..typing import CreateResult, Messages from ...typing import CreateResult, Messages
from .base_provider import AbstractProvider from ..base_provider import AbstractProvider
from .helper import format_prompt from ..helper import format_prompt
from ..webdriver import WebDriver, WebDriverSession from ...webdriver import WebDriver, WebDriverSession
class PerplexityAi(AbstractProvider): class PerplexityAi(AbstractProvider):
url = "https://www.perplexity.ai" url = "https://www.perplexity.ai"

View File

@ -2,9 +2,9 @@ from __future__ import annotations
import time, json, time import time, json, time
from ..typing import CreateResult, Messages from ...typing import CreateResult, Messages
from .base_provider import AbstractProvider from ..base_provider import AbstractProvider
from ..webdriver import WebDriver, WebDriverSession from ...webdriver import WebDriver, WebDriverSession
class TalkAi(AbstractProvider): class TalkAi(AbstractProvider):
url = "https://talkai.info" url = "https://talkai.info"

View File

@ -1 +1,5 @@
from .AItianhuSpace import AItianhuSpace
from .MyShell import MyShell
from .PerplexityAi import PerplexityAi
from .Phind import Phind from .Phind import Phind
from .TalkAi import TalkAi