Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: let safetensors and MLX vision models read transparent images (#13939)
Nilay159084e289e1
diff --git a/studio/backend/core/inference/mcp_images.py b/studio/backend/core/inference/mcp_images.pyindex 97d4075915f..eb2bc067aee 100644--- a/studio/backend/core/inference/mcp_images.py+++ b/studio/backend/core/inference/mcp_images.py@@ -356,8 +356,8 @@ def flattened_rgb(image, background = None):"""from PIL import Image, ImageChops, ImageStat- # Match routes/inference.py's _image_bytes_to_png_b64: scale declared I;16- # values to 8-bit before RGB conversion clips them. I;16B/I;16L must pass+ # Scale declared I;16 values to 8-bit before RGB conversion clips them; plain+ # "I" and "F" declare no range, so they convert as-is. I;16B/I;16L must pass# through "I" because they reject point().if image.mode.startswith("I;16"):if image.mode != "I;16":diff --git a/studio/backend/routes/inference.py b/studio/backend/routes/inference.pyindex 1327837179a..617c5dfdb3c 100644--- a/studio/backend/routes/inference.py+++ b/studio/backend/routes/inference.py@@ -22135,7 +22135,8 @@ async def get_api_monitor_entry(def _decode_and_resize_image(backend, encoded: str):"""Off the event loop. The decode is forced so a truncated picture is a bad request here, and- all but RGB/RGBA converts because CMYK, PA, F and LAB decode and none can be saved back."""+ the result is flattened RGB: CMYK, PA, F and LAB decode and none can be saved back, and a plain+ convert would turn a transparent background black under dark ink."""from PIL import Imagefrom io import BytesIO@@ -22144,10 +22145,7 @@ def _decode_and_resize_image(backend, encoded: str):image.load()image = exif_upright(image)# After the resize: converting first resamples interpolated RGB, a different picture.- image = _scaled_from_16_bit(backend.resize_image(image))- if image.mode not in ("RGB", "RGBA"):- image = image.convert("RGB")- return image+ return _mcp_flattened_rgb(backend.resize_image(image))# Serving a whole conversation's images made the retained set grow with the request, where one@@ -42057,24 +42055,11 @@ def _select_anthropic_server_tools(return [tool for tool in available if tool["function"]["name"] in selected_names]-def _scaled_from_16_bit(image):- """8-bit values for a 16-bit source, which ``convert`` would otherwise read as 8-bit and clip- above 255, turning the picture nearly all white. The I;16 family only: plain "I" and "F"- declare no range, so a TIFF whose samples already sit in 0..255 would be scaled to black.- I;16B / I;16L reject point(), hence the normalisation to "I"."""- if not image.mode.startswith("I;16"):- return image- if image.mode != "I;16":- image = image.convert("I")- return image.point(lambda v: v * (1.0 / 257), mode = "L")--def _pil_to_png_b64(img) -> str:buf = io.BytesIO()# Composited, not merely converted: convert("RGB") keeps whatever colour sits# under the alpha, so a transparent attachment whose background was never painted- # reaches the worker as black -- and its dark text with it. The ordinary IPC path- # carries the PNG's alpha through, so only this serialisation flattened it.+ # reaches the worker as black -- and its dark text with it._mcp_flattened_rgb(img).save(buf, format = "PNG")return base64.b64encode(buf.getvalue()).decode("ascii")diff --git a/studio/backend/tests/test_openai_multi_image_route.py b/studio/backend/tests/test_openai_multi_image_route.pyindex 830300b8e40..e7c3d78e9ee 100644--- a/studio/backend/tests/test_openai_multi_image_route.py+++ b/studio/backend/tests/test_openai_multi_image_route.py@@ -127,16 +127,44 @@ def test_what_cannot_be_served_is_answered_as_a_bad_request(def test_an_image_reaches_the_backend_in_a_mode_it_can_be_handed_over_in(monkeypatch, mode, container):- """The first three reopen in a mode PNG cannot write and must convert; RGBA must not."""+ """All arrive as RGB; the first three reopen in a mode PNG cannot write."""part = _part(Image.new(mode, (16, 16)), container = container)delivered = _call(monkeypatch, [ChatMessage(role = "user", content = [part])]).calls[0]["image"]- assert delivered.mode == ("RGBA" if mode == "RGBA" else "RGB")+ assert delivered.mode == "RGB"# The boundary itself, not a claim about which modes it takes.delivered.save(io.BytesIO(), format = "PNG")+def _ink_on_nothing(mode):+ image = Image.new("RGBA", (200, 60), (0, 0, 0, 0))+ image.paste((0, 0, 0, 255), (20, 20, 180, 40))+ return image.convert(mode)++[email protected]("mode", ["RGBA", "LA", "P"])+def test_dark_ink_on_a_transparent_image_stays_visible_to_the_model(monkeypatch, mode):+ turn = [ChatMessage(role = "user", content = [_part(_ink_on_nothing(mode))])]++ delivered = _call(monkeypatch, turn).calls[0]["image"]++ seen = delivered.convert("RGB")+ assert seen.getpixel((5, 5)) == (255, 255, 255)+ assert seen.getpixel((100, 30)) == (0, 0, 0)+++def test_an_opaque_photo_reaches_the_model_unchanged(monkeypatch):+ source = Image.new("RGB", (32, 32), (30, 140, 200))+ source.paste((250, 240, 10), (0, 0, 16, 16))+ turn = [ChatMessage(role = "user", content = [_part(source)])]++ delivered = _call(monkeypatch, turn).calls[0]["image"]++ assert delivered.mode == "RGB"+ assert delivered.tobytes() == source.tobytes()++def test_each_image_is_decoded_once_for_a_request_that_rebuilds_the_conversation(monkeypatch):"""A client-tool request renders a rebuilt conversation, which used to decode the whole set asecond time -- twice the work and both full sets resident, on exactly the largest requests."""