mirror of https://github.com/xtekky/gpt4free.git
parent
04a5e33ebe
commit
38dbe4b8e5
|
@ -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",
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
return CreateImagesProvider(
|
||||
provider,
|
||||
service.create_completion,
|
||||
service.create_async
|
||||
)
|
|
@ -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"
|
|
@ -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):
|
|
@ -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"
|
|
@ -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):
|
|
@ -18,4 +18,8 @@ from .Acytoo import Acytoo
|
|||
from .Aibn import Aibn
|
||||
from .Ails import Ails
|
||||
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
|
|
@ -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
|
|
@ -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/"
|
|
@ -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"
|
|
@ -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"
|
|
@ -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"
|
|
@ -1 +1,5 @@
|
|||
from .Phind import Phind
|
||||
from .AItianhuSpace import AItianhuSpace
|
||||
from .MyShell import MyShell
|
||||
from .PerplexityAi import PerplexityAi
|
||||
from .Phind import Phind
|
||||
from .TalkAi import TalkAi
|
Loading…
Reference in New Issue