Spaces:
Running on Zero
Forward the visitor's token at last, and get a trained LoRA into the rack
Browse filesGradio fills gr.Request and gr.Progress only among positional parameters and
stops at *args. run() has declared `request` after `*slot_values` since the
first commit, so it was always None: the visitor's x-ip-token never reached
the conditioner, and every encode was booked to this Space's shared IP
allowance - the "exceeded your ZeroGPU quota ... Authenticate" refusals in
the log. It now stands before `*slot_values`, as it does in the training
handler.
The first trained LoRA never reached the generation: the rack reads `file:`
only from the Space's outputs, and the path a later event receives is
Gradio's cached copy. The file is now checked and copied back first, a
trained or re-uploaded LoRA is placed in the rack by itself, and a truncated
download says so.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@@ -42,10 +42,16 @@ def run(
|
|
| 42 |
seed,
|
| 43 |
randomize,
|
| 44 |
context,
|
|
|
|
| 45 |
*slot_values,
|
| 46 |
-
request: gr.Request = None,
|
| 47 |
):
|
| 48 |
-
"""One Generate press. `slot_values` is `key, url, weight` per slot, in slot order.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
if not (prompt or "").strip():
|
| 50 |
raise gr.Error("MiniMax-H3 always takes a prompt, keyframes or not.")
|
| 51 |
|
|
@@ -151,59 +157,89 @@ def train_cost(images, steps):
|
|
| 151 |
)
|
| 152 |
|
| 153 |
|
| 154 |
-
def
|
| 155 |
-
"""
|
| 156 |
-
ip_token = request.headers.get("x-ip-token") if request is not None else None
|
| 157 |
-
try:
|
| 158 |
-
path, report = lora_train.train(
|
| 159 |
-
list(images or []), caption, ratio, int(rank), int(steps), ip_token=ip_token, progress=progress
|
| 160 |
-
)
|
| 161 |
-
except Exception as error:
|
| 162 |
-
raise gr.Error(f"{type(error).__name__}: {error}") from error
|
| 163 |
-
return path, report
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
def add_to_rack(path, *slot_keys):
|
| 167 |
-
"""A LoRA file into the first empty rack slot, as a Custom `file:` source.
|
| 168 |
|
| 169 |
-
|
|
|
|
|
|
|
| 170 |
"""
|
| 171 |
-
untouched = [gr.update()] * (3 * SLOTS)
|
| 172 |
-
if not path:
|
| 173 |
-
raise gr.Error("Train a LoRA, or load one trained before, first.")
|
| 174 |
-
free = [index for index, key in enumerate(slot_keys) if not key or key == lora_stack.NONE_CHOICE]
|
| 175 |
-
if not free:
|
| 176 |
-
raise gr.Error("Every rack slot is taken - empty one first.")
|
| 177 |
-
index = free[0]
|
| 178 |
-
keys, urls, weights = untouched[:SLOTS], untouched[SLOTS : 2 * SLOTS], untouched[2 * SLOTS :]
|
| 179 |
-
keys[index] = gr.update(value=lora_stack.CUSTOM_CHOICE)
|
| 180 |
-
urls[index] = gr.update(value=f"file:{path}", visible=True)
|
| 181 |
-
weights[index] = gr.update(value=0.8)
|
| 182 |
-
gr.Info(f"Added to rack slot {index + 1}. Put the caption's name in the prompt.")
|
| 183 |
-
return [*keys, *urls, *weights]
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
def keep_trained(path):
|
| 187 |
-
"""A LoRA file trained before, copied where a rack slot may read it from."""
|
| 188 |
import shutil
|
| 189 |
|
| 190 |
from safetensors import safe_open
|
| 191 |
|
| 192 |
if not path:
|
| 193 |
-
|
| 194 |
try:
|
| 195 |
with safe_open(path, framework="pt") as handle:
|
| 196 |
keys = list(handle.keys())
|
| 197 |
except Exception as error:
|
| 198 |
-
raise gr.Error(
|
|
|
|
|
|
|
|
|
|
| 199 |
if not keys or not all(".lora_A." in key or ".lora_B." in key for key in keys):
|
| 200 |
raise gr.Error("That file holds more than LoRA factors; it is not one this tab trained.")
|
| 201 |
-
os.
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
shutil.copyfile(path, target)
|
| 204 |
return target
|
| 205 |
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
def rack_updates(entries):
|
| 208 |
"""A recorded rack into updates for every slot's key, url and weight, in that order."""
|
| 209 |
slots = []
|
|
@@ -1159,16 +1195,17 @@ with gr.Blocks(title="MiniMax H3 Turbo LoRAs") as demo:
|
|
| 1159 |
|
| 1160 |
for widget in (train_images, train_steps):
|
| 1161 |
widget.change(fn=train_cost, inputs=[train_images, train_steps], outputs=[train_cost_html])
|
|
|
|
|
|
|
| 1162 |
train_button.click(
|
| 1163 |
fn=run_train,
|
| 1164 |
-
inputs=[train_images, train_caption, train_ratio, train_rank, train_steps],
|
| 1165 |
-
outputs=[trained_file, train_report],
|
| 1166 |
)
|
| 1167 |
-
rack_outputs = [*slot_keys, *slot_urls, *slot_weights]
|
| 1168 |
to_rack.click(fn=add_to_rack, inputs=[trained_file, *slot_keys], outputs=rack_outputs)
|
| 1169 |
-
# A file trained before is copied into the Space's own outputs - the only place a rack slot reads
|
| 1170 |
-
# and
|
| 1171 |
-
reuse_file.upload(fn=
|
| 1172 |
|
| 1173 |
|
| 1174 |
if __name__ == "__main__":
|
|
|
|
| 42 |
seed,
|
| 43 |
randomize,
|
| 44 |
context,
|
| 45 |
+
request: gr.Request,
|
| 46 |
*slot_values,
|
|
|
|
| 47 |
):
|
| 48 |
+
"""One Generate press. `slot_values` is `key, url, weight` per slot, in slot order.
|
| 49 |
+
|
| 50 |
+
`request` stands before `*slot_values` on purpose: Gradio fills special parameters only among the positional
|
| 51 |
+
ones and stops at `*args`, so placed after it - where it sat from the first commit until 2026-11-04 - it was
|
| 52 |
+
always None, and the visitor's `x-ip-token` never reached the conditioner. Every encode was booked to this
|
| 53 |
+
Space's shared IP allowance instead, which is what kept running out.
|
| 54 |
+
"""
|
| 55 |
if not (prompt or "").strip():
|
| 56 |
raise gr.Error("MiniMax-H3 always takes a prompt, keyframes or not.")
|
| 57 |
|
|
|
|
| 157 |
)
|
| 158 |
|
| 159 |
|
| 160 |
+
def keep_trained(path):
|
| 161 |
+
"""A LoRA file - fresh from training, or handed back - where a rack slot may read it from. Returns that path.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
+
Gradio serves a returned file out of its own cache, so the path a later event receives is never the one the
|
| 164 |
+
handler wrote. The rack reads `file:` only from the Space's outputs, and the first trained LoRA was refused
|
| 165 |
+
there on 2026-11-04 for exactly that reason - so the file is checked and brought back first.
|
| 166 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
import shutil
|
| 168 |
|
| 169 |
from safetensors import safe_open
|
| 170 |
|
| 171 |
if not path:
|
| 172 |
+
raise gr.Error("Train a LoRA, or load one trained before, first.")
|
| 173 |
try:
|
| 174 |
with safe_open(path, framework="pt") as handle:
|
| 175 |
keys = list(handle.keys())
|
| 176 |
except Exception as error:
|
| 177 |
+
raise gr.Error(
|
| 178 |
+
f"Not a readable LoRA file ({type(error).__name__}) - if it came from a download, it may not have "
|
| 179 |
+
"finished: a rank-16 LoRA is about 80 MB."
|
| 180 |
+
) from error
|
| 181 |
if not keys or not all(".lora_A." in key or ".lora_B." in key for key in keys):
|
| 182 |
raise gr.Error("That file holds more than LoRA factors; it is not one this tab trained.")
|
| 183 |
+
outputs = os.path.realpath(h3_engine.OUTPUT_DIR)
|
| 184 |
+
if os.path.realpath(path).startswith(outputs + os.sep):
|
| 185 |
+
return path
|
| 186 |
+
os.makedirs(outputs, exist_ok=True)
|
| 187 |
+
name = os.path.basename(path)
|
| 188 |
+
target = os.path.join(outputs, name if name.startswith("h3-identity-lora-") else f"uploaded-{name}")
|
| 189 |
shutil.copyfile(path, target)
|
| 190 |
return target
|
| 191 |
|
| 192 |
|
| 193 |
+
def rack_with(path, slot_keys):
|
| 194 |
+
"""`(updates, note)`: `path` placed in the first empty rack slot. Updates are key, url, weight for every slot."""
|
| 195 |
+
untouched = [gr.update()] * (3 * SLOTS)
|
| 196 |
+
keys, urls, weights = untouched[:SLOTS], untouched[SLOTS : 2 * SLOTS], untouched[2 * SLOTS :]
|
| 197 |
+
source = f"file:{path}"
|
| 198 |
+
free =[index for index, key in enumerate(slot_keys) if not key or key == lora_stack.NONE_CHOICE]
|
| 199 |
+
if not free:
|
| 200 |
+
return untouched, "Every rack slot is taken - empty one, then press Add to rack."
|
| 201 |
+
index = free[0]
|
| 202 |
+
keys[index] = gr.update(value=lora_stack.CUSTOM_CHOICE)
|
| 203 |
+
urls[index] = gr.update(value=source, visible=True)
|
| 204 |
+
weights[index] = gr.update(value=0.8)
|
| 205 |
+
return [*keys, *urls, *weights], f"In rack slot {index + 1} at weight 0.8 - put the caption's name in the prompt."
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
def run_train(images, caption, ratio, rank, steps, request: gr.Request, progress=gr.Progress(), *slot_keys):
|
| 209 |
+
"""One Train press: pictures and a caption into a LoRA file, placed in the rack straight away.
|
| 210 |
+
|
| 211 |
+
`request` and `progress` stand before `*slot_keys` because Gradio fills them only among positional parameters.
|
| 212 |
+
"""
|
| 213 |
+
ip_token = request.headers.get("x-ip-token") if request is not None else None
|
| 214 |
+
try:
|
| 215 |
+
path, report = lora_train.train(
|
| 216 |
+
list(images or []), caption, ratio, int(rank), int(steps), ip_token=ip_token, progress=progress
|
| 217 |
+
)
|
| 218 |
+
except Exception as error:
|
| 219 |
+
raise gr.Error(f"{type(error).__name__}: {error}") from error
|
| 220 |
+
updates, note = rack_with(path, slot_keys)
|
| 221 |
+
return [path, f"{report}\n{note}", *updates]
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
def add_to_rack(path, *slot_keys):
|
| 225 |
+
"""The LoRA in the Trained LoRA slot into the first empty rack slot. Outputs: key, url, weight per slot."""
|
| 226 |
+
updates, note = rack_with(keep_trained(path), slot_keys)
|
| 227 |
+
if "taken" in note:
|
| 228 |
+
raise gr.Error(note)
|
| 229 |
+
gr.Info(note)
|
| 230 |
+
return updates
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
def load_trained(path, *slot_keys):
|
| 234 |
+
"""A LoRA file trained before: checked, kept, and placed in the rack. Outputs: the file, then the slots."""
|
| 235 |
+
if not path:
|
| 236 |
+
return [gr.update(), *([gr.update()] * (3 * SLOTS))]
|
| 237 |
+
target = keep_trained(path)
|
| 238 |
+
updates, note = rack_with(target, slot_keys)
|
| 239 |
+
gr.Info(note)
|
| 240 |
+
return [target, *updates]
|
| 241 |
+
|
| 242 |
+
|
| 243 |
def rack_updates(entries):
|
| 244 |
"""A recorded rack into updates for every slot's key, url and weight, in that order."""
|
| 245 |
slots = []
|
|
|
|
| 1195 |
|
| 1196 |
for widget in (train_images, train_steps):
|
| 1197 |
widget.change(fn=train_cost, inputs=[train_images, train_steps], outputs=[train_cost_html])
|
| 1198 |
+
rack_outputs = [*slot_keys, *slot_urls, *slot_weights]
|
| 1199 |
+
# A trained LoRA goes into the rack by itself; the button is for putting it back after the rack was cleared.
|
| 1200 |
train_button.click(
|
| 1201 |
fn=run_train,
|
| 1202 |
+
inputs=[train_images, train_caption, train_ratio, train_rank, train_steps, *slot_keys],
|
| 1203 |
+
outputs=[trained_file, train_report, *rack_outputs],
|
| 1204 |
)
|
|
|
|
| 1205 |
to_rack.click(fn=add_to_rack, inputs=[trained_file, *slot_keys], outputs=rack_outputs)
|
| 1206 |
+
# A file trained before is checked, copied into the Space's own outputs - the only place a rack slot reads
|
| 1207 |
+
# `file:` from - and placed in the rack, exactly as a fresh one is.
|
| 1208 |
+
reuse_file.upload(fn=load_trained, inputs=[reuse_file, *slot_keys], outputs=[trained_file, *rack_outputs])
|
| 1209 |
|
| 1210 |
|
| 1211 |
if __name__ == "__main__":
|