2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Module to handle image uploading and processing for Bing AI integrations.
|
|
|
|
"""
|
|
|
|
from __future__ import annotations
|
2024-01-26 09:54:13 +03:00
|
|
|
|
2024-01-10 12:34:56 +03:00
|
|
|
import json
|
2024-01-13 16:01:28 +03:00
|
|
|
import math
|
2024-01-26 09:54:13 +03:00
|
|
|
from aiohttp import ClientSession, FormData
|
2024-01-14 09:45:41 +03:00
|
|
|
|
|
|
|
from ...typing import ImageType, Tuple
|
2024-01-26 09:54:13 +03:00
|
|
|
from ...image import to_image, process_image, to_base64_jpg, ImageRequest, Image
|
2024-03-12 20:45:22 +03:00
|
|
|
from ...requests import raise_for_status
|
2024-01-13 17:37:36 +03:00
|
|
|
|
2024-01-14 09:45:41 +03:00
|
|
|
IMAGE_CONFIG = {
|
2024-01-13 17:37:36 +03:00
|
|
|
"maxImagePixels": 360000,
|
2024-01-13 17:56:29 +03:00
|
|
|
"imageCompressionRate": 0.7,
|
2024-01-14 09:45:41 +03:00
|
|
|
"enableFaceBlurDebug": False,
|
2024-01-13 17:37:36 +03:00
|
|
|
}
|
2024-01-10 12:34:56 +03:00
|
|
|
|
|
|
|
async def upload_image(
|
2024-01-14 09:45:41 +03:00
|
|
|
session: ClientSession,
|
|
|
|
image_data: ImageType,
|
|
|
|
tone: str,
|
2024-03-12 20:45:22 +03:00
|
|
|
headers: dict
|
2024-01-26 09:54:13 +03:00
|
|
|
) -> ImageRequest:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Uploads an image to Bing's AI service and returns the image response.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session (ClientSession): The active session.
|
|
|
|
image_data (bytes): The image data to be uploaded.
|
|
|
|
tone (str): The tone of the conversation.
|
|
|
|
proxy (str, optional): Proxy if any. Defaults to None.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
RuntimeError: If the image upload fails.
|
|
|
|
|
|
|
|
Returns:
|
2024-01-26 09:54:13 +03:00
|
|
|
ImageRequest: The response from the image upload.
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
image = to_image(image_data)
|
|
|
|
new_width, new_height = calculate_new_dimensions(image)
|
2024-01-26 09:54:13 +03:00
|
|
|
image = process_image(image, new_width, new_height)
|
|
|
|
img_binary_data = to_base64_jpg(image, IMAGE_CONFIG['imageCompressionRate'])
|
2024-01-14 09:45:41 +03:00
|
|
|
|
2024-01-26 09:54:13 +03:00
|
|
|
data = build_image_upload_payload(img_binary_data, tone)
|
2024-01-14 09:45:41 +03:00
|
|
|
|
2024-03-12 20:45:22 +03:00
|
|
|
async with session.post("https://www.bing.com/images/kblob", data=data, headers=prepare_headers(headers)) as response:
|
|
|
|
await raise_for_status(response, "Failed to upload image")
|
2024-01-14 09:45:41 +03:00
|
|
|
return parse_image_response(await response.json())
|
|
|
|
|
2024-01-26 09:54:13 +03:00
|
|
|
def calculate_new_dimensions(image: Image) -> Tuple[int, int]:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Calculates the new dimensions for the image based on the maximum allowed pixels.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
image (Image): The PIL Image object.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Tuple[int, int]: The new width and height for the image.
|
|
|
|
"""
|
|
|
|
width, height = image.size
|
|
|
|
max_image_pixels = IMAGE_CONFIG['maxImagePixels']
|
|
|
|
if max_image_pixels / (width * height) < 1:
|
|
|
|
scale_factor = math.sqrt(max_image_pixels / (width * height))
|
|
|
|
return int(width * scale_factor), int(height * scale_factor)
|
|
|
|
return width, height
|
|
|
|
|
2024-01-26 09:54:13 +03:00
|
|
|
def build_image_upload_payload(image_bin: str, tone: str) -> FormData:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Builds the payload for image uploading.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
image_bin (str): Base64 encoded image binary data.
|
|
|
|
tone (str): The tone of the conversation.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Tuple[str, str]: The data and boundary for the payload.
|
|
|
|
"""
|
2024-01-26 09:54:13 +03:00
|
|
|
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
|
2024-01-14 09:45:41 +03:00
|
|
|
|
|
|
|
def build_knowledge_request(tone: str) -> dict:
|
|
|
|
"""
|
|
|
|
Builds the knowledge request payload.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
tone (str): The tone of the conversation.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict: The knowledge request payload.
|
|
|
|
"""
|
|
|
|
return {
|
2024-01-23 03:47:55 +03:00
|
|
|
"imageInfo": {},
|
|
|
|
"knowledgeRequest": {
|
|
|
|
'invokedSkills': ["ImageById"],
|
|
|
|
'subscriptionId': "Bing.Chat.Multimodal",
|
|
|
|
'invokedSkillsRequestData': {
|
|
|
|
'enableFaceBlur': True
|
|
|
|
},
|
|
|
|
'convoData': {
|
|
|
|
'convoid': "",
|
|
|
|
'convotone': tone
|
|
|
|
}
|
2024-01-10 12:34:56 +03:00
|
|
|
}
|
|
|
|
}
|
2024-01-14 09:45:41 +03:00
|
|
|
|
2024-03-12 20:45:22 +03:00
|
|
|
def prepare_headers(headers: dict) -> dict:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Prepares the headers for the image upload request.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
session (ClientSession): The active session.
|
|
|
|
boundary (str): The boundary string for the multipart/form-data.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict: The headers for the request.
|
|
|
|
"""
|
|
|
|
headers["Referer"] = 'https://www.bing.com/search?q=Bing+AI&showconv=1&FORM=hpcodx'
|
|
|
|
headers["Origin"] = 'https://www.bing.com'
|
|
|
|
return headers
|
|
|
|
|
2024-01-26 09:54:13 +03:00
|
|
|
def parse_image_response(response: dict) -> ImageRequest:
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
Parses the response from the image upload.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
response (dict): The response dictionary.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
RuntimeError: If parsing the image info fails.
|
|
|
|
|
|
|
|
Returns:
|
2024-01-26 09:54:13 +03:00
|
|
|
ImageRequest: The parsed image response.
|
2024-01-14 09:45:41 +03:00
|
|
|
"""
|
|
|
|
if not response.get('blobId'):
|
|
|
|
raise RuntimeError("Failed to parse image info.")
|
|
|
|
|
|
|
|
result = {'bcid': response.get('blobId', ""), 'blurredBcid': response.get('processedBlobId', "")}
|
|
|
|
result["imageUrl"] = f"https://www.bing.com/images/blob?bcid={result['blurredBcid'] or result['bcid']}"
|
|
|
|
|
|
|
|
result['originalImageUrl'] = (
|
|
|
|
f"https://www.bing.com/images/blob?bcid={result['blurredBcid']}"
|
|
|
|
if IMAGE_CONFIG["enableFaceBlurDebug"] else
|
|
|
|
f"https://www.bing.com/images/blob?bcid={result['bcid']}"
|
2024-01-10 12:34:56 +03:00
|
|
|
)
|
2024-01-29 20:14:46 +03:00
|
|
|
return ImageRequest(result)
|