From feb83c168b0a57ecd8c511aa654209c5f40da30e Mon Sep 17 00:00:00 2001 From: H Lohaus Date: Fri, 26 Jan 2024 07:54:13 +0100 Subject: New minimum requirements (#1515) * New minimum requirements * Add ConversationStyleOptionSets to Bing * Add image.ImageRequest * Improve python version support * Improve unittests --- g4f/Provider/bing/conversation.py | 2 ++ g4f/Provider/bing/create_images.py | 24 +++++++++++++++--- g4f/Provider/bing/upload_image.py | 51 +++++++++++++++----------------------- 3 files changed, 42 insertions(+), 35 deletions(-) (limited to 'g4f/Provider/bing') diff --git a/g4f/Provider/bing/conversation.py b/g4f/Provider/bing/conversation.py index 36ada3b0..388bdd6b 100644 --- a/g4f/Provider/bing/conversation.py +++ b/g4f/Provider/bing/conversation.py @@ -1,3 +1,5 @@ +from __future__ import annotations + from aiohttp import ClientSession class Conversation: 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) diff --git a/g4f/Provider/bing/upload_image.py b/g4f/Provider/bing/upload_image.py index bb5687a8..f9e11561 100644 --- a/g4f/Provider/bing/upload_image.py +++ b/g4f/Provider/bing/upload_image.py @@ -1,17 +1,14 @@ """ Module to handle image uploading and processing for Bing AI integrations. """ - from __future__ import annotations -import string -import random + import json import math -from aiohttp import ClientSession -from PIL import Image +from aiohttp import ClientSession, FormData from ...typing import ImageType, Tuple -from ...image import to_image, process_image, to_base64, ImageResponse +from ...image import to_image, process_image, to_base64_jpg, ImageRequest, Image IMAGE_CONFIG = { "maxImagePixels": 360000, @@ -24,7 +21,7 @@ async def upload_image( image_data: ImageType, tone: str, proxy: str = None -) -> ImageResponse: +) -> ImageRequest: """ Uploads an image to Bing's AI service and returns the image response. @@ -38,22 +35,22 @@ async def upload_image( RuntimeError: If the image upload fails. Returns: - ImageResponse: The response from the image upload. + ImageRequest: The response from the image upload. """ image = to_image(image_data) new_width, new_height = calculate_new_dimensions(image) - processed_img = process_image(image, new_width, new_height) - img_binary_data = to_base64(processed_img, IMAGE_CONFIG['imageCompressionRate']) + image = process_image(image, new_width, new_height) + img_binary_data = to_base64_jpg(image, IMAGE_CONFIG['imageCompressionRate']) - data, boundary = build_image_upload_payload(img_binary_data, tone) - headers = prepare_headers(session, boundary) + data = build_image_upload_payload(img_binary_data, tone) + headers = prepare_headers(session) async with session.post("https://www.bing.com/images/kblob", data=data, headers=headers, proxy=proxy) as response: if response.status != 200: raise RuntimeError("Failed to upload image.") return parse_image_response(await response.json()) -def calculate_new_dimensions(image: Image.Image) -> Tuple[int, int]: +def calculate_new_dimensions(image: Image) -> Tuple[int, int]: """ Calculates the new dimensions for the image based on the maximum allowed pixels. @@ -70,7 +67,7 @@ def calculate_new_dimensions(image: Image.Image) -> Tuple[int, int]: return int(width * scale_factor), int(height * scale_factor) return width, height -def build_image_upload_payload(image_bin: str, tone: str) -> Tuple[str, str]: +def build_image_upload_payload(image_bin: str, tone: str) -> FormData: """ Builds the payload for image uploading. @@ -81,18 +78,11 @@ def build_image_upload_payload(image_bin: str, tone: str) -> Tuple[str, str]: Returns: Tuple[str, str]: The data and boundary for the payload. """ - boundary = "----WebKitFormBoundary" + ''.join(random.choices(string.ascii_letters + string.digits, k=16)) - data = f"""--{boundary} -Content-Disposition: form-data; name="knowledgeRequest" - -{json.dumps(build_knowledge_request(tone), ensure_ascii=False)} ---{boundary} -Content-Disposition: form-data; name="imageBase64" - -{image_bin} ---{boundary}-- -""" - return data, boundary + data = FormData() + knowledge_request = json.dumps(build_knowledge_request(tone), ensure_ascii=False) + data.add_field('knowledgeRequest', knowledge_request, content_type="application/json") + data.add_field('imageBase64', image_bin) + return data def build_knowledge_request(tone: str) -> dict: """ @@ -119,7 +109,7 @@ def build_knowledge_request(tone: str) -> dict: } } -def prepare_headers(session: ClientSession, boundary: str) -> dict: +def prepare_headers(session: ClientSession) -> dict: """ Prepares the headers for the image upload request. @@ -131,12 +121,11 @@ def prepare_headers(session: ClientSession, boundary: str) -> dict: dict: The headers for the request. """ headers = session.headers.copy() - headers["Content-Type"] = f'multipart/form-data; boundary={boundary}' headers["Referer"] = 'https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx' headers["Origin"] = 'https://www.bing.com' return headers -def parse_image_response(response: dict) -> ImageResponse: +def parse_image_response(response: dict) -> ImageRequest: """ Parses the response from the image upload. @@ -147,7 +136,7 @@ def parse_image_response(response: dict) -> ImageResponse: RuntimeError: If parsing the image info fails. Returns: - ImageResponse: The parsed image response. + ImageRequest: The parsed image response. """ if not response.get('blobId'): raise RuntimeError("Failed to parse image info.") @@ -160,4 +149,4 @@ def parse_image_response(response: dict) -> ImageResponse: if IMAGE_CONFIG["enableFaceBlurDebug"] else f"https://www.bing.com/images/blob?bcid={result['bcid']}" ) - return ImageResponse(result["imageUrl"], "", result) \ No newline at end of file + return ImageRequest(result["imageUrl"], "", result) \ No newline at end of file -- cgit v1.2.3