Shalmoni commited on
Commit
a71fa9d
·
verified ·
1 Parent(s): 972f4dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -160
app.py CHANGED
@@ -1,179 +1,157 @@
1
- import io, uuid, base64, requests, random
 
2
  from PIL import Image
3
  import gradio as gr
4
 
5
- # ====== CONFIG ======
6
- # Your Modal endpoint (POST with multipart body; prompt/seed in querystring)
7
- MODAL_URL = "https://moonmath-ai--moonmath-i2v-backend-moonmathinference-run.modal.run"
8
- REQUEST_TIMEOUT_SEC = 600 # adjust if your backend needs longer
9
-
10
- # ====== BACKEND CALLER ======
11
- def call_modal_backend(prompt: str, image: Image.Image | None, seed: int | None):
12
- """
13
- Sends prompt + optional image to the Modal backend.
14
- Accepts:
15
- - raw MP4 bytes response
16
- - JSON with video_url or base64 video
17
- Returns a path or URL usable by gr.Video.
18
- """
19
- if not prompt and image is None:
20
- raise gr.Error("Please provide a prompt or upload an image.")
21
-
22
- # Build multipart body if image provided
23
- files = None
24
- if image is not None:
25
- buf = io.BytesIO()
26
- image.save(buf, format="PNG") # change to JPEG if your backend expects it
27
- buf.seek(0)
28
- files = {"image_bytes": ("input.png", buf, "image/png")}
29
-
30
- # Query string params
31
- params = {}
32
- if prompt:
33
- params["prompt"] = prompt
34
- if seed is not None:
35
- params["seed"] = str(seed)
36
-
37
- # Perform request
38
- res = requests.post(
39
- MODAL_URL,
40
- params=params,
41
- files=files,
42
- headers={"accept": "application/json"},
43
- timeout=REQUEST_TIMEOUT_SEC,
44
- )
45
- res.raise_for_status()
46
-
47
- ctype = (res.headers.get("content-type") or "").lower()
48
-
49
- # 1) Raw MP4 bytes directly
50
- if "video/mp4" in ctype or ctype.startswith("application/octet-stream"):
51
- mp4_path = f"out_{uuid.uuid4().hex[:8]}.mp4"
52
- with open(mp4_path, "wb") as f:
53
- f.write(res.content)
54
- return mp4_path
55
-
56
- # 2) JSON (URL or base64)
57
- if "application/json" in ctype:
58
- data = res.json()
59
- url = data.get("video_url") or data.get("url") or data.get("result", {}).get("video_url")
60
- if url:
61
- return url # gr.Video can stream a URL
62
-
63
- b64 = (
64
- data.get("video_b64")
65
- or data.get("video_bytes")
66
- or data.get("result", {}).get("video_b64")
67
- )
68
- if b64:
69
- if "," in b64: # strip data: header if present
70
- b64 = b64.split(",", 1)[1]
71
- blob = base64.b64decode(b64)
72
- mp4_path = f"out_{uuid.uuid4().hex[:8]}.mp4"
73
- with open(mp4_path, "wb") as f:
74
- f.write(blob)
75
- return mp4_path
76
-
77
- raise gr.Error(f"Backend JSON did not contain a video field. Keys: {list(data.keys())}")
78
-
79
- # 3) Fallback: write bytes as mp4
80
- mp4_path = f"out_{uuid.uuid4().hex[:8]}.mp4"
81
- with open(mp4_path, "wb") as f:
82
- f.write(res.content)
83
- return mp4_path
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
- /* Big rounded prompt box */
120
- #prompt-box textarea {
121
- border-radius: 28px !important;
122
- min-height: 180px;
123
- font-size: 18px;
124
- line-height: 1.45;
125
- padding: 18px 22px;
126
  }
127
 
128
- /* Rounded square image card */
129
- #add-image .wrap,
130
- #add-image .input-image,
131
- #add-image .empty {
132
- border-radius: 28px !important;
133
- min-width: 240px;
134
- min-height: 240px;
135
  }
136
 
137
- /* Pill generate button */
138
- #gen-btn button {
139
- border-radius: 999px !important;
140
- padding: 12px 24px;
141
- font-size: 18px;
142
  }
143
  """
144
 
145
- # ====== APP ======
146
- with gr.Blocks(css=CUSTOM_CSS, title="Stitch UI Modal Hook") as demo:
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
- # Row 2: Left image card, right controls (seed + generate)
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
- video_out = gr.Video(label="Output Video", interactive=False, autoplay=True)
170
- info_out = gr.Markdown("")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
 
172
- gen_btn.click(
173
- fn=on_generate,
174
- inputs=[prompt_tb, img_in, seed_in, lock_long],
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()