From 530b48e2dce027dab3eca659e08f1736708a7236 Mon Sep 17 00:00:00 2001
From: hit-cxf <2024111082@stu.hit.edu.cn>
Date: Wed, 13 May 2026 21:36:47 +0800
Subject: [PATCH] 接入Pixelle的html模版
---
aigc-director/aigc-claw/backend/api/routers/pipelines.py | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 110 insertions(+), 1 deletions(-)
diff --git a/aigc-director/aigc-claw/backend/api/routers/pipelines.py b/aigc-director/aigc-claw/backend/api/routers/pipelines.py
index 38e7efd..1774e50 100644
--- a/aigc-director/aigc-claw/backend/api/routers/pipelines.py
+++ b/aigc-director/aigc-claw/backend/api/routers/pipelines.py
@@ -1,7 +1,12 @@
+import base64
+import os
+import re
+from html import escape
+from urllib.parse import quote
from typing import Optional
from fastapi import APIRouter, BackgroundTasks, HTTPException, Query
-from fastapi.responses import StreamingResponse
+from fastapi.responses import HTMLResponse, StreamingResponse
from api.schemas.pipelines import (
ActionTransferPipelineRequest,
@@ -9,12 +14,78 @@
GenericPipelineRequest,
StandardPipelineRequest,
)
+from config import BASE_DIR
from pipelines.api_media import list_api_workflows
from pipelines.events import task_event_stream
from pipelines.runner import PIPELINE_REGISTRY, run_pipeline_task
from pipelines.storage import create_task, delete_task, list_tasks, load_task
+from pipelines.utils import TEMPLATE_FIELD_DEFAULTS, template_custom_fields, template_media_spec
router = APIRouter(tags=["Pipelines"])
+
+TEMPLATE_DIR = os.path.join(str(BASE_DIR), "templates")
+DEMO_IMAGE_PATH = os.path.join(TEMPLATE_DIR, "demo", "default_image.png")
+TEMPLATE_SIZES = {
+ "1080x1920": {"ratio": "9:16", "width": 1080, "height": 1920},
+ "1080x1080": {"ratio": "1:1", "width": 1080, "height": 1080},
+ "1920x1080": {"ratio": "16:9", "width": 1920, "height": 1080},
+}
+PLACEHOLDER_IMAGE_FALLBACK = (
+ "data:image/svg+xml;utf8,"
+ "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1024 1024'>"
+ "<defs><linearGradient id='g' x1='0' y1='0' x2='1' y2='1'>"
+ "<stop stop-color='%2388b7ff'/><stop offset='1' stop-color='%23f8d58a'/>"
+ "</linearGradient></defs>"
+ "<rect width='1024' height='1024' fill='url(%23g)'/>"
+ "<circle cx='760' cy='220' r='110' fill='%23ffffff' fill-opacity='.45'/>"
+ "<path d='M90 820 360 520l170 170 130-150 270 280Z' fill='%23ffffff' fill-opacity='.55'/>"
+ "</svg>"
+)
+
+
+def _demo_image_data_uri() -> str:
+ if os.path.exists(DEMO_IMAGE_PATH):
+ with open(DEMO_IMAGE_PATH, "rb") as f:
+ encoded = base64.b64encode(f.read()).decode("ascii")
+ return f"data:image/png;base64,{encoded}"
+ return PLACEHOLDER_IMAGE_FALLBACK
+
+
+def _template_label(filename: str) -> str:
+ name = os.path.splitext(filename)[0]
+ for prefix in ("image_", "static_", "video_", "asset_"):
+ if name.startswith(prefix):
+ name = name[len(prefix):]
+ break
+ return name.replace("_", " ").title()
+
+
+def _template_path(size: str, filename: str) -> str:
+ if size not in TEMPLATE_SIZES or "/" in filename or "\\" in filename or not filename.endswith(".html"):
+ raise HTTPException(404, "Template not found")
+ path = os.path.abspath(os.path.join(TEMPLATE_DIR, size, filename))
+ root = os.path.abspath(os.path.join(TEMPLATE_DIR, size))
+ if not path.startswith(root + os.sep) or not os.path.exists(path):
+ raise HTTPException(404, "Template not found")
+ return path
+
+
+def _render_preview_html(raw: str) -> str:
+ replacements = {
+ **TEMPLATE_FIELD_DEFAULTS,
+ "image": _demo_image_data_uri(),
+ }
+
+ def repl(match: re.Match) -> str:
+ token = match.group(1).strip()
+ key = token.split(":", 1)[0].split("=", 1)[0].strip()
+ if key in replacements:
+ return replacements[key]
+ if "=" in token:
+ return escape(token.split("=", 1)[1].strip())
+ return ""
+
+ return re.sub(r"\{\{\s*([^{}]+?)\s*\}\}", repl, raw)
def _start_task(background_tasks: BackgroundTasks, pipeline: str, params: dict):
@@ -103,6 +174,44 @@
}
+@router.get("/api/pipelines/standard/templates")
+async def get_standard_templates():
+ templates = []
+ for size, meta in TEMPLATE_SIZES.items():
+ folder = os.path.join(TEMPLATE_DIR, size)
+ if not os.path.isdir(folder):
+ continue
+ for filename in sorted(os.listdir(folder)):
+ if not filename.endswith(".html"):
+ continue
+ template_id = f"{size}/{filename}"
+ try:
+ media = template_media_spec(template_id)
+ except Exception:
+ continue
+ encoded_filename = quote(filename)
+ templates.append({
+ "id": template_id,
+ "name": os.path.splitext(filename)[0],
+ "label": _template_label(filename),
+ "size": size,
+ "ratio": meta["ratio"],
+ "width": meta["width"],
+ "height": meta["height"],
+ **media,
+ "fields": template_custom_fields(template_id),
+ "preview_url": f"/api/pipelines/standard/templates/{size}/{encoded_filename}/preview",
+ })
+ return {"templates": templates}
+
+
+@router.get("/api/pipelines/standard/templates/{size}/{filename}/preview", response_class=HTMLResponse)
+async def preview_standard_template(size: str, filename: str):
+ path = _template_path(size, filename)
+ with open(path, "r", encoding="utf-8") as f:
+ return HTMLResponse(_render_preview_html(f.read()))
+
+
@router.post("/api/pipelines/standard/tasks")
async def start_standard_pipeline(req: StandardPipelineRequest, background_tasks: BackgroundTasks):
return _start_task(background_tasks, "standard", req.model_dump(exclude_none=True))
--
Gitblit v1.8.0