Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,22 +9,23 @@ import gradio as gr
|
|
| 9 |
HORDE_URL = "https://stablehorde.net/api/v2/generate/async"
|
| 10 |
HORDE_STATUS = "https://stablehorde.net/api/v2/generate/status/{id}"
|
| 11 |
|
| 12 |
-
# HF Space secret recommended for priority
|
| 13 |
HORDE_API_KEY = os.getenv("HORDE_API_KEY", "")
|
| 14 |
CLIENT_AGENT = "StitchMaster/0.1 (https://huggingface.co/spaces/your-space)"
|
| 15 |
|
| 16 |
DEFAULT_STEPS = 24
|
| 17 |
-
DEFAULT_W =
|
| 18 |
-
DEFAULT_H =
|
| 19 |
POLL_INTERVAL = 2.5
|
| 20 |
POLL_TIMEOUT = 240 # bump if queues are long
|
| 21 |
MODEL = None # or set e.g. "SDXL 1.0"
|
| 22 |
|
| 23 |
def _headers():
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
| 28 |
|
| 29 |
# =========================
|
| 30 |
# Horde client with debugging
|
|
@@ -53,32 +54,47 @@ def horde_txt2img(prompt: str,
|
|
| 53 |
if model:
|
| 54 |
payload["models"] = [model]
|
| 55 |
|
| 56 |
-
# Submit
|
| 57 |
try:
|
| 58 |
-
submit = requests.post(
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
dbg.append(f"SUBMIT status={submit.status_code}")
|
| 62 |
if submit.status_code >= 300:
|
| 63 |
dbg.append(f"SUBMIT body={submit.text[:500]}")
|
| 64 |
submit.raise_for_status()
|
|
|
|
| 65 |
submit_j = submit.json()
|
| 66 |
job_id = submit_j.get("id")
|
| 67 |
if not job_id:
|
| 68 |
dbg.append(f"SUBMIT json={submit_j}")
|
| 69 |
raise gr.Error("Horde submit succeeded but no job id returned.")
|
| 70 |
dbg.append(f"JOB id={job_id}")
|
| 71 |
-
|
|
|
|
| 72 |
dbg.append("SUBMIT exception:\n" + traceback.format_exc())
|
| 73 |
return None, "\n".join(dbg)
|
| 74 |
|
| 75 |
-
# Poll
|
| 76 |
start = time.time()
|
| 77 |
while True:
|
| 78 |
try:
|
| 79 |
-
status_r = requests.get(
|
| 80 |
-
HORDE_STATUS.format(id=job_id), headers=_headers(), timeout=30
|
| 81 |
-
)
|
| 82 |
if status_r.status_code >= 300:
|
| 83 |
dbg.append(f"POLL status={status_r.status_code}")
|
| 84 |
dbg.append(f"POLL body={status_r.text[:500]}")
|
|
@@ -123,14 +139,13 @@ def horde_txt2img(prompt: str,
|
|
| 123 |
return None, "\n".join(dbg)
|
| 124 |
|
| 125 |
def generate_image(prompt, steps, size):
|
| 126 |
-
# size like "
|
| 127 |
try:
|
| 128 |
w, h = [int(x.strip()) for x in size.lower().split("x")]
|
| 129 |
except Exception:
|
| 130 |
w, h = DEFAULT_W, DEFAULT_H
|
| 131 |
img, debug = horde_txt2img(prompt, steps=steps, width=w, height=h)
|
| 132 |
if img is None:
|
| 133 |
-
# Show a friendly error plus the debug log
|
| 134 |
gr.Warning("Generation failed. See debug log for details.")
|
| 135 |
return img, debug
|
| 136 |
|
|
@@ -162,22 +177,22 @@ CUSTOM_CSS = """
|
|
| 162 |
}
|
| 163 |
"""
|
| 164 |
|
| 165 |
-
with gr.Blocks(css=
|
| 166 |
gr.Markdown("### Image Checkpoints (Stable Horde)\nGenerate per-prompt frames. If a run fails, check the debug panel.")
|
| 167 |
|
| 168 |
with gr.Row():
|
| 169 |
steps = gr.Slider(8, 50, value=DEFAULT_STEPS, step=1, label="Steps (quality/time)")
|
| 170 |
size = gr.Dropdown(
|
| 171 |
-
choices=["512x512", "
|
| 172 |
value=f"{DEFAULT_W}x{DEFAULT_H}",
|
| 173 |
label="Resolution"
|
| 174 |
)
|
| 175 |
|
| 176 |
-
#
|
| 177 |
debug_box = gr.Code(label="Debug log", interactive=False)
|
| 178 |
|
| 179 |
prompt_boxes, gen_buttons, img_outputs = [], [], []
|
| 180 |
-
for i in range(1,
|
| 181 |
with gr.Row():
|
| 182 |
with gr.Column(scale=1, min_width=320):
|
| 183 |
p = gr.Textbox(
|
|
|
|
| 9 |
HORDE_URL = "https://stablehorde.net/api/v2/generate/async"
|
| 10 |
HORDE_STATUS = "https://stablehorde.net/api/v2/generate/status/{id}"
|
| 11 |
|
| 12 |
+
# HF Space secret recommended for priority
|
| 13 |
HORDE_API_KEY = os.getenv("HORDE_API_KEY", "")
|
| 14 |
CLIENT_AGENT = "StitchMaster/0.1 (https://huggingface.co/spaces/your-space)"
|
| 15 |
|
| 16 |
DEFAULT_STEPS = 24
|
| 17 |
+
DEFAULT_W = 704 # keep defaults under the 715px threshold
|
| 18 |
+
DEFAULT_H = 704
|
| 19 |
POLL_INTERVAL = 2.5
|
| 20 |
POLL_TIMEOUT = 240 # bump if queues are long
|
| 21 |
MODEL = None # or set e.g. "SDXL 1.0"
|
| 22 |
|
| 23 |
def _headers():
|
| 24 |
+
# Always send an apikey; fallback to anonymous for testing
|
| 25 |
+
return {
|
| 26 |
+
"Client-Agent": CLIENT_AGENT,
|
| 27 |
+
"apikey": HORDE_API_KEY if HORDE_API_KEY else "0000000000"
|
| 28 |
+
}
|
| 29 |
|
| 30 |
# =========================
|
| 31 |
# Horde client with debugging
|
|
|
|
| 54 |
if model:
|
| 55 |
payload["models"] = [model]
|
| 56 |
|
| 57 |
+
# -------- Submit (with KudosUpfront fallback) --------
|
| 58 |
try:
|
| 59 |
+
submit = requests.post(HORDE_URL, json=payload, headers=_headers(), timeout=30)
|
| 60 |
+
|
| 61 |
+
# Auto-fallback if KudosUpfront required
|
| 62 |
+
if submit.status_code == 403:
|
| 63 |
+
try:
|
| 64 |
+
body = submit.json()
|
| 65 |
+
except Exception:
|
| 66 |
+
body = {"message": submit.text}
|
| 67 |
+
msg = (body.get("message") or "").lower()
|
| 68 |
+
rc = body.get("rc") or ""
|
| 69 |
+
if "kudos" in msg or rc == "KudosUpfront":
|
| 70 |
+
# Clamp params to stay under upfront limits
|
| 71 |
+
payload["params"]["steps"] = min(int(payload["params"]["steps"]), 30)
|
| 72 |
+
payload["params"]["width"] = min(int(payload["params"]["width"]), 704)
|
| 73 |
+
payload["params"]["height"] = min(int(payload["params"]["height"]), 704)
|
| 74 |
+
dbg.append("Fallback applied: steps<=30, width/height<=704. Retrying submit...")
|
| 75 |
+
submit = requests.post(HORDE_URL, json=payload, headers=_headers(), timeout=30)
|
| 76 |
+
|
| 77 |
dbg.append(f"SUBMIT status={submit.status_code}")
|
| 78 |
if submit.status_code >= 300:
|
| 79 |
dbg.append(f"SUBMIT body={submit.text[:500]}")
|
| 80 |
submit.raise_for_status()
|
| 81 |
+
|
| 82 |
submit_j = submit.json()
|
| 83 |
job_id = submit_j.get("id")
|
| 84 |
if not job_id:
|
| 85 |
dbg.append(f"SUBMIT json={submit_j}")
|
| 86 |
raise gr.Error("Horde submit succeeded but no job id returned.")
|
| 87 |
dbg.append(f"JOB id={job_id}")
|
| 88 |
+
|
| 89 |
+
except Exception:
|
| 90 |
dbg.append("SUBMIT exception:\n" + traceback.format_exc())
|
| 91 |
return None, "\n".join(dbg)
|
| 92 |
|
| 93 |
+
# -------- Poll --------
|
| 94 |
start = time.time()
|
| 95 |
while True:
|
| 96 |
try:
|
| 97 |
+
status_r = requests.get(HORDE_STATUS.format(id=job_id), headers=_headers(), timeout=30)
|
|
|
|
|
|
|
| 98 |
if status_r.status_code >= 300:
|
| 99 |
dbg.append(f"POLL status={status_r.status_code}")
|
| 100 |
dbg.append(f"POLL body={status_r.text[:500]}")
|
|
|
|
| 139 |
return None, "\n".join(dbg)
|
| 140 |
|
| 141 |
def generate_image(prompt, steps, size):
|
| 142 |
+
# size like "704x704"
|
| 143 |
try:
|
| 144 |
w, h = [int(x.strip()) for x in size.lower().split("x")]
|
| 145 |
except Exception:
|
| 146 |
w, h = DEFAULT_W, DEFAULT_H
|
| 147 |
img, debug = horde_txt2img(prompt, steps=steps, width=w, height=h)
|
| 148 |
if img is None:
|
|
|
|
| 149 |
gr.Warning("Generation failed. See debug log for details.")
|
| 150 |
return img, debug
|
| 151 |
|
|
|
|
| 177 |
}
|
| 178 |
"""
|
| 179 |
|
| 180 |
+
with gr.Blocks(css=CUSTOM_CSS, title="Image Checkpoints – Stable Horde") as demo:
|
| 181 |
gr.Markdown("### Image Checkpoints (Stable Horde)\nGenerate per-prompt frames. If a run fails, check the debug panel.")
|
| 182 |
|
| 183 |
with gr.Row():
|
| 184 |
steps = gr.Slider(8, 50, value=DEFAULT_STEPS, step=1, label="Steps (quality/time)")
|
| 185 |
size = gr.Dropdown(
|
| 186 |
+
choices=["512x512", "704x704", "704x512", "640x640"],
|
| 187 |
value=f"{DEFAULT_W}x{DEFAULT_H}",
|
| 188 |
label="Resolution"
|
| 189 |
)
|
| 190 |
|
| 191 |
+
# Shared debug panel
|
| 192 |
debug_box = gr.Code(label="Debug log", interactive=False)
|
| 193 |
|
| 194 |
prompt_boxes, gen_buttons, img_outputs = [], [], []
|
| 195 |
+
for i in range(1, 4 + 1):
|
| 196 |
with gr.Row():
|
| 197 |
with gr.Column(scale=1, min_width=320):
|
| 198 |
p = gr.Textbox(
|