diff options
author | H Lohaus <hlohaus@users.noreply.github.com> | 2024-01-26 07:54:13 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-26 07:54:13 +0100 |
commit | feb83c168b0a57ecd8c511aa654209c5f40da30e (patch) | |
tree | 84ef9a576064b7480e339426d9966b17a3102cad /g4f/Provider/bing/upload_image.py | |
parent | Merge pull request #1510 from hlohaus/sort (diff) | |
download | gpt4free-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/upload_image.py')
-rw-r--r-- | g4f/Provider/bing/upload_image.py | 51 |
1 files changed, 20 insertions, 31 deletions
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 |