Spaces:
Running on Zero
Running on Zero
Upload app.py
Browse files6 unmodified lines789101112606 unmodified lines61962062162262362426 unmodified lines65165265365465565669 unmodified lines72672772872973073121 unmodified lines7537547557567577586 unmodified linesfrom __future__ import annotationsimport osimport tempfileimport time606 unmodified linesreturn path, refined, gr.update(visible=bool(refined))def _fill_lora_slots(files, *current):"""Drop `.safetensors` files on the uploader and their paths land in the first free slots, so a local adapterneeds no typing at all."""26 unmodified linesswitches a slot off without clearing it. Adapters have to be trained against the `transformer_ref/` partition."""CSS = """.main.fillable { max-width: 1250px !important; }.dark .gradio-container { color: var(--body-text-color); }69 unmodified linessteps = gr.Slider(label="Steps", minimum=10, maximum=40, step=1, value=28)seed = gr.Number(label="Seed", value=42, precision=0)with gr.Column():result = gr.Video(label="Video + soundtrack")# An output, so it can be revealed only for a request that asked for a rewrite.21 unmodified lineslora_inputs = [field for pair in zip(lora_references, lora_scales) for field in pair]lora_upload.upload(_fill_lora_slots, [lora_upload, *lora_references], lora_references, api_name=False)# Same order as `generate`'s signature: the five leading columns first, then the remaining image slots, then the# LoRA fields the `*lora_fields` tail collects.request = [6 unmodified lines78910111213606 unmodified lines62062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667726 unmodified lines70470570670770870971071171271371469 unmodified lines78478578678778878979079179279379479579679721 unmodified lines8198208218228238248258268278288296 unmodified linesfrom __future__ import annotationsimport jsonimport osimport tempfileimport time606 unmodified linesreturn path, refined, gr.update(visible=bool(refined))# ----------------------------------------------------------------------------------------------------------------# Settings file# ----------------------------------------------------------------------------------------------------------------# Everything typed rather than uploaded, so a session can be picked up where it was left off. The references# themselves are deliberately left out: gradio hands them over as paths into a per-session temporary directory that# is gone by the next visit, so a saved path would restore as a dead file rather than as the image.SETTINGS_VERSION = 1SETTINGS_KEYS = (["prompt", "upsample", "canvas", "match", "duration", "steps", "seed"]+ [f"lora_{slot + 1}" for slot in range(LORA_SLOTS)]+ [f"lora_{slot + 1}_scale" for slot in range(LORA_SLOTS)])def save_settings(*values):"""Write the current controls to a `.json` and reveal it for download."""payload = {"version": SETTINGS_VERSION, "saved": time.strftime("%Y-%m-%d %H:%M:%S")}payload.update(dict(zip(SETTINGS_KEYS, values)))directory = os.path.join(tempfile.gettempdir(), "h3-settings")os.makedirs(directory, exist_ok=True)path = os.path.join(directory, f"h3-settings-{int(time.time())}.json")with open(path, "w", encoding="utf-8") as handle:json.dump(payload, handle, ensure_ascii=False, indent=2, default=str)return gr.update(value=path, visible=True)def load_settings(path):"""Restore the controls from a `.json`. A key the file does not carry leaves its control alone, so a settingsfile written by an older version of this Space still loads."""if not path:return [gr.update() for _ in SETTINGS_KEYS]try:with open(path, encoding="utf-8") as handle:payload = json.load(handle)except Exception as error:raise gr.Error(f"Файлът с настройки не се чете: `{type(error).__name__}: {error}`")if not isinstance(payload, dict):raise gr.Error("Това не е файл с настройки на този Space.")updates = []for key in SETTINGS_KEYS:value = payload.get(key)# An unknown canvas label would be rejected by the conditioner, which is the wrong place to find out.if value is None or (key == "canvas" and value not in CANVASES):updates.append(gr.update())else:updates.append(gr.update(value=value))return updatesdef _fill_lora_slots(files, *current):"""Drop `.safetensors` files on the uploader and their paths land in the first free slots, so a local adapterneeds no typing at all."""26 unmodified linesswitches a slot off without clearing it. Adapters have to be trained against the `transformer_ref/` partition."""SETTINGS_HELP = """Saves the prompt, the canvas, the sliders and the LoRA slots — everything typed rather thanuploaded. Images, audio and video are not saved: gradio keeps them in a temporary folder that is gone by the nextvisit, so a saved path would come back as a dead file."""CSS = """.main.fillable { max-width: 1250px !important; }.dark .gradio-container { color: var(--body-text-color); }69 unmodified linessteps = gr.Slider(label="Steps", minimum=10, maximum=40, step=1, value=28)seed = gr.Number(label="Seed", value=42, precision=0)with gr.Accordion("Settings file", open=False):gr.Markdown(SETTINGS_HELP)save = gr.Button("Save settings to .json", size="sm")settings_download = gr.File(label="Your settings", visible=False, interactive=False)settings_upload = gr.File(label="Load a settings .json", file_types=[".json"], type="filepath")with gr.Column():result = gr.Video(label="Video + soundtrack")# An output, so it can be revealed only for a request that asked for a rewrite.21 unmodified lineslora_inputs = [field for pair in zip(lora_references, lora_scales) for field in pair]lora_upload.upload(_fill_lora_slots, [lora_upload, *lora_references], lora_references, api_name=False)# Same order as `SETTINGS_KEYS`.settings_fields = [prompt, upsample, canvas, match, duration, steps, seed, *lora_references, *lora_scales]save.click(save_settings, settings_fields, settings_download, api_name=False)settings_upload.upload(load_settings, settings_upload, settings_fields, api_name=False)# Same order as `generate`'s signature: the five leading columns first, then the remaining image slots, then the# LoRA fields the `*lora_fields` tail collects.request = [
app.py+71