Upload v2/workflow.py with huggingface_hub
Browse files- v2/workflow.py +95 -0
v2/workflow.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import traceback
|
| 5 |
+
from dataclasses import asdict
|
| 6 |
+
from datetime import datetime, timezone
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import Any
|
| 9 |
+
|
| 10 |
+
import numpy as np
|
| 11 |
+
from PIL import Image
|
| 12 |
+
|
| 13 |
+
from .config import WorkflowConfig, ensure_job_dirs
|
| 14 |
+
from .stages import (
|
| 15 |
+
materialize_artifacts,
|
| 16 |
+
run_rig_facefusion_stage,
|
| 17 |
+
run_shape_stage,
|
| 18 |
+
run_texture_stage,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class UnifiedWorkflow:
|
| 23 |
+
def __init__(
|
| 24 |
+
self, config: WorkflowConfig, output_dir: Path, seed: int, tex_seed: int
|
| 25 |
+
):
|
| 26 |
+
self.config = config
|
| 27 |
+
self.output_dir = output_dir
|
| 28 |
+
self.seed = int(seed)
|
| 29 |
+
self.tex_seed = int(tex_seed)
|
| 30 |
+
self.artifacts_dir, self.logs_dir = ensure_job_dirs(output_dir)
|
| 31 |
+
|
| 32 |
+
def run(self, image_path: Path) -> dict[str, Any]:
|
| 33 |
+
started = datetime.now(timezone.utc)
|
| 34 |
+
manifest: dict[str, Any] = {
|
| 35 |
+
"workflow": "meshforge-v2-unified",
|
| 36 |
+
"started_at": started.isoformat(),
|
| 37 |
+
"input": {"image": str(image_path)},
|
| 38 |
+
"config": asdict(self.config),
|
| 39 |
+
"seed": self.seed,
|
| 40 |
+
"tex_seed": self.tex_seed,
|
| 41 |
+
"stages": [],
|
| 42 |
+
"artifacts": {},
|
| 43 |
+
"status": "running",
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
all_outputs: dict[str, Any] = {}
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
image_array = np.array(Image.open(image_path).convert("RGB"))
|
| 50 |
+
|
| 51 |
+
shape = run_shape_stage(image_array, self.config, self.seed)
|
| 52 |
+
all_outputs.update(shape)
|
| 53 |
+
manifest["stages"].append(
|
| 54 |
+
{"name": "shape", "status": "ok", "detail": shape["status"]}
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
texture = run_texture_stage(
|
| 58 |
+
shape["shape_glb"], image_array, self.config, self.tex_seed
|
| 59 |
+
)
|
| 60 |
+
all_outputs.update(texture)
|
| 61 |
+
manifest["stages"].append(
|
| 62 |
+
{"name": "texture", "status": "ok", "detail": texture["status"]}
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
fused = run_rig_facefusion_stage(
|
| 66 |
+
texture["textured_glb"], image_array, self.config
|
| 67 |
+
)
|
| 68 |
+
all_outputs.update(fused)
|
| 69 |
+
manifest["stages"].append(
|
| 70 |
+
{
|
| 71 |
+
"name": "rig_pshuman_fusion",
|
| 72 |
+
"status": "ok",
|
| 73 |
+
"detail": fused["status"],
|
| 74 |
+
}
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
manifest["artifacts"] = materialize_artifacts(
|
| 78 |
+
all_outputs, self.artifacts_dir
|
| 79 |
+
)
|
| 80 |
+
manifest["status"] = "completed"
|
| 81 |
+
except Exception as exc:
|
| 82 |
+
manifest["status"] = "failed"
|
| 83 |
+
manifest["error"] = {
|
| 84 |
+
"message": str(exc),
|
| 85 |
+
"traceback": traceback.format_exc(),
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
manifest["finished_at"] = datetime.now(timezone.utc).isoformat()
|
| 89 |
+
self._write_manifest(manifest)
|
| 90 |
+
return manifest
|
| 91 |
+
|
| 92 |
+
def _write_manifest(self, manifest: dict[str, Any]) -> None:
|
| 93 |
+
out = self.output_dir / "manifest.json"
|
| 94 |
+
out.parent.mkdir(parents=True, exist_ok=True)
|
| 95 |
+
out.write_text(json.dumps(manifest, indent=2), encoding="utf-8")
|