Skip to content

aiman-labs/ergouzi-agent-skills / skills/ergouzi-image-gen

ergouzi-image-gen

Verified

Submit and complete Ergouzi image generation, editing, virtual try-on, upscaling, and background-removal tasks through ergouzi.life, including tasks that use local image files or HTTPS URLs. Use when Codex must call an ergouzi/e-image* or ergouzi/e-rmbg model, poll the asynchronous task, download its outputs, resume a task ID, or cancel an image task.

FileKindSize
SKILL.mdskill md6.6 KB
agents/openai.yamlconfig208 B
references/ai-guide.mdmarkdown932 B
references/model-reference.mdmarkdown5.2 KB
scripts/client.pyscript20.1 KB
scripts/configure.pyscript2.1 KB
scripts/media.pyscript5.8 KB
scripts/run.pyscript18.2 KB

scripts/media.py

View on GitHub
#!/usr/bin/env python3
"""Validate and resolve Ergouzi image model media inputs."""

from __future__ import annotations

import base64
import json
from dataclasses import dataclass
from pathlib import Path
from typing import Any
from urllib.parse import urlparse

from client import ClientError


LOCAL_FILE_KEY = "$local_file"
FORBIDDEN_INPUT_FIELDS = frozenset({"hf_api_token"})
MAX_EXPANDED_INPUT_BYTES = 4 * 1024 * 1024
MAX_LOCAL_MEDIA_BYTES = 3 * 1024 * 1024
IMAGE_TYPES = frozenset({"image/jpeg", "image/png", "image/webp"})


@dataclass(frozen=True)
class MediaField:
    types: frozenset[str]
    multiple: bool = False


@dataclass(frozen=True)
class PreparedMedia:
    path: Path
    media_type: str


MODEL_MEDIA_FIELDS = {
    "ergouzi/e-image": {},
    "ergouzi/e-image-edit": {
        "images": MediaField(IMAGE_TYPES, multiple=True),
    },
    "ergouzi/e-image-ideogram": {},
    "ergouzi/e-image-try-on": {
        "person_image": MediaField(IMAGE_TYPES),
        "garment_images": MediaField(IMAGE_TYPES, multiple=True),
        "reference_pose": MediaField(IMAGE_TYPES),
    },
    "ergouzi/e-image-upscale": {
        "image": MediaField(IMAGE_TYPES),
    },
    "ergouzi/e-rmbg": {
        "image": MediaField(IMAGE_TYPES),
    },
}


class MediaResolver:
    def prepare(self, value: Any, field: MediaField) -> Any:
        if field.multiple:
            if not isinstance(value, list):
                raise ClientError("Media field must be an array")
            return [self._prepare_one(item, field.types) for item in value]
        return self._prepare_one(value, field.types)

    def _prepare_one(self, value: Any, allowed_types: frozenset[str]) -> str | PreparedMedia:
        if isinstance(value, str):
            return validate_media_reference(value, allowed_types)
        if not isinstance(value, dict) or set(value) != {LOCAL_FILE_KEY}:
            raise ClientError("Media input must be an HTTPS URL, data URI, or $local_file object")
        source = value[LOCAL_FILE_KEY]
        if not isinstance(source, str) or not source.strip():
            raise ClientError(f"{LOCAL_FILE_KEY} must contain a local file path")
        path = Path(source).expanduser().resolve()
        if not path.is_file():
            raise ClientError(f"Local media file does not exist: {path}")
        size = path.stat().st_size
        if size == 0:
            raise ClientError(f"Local media file is empty: {path}")
        if size > MAX_LOCAL_MEDIA_BYTES:
            raise ClientError(
                "Local media is too large for the 4 MiB API request; use an HTTPS URL"
            )
        media_type = detect_media_type(path)
        if media_type not in allowed_types:
            raise ClientError(f"Unsupported media type for this field: {media_type or path.suffix}")
        return PreparedMedia(path, media_type)

    def encode_local(self, value: Any) -> Any:
        if isinstance(value, PreparedMedia):
            encoded = base64.b64encode(value.path.read_bytes()).decode("ascii")
            return f"data:{value.media_type};base64,{encoded}"
        if isinstance(value, list):
            return [self.encode_local(item) for item in value]
        if isinstance(value, dict):
            return {key: self.encode_local(item) for key, item in value.items()}
        return value


def resolve_media_inputs(
    model: str, model_input: dict[str, Any]
) -> dict[str, Any]:
    forbidden = find_forbidden_input_field(model_input)
    if forbidden:
        raise ClientError(f"{forbidden} is not accepted; use the configured Ergouzi API key")
    fields = MODEL_MEDIA_FIELDS.get(model, {})
    resolved = dict(model_input)
    resolver = MediaResolver()
    for name, field in fields.items():
        if name in resolved:
            resolved[name] = resolver.prepare(resolved[name], field)
    if contains_local_placeholder(resolved):
        raise ClientError(f"{LOCAL_FILE_KEY} is only allowed in documented media fields")
    resolved = resolver.encode_local(resolved)
    if len(json.dumps(resolved, ensure_ascii=False).encode("utf-8")) > MAX_EXPANDED_INPUT_BYTES:
        raise ClientError(
            "Expanded input exceeds the 4 MiB API request limit; use HTTPS URLs for media"
        )
    return resolved


def find_forbidden_input_field(value: Any) -> str | None:
    if isinstance(value, dict):
        for key, item in value.items():
            if key in FORBIDDEN_INPUT_FIELDS:
                return key
            found = find_forbidden_input_field(item)
            if found:
                return found
    elif isinstance(value, list):
        for item in value:
            found = find_forbidden_input_field(item)
            if found:
                return found
    return None


def validate_media_reference(value: str, allowed_types: frozenset[str]) -> str:
    parsed = urlparse(value)
    if parsed.scheme == "https" and parsed.netloc:
        return value
    if value.startswith("data:"):
        header = value.split(",", 1)[0]
        media_type = header[5:].split(";", 1)[0].lower()
        if media_type in allowed_types and ";base64" in header.lower():
            return value
    raise ClientError("Media input must use HTTPS or a supported base64 data URI")


def detect_media_type(path: Path) -> str:
    with path.open("rb") as handle:
        data = handle.read(512)
    if len(data) >= 3 and data[:3] == b"\xff\xd8\xff":
        return "image/jpeg"
    if data.startswith(b"\x89PNG\r\n\x1a\n"):
        return "image/png"
    if len(data) >= 12 and data[:4] == b"RIFF" and data[8:12] == b"WEBP":
        return "image/webp"
    return ""


def contains_local_placeholder(value: Any) -> bool:
    if isinstance(value, dict):
        if set(value) == {LOCAL_FILE_KEY}:
            return True
        return any(contains_local_placeholder(item) for item in value.values())
    if isinstance(value, list):
        return any(contains_local_placeholder(item) for item in value)
    return False
reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexUnverifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall