Add handler_feed.py
Browse files- handler_feed.py +113 -0
handler_feed.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
RunPod handler for Mongle feed generation.
|
| 3 |
+
|
| 4 |
+
Input:
|
| 5 |
+
{
|
| 6 |
+
"input": {
|
| 7 |
+
"appearance": {...},
|
| 8 |
+
"quest_ko": "공원에서 30분 달리기를 완료했어요!",
|
| 9 |
+
"quest_en": "optional English scene prompt",
|
| 10 |
+
"name": "optional_job_name",
|
| 11 |
+
"feed_seed": 123
|
| 12 |
+
}
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
Output:
|
| 16 |
+
{
|
| 17 |
+
"name": "...",
|
| 18 |
+
"quest": {
|
| 19 |
+
"quest_ko": "...",
|
| 20 |
+
"quest_en": "..."
|
| 21 |
+
},
|
| 22 |
+
"feed_image": "<base64 png>"
|
| 23 |
+
}
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
import base64
|
| 27 |
+
import io
|
| 28 |
+
import json
|
| 29 |
+
import os
|
| 30 |
+
import traceback
|
| 31 |
+
from pathlib import Path
|
| 32 |
+
|
| 33 |
+
import runpod
|
| 34 |
+
from PIL import Image
|
| 35 |
+
|
| 36 |
+
from test_text2feed_pipeline import DEFAULT_QUEST_KO, translate_quest
|
| 37 |
+
from src.feed.feed_generator_1 import generate, load_pipeline, unload_pipeline
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
OUTPUT_ROOT = Path("outputs/handler_feed")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _encode_png(image_or_path) -> str:
|
| 44 |
+
if isinstance(image_or_path, (str, Path)):
|
| 45 |
+
image = Image.open(image_or_path).convert("RGB")
|
| 46 |
+
else:
|
| 47 |
+
image = image_or_path.convert("RGB")
|
| 48 |
+
buffer = io.BytesIO()
|
| 49 |
+
image.save(buffer, format="PNG")
|
| 50 |
+
return base64.b64encode(buffer.getvalue()).decode("utf-8")
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def _load_appearance(payload: dict):
|
| 54 |
+
appearance = payload.get("appearance")
|
| 55 |
+
if appearance is not None:
|
| 56 |
+
if isinstance(appearance, str):
|
| 57 |
+
return json.loads(appearance)
|
| 58 |
+
return appearance
|
| 59 |
+
|
| 60 |
+
appearance_path = payload.get("appearance_path")
|
| 61 |
+
if appearance_path:
|
| 62 |
+
return json.loads(Path(appearance_path).read_text(encoding="utf-8"))
|
| 63 |
+
|
| 64 |
+
raise ValueError("Missing input.appearance or input.appearance_path.")
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def handler(event):
|
| 68 |
+
payload = event.get("input", {}) if isinstance(event, dict) else {}
|
| 69 |
+
name = payload.get("name") or "feed_job"
|
| 70 |
+
out_dir = OUTPUT_ROOT / name
|
| 71 |
+
out_dir.mkdir(parents=True, exist_ok=True)
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
appearance = _load_appearance(payload)
|
| 75 |
+
quest_ko = payload.get("quest_ko") or DEFAULT_QUEST_KO
|
| 76 |
+
quest_en = payload.get("quest_en") or translate_quest(quest_ko)
|
| 77 |
+
feed_seed = int(payload.get("feed_seed", payload.get("seed", 123)))
|
| 78 |
+
|
| 79 |
+
pipe = load_pipeline()
|
| 80 |
+
try:
|
| 81 |
+
image = generate(appearance, quest_en, pipe, seed=feed_seed)
|
| 82 |
+
finally:
|
| 83 |
+
unload_pipeline(pipe)
|
| 84 |
+
|
| 85 |
+
feed_path = out_dir / "feed.png"
|
| 86 |
+
image.save(feed_path)
|
| 87 |
+
|
| 88 |
+
quest = {"quest_ko": quest_ko, "quest_en": quest_en}
|
| 89 |
+
result_data = {
|
| 90 |
+
"name": name,
|
| 91 |
+
"appearance": appearance,
|
| 92 |
+
"quest": quest,
|
| 93 |
+
"feed_image_path": str(feed_path).replace("\\", "/"),
|
| 94 |
+
}
|
| 95 |
+
(out_dir / "result.json").write_text(
|
| 96 |
+
json.dumps(result_data, ensure_ascii=False, indent=2),
|
| 97 |
+
encoding="utf-8",
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
return {
|
| 101 |
+
"name": name,
|
| 102 |
+
"quest": quest,
|
| 103 |
+
"feed_image": _encode_png(feed_path),
|
| 104 |
+
"output_dir": str(out_dir).replace("\\", "/"),
|
| 105 |
+
}
|
| 106 |
+
except Exception:
|
| 107 |
+
error_text = traceback.format_exc()
|
| 108 |
+
(out_dir / "error.log").write_text(error_text, encoding="utf-8")
|
| 109 |
+
return {"error": error_text, "name": name, "output_dir": str(out_dir).replace("\\", "/")}
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
if os.getenv("RUNPOD_SERVERLESS", "1") == "1":
|
| 113 |
+
runpod.serverless.start({"handler": handler})
|