416 lines
14 KiB
Python
416 lines
14 KiB
Python
"""Local isolated product instance + HTTP fake LLM. Never touches deployment data."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import asyncio
|
|
import io
|
|
import json
|
|
import subprocess
|
|
import threading
|
|
import time
|
|
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
from pathlib import Path
|
|
|
|
import uvicorn
|
|
from calliope import config
|
|
from calliope.db import get_db, migrate_db
|
|
from calliope.main import create_app
|
|
from PIL import Image, ImageDraw
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--data", required=True, type=Path)
|
|
parser.add_argument("--port", type=int, default=18740)
|
|
parser.add_argument("--resume", action="store_true")
|
|
args = parser.parse_args()
|
|
root = args.data.resolve()
|
|
root.mkdir(parents=True, exist_ok=True)
|
|
config.CONFIG_FILE = root / "calliope_config.json"
|
|
s = config.settings
|
|
prior_assignments = None
|
|
if args.resume:
|
|
s.load_config_file()
|
|
prior_assignments = dict(s.agent_llm_assignments)
|
|
s.data_dir = root
|
|
s.assets_dir = root / "assets"
|
|
s.assets_dir.mkdir(exist_ok=True)
|
|
s.db_name = "fixture.db"
|
|
s.dry_run = True
|
|
s.agent_shell_enabled = False
|
|
s.comfyui_endpoints = [
|
|
{
|
|
"id": "fixture-comfy",
|
|
"name": "本地隔离假服务",
|
|
"base_url": "http://127.0.0.1:1",
|
|
"auth_type": "none",
|
|
"auth_token": "",
|
|
}
|
|
]
|
|
s.comfyui_active_id = "fixture-comfy"
|
|
|
|
|
|
# Fake provider is intentionally a real HTTP boundary; product code is unchanged.
|
|
class Fake(BaseHTTPRequestHandler):
|
|
def log_message(self, *_args):
|
|
pass
|
|
|
|
def do_GET(self):
|
|
payload = json.dumps(
|
|
{
|
|
"system": {"comfyui_version": "fixture-0.3"},
|
|
"devices": [{"name": "Fixture GPU"}],
|
|
}
|
|
).encode()
|
|
self.send_response(200)
|
|
self.send_header("Content-Type", "application/json")
|
|
self.send_header("Content-Length", str(len(payload)))
|
|
self.end_headers()
|
|
self.wfile.write(payload)
|
|
|
|
def do_POST(self):
|
|
payload = json.loads(self.rfile.read(int(self.headers["Content-Length"])))
|
|
control_path = root / "control.json"
|
|
control = json.loads(control_path.read_text()) if control_path.exists() else {}
|
|
with (root / "provider.jsonl").open("a") as f:
|
|
# No prompt or secret logging: actual model, direction instruction, stream and timestamps only.
|
|
f.write(
|
|
json.dumps(
|
|
{
|
|
"at": time.time(),
|
|
"model": payload.get("model"),
|
|
"stream": payload.get("stream"),
|
|
"phase": "start",
|
|
}
|
|
)
|
|
+ "\n"
|
|
)
|
|
if control.get("blocking") and payload.get("stream"):
|
|
self.send_response(405)
|
|
self.end_headers()
|
|
return
|
|
self.send_response(control.get("status", 200))
|
|
self.send_header(
|
|
"Content-Type",
|
|
"text/event-stream" if payload.get("stream") else "application/json",
|
|
)
|
|
self.end_headers()
|
|
if control.get("status", 200) != 200:
|
|
return
|
|
text = next(
|
|
(m["content"] for m in payload["messages"] if m["role"] == "user"), "ping"
|
|
)
|
|
from calliope.routers.prompt_tools import fragments
|
|
|
|
protected = fragments(text)
|
|
instruction = payload["messages"][0]["content"]
|
|
result = (
|
|
"一只猫,柔和侧光,电影质感"
|
|
if "from English to Chinese" in instruction
|
|
else "A cat in soft side lighting, cinematic composition"
|
|
) + (" · 润色" if "Polish" in instruction else "")
|
|
if protected:
|
|
result += ", " + ", ".join(protected)
|
|
if control.get("broken"):
|
|
result = "Broken output"
|
|
delay = control.get("delay", 0.15)
|
|
try:
|
|
time.sleep(delay)
|
|
if payload.get("stream"):
|
|
for piece in [result[: len(result) // 2], result[len(result) // 2 :]]:
|
|
self.wfile.write(
|
|
(
|
|
"data: "
|
|
+ json.dumps({"choices": [{"delta": {"content": piece}}]})
|
|
+ "\n\n"
|
|
).encode()
|
|
)
|
|
self.wfile.flush()
|
|
time.sleep(control.get("chunk_delay", 0.05))
|
|
self.wfile.write(b"data: [DONE]\n\n")
|
|
else:
|
|
self.wfile.write(
|
|
json.dumps({"choices": [{"message": {"content": result}}]}).encode()
|
|
)
|
|
except (BrokenPipeError, ConnectionResetError):
|
|
with (root / "provider.jsonl").open("a") as f:
|
|
f.write(json.dumps({"at": time.time(), "phase": "disconnected"}) + "\n")
|
|
|
|
|
|
provider = ThreadingHTTPServer(("127.0.0.1", 0), Fake)
|
|
thread = threading.Thread(target=provider.serve_forever, daemon=True)
|
|
thread.start()
|
|
s.llm_profiles = [
|
|
{
|
|
"id": "fixture-a",
|
|
"name": "隔离翻译模型",
|
|
"model": "fixture-translate",
|
|
"base_url": f"http://127.0.0.1:{provider.server_port}/v1",
|
|
"api_key": "",
|
|
},
|
|
{
|
|
"id": "fixture-b",
|
|
"name": "隔离润色模型",
|
|
"model": "fixture-polish",
|
|
"base_url": f"http://127.0.0.1:{provider.server_port}/v1",
|
|
"api_key": "",
|
|
},
|
|
]
|
|
s.comfyui_endpoints[0]["base_url"] = f"http://127.0.0.1:{provider.server_port}"
|
|
s.llm_profiles.append(
|
|
{
|
|
"id": "fixture-c",
|
|
"name": "本地候选模型",
|
|
"model": "fixture-local",
|
|
"base_url": "http://127.0.0.1:1/v1",
|
|
"api_key": "",
|
|
}
|
|
)
|
|
s.llm_active_id = "fixture-a"
|
|
s.agent_llm_assignments = {
|
|
"translate": None,
|
|
"polish": "fixture-b",
|
|
"style": "fixture-b",
|
|
"video": "fixture-b",
|
|
}
|
|
s.apply_active_llm()
|
|
if prior_assignments is not None:
|
|
s.agent_llm_assignments = prior_assignments
|
|
s.save_config_file()
|
|
asyncio.run(migrate_db(s.db_path))
|
|
from calliope.routers import prompt_tools
|
|
|
|
prompt_tools.TOTAL_TIMEOUT = 1.5 # Accelerated total clock only in isolated QA process.
|
|
# A fixture-only deterministic media set, separate from product source and approved originals.
|
|
uploads = s.assets_dir / "uploads"
|
|
uploads.mkdir(exist_ok=True)
|
|
for index in range(14):
|
|
path = uploads / f"样本素材-{index + 1:02}.png"
|
|
im = Image.new(
|
|
"RGB",
|
|
(960 if index % 2 else 640, 640 if index % 2 else 960),
|
|
(28 + index * 8, 45 + index * 6, 60 + index * 5),
|
|
)
|
|
draw = ImageDraw.Draw(im)
|
|
draw.ellipse((80, 80, 400, 400), fill=(190, 145, 80))
|
|
draw.rectangle((100, 430, 530, 530), fill=(40, 110, 125))
|
|
im.save(path)
|
|
buffer = io.BytesIO()
|
|
Image.new("RGB", (640, 360), (34, 75, 95)).save(buffer, format="JPEG")
|
|
ffmpeg = Path.home() / ".cache/ms-playwright/ffmpeg-1011/ffmpeg-linux"
|
|
if ffmpeg.exists():
|
|
subprocess.run(
|
|
[
|
|
str(ffmpeg),
|
|
"-y",
|
|
"-f",
|
|
"image2pipe",
|
|
"-r",
|
|
"12",
|
|
"-c:v",
|
|
"mjpeg",
|
|
"-i",
|
|
"pipe:0",
|
|
"-c:v",
|
|
"libvpx",
|
|
"-f",
|
|
"webm",
|
|
str(uploads / "视频样本.webm"),
|
|
],
|
|
input=buffer.getvalue() * 24,
|
|
stdout=subprocess.DEVNULL,
|
|
stderr=subprocess.DEVNULL,
|
|
check=True,
|
|
)
|
|
(uploads / "真实读取失败.png").write_bytes(b"bad media")
|
|
Image.new("RGB", (1, 1), (255, 0, 0)).save(uploads / "小于1KB可解码.png")
|
|
conn = get_db(s.db_path)
|
|
if not conn.execute("SELECT 1 FROM projects LIMIT 1").fetchone():
|
|
for index in range(5):
|
|
title = [
|
|
"雾港来信",
|
|
"夏日站台",
|
|
"城市漫游者",
|
|
"旧照片里的猫",
|
|
"HEL690 冒烟项目",
|
|
][index]
|
|
cover = (
|
|
str(uploads / f"样本素材-{index + 1:02}.png")
|
|
if index not in [2, 4]
|
|
else None
|
|
)
|
|
pid = conn.execute(
|
|
"INSERT INTO projects(title,idea,cover_path) VALUES(?,?,?)",
|
|
(title, "隔离验收夹具", cover),
|
|
).lastrowid
|
|
for n in range(3 if index == 0 else 1):
|
|
conn.execute(
|
|
"INSERT INTO characters(project_id,name) VALUES(?,?)",
|
|
(pid, "角色 " + str(n + 1)),
|
|
)
|
|
for n in range(12 if index == 0 else 3):
|
|
conn.execute(
|
|
"INSERT INTO scenes(project_id,order_index,heading) VALUES(?,?,?)",
|
|
(pid, n + 1, "场景 " + str(n + 1)),
|
|
)
|
|
for key in ["script", "style", "storyboard", "assets", "shot"]:
|
|
conn.execute(
|
|
"INSERT INTO project_stages(project_id,stage_key,status,content_json) VALUES(?,?,?,?)",
|
|
(
|
|
pid,
|
|
key,
|
|
"confirmed"
|
|
if (index == 3 or (index == 0 and key in ["script", "style"]))
|
|
else "awaiting_confirm"
|
|
if (
|
|
index == 0
|
|
and key == "storyboard"
|
|
or index == 1
|
|
and key == "style"
|
|
)
|
|
else "todo",
|
|
"{}",
|
|
),
|
|
)
|
|
for index, kind in enumerate(["image", "image", "video"]):
|
|
workflow = {
|
|
"1": {
|
|
"class_type": "CLIPTextEncode",
|
|
"inputs": {"text": "一只猫,柔和侧光"},
|
|
"_meta": {"title": "Prompt (Input:prompt)"},
|
|
},
|
|
"2": {
|
|
"class_type": "CLIPTextEncode",
|
|
"inputs": {"text": "模糊,低画质"},
|
|
"_meta": {"title": "Negative (Input:negative)"},
|
|
},
|
|
"9": {
|
|
"class_type": "SaveImage" if kind == "image" else "SaveVideo",
|
|
"inputs": {"images": ["1", 0]},
|
|
"_meta": {"title": "Output (Output:" + kind + ")"},
|
|
},
|
|
}
|
|
from calliope.models.schemas import WorkflowCreate
|
|
from calliope.routers.workflows import create_workflow
|
|
|
|
conn.commit()
|
|
asyncio.run(
|
|
create_workflow(
|
|
WorkflowCreate(
|
|
name="隔离工作流 " + str(index + 1),
|
|
kind=kind,
|
|
workflow_json=workflow,
|
|
usage="text2image" if kind == "image" else "text2video",
|
|
)
|
|
)
|
|
)
|
|
content_by_stage = {
|
|
"style": {
|
|
"directions": [
|
|
{
|
|
"id": "s1",
|
|
"name": "柔光",
|
|
"description": "隔离夹具",
|
|
"prompt": "一只猫,柔和侧光",
|
|
"negative_prompt": "模糊",
|
|
},
|
|
{
|
|
"id": "s2",
|
|
"name": "暮色",
|
|
"description": "隔离夹具",
|
|
"prompt": "夜景中的猫",
|
|
},
|
|
]
|
|
},
|
|
"assets": {
|
|
"assets": [
|
|
{
|
|
"id": "a1",
|
|
"kind": "character",
|
|
"name": "猫",
|
|
"description": "隔离夹具",
|
|
"prompt": "一只猫,柔和侧光",
|
|
"negative_prompt": "模糊",
|
|
}
|
|
]
|
|
},
|
|
"shot": {
|
|
"shots": [
|
|
{
|
|
"id": "v1",
|
|
"shot_no": "S1",
|
|
"description": "隔离夹具",
|
|
"prompt": "镜头缓慢推进",
|
|
"duration_sec": 5,
|
|
}
|
|
]
|
|
},
|
|
}
|
|
for key, content in content_by_stage.items():
|
|
conn.execute(
|
|
"UPDATE project_stages SET content_json=?,status='awaiting_confirm' WHERE project_id=1 AND stage_key=?",
|
|
(json.dumps(content), key),
|
|
)
|
|
scratch = conn.execute(
|
|
"INSERT INTO projects(title,status) VALUES('隔离独立创作','system')"
|
|
).lastrowid
|
|
for index in range(20):
|
|
status = (
|
|
"done"
|
|
if index < 12
|
|
else [
|
|
"running",
|
|
"pending",
|
|
"failed",
|
|
"cancelled",
|
|
"cancelled",
|
|
"cancelled",
|
|
"cancelled",
|
|
"cancelled",
|
|
][index - 12]
|
|
)
|
|
kind = "video" if index % 2 else "image"
|
|
output = (
|
|
str(uploads / "视频样本.webm")
|
|
if kind == "video"
|
|
else str(uploads / f"样本素材-{index % 14 + 1:02}.png")
|
|
)
|
|
conn.execute(
|
|
"INSERT INTO jobs(project_id,kind,status,payload_json,output_paths_json,workflow_id) VALUES(?,?,?,?,?,?)",
|
|
(
|
|
scratch,
|
|
kind,
|
|
status,
|
|
json.dumps(
|
|
{
|
|
"source": "studio-" + kind,
|
|
"input_values": {"1": "一只猫,柔和侧光", "2": "模糊"},
|
|
}
|
|
),
|
|
json.dumps([output] if status == "done" else []),
|
|
2 if kind == "image" else 3,
|
|
),
|
|
)
|
|
conn.commit()
|
|
conn.close()
|
|
(root / "runtime.json").write_text(
|
|
json.dumps(
|
|
{"port": args.port, "provider_port": provider.server_port, "data": str(root)}
|
|
)
|
|
)
|
|
from calliope.queue.manager import queue_manager
|
|
|
|
queue_manager.paused = (
|
|
True # Keep seeded QA queue states stable; never execute GPU jobs.
|
|
)
|
|
try:
|
|
uvicorn.run(
|
|
create_app(Path(__file__).resolve().parents[2] / "calliope-web" / "build"),
|
|
host="127.0.0.1",
|
|
port=args.port,
|
|
log_level="warning",
|
|
)
|
|
finally:
|
|
provider.shutdown()
|
|
thread.join()
|
|
provider.server_close()
|