summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/bing/create_images.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-01-26 07:54:13 +0100
committerGitHub <noreply@github.com>2024-01-26 07:54:13 +0100
commitfeb83c168b0a57ecd8c511aa654209c5f40da30e (patch)
tree84ef9a576064b7480e339426d9966b17a3102cad /g4f/Provider/bing/create_images.py
parentMerge pull request #1510 from hlohaus/sort (diff)
downloadgpt4free-feb83c168b0a57ecd8c511aa654209c5f40da30e.tar
gpt4free-feb83c168b0a57ecd8c511aa654209c5f40da30e.tar.gz
gpt4free-feb83c168b0a57ecd8c511aa654209c5f40da30e.tar.bz2
gpt4free-feb83c168b0a57ecd8c511aa654209c5f40da30e.tar.lz
gpt4free-feb83c168b0a57ecd8c511aa654209c5f40da30e.tar.xz
gpt4free-feb83c168b0a57ecd8c511aa654209c5f40da30e.tar.zst
gpt4free-feb83c168b0a57ecd8c511aa654209c5f40da30e.zip
Diffstat (limited to 'g4f/Provider/bing/create_images.py')
-rw-r--r--g4f/Provider/bing/create_images.py24
1 files changed, 20 insertions, 4 deletions
diff --git a/g4f/Provider/bing/create_images.py b/g4f/Provider/bing/create_images.py
index af39ef1e..4fa85929 100644
--- a/g4f/Provider/bing/create_images.py
+++ b/g4f/Provider/bing/create_images.py
@@ -2,21 +2,28 @@
This module provides functionalities for creating and managing images using Bing's service.
It includes functions for user login, session creation, image creation, and processing.
"""
+from __future__ import annotations
import asyncio
import time
import json
import os
from aiohttp import ClientSession, BaseConnector
-from bs4 import BeautifulSoup
from urllib.parse import quote
from typing import Generator, List, Dict
+try:
+ from bs4 import BeautifulSoup
+ has_requirements = True
+except ImportError:
+ has_requirements = False
+
from ..create_images import CreateImagesProvider
from ..helper import get_cookies, get_connector
from ...webdriver import WebDriver, get_driver_cookies, get_browser
from ...base_provider import ProviderType
from ...image import ImageResponse
+from ...errors import MissingRequirementsError, MissingAccessToken
BING_URL = "https://www.bing.com"
TIMEOUT_LOGIN = 1200
@@ -97,6 +104,8 @@ async def create_images(session: ClientSession, prompt: str, proxy: str = None,
Raises:
RuntimeError: If image creation fails or times out.
"""
+ if not has_requirements:
+ raise MissingRequirementsError('Install "beautifulsoup4" package')
url_encoded_prompt = quote(prompt)
payload = f"q={url_encoded_prompt}&rt=4&FORM=GENCRE"
url = f"{BING_URL}/images/create?q={url_encoded_prompt}&rt=4&FORM=GENCRE"
@@ -193,7 +202,11 @@ class CreateImagesBing:
Yields:
Generator[str, None, None]: The final output as markdown formatted string with images.
"""
- cookies = self.cookies or get_cookies(".bing.com")
+ try:
+ cookies = self.cookies or get_cookies(".bing.com")
+ except MissingRequirementsError as e:
+ raise MissingAccessToken(f'Missing "_U" cookie. {e}')
+
if "_U" not in cookies:
login_url = os.environ.get("G4F_LOGIN_URL")
if login_url:
@@ -211,9 +224,12 @@ class CreateImagesBing:
Returns:
str: Markdown formatted string with images.
"""
- cookies = self.cookies or get_cookies(".bing.com")
+ try:
+ cookies = self.cookies or get_cookies(".bing.com")
+ except MissingRequirementsError as e:
+ raise MissingAccessToken(f'Missing "_U" cookie. {e}')
if "_U" not in cookies:
- raise RuntimeError('"_U" cookie is missing')
+ raise MissingAccessToken('Missing "_U" cookie')
proxy = os.environ.get("G4F_PROXY")
async with create_session(cookies, proxy) as session:
images = await create_images(session, prompt, self.proxy)