From d0fcdfbb9c5da67c3259a62525aa10b7c9d08753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?AI=20=EA=B8=B0=EB=B0=98=20=ED=81=B4=EB=9D=BC=EC=9A=B0?= =?UTF-8?q?=EB=93=9C=20MLOps=2C=20TA=2C=20SA=2C=20AA=2C=20=ED=85=8C?= =?UTF-8?q?=ED=81=AC=EB=A6=AC=EB=93=9C?= <41275565+edumgt@users.noreply.github.com> Date: Thu, 12 Feb 2026 15:42:42 +0900 Subject: [PATCH] Fix generated image preview rendering fallback --- backend/app/generator.py | 10 +++++++--- frontend/app.js | 3 ++- tests/test_api.py | 1 + 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/backend/app/generator.py b/backend/app/generator.py index 7641613..a189f95 100644 --- a/backend/app/generator.py +++ b/backend/app/generator.py @@ -1,6 +1,7 @@ from __future__ import annotations import html +import base64 import textwrap import uuid from pathlib import Path @@ -34,6 +35,7 @@ class GenerationResult(BaseModel): height: int model_id: str prompt: str + data_url: str | None = None class GeneratorService: @@ -63,13 +65,14 @@ def generate(self, request: GenerateRequest) -> GenerationResult: width, height = self.VIDEO_SIZES[request.video_size] label = f"VIDEO PREVIEW ({request.video_size})" - output_path = self._create_preview_svg( + output_path, svg_content = self._create_preview_svg( model_id=request.model_id, prompt=request.prompt, width=width, height=height, label=label, ) + encoded_svg = base64.b64encode(svg_content.encode("utf-8")).decode("ascii") return GenerationResult( file_url=f"/outputs/{output_path.name}", media_type="image/svg+xml", @@ -77,9 +80,10 @@ def generate(self, request: GenerateRequest) -> GenerationResult: height=height, model_id=request.model_id, prompt=request.prompt, + data_url=f"data:image/svg+xml;base64,{encoded_svg}", ) - def _create_preview_svg(self, model_id: str, prompt: str, width: int, height: int, label: str) -> Path: + def _create_preview_svg(self, model_id: str, prompt: str, width: int, height: int, label: str) -> tuple[Path, str]: escaped_lines = textwrap.wrap(f"Prompt: {prompt}", width=60) text_lines = [ label, @@ -105,4 +109,4 @@ def _create_preview_svg(self, model_id: str, prompt: str, width: int, height: in file_name = f"asset_{uuid.uuid4().hex[:8]}.svg" path = self.output_dir / file_name path.write_text(svg, encoding="utf-8") - return path + return path, svg diff --git a/frontend/app.js b/frontend/app.js index e698fd5..99f827a 100644 --- a/frontend/app.js +++ b/frontend/app.js @@ -65,7 +65,8 @@ async function generate(event) { body: JSON.stringify(payload), }); - resultImage.src = `${data.file_url}?t=${Date.now()}`; + const cacheBustedFileUrl = `${data.file_url}?t=${Date.now()}`; + resultImage.src = data.data_url || cacheBustedFileUrl; resultImage.classList.remove('hidden'); statusEl.textContent = `완료: ${data.model_id} / ${data.width}x${data.height}`; } catch (error) { diff --git a/tests/test_api.py b/tests/test_api.py index c80e5a0..12419a5 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -44,3 +44,4 @@ def test_generate_image() -> None: assert payload["width"] == 512 assert payload["height"] == 512 assert payload["file_url"].startswith("/outputs/asset_") + assert payload["data_url"].startswith("data:image/svg+xml;base64,")