Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,179 +1,157 @@
|
|
| 1 |
-
import
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
params
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
# ====== UI CALLBACK ======
|
| 86 |
-
def on_generate(prompt, image, seed, lock_longshot):
|
| 87 |
-
"""
|
| 88 |
-
lock_longshot is included so you can later inject constraints server-side if needed.
|
| 89 |
-
For now it simply forwards prompt & image to your Modal backend.
|
| 90 |
-
"""
|
| 91 |
-
# If user left seed blank, generate one
|
| 92 |
-
if seed is None or str(seed).strip() == "":
|
| 93 |
-
seed_val = random.randint(0, 2**31 - 1)
|
| 94 |
-
else:
|
| 95 |
-
# Gradio Number returns float; cast safely
|
| 96 |
-
try:
|
| 97 |
-
seed_val = int(seed)
|
| 98 |
-
except Exception:
|
| 99 |
-
seed_val = random.randint(0, 2**31 - 1)
|
| 100 |
-
|
| 101 |
-
# (Optional) reinforce long-shot constraints in prompt (safe no-op if you don’t need it)
|
| 102 |
-
if lock_longshot and prompt:
|
| 103 |
-
musts = [
|
| 104 |
-
"single continuous long shot",
|
| 105 |
-
"no cuts, no new shot, no angle switch",
|
| 106 |
-
"smooth camera motion (pan/tilt/zoom only)",
|
| 107 |
-
"unbroken continuity"
|
| 108 |
-
]
|
| 109 |
-
prompt = prompt.strip() + ". " + "; ".join(musts)
|
| 110 |
-
|
| 111 |
-
video_path_or_url = call_modal_backend(prompt, image, seed_val)
|
| 112 |
-
info = f"Seed: {seed_val}"
|
| 113 |
-
return video_path_or_url, info
|
| 114 |
-
|
| 115 |
-
# ====== STYLE ======
|
| 116 |
CUSTOM_CSS = """
|
| 117 |
.gradio-container { padding: 24px; }
|
| 118 |
|
| 119 |
-
/*
|
| 120 |
-
|
| 121 |
-
border-radius:
|
| 122 |
-
min-height:
|
| 123 |
-
font-size:
|
| 124 |
-
line-height: 1.
|
| 125 |
-
padding:
|
| 126 |
}
|
| 127 |
|
| 128 |
-
/*
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
min-width: 240px;
|
| 134 |
-
min-height: 240px;
|
| 135 |
}
|
| 136 |
|
| 137 |
-
/*
|
| 138 |
-
|
| 139 |
-
border-radius:
|
| 140 |
-
padding: 12px 24px;
|
| 141 |
-
font-size: 18px;
|
| 142 |
}
|
| 143 |
"""
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
gr.Markdown("### Stitch – turn prompt/image into a generated video (Modal backend)")
|
| 148 |
-
|
| 149 |
-
# Row 1: Big rounded prompt input
|
| 150 |
-
prompt_tb = gr.Textbox(
|
| 151 |
-
label=None,
|
| 152 |
-
placeholder="Prompt input",
|
| 153 |
-
lines=8,
|
| 154 |
-
elem_id="prompt-box"
|
| 155 |
-
)
|
| 156 |
|
| 157 |
-
#
|
| 158 |
-
with gr.Row():
|
| 159 |
-
with gr.Column(scale=1, min_width=300):
|
| 160 |
-
img_in = gr.Image(label="Add Image", type="pil", elem_id="add-image")
|
| 161 |
-
with gr.Column(scale=3, min_width=300):
|
| 162 |
-
with gr.Row():
|
| 163 |
-
seed_in = gr.Number(value=None, label="Seed (optional)")
|
| 164 |
-
lock_long = gr.Checkbox(value=True, label="Lock camera (long shot, no cuts)")
|
| 165 |
-
gen_btn = gr.Button("Generate", elem_id="gen-btn")
|
| 166 |
-
|
| 167 |
-
# Output
|
| 168 |
with gr.Row():
|
| 169 |
-
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
outputs=[video_out, info_out]
|
| 176 |
)
|
| 177 |
|
| 178 |
if __name__ == "__main__":
|
| 179 |
-
demo.launch()
|
|
|
|
| 1 |
+
import time, base64, io, os, requests
|
| 2 |
+
from typing import Optional
|
| 3 |
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# =========================
|
| 7 |
+
# Stable Horde config
|
| 8 |
+
# =========================
|
| 9 |
+
HORDE_URL = "https://stablehorde.net/api/v2/generate/async"
|
| 10 |
+
HORDE_STATUS = "https://stablehorde.net/api/v2/generate/status/{id}"
|
| 11 |
+
|
| 12 |
+
# Optional: set an API key in your Space secrets for better priority:
|
| 13 |
+
# Settings → Repository secrets → add HORDE_API_KEY
|
| 14 |
+
HORDE_API_KEY = os.getenv("HORDE_API_KEY", "")
|
| 15 |
+
|
| 16 |
+
DEFAULT_STEPS = 24
|
| 17 |
+
DEFAULT_W = 768
|
| 18 |
+
DEFAULT_H = 768
|
| 19 |
+
POLL_INTERVAL = 2.5 # seconds between polls
|
| 20 |
+
POLL_TIMEOUT = 180 # max seconds to wait for a job
|
| 21 |
+
MODEL = None # let Horde choose; or set e.g. "SDXL 1.0"
|
| 22 |
+
|
| 23 |
+
# =========================
|
| 24 |
+
# Horde client
|
| 25 |
+
# =========================
|
| 26 |
+
def horde_txt2img(prompt: str,
|
| 27 |
+
steps: int = DEFAULT_STEPS,
|
| 28 |
+
width: int = DEFAULT_W,
|
| 29 |
+
height: int = DEFAULT_H,
|
| 30 |
+
model: Optional[str] = MODEL) -> Image.Image:
|
| 31 |
+
if not prompt or not prompt.strip():
|
| 32 |
+
raise gr.Error("Please enter a prompt.")
|
| 33 |
+
|
| 34 |
+
payload = {
|
| 35 |
+
"prompt": prompt.strip(),
|
| 36 |
+
"params": {
|
| 37 |
+
"steps": int(steps),
|
| 38 |
+
"width": int(width),
|
| 39 |
+
"height": int(height),
|
| 40 |
+
"n": 1
|
| 41 |
+
},
|
| 42 |
+
"nsfw": False,
|
| 43 |
+
"censor_nsfw": True
|
| 44 |
+
}
|
| 45 |
+
if model:
|
| 46 |
+
payload["models"] = [model]
|
| 47 |
+
|
| 48 |
+
# Submit
|
| 49 |
+
headers = {"apikey": HORDE_API_KEY} if HORDE_API_KEY else {}
|
| 50 |
+
submit = requests.post(HORDE_URL, json=payload, headers=headers, timeout=30)
|
| 51 |
+
submit.raise_for_status()
|
| 52 |
+
job_id = submit.json()["id"]
|
| 53 |
+
|
| 54 |
+
# Poll
|
| 55 |
+
start = time.time()
|
| 56 |
+
while True:
|
| 57 |
+
status = requests.get(HORDE_STATUS.format(id=job_id), timeout=30).json()
|
| 58 |
+
if status.get("faulted"):
|
| 59 |
+
raise gr.Error(f"Horde job faulted: {status}")
|
| 60 |
+
if status.get("done"):
|
| 61 |
+
gens = status.get("generations") or []
|
| 62 |
+
if not gens:
|
| 63 |
+
raise gr.Error("Horde finished but returned no generations.")
|
| 64 |
+
b64 = gens[0]["img"]
|
| 65 |
+
img_bytes = base64.b64decode(b64)
|
| 66 |
+
return Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 67 |
+
|
| 68 |
+
if time.time() - start > POLL_TIMEOUT:
|
| 69 |
+
raise gr.Error("Timed out waiting for Horde. Try again or reduce steps/size.")
|
| 70 |
+
time.sleep(POLL_INTERVAL)
|
| 71 |
+
|
| 72 |
+
# Wrapper for Gradio (adds optional negative prompt + long-shot toggle if you want later)
|
| 73 |
+
def generate_image(prompt, steps, size):
|
| 74 |
+
# size like "768x768"
|
| 75 |
+
try:
|
| 76 |
+
w, h = [int(x.strip()) for x in size.lower().split("x")]
|
| 77 |
+
except Exception:
|
| 78 |
+
w, h = DEFAULT_W, DEFAULT_H
|
| 79 |
+
img = horde_txt2img(prompt, steps=steps, width=w, height=h)
|
| 80 |
+
return img
|
| 81 |
+
|
| 82 |
+
# =========================
|
| 83 |
+
# UI
|
| 84 |
+
# =========================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
CUSTOM_CSS = """
|
| 86 |
.gradio-container { padding: 24px; }
|
| 87 |
|
| 88 |
+
/* Rounded prompt boxes */
|
| 89 |
+
.prompt-box textarea {
|
| 90 |
+
border-radius: 18px !important;
|
| 91 |
+
min-height: 90px;
|
| 92 |
+
font-size: 16px;
|
| 93 |
+
line-height: 1.4;
|
| 94 |
+
padding: 14px 16px;
|
| 95 |
}
|
| 96 |
|
| 97 |
+
/* Pill buttons */
|
| 98 |
+
.pill button {
|
| 99 |
+
border-radius: 999px !important;
|
| 100 |
+
padding: 10px 18px;
|
| 101 |
+
font-size: 15px;
|
|
|
|
|
|
|
| 102 |
}
|
| 103 |
|
| 104 |
+
/* Blue-ish image boxes feel (border radius) */
|
| 105 |
+
.image-out .wrap, .image-out .svelte-1ipelgc {
|
| 106 |
+
border-radius: 22px !important;
|
|
|
|
|
|
|
| 107 |
}
|
| 108 |
"""
|
| 109 |
|
| 110 |
+
with gr.Blocks(css=CUSTOM_CSS, title="Stitch – Image Checkpoints (Stable Horde)") as demo:
|
| 111 |
+
gr.Markdown("### Image Checkpoints (Stable Horde) — generate per-prompt frames")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
+
# Global controls (applies to all generators)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
with gr.Row():
|
| 115 |
+
steps = gr.Slider(8, 50, value=DEFAULT_STEPS, step=1, label="Steps (quality/time)")
|
| 116 |
+
size = gr.Dropdown(
|
| 117 |
+
choices=["512x512", "768x768", "1024x576", "1024x768"],
|
| 118 |
+
value=f"{DEFAULT_W}x{DEFAULT_H}",
|
| 119 |
+
label="Resolution"
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# 4 rows: [Prompt + Button] | [Image Output]
|
| 123 |
+
prompt_boxes = []
|
| 124 |
+
gen_buttons = []
|
| 125 |
+
img_outputs = []
|
| 126 |
+
|
| 127 |
+
for i in range(1, 5):
|
| 128 |
+
with gr.Row():
|
| 129 |
+
with gr.Column(scale=1, min_width=320):
|
| 130 |
+
p = gr.Textbox(
|
| 131 |
+
placeholder=f"Prompt input (Image {i})",
|
| 132 |
+
lines=4,
|
| 133 |
+
label=None,
|
| 134 |
+
elem_classes=["prompt-box"]
|
| 135 |
+
)
|
| 136 |
+
b = gr.Button(f"Generate image {i}", elem_classes=["pill"])
|
| 137 |
+
with gr.Column(scale=2, min_width=380):
|
| 138 |
+
img = gr.Image(label=f"Image {i} output", type="pil", elem_classes=["image-out"])
|
| 139 |
+
prompt_boxes.append(p)
|
| 140 |
+
gen_buttons.append(b)
|
| 141 |
+
img_outputs.append(img)
|
| 142 |
+
|
| 143 |
+
# Wire callbacks
|
| 144 |
+
for i in range(4):
|
| 145 |
+
gen_buttons[i].click(
|
| 146 |
+
fn=generate_image,
|
| 147 |
+
inputs=[prompt_boxes[i], steps, size],
|
| 148 |
+
outputs=[img_outputs[i]]
|
| 149 |
+
)
|
| 150 |
|
| 151 |
+
gr.Markdown(
|
| 152 |
+
"> Tip: You can generate images independently per row. Steps ↑ = higher quality but slower. "
|
| 153 |
+
"Stable Horde is free (crowd GPUs), so expect occasional queue time."
|
|
|
|
| 154 |
)
|
| 155 |
|
| 156 |
if __name__ == "__main__":
|
| 157 |
+
demo.queue().launch()
|