Spaces:
Running on Zero
Running on Zero
Upload app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ gradio API for every request; `reference_encoder` stays here, next to the autoen
|
|
| 7 |
|
| 8 |
from __future__ import annotations
|
| 9 |
|
|
|
|
| 10 |
import os
|
| 11 |
import tempfile
|
| 12 |
import time
|
|
@@ -619,6 +620,58 @@ def generate(
|
|
| 619 |
return path, refined, gr.update(visible=bool(refined))
|
| 620 |
|
| 621 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 622 |
def _fill_lora_slots(files, *current):
|
| 623 |
"""Drop `.safetensors` files on the uploader and their paths land in the first free slots, so a local adapter
|
| 624 |
needs no typing at all."""
|
|
@@ -651,6 +704,11 @@ LORA_HELP = """Each slot takes a Hugging Face repo (`owner/repo`), a file inside
|
|
| 651 |
switches a slot off without clearing it. Adapters have to be trained against the `transformer_ref/` partition.
|
| 652 |
"""
|
| 653 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
CSS = """
|
| 655 |
.main.fillable { max-width: 1250px !important; }
|
| 656 |
.dark .gradio-container { color: var(--body-text-color); }
|
|
@@ -726,6 +784,14 @@ with gr.Blocks(title="MiniMax-H3 Reference") as demo:
|
|
| 726 |
steps = gr.Slider(label="Steps", minimum=10, maximum=40, step=1, value=28)
|
| 727 |
seed = gr.Number(label="Seed", value=42, precision=0)
|
| 728 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 729 |
with gr.Column():
|
| 730 |
result = gr.Video(label="Video + soundtrack")
|
| 731 |
# An output, so it can be revealed only for a request that asked for a rewrite.
|
|
@@ -753,6 +819,11 @@ with gr.Blocks(title="MiniMax-H3 Reference") as demo:
|
|
| 753 |
lora_inputs = [field for pair in zip(lora_references, lora_scales) for field in pair]
|
| 754 |
lora_upload.upload(_fill_lora_slots, [lora_upload, *lora_references], lora_references, api_name=False)
|
| 755 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 756 |
# Same order as `generate`'s signature: the five leading columns first, then the remaining image slots, then the
|
| 757 |
# LoRA fields the `*lora_fields` tail collects.
|
| 758 |
request = [
|
|
|
|
| 7 |
|
| 8 |
from __future__ import annotations
|
| 9 |
|
| 10 |
+
import json
|
| 11 |
import os
|
| 12 |
import tempfile
|
| 13 |
import time
|
|
|
|
| 620 |
return path, refined, gr.update(visible=bool(refined))
|
| 621 |
|
| 622 |
|
| 623 |
+
# ----------------------------------------------------------------------------------------------------------------
|
| 624 |
+
# Settings file
|
| 625 |
+
# ----------------------------------------------------------------------------------------------------------------
|
| 626 |
+
# Everything typed rather than uploaded, so a session can be picked up where it was left off. The references
|
| 627 |
+
# themselves are deliberately left out: gradio hands them over as paths into a per-session temporary directory that
|
| 628 |
+
# is gone by the next visit, so a saved path would restore as a dead file rather than as the image.
|
| 629 |
+
|
| 630 |
+
SETTINGS_VERSION = 1
|
| 631 |
+
SETTINGS_KEYS = (
|
| 632 |
+
["prompt", "upsample", "canvas", "match", "duration", "steps", "seed"]
|
| 633 |
+
+ [f"lora_{slot + 1}" for slot in range(LORA_SLOTS)]
|
| 634 |
+
+ [f"lora_{slot + 1}_scale" for slot in range(LORA_SLOTS)]
|
| 635 |
+
)
|
| 636 |
+
|
| 637 |
+
|
| 638 |
+
def save_settings(*values):
|
| 639 |
+
"""Write the current controls to a `.json` and reveal it for download."""
|
| 640 |
+
payload = {"version": SETTINGS_VERSION, "saved": time.strftime("%Y-%m-%d %H:%M:%S")}
|
| 641 |
+
payload.update(dict(zip(SETTINGS_KEYS, values)))
|
| 642 |
+
|
| 643 |
+
directory = os.path.join(tempfile.gettempdir(), "h3-settings")
|
| 644 |
+
os.makedirs(directory, exist_ok=True)
|
| 645 |
+
path = os.path.join(directory, f"h3-settings-{int(time.time())}.json")
|
| 646 |
+
with open(path, "w", encoding="utf-8") as handle:
|
| 647 |
+
json.dump(payload, handle, ensure_ascii=False, indent=2, default=str)
|
| 648 |
+
return gr.update(value=path, visible=True)
|
| 649 |
+
|
| 650 |
+
|
| 651 |
+
def load_settings(path):
|
| 652 |
+
"""Restore the controls from a `.json`. A key the file does not carry leaves its control alone, so a settings
|
| 653 |
+
file written by an older version of this Space still loads."""
|
| 654 |
+
if not path:
|
| 655 |
+
return [gr.update() for _ in SETTINGS_KEYS]
|
| 656 |
+
try:
|
| 657 |
+
with open(path, encoding="utf-8") as handle:
|
| 658 |
+
payload = json.load(handle)
|
| 659 |
+
except Exception as error:
|
| 660 |
+
raise gr.Error(f"Файлът с настройки не се чете: `{type(error).__name__}: {error}`")
|
| 661 |
+
if not isinstance(payload, dict):
|
| 662 |
+
raise gr.Error("Това не е файл с настройки на този Space.")
|
| 663 |
+
|
| 664 |
+
updates = []
|
| 665 |
+
for key in SETTINGS_KEYS:
|
| 666 |
+
value = payload.get(key)
|
| 667 |
+
# An unknown canvas label would be rejected by the conditioner, which is the wrong place to find out.
|
| 668 |
+
if value is None or (key == "canvas" and value not in CANVASES):
|
| 669 |
+
updates.append(gr.update())
|
| 670 |
+
else:
|
| 671 |
+
updates.append(gr.update(value=value))
|
| 672 |
+
return updates
|
| 673 |
+
|
| 674 |
+
|
| 675 |
def _fill_lora_slots(files, *current):
|
| 676 |
"""Drop `.safetensors` files on the uploader and their paths land in the first free slots, so a local adapter
|
| 677 |
needs no typing at all."""
|
|
|
|
| 704 |
switches a slot off without clearing it. Adapters have to be trained against the `transformer_ref/` partition.
|
| 705 |
"""
|
| 706 |
|
| 707 |
+
SETTINGS_HELP = """Saves the prompt, the canvas, the sliders and the LoRA slots — everything typed rather than
|
| 708 |
+
uploaded. Images, audio and video are not saved: gradio keeps them in a temporary folder that is gone by the next
|
| 709 |
+
visit, so a saved path would come back as a dead file.
|
| 710 |
+
"""
|
| 711 |
+
|
| 712 |
CSS = """
|
| 713 |
.main.fillable { max-width: 1250px !important; }
|
| 714 |
.dark .gradio-container { color: var(--body-text-color); }
|
|
|
|
| 784 |
steps = gr.Slider(label="Steps", minimum=10, maximum=40, step=1, value=28)
|
| 785 |
seed = gr.Number(label="Seed", value=42, precision=0)
|
| 786 |
|
| 787 |
+
with gr.Accordion("Settings file", open=False):
|
| 788 |
+
gr.Markdown(SETTINGS_HELP)
|
| 789 |
+
save = gr.Button("Save settings to .json", size="sm")
|
| 790 |
+
settings_download = gr.File(label="Your settings", visible=False, interactive=False)
|
| 791 |
+
settings_upload = gr.File(
|
| 792 |
+
label="Load a settings .json", file_types=[".json"], type="filepath"
|
| 793 |
+
)
|
| 794 |
+
|
| 795 |
with gr.Column():
|
| 796 |
result = gr.Video(label="Video + soundtrack")
|
| 797 |
# An output, so it can be revealed only for a request that asked for a rewrite.
|
|
|
|
| 819 |
lora_inputs = [field for pair in zip(lora_references, lora_scales) for field in pair]
|
| 820 |
lora_upload.upload(_fill_lora_slots, [lora_upload, *lora_references], lora_references, api_name=False)
|
| 821 |
|
| 822 |
+
# Same order as `SETTINGS_KEYS`.
|
| 823 |
+
settings_fields = [prompt, upsample, canvas, match, duration, steps, seed, *lora_references, *lora_scales]
|
| 824 |
+
save.click(save_settings, settings_fields, settings_download, api_name=False)
|
| 825 |
+
settings_upload.upload(load_settings, settings_upload, settings_fields, api_name=False)
|
| 826 |
+
|
| 827 |
# Same order as `generate`'s signature: the five leading columns first, then the remaining image slots, then the
|
| 828 |
# LoRA fields the `*lora_fields` tail collects.
|
| 829 |
request = [
|