Spaces:
Sleeping
Sleeping
Commit
·
ebc87be
1
Parent(s):
9ca4e02
should be faster
Browse files
server.py
CHANGED
|
@@ -41,6 +41,9 @@ class ChatCompletionRequest(BaseModel):
|
|
| 41 |
messages: List[Dict[str, Any]]
|
| 42 |
max_tokens: int = 8192
|
| 43 |
|
|
|
|
|
|
|
|
|
|
| 44 |
def _decode_base64_image(data_url: str) -> Image.Image:
|
| 45 |
try:
|
| 46 |
is_data_url = data_url.startswith("data:")
|
|
@@ -52,6 +55,21 @@ def _decode_base64_image(data_url: str) -> Image.Image:
|
|
| 52 |
logger.debug("Decoding image from raw base64 string; length=%d", len(b64data))
|
| 53 |
img_bytes = base64.b64decode(b64data)
|
| 54 |
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
try:
|
| 56 |
logger.debug("Decoded image: size=%sx%s mode=%s", img.width, img.height, img.mode)
|
| 57 |
except Exception:
|
|
|
|
| 41 |
messages: List[Dict[str, Any]]
|
| 42 |
max_tokens: int = 8192
|
| 43 |
|
| 44 |
+
MAX_IMAGE_WIDTH = 512
|
| 45 |
+
MAX_IMAGE_HEIGHT = 388
|
| 46 |
+
|
| 47 |
def _decode_base64_image(data_url: str) -> Image.Image:
|
| 48 |
try:
|
| 49 |
is_data_url = data_url.startswith("data:")
|
|
|
|
| 55 |
logger.debug("Decoding image from raw base64 string; length=%d", len(b64data))
|
| 56 |
img_bytes = base64.b64decode(b64data)
|
| 57 |
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 58 |
+
orig_w, orig_h = img.width, img.height
|
| 59 |
+
# Downscale if larger than bounds, preserving aspect ratio
|
| 60 |
+
if orig_w > MAX_IMAGE_WIDTH or orig_h > MAX_IMAGE_HEIGHT:
|
| 61 |
+
target = (MAX_IMAGE_WIDTH, MAX_IMAGE_HEIGHT)
|
| 62 |
+
img = img.copy()
|
| 63 |
+
img.thumbnail(target, Image.LANCZOS)
|
| 64 |
+
logger.debug(
|
| 65 |
+
"Resized image from %sx%s to %sx%s (bounds %sx%s)",
|
| 66 |
+
orig_w,
|
| 67 |
+
orig_h,
|
| 68 |
+
img.width,
|
| 69 |
+
img.height,
|
| 70 |
+
MAX_IMAGE_WIDTH,
|
| 71 |
+
MAX_IMAGE_HEIGHT,
|
| 72 |
+
)
|
| 73 |
try:
|
| 74 |
logger.debug("Decoded image: size=%sx%s mode=%s", img.width, img.height, img.mode)
|
| 75 |
except Exception:
|