From 38dbe4b8e5ca7f9bc0508e1ba1bf878fd6d8c19c Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Mon, 22 Jan 2024 03:38:11 +0100 Subject: [PATCH] Improve CreateImagesBing Sort providers by category --- g4f/Provider/{needs_auth => }/HuggingChat.py | 6 +- g4f/Provider/__init__.py | 9 +-- g4f/Provider/bing/create_images.py | 63 ++++++++------------ g4f/Provider/{ => deprecated}/Aichat.py | 10 ++-- g4f/Provider/{ => deprecated}/Berlin.py | 6 +- g4f/Provider/{ => deprecated}/Opchatgpts.py | 6 +- g4f/Provider/{ => deprecated}/Yqcloud.py | 6 +- g4f/Provider/deprecated/__init__.py | 6 +- g4f/Provider/needs_auth/__init__.py | 1 - g4f/Provider/{ => selenium}/AItianhuSpace.py | 10 ++-- g4f/Provider/{ => selenium}/MyShell.py | 8 +-- g4f/Provider/{ => selenium}/PerplexityAi.py | 8 +-- g4f/Provider/{ => selenium}/TalkAi.py | 6 +- g4f/Provider/selenium/__init__.py | 6 +- 14 files changed, 68 insertions(+), 83 deletions(-) rename g4f/Provider/{needs_auth => }/HuggingChat.py (94%) rename g4f/Provider/{ => deprecated}/Aichat.py (92%) rename g4f/Provider/{ => deprecated}/Berlin.py (95%) rename g4f/Provider/{ => deprecated}/Opchatgpts.py (92%) rename g4f/Provider/{ => deprecated}/Yqcloud.py (92%) rename g4f/Provider/{ => selenium}/AItianhuSpace.py (93%) rename g4f/Provider/{ => selenium}/MyShell.py (89%) rename g4f/Provider/{ => selenium}/PerplexityAi.py (94%) rename g4f/Provider/{ => selenium}/TalkAi.py (93%) diff --git a/g4f/Provider/needs_auth/HuggingChat.py b/g4f/Provider/HuggingChat.py similarity index 94% rename from g4f/Provider/needs_auth/HuggingChat.py rename to g4f/Provider/HuggingChat.py index e4fa237d..4a42b3c8 100644 --- a/g4f/Provider/needs_auth/HuggingChat.py +++ b/g4f/Provider/HuggingChat.py @@ -4,9 +4,9 @@ import json, uuid from aiohttp import ClientSession -from ...typing import AsyncResult, Messages -from ..base_provider import AsyncGeneratorProvider -from ..helper import format_prompt, get_cookies +from ..typing import AsyncResult, Messages +from .base_provider import AsyncGeneratorProvider +from .helper import format_prompt, get_cookies map = { "openchat/openchat_3.5": "openchat/openchat-3.5-1210", diff --git a/g4f/Provider/__init__.py b/g4f/Provider/__init__.py index 7a3dbb76..ee8d2c1b 100644 --- a/g4f/Provider/__init__.py +++ b/g4f/Provider/__init__.py @@ -11,11 +11,8 @@ from .selenium import * from .Aura import Aura from .AiAsk import AiAsk -from .Aichat import Aichat from .AiChatOnline import AiChatOnline from .AItianhu import AItianhu -from .AItianhuSpace import AItianhuSpace -from .Berlin import Berlin from .Bing import Bing from .ChatAnywhere import ChatAnywhere from .ChatBase import ChatBase @@ -43,20 +40,16 @@ from .GptGo import GptGo from .GptGod import GptGod from .GptTalkRu import GptTalkRu from .Hashnode import Hashnode +from .HuggingChat import HuggingChat from .Koala import Koala from .Liaobots import Liaobots from .Llama2 import Llama2 -from .MyShell import MyShell from .OnlineGpt import OnlineGpt -from .Opchatgpts import Opchatgpts -from .PerplexityAi import PerplexityAi from .Phind import Phind from .Pi import Pi -from .TalkAi import TalkAi from .Vercel import Vercel from .Ylokh import Ylokh from .You import You -from .Yqcloud import Yqcloud from .Bestim import Bestim import sys diff --git a/g4f/Provider/bing/create_images.py b/g4f/Provider/bing/create_images.py index 060cd184..288a3d90 100644 --- a/g4f/Provider/bing/create_images.py +++ b/g4f/Provider/bing/create_images.py @@ -13,10 +13,10 @@ from urllib.parse import quote from typing import Generator, List, Dict 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 ...base_provider import ProviderType -from ...image import format_images_markdown +from ...image import ImageResponse BING_URL = "https://www.bing.com" TIMEOUT_LOGIN = 1200 @@ -161,23 +161,7 @@ def read_images(html_content: str) -> List[str]: raise RuntimeError("No images found") return images -async def create_images_markdown(cookies: Dict[str, str], prompt: str, proxy: str = None) -> 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]: +def get_cookies_from_browser(proxy: str = None) -> dict[str, str]: """ 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. Returns: - Dict[str, str]: Retrieved cookies. + dict[str, str]: Retrieved cookies. """ with get_browser(proxy=proxy) as driver: wait_for_login(driver) @@ -194,48 +178,47 @@ def get_cookies_from_browser(proxy: str = None) -> Dict[str, str]: class CreateImagesBing: """A class for creating images using Bing.""" - - _cookies: Dict[str, str] = {} - @classmethod - def create_completion(cls, prompt: str, cookies: Dict[str, str] = None, proxy: str = None) -> Generator[str, None, None]: + 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]: """ Generator for creating imagecompletion based on a prompt. Args: 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: Generator[str, None, None]: The final output as markdown formatted string with images. """ - loop = get_event_loop() - cookies = cookies or cls._cookies or get_cookies(".bing.com") + cookies = self.cookies or get_cookies(".bing.com") 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" - cls._cookies = cookies = get_cookies_from_browser(proxy) - yield loop.run_until_complete(create_images_markdown(cookies, prompt, proxy)) + self.cookies = get_cookies_from_browser(self.proxy) + yield asyncio.run(self.create_async(prompt)) - @classmethod - async def create_async(cls, prompt: str, cookies: Dict[str, str] = None, proxy: str = None) -> str: + 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. - cookies (Dict[str, str], optional): Cookies for the session. If None, cookies are retrieved automatically. - proxy (str, optional): Proxy configuration. Returns: 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: - cls._cookies = cookies = get_cookies_from_browser(proxy) - return await create_images_markdown(cookies, prompt, proxy) + raise RuntimeError('"_U" cookie is missing') + 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: """ @@ -247,4 +230,8 @@ def patch_provider(provider: ProviderType) -> CreateImagesProvider: Returns: CreateImagesProvider: The patched provider with image creation capabilities. """ - return CreateImagesProvider(provider, CreateImagesBing.create_completion, CreateImagesBing.create_async) \ No newline at end of file + return CreateImagesProvider( + provider, + service.create_completion, + service.create_async + ) \ No newline at end of file diff --git a/g4f/Provider/Aichat.py b/g4f/Provider/deprecated/Aichat.py similarity index 92% rename from g4f/Provider/Aichat.py rename to g4f/Provider/deprecated/Aichat.py index 41ea9a96..68c9c546 100644 --- a/g4f/Provider/Aichat.py +++ b/g4f/Provider/deprecated/Aichat.py @@ -1,11 +1,9 @@ from __future__ import annotations -from aiohttp import ClientSession - -from ..typing import Messages -from .base_provider import AsyncProvider, format_prompt -from .helper import get_cookies -from ..requests import StreamSession +from ...typing import Messages +from ..base_provider import AsyncProvider, format_prompt +from ..helper import get_cookies +from ...requests import StreamSession class Aichat(AsyncProvider): url = "https://chat-gpt.org/chat" diff --git a/g4f/Provider/Berlin.py b/g4f/Provider/deprecated/Berlin.py similarity index 95% rename from g4f/Provider/Berlin.py rename to g4f/Provider/deprecated/Berlin.py index ac376fab..5e81705a 100644 --- a/g4f/Provider/Berlin.py +++ b/g4f/Provider/deprecated/Berlin.py @@ -5,9 +5,9 @@ import uuid import json from aiohttp import ClientSession -from ..typing import AsyncResult, Messages -from .base_provider import AsyncGeneratorProvider -from .helper import format_prompt +from ...typing import AsyncResult, Messages +from ..base_provider import AsyncGeneratorProvider +from ..helper import format_prompt class Berlin(AsyncGeneratorProvider): diff --git a/g4f/Provider/Opchatgpts.py b/g4f/Provider/deprecated/Opchatgpts.py similarity index 92% rename from g4f/Provider/Opchatgpts.py rename to g4f/Provider/deprecated/Opchatgpts.py index 8c2987fa..94b1d099 100644 --- a/g4f/Provider/Opchatgpts.py +++ b/g4f/Provider/deprecated/Opchatgpts.py @@ -3,9 +3,9 @@ from __future__ import annotations import random, string, json from aiohttp import ClientSession -from ..typing import Messages, AsyncResult -from .base_provider import AsyncGeneratorProvider -from .helper import get_random_string +from ...typing import Messages, AsyncResult +from ..base_provider import AsyncGeneratorProvider +from ..helper import get_random_string class Opchatgpts(AsyncGeneratorProvider): url = "https://opchatgpts.net" diff --git a/g4f/Provider/Yqcloud.py b/g4f/Provider/deprecated/Yqcloud.py similarity index 92% rename from g4f/Provider/Yqcloud.py rename to g4f/Provider/deprecated/Yqcloud.py index 2829c5bf..2ec6931a 100644 --- a/g4f/Provider/Yqcloud.py +++ b/g4f/Provider/deprecated/Yqcloud.py @@ -1,10 +1,10 @@ from __future__ import annotations import random -from ..requests import StreamSession +from ...requests import StreamSession -from ..typing import AsyncResult, Messages -from .base_provider import AsyncGeneratorProvider, format_prompt +from ...typing import AsyncResult, Messages +from ..base_provider import AsyncGeneratorProvider, format_prompt class Yqcloud(AsyncGeneratorProvider): diff --git a/g4f/Provider/deprecated/__init__.py b/g4f/Provider/deprecated/__init__.py index ca5ac83e..4d7eb5da 100644 --- a/g4f/Provider/deprecated/__init__.py +++ b/g4f/Provider/deprecated/__init__.py @@ -18,4 +18,8 @@ from .Acytoo import Acytoo from .Aibn import Aibn from .Ails import Ails from .ChatgptDuo import ChatgptDuo -from .Cromicle import Cromicle \ No newline at end of file +from .Cromicle import Cromicle +from .Opchatgpts import Opchatgpts +from .Yqcloud import Yqcloud +from .Aichat import Aichat +from .Berlin import Berlin \ No newline at end of file diff --git a/g4f/Provider/needs_auth/__init__.py b/g4f/Provider/needs_auth/__init__.py index b85cd36a..46e1f740 100644 --- a/g4f/Provider/needs_auth/__init__.py +++ b/g4f/Provider/needs_auth/__init__.py @@ -2,7 +2,6 @@ from .Bard import Bard from .Raycast import Raycast from .Theb import Theb from .ThebApi import ThebApi -from .HuggingChat import HuggingChat from .OpenaiChat import OpenaiChat from .OpenAssistant import OpenAssistant from .Poe import Poe \ No newline at end of file diff --git a/g4f/Provider/AItianhuSpace.py b/g4f/Provider/selenium/AItianhuSpace.py similarity index 93% rename from g4f/Provider/AItianhuSpace.py rename to g4f/Provider/selenium/AItianhuSpace.py index 11725db7..9878bd5a 100644 --- a/g4f/Provider/AItianhuSpace.py +++ b/g4f/Provider/selenium/AItianhuSpace.py @@ -3,11 +3,11 @@ from __future__ import annotations import time import random -from ..typing import CreateResult, Messages -from .base_provider import AbstractProvider -from .helper import format_prompt, get_random_string -from ..webdriver import WebDriver, WebDriverSession -from .. import debug +from ...typing import CreateResult, Messages +from ..base_provider import AbstractProvider +from ..helper import format_prompt, get_random_string +from ...webdriver import WebDriver, WebDriverSession +from ... import debug class AItianhuSpace(AbstractProvider): url = "https://chat3.aiyunos.top/" diff --git a/g4f/Provider/MyShell.py b/g4f/Provider/selenium/MyShell.py similarity index 89% rename from g4f/Provider/MyShell.py rename to g4f/Provider/selenium/MyShell.py index 145cc0bf..a3f246ff 100644 --- a/g4f/Provider/MyShell.py +++ b/g4f/Provider/selenium/MyShell.py @@ -2,10 +2,10 @@ from __future__ import annotations import time, json -from ..typing import CreateResult, Messages -from .base_provider import AbstractProvider -from .helper import format_prompt -from ..webdriver import WebDriver, WebDriverSession, bypass_cloudflare +from ...typing import CreateResult, Messages +from ..base_provider import AbstractProvider +from ..helper import format_prompt +from ...webdriver import WebDriver, WebDriverSession, bypass_cloudflare class MyShell(AbstractProvider): url = "https://app.myshell.ai/chat" diff --git a/g4f/Provider/PerplexityAi.py b/g4f/Provider/selenium/PerplexityAi.py similarity index 94% rename from g4f/Provider/PerplexityAi.py rename to g4f/Provider/selenium/PerplexityAi.py index 023968dc..4796f709 100644 --- a/g4f/Provider/PerplexityAi.py +++ b/g4f/Provider/selenium/PerplexityAi.py @@ -6,10 +6,10 @@ from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys -from ..typing import CreateResult, Messages -from .base_provider import AbstractProvider -from .helper import format_prompt -from ..webdriver import WebDriver, WebDriverSession +from ...typing import CreateResult, Messages +from ..base_provider import AbstractProvider +from ..helper import format_prompt +from ...webdriver import WebDriver, WebDriverSession class PerplexityAi(AbstractProvider): url = "https://www.perplexity.ai" diff --git a/g4f/Provider/TalkAi.py b/g4f/Provider/selenium/TalkAi.py similarity index 93% rename from g4f/Provider/TalkAi.py rename to g4f/Provider/selenium/TalkAi.py index d4efd269..5aeb48ac 100644 --- a/g4f/Provider/TalkAi.py +++ b/g4f/Provider/selenium/TalkAi.py @@ -2,9 +2,9 @@ from __future__ import annotations import time, json, time -from ..typing import CreateResult, Messages -from .base_provider import AbstractProvider -from ..webdriver import WebDriver, WebDriverSession +from ...typing import CreateResult, Messages +from ..base_provider import AbstractProvider +from ...webdriver import WebDriver, WebDriverSession class TalkAi(AbstractProvider): url = "https://talkai.info" diff --git a/g4f/Provider/selenium/__init__.py b/g4f/Provider/selenium/__init__.py index 80c48d14..a8c18a49 100644 --- a/g4f/Provider/selenium/__init__.py +++ b/g4f/Provider/selenium/__init__.py @@ -1 +1,5 @@ -from .Phind import Phind \ No newline at end of file +from .AItianhuSpace import AItianhuSpace +from .MyShell import MyShell +from .PerplexityAi import PerplexityAi +from .Phind import Phind +from .TalkAi import TalkAi \ No newline at end of file