Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions backend/app/generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import html
import base64
import textwrap
import uuid
from pathlib import Path
Expand Down Expand Up @@ -34,6 +35,7 @@ class GenerationResult(BaseModel):
height: int
model_id: str
prompt: str
data_url: str | None = None


class GeneratorService:
Expand Down Expand Up @@ -63,23 +65,25 @@ 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",
width=width,
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,
Expand All @@ -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
3 changes: 2 additions & 1 deletion frontend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,")
Loading