#!/usr/bin/env python3 """One FLUX-1-schnell image via Cloudflare Workers AI (10k free neurons/day). Usage: cf-flux.py [steps] [seed] Creds come from backnforth/.env — never printed, never committed.""" import base64 import json import os import sys import urllib.request out, prompt = sys.argv[1], sys.argv[2] steps = int(sys.argv[3]) if len(sys.argv) > 3 else 8 seed = int(sys.argv[4]) if len(sys.argv) > 4 else 0 env = {} with open(os.path.expanduser('~/Documents/backnforth/.env')) as f: for line in f: if '=' in line and not line.startswith('#'): k, _, v = line.strip().partition('=') env[k] = v req = urllib.request.Request( f"https://api.cloudflare.com/client/v4/accounts/{env['CLOUDFLARE_ACCOUNT_ID']}/ai/run/@cf/black-forest-labs/flux-1-schnell", data=json.dumps({'prompt': prompt, 'steps': steps, 'seed': seed}).encode(), headers={'Authorization': f"Bearer {env['CLOUDFLARE_API_TOKEN']}", 'Content-Type': 'application/json'}, ) r = json.load(urllib.request.urlopen(req, timeout=120)) if not r.get('success'): sys.exit('CF error: ' + json.dumps(r.get('errors'))) with open(out, 'wb') as f: f.write(base64.b64decode(r['result']['image'])) print(out)