summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/bing/upload_image.py
blob: 2788edfd0f46e8f74e308c666dd54f8100747319 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from __future__ import annotations

import string
import random
import json
import re
import io
import base64
import numpy as np
from PIL import Image
from aiohttp import ClientSession

async def upload_image(
    session: ClientSession,
    image: str,
    tone: str,
    proxy: str = None
):
    try:
        image_config = {
            "maxImagePixels": 360000,
            "imageCompressionRate": 0.7,
            "enableFaceBlurDebug": 0,
        }
        is_data_uri_an_image(image)
        img_binary_data = extract_data_uri(image)
        is_accepted_format(img_binary_data)
        img = Image.open(io.BytesIO(img_binary_data))
        width, height = img.size
        max_image_pixels = image_config['maxImagePixels']
        if max_image_pixels / (width * height) < 1:
            new_width = int(width * np.sqrt(max_image_pixels / (width * height)))
            new_height = int(height * np.sqrt(max_image_pixels / (width * height)))
        else:
            new_width = width
            new_height = height
        try:
            orientation = get_orientation(img)
        except Exception:
            orientation = None
        new_img = process_image(orientation, img, new_width, new_height)
        new_img_binary_data = compress_image_to_base64(new_img, image_config['imageCompressionRate'])
        data, boundary = build_image_upload_api_payload(new_img_binary_data, tone)
        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'
        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.")
            image_info = await response.json()
            if not image_info.get('blobId'):
                raise RuntimeError("Failed to parse image info.")
            result = {'bcid': image_info.get('blobId', "")}
            result['blurredBcid'] = image_info.get('processedBlobId', "")
            if result['blurredBcid'] != "":
                result["imageUrl"] = "https://www.bing.com/images/blob?bcid=" + result['blurredBcid']
            elif result['bcid'] != "":
                result["imageUrl"] = "https://www.bing.com/images/blob?bcid=" + result['bcid']
            result['originalImageUrl'] = (
                "https://www.bing.com/images/blob?bcid="
                + result['blurredBcid']
                if image_config["enableFaceBlurDebug"]
                else "https://www.bing.com/images/blob?bcid="
                + result['bcid']
            )
            return result
    except Exception as e:
        raise RuntimeError(f"Add image failed: {e}")
    

def build_image_upload_api_payload(image_bin: str, tone: str):
    payload = {
        'invokedSkills': ["ImageById"],
        'subscriptionId': "Bing.Chat.Multimodal",
        'invokedSkillsRequestData': {
            'enableFaceBlur': True
        },
        'convoData': {
            'convoid': "",
            'convotone': tone
        }
    }
    knowledge_request = {
        'imageInfo': {},
        'knowledgeRequest': payload
    }
    boundary="----WebKitFormBoundary" + ''.join(random.choices(string.ascii_letters + string.digits, k=16))
    data = (
        f'--{boundary}'
        + '\r\nContent-Disposition: form-data; name="knowledgeRequest"\r\n\r\n'
        + json.dumps(knowledge_request, ensure_ascii=False)
        + "\r\n--"
        + boundary
        + '\r\nContent-Disposition: form-data; name="imageBase64"\r\n\r\n'
        + image_bin
        + "\r\n--"
        + boundary
        + "--\r\n"
    )
    return data, boundary

def is_data_uri_an_image(data_uri: str):
    try:
        # Check if the data URI starts with 'data:image' and contains an image format (e.g., jpeg, png, gif)
        if not re.match(r'data:image/(\w+);base64,', data_uri):
            raise ValueError("Invalid data URI image.")
            # Extract the image format from the data URI
        image_format = re.match(r'data:image/(\w+);base64,', data_uri).group(1)
        # Check if the image format is one of the allowed formats (jpg, jpeg, png, gif)
        if image_format.lower() not in ['jpeg', 'jpg', 'png', 'gif']:
            raise ValueError("Invalid image format (from mime file type).")
    except Exception as e:
        raise e

def is_accepted_format(binary_data: bytes) -> bool:
        try:
            check = False
            if binary_data.startswith(b'\xFF\xD8\xFF'):
                check = True  # It's a JPEG image
            elif binary_data.startswith(b'\x89PNG\r\n\x1a\n'):
                check = True  # It's a PNG image
            elif binary_data.startswith(b'GIF87a') or binary_data.startswith(b'GIF89a'):
                check = True  # It's a GIF image
            elif binary_data.startswith(b'\x89JFIF') or binary_data.startswith(b'JFIF\x00'):
                check = True  # It's a JPEG image
            elif binary_data.startswith(b'\xFF\xD8'):
                check = True  # It's a JPEG image
            elif binary_data.startswith(b'RIFF') and binary_data[8:12] == b'WEBP':
                check = True  # It's a WebP image
            # else we raise ValueError
            if not check:
                raise ValueError("Invalid image format (from magic code).")
        except Exception as e:
            raise e
    
def extract_data_uri(data_uri: str) -> bytes:
    try:
        data = data_uri.split(",")[1]
        data = base64.b64decode(data)
        return data
    except Exception as e:
        raise e

def get_orientation(data: bytes) -> int:
    try:
        if data[:2] != b'\xFF\xD8':
            raise Exception('NotJpeg')
        with Image.open(data) as img:
            exif_data = img._getexif()
            if exif_data is not None:
                orientation = exif_data.get(274)  # 274 corresponds to the orientation tag in EXIF
                if orientation is not None:
                    return orientation
    except Exception:
        pass

def process_image(orientation: int, img: Image.Image, new_width: int, new_height: int) -> Image.Image:
    try:
        # Initialize the canvas
        new_img = Image.new("RGB", (new_width, new_height), color="#FFFFFF")
        if orientation:
            if orientation > 4:
                img = img.transpose(Image.FLIP_LEFT_RIGHT)
            if orientation in [3, 4]:
                img = img.transpose(Image.ROTATE_180)
            if orientation in [5, 6]:
                img = img.transpose(Image.ROTATE_270)
            if orientation in [7, 8]:
                img = img.transpose(Image.ROTATE_90)
        new_img.paste(img, (0, 0))
        return new_img
    except Exception as e:
        raise e
    
def compress_image_to_base64(img, compression_rate) -> str:
    try:
        output_buffer = io.BytesIO()
        img.save(output_buffer, format="JPEG", quality=int(compression_rate * 100))
        return base64.b64encode(output_buffer.getvalue()).decode('utf-8')
    except Exception as e:
        raise e