#!/usr/bin/env python3
"""Cek status prediction AMARI scene 1."""
import json, re, urllib.request

tools = open('TOOLS.md', encoding='utf-8').read()
m = re.search(r'Replicate API Key[\s\S]*?Token:\*\*\s*`([^`]+)`', tools)
KEY = m.group(1).strip()
UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0 Safari/537.36"

for pid in ["fmr8d1w87hrmy0czwrqt20zwwc", "7xsnfpegb5rmr0czwrfsc27wn4"]:
    req = urllib.request.Request(f"https://api.replicate.com/v1/predictions/{pid}",
        headers={"Authorization": f"Bearer {KEY}", "User-Agent": UA})
    try:
        with urllib.request.urlopen(req, timeout=30) as r:
            d = json.loads(r.read())
        print(pid, "->", d.get("status"), "| error:", d.get("error"))
        out = d.get("output")
        if out:
            print("   OUTPUT:", out)
        metrics = d.get("metrics")
        if metrics:
            print("   METRICS:", json.dumps(metrics))
    except Exception as e:
        print(pid, "-> ERR:", e)
