Spaces:
Sleeping
Sleeping
| # render_engine.sh — OpenClaw wrapper for the video-render-engine HF Space. | |
| # Config values (base_url, poll interval/timeout) come from config.yaml so | |
| # the YAML and this script stay in sync as required by convention. | |
| set -euo pipefail | |
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| CONFIG_FILE="${SCRIPT_DIR}/config.yaml" | |
| _yaml() { | |
| # tiny dependency-free YAML value getter for flat "key: value" lines | |
| grep -E "^\s*${1}:" "$CONFIG_FILE" | head -1 | sed -E "s/^[^:]+:\s*\"?([^\"]*)\"?\s*$/\1/" | |
| } | |
| BASE_URL=$(_yaml "base_url") | |
| POLL_INTERVAL=$(_yaml "poll_interval_seconds") | |
| POLL_TIMEOUT=$(_yaml "poll_timeout_seconds") | |
| POLL_INTERVAL=${POLL_INTERVAL:-10} | |
| POLL_TIMEOUT=${POLL_TIMEOUT:-3600} | |
| usage() { | |
| echo "Usage:" | |
| echo " $0 render <request.json>" | |
| echo " $0 status <job_id>" | |
| echo " $0 download <job_id> <local_output_dir>" | |
| exit 1 | |
| } | |
| cmd_render() { | |
| local req_file="$1" | |
| [ -f "$req_file" ] || { echo "request file not found: $req_file" >&2; exit 1; } | |
| local resp | |
| resp=$(curl -sS -X POST "${BASE_URL}/render" \ | |
| -H "Content-Type: application/json" \ | |
| --data-binary "@${req_file}") | |
| local job_id | |
| job_id=$(echo "$resp" | grep -o '"job_id":"[^"]*"' | head -1 | cut -d'"' -f4) | |
| if [ -z "$job_id" ]; then | |
| echo "render request failed, response: $resp" >&2 | |
| exit 1 | |
| fi | |
| echo "$job_id" | |
| } | |
| cmd_status() { | |
| local job_id="$1" | |
| local elapsed=0 | |
| while [ "$elapsed" -lt "$POLL_TIMEOUT" ]; do | |
| local resp | |
| resp=$(curl -sS "${BASE_URL}/status/${job_id}") | |
| local status | |
| status=$(echo "$resp" | grep -o '"status":"[^"]*"' | head -1 | cut -d'"' -f4) | |
| echo "$resp" | |
| case "$status" in | |
| completed|failed) | |
| return 0 | |
| ;; | |
| esac | |
| sleep "$POLL_INTERVAL" | |
| elapsed=$((elapsed + POLL_INTERVAL)) | |
| done | |
| echo "polling timed out after ${POLL_TIMEOUT}s" >&2 | |
| exit 1 | |
| } | |
| cmd_download() { | |
| local job_id="$1" | |
| local out_dir="$2" | |
| mkdir -p "$out_dir" | |
| local resp | |
| resp=$(curl -sS "${BASE_URL}/status/${job_id}") | |
| local video thumbnail | |
| video=$(echo "$resp" | grep -o '"video":"[^"]*"' | head -1 | cut -d'"' -f4) | |
| thumbnail=$(echo "$resp" | grep -o '"thumbnail":"[^"]*"' | head -1 | cut -d'"' -f4) | |
| if [ -n "$video" ] && [ "$video" != "null" ]; then | |
| curl -sS "${BASE_URL}/outputs/${video}" -o "${out_dir}/${video}" | |
| echo "saved ${out_dir}/${video}" | |
| fi | |
| if [ -n "$thumbnail" ] && [ "$thumbnail" != "null" ]; then | |
| curl -sS "${BASE_URL}/outputs/${thumbnail}" -o "${out_dir}/${thumbnail}" | |
| echo "saved ${out_dir}/${thumbnail}" | |
| fi | |
| } | |
| [ $# -ge 1 ] || usage | |
| case "$1" in | |
| render) [ $# -eq 2 ] || usage; cmd_render "$2" ;; | |
| status) [ $# -eq 2 ] || usage; cmd_status "$2" ;; | |
| download) [ $# -eq 3 ] || usage; cmd_download "$2" "$3" ;; | |
| *) usage ;; | |
| esac | |