agent_john
add cli.py
d734fe6
Raw History Blame Contribute Delete
2.48 kB
#!/usr/bin/env python3
"""
crawlier-image-studio CLI - quick command-line access to the image API.
Usage:
export CRAWLIER_API_URL=https://984183868db966.lhr.life
python cli.py "a serene mountain lake" --size 1024
python cli.py pricing
python cli.py health
"""
import argparse, json, sys, os, urllib.request, urllib.error
DEFAULT_URL = os.environ.get("CRAWLIER_API_URL", "https://984183868db966.lhr.life")
def cmd_generate(args):
url = f"{args.url.rstrip('/')}/v1/generate"
body = {"prompt": args.prompt, "model": args.model, "size": args.size, "n": args.n}
req = urllib.request.Request(
url, data=json.dumps(body).encode(),
headers={"Content-Type": "application/json"}, method="POST")
try:
with urllib.request.urlopen(req, timeout=60) as r:
data = json.loads(r.read())
except urllib.error.HTTPError as e:
print(f"ERROR {e.code}: {e.read().decode()}", file=sys.stderr)
return 2
if args.json:
print(json.dumps(data, indent=2))
else:
for img in data.get("images", []):
print(img.get("url", img))
b = data.get("billing", {})
if b:
print(f"# cost=${b.get('cost_usd')} price=${b.get('price_usd')} elapsed_ms={data.get('elapsed_ms')}", file=sys.stderr)
return 0
def cmd_pricing(args):
with urllib.request.urlopen(f"{args.url.rstrip('/')}/v1/pricing", timeout=10) as r:
print(r.read().decode())
return 0
def cmd_health(args):
with urllib.request.urlopen(f"{args.url.rstrip('/')}/healthz", timeout=10) as r:
print(r.read().decode())
return 0
def main():
p = argparse.ArgumentParser(description="Crawlier Image Studio CLI")
p.add_argument("--url", default=DEFAULT_URL, help="API base URL")
sub = p.add_subparsers(dest="cmd", required=True)
g = sub.add_parser("generate", aliases=["g","gen"])
g.add_argument("prompt")
g.add_argument("--model", default="image-01", choices=["image-01","image-02"])
g.add_argument("--size", default="1024", choices=["512","1024","2048"])
g.add_argument("-n", type=int, default=1)
g.add_argument("--json", action="store_true")
g.set_defaults(func=cmd_generate)
pr = sub.add_parser("pricing", aliases=["p","price"])
pr.set_defaults(func=cmd_pricing)
h = sub.add_parser("health", aliases=["h"])
h.set_defaults(func=cmd_health)
args = p.parse_args()
sys.exit(args.func(args))
if __name__ == "__main__":
main()