import argparse import gradio as gr from lib import ( Config, # disable_progress_bars, download_repo_files, generate, read_file, read_json, ) # Update refresh button hover text seed_js = """ (seed) => { const button = document.getElementById("refresh"); button.style.setProperty("--seed", `"${seed}"`); return seed; } """ # The CSS `content` attribute expects a string so we need to wrap the number in quotes refresh_seed_js = """ () => { const n = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER); const button = document.getElementById("refresh"); button.style.setProperty("--seed", `"${n}"`); return n; } """ # Update width and height on aspect ratio change aspect_ratio_js = """ (ar, w, h) => { if (!ar) return [w, h]; const [width, height] = ar.split(","); return [parseInt(width), parseInt(height)]; } """ # Show "Custom" aspect ratio when manually changing width or height, or one of the predefined ones custom_aspect_ratio_js = """ (w, h) => { if (w === 768 && h === 1344) return "768,1344"; if (w === 896 && h === 1152) return "896,1152"; if (w === 1024 && h === 1024) return "1024,1024"; if (w === 1152 && h === 896) return "1152,896"; if (w === 1344 && h === 768) return "1344,768"; return null; } """ # Inject prompts into random function random_prompt_js = f""" (prompt) => {{ const prompts = {read_json("data/prompts.json")}; const filtered = prompts.filter(p => p !== prompt); return filtered[Math.floor(Math.random() * filtered.length)]; }} """ # Transform the raw inputs before generation def generate_fn(*args, progress=gr.Progress(track_tqdm=True)): if len(args) > 0: prompt = args[0] else: prompt = None if prompt is None or prompt.strip() == "": raise gr.Error("You must enter a prompt") try: # if Config.ZERO_GPU: # progress((0, 100), desc="ZeroGPU init") images = generate(*args, Error=gr.Error, Info=gr.Info, progress=progress) except RuntimeError: raise gr.Error("Error: Please try again") return images with gr.Blocks( head=read_file("./partials/head.html"), css="./app.css", js="./app.js", theme=gr.themes.Default( # colors neutral_hue=gr.themes.colors.gray, primary_hue=gr.themes.colors.orange, secondary_hue=gr.themes.colors.blue, # sizing text_size=gr.themes.sizes.text_md, radius_size=gr.themes.sizes.radius_sm, spacing_size=gr.themes.sizes.spacing_md, # fonts font=[gr.themes.GoogleFont("Inter"), "sans-serif"], font_mono=[gr.themes.GoogleFont("Ubuntu Mono"), "monospace"], ).set( layout_gap="8px", block_shadow="0 0 #0000", block_shadow_dark="0 0 #0000", block_background_fill=gr.themes.colors.gray.c50, block_background_fill_dark=gr.themes.colors.gray.c900, ), ) as demo: gr.HTML(read_file("./partials/intro.html")) with gr.Tabs(): with gr.TabItem("đ Home"): with gr.Column(): output_images = gr.Gallery( elem_classes=["gallery"], show_share_button=False, object_fit="cover", interactive=False, show_label=False, label="Output", format="png", columns=2, ) prompt = gr.Textbox( placeholder="What do you want to see?", autoscroll=False, show_label=False, label="Prompt", max_lines=3, lines=3, ) with gr.Row(): generate_btn = gr.Button("Generate", variant="primary") random_btn = gr.Button( elem_classes=["icon-button", "popover"], variant="secondary", elem_id="random", min_width=0, value="đ˛", ) refresh_btn = gr.Button( elem_classes=["icon-button", "popover"], variant="secondary", elem_id="refresh", min_width=0, value="đ", ) clear_btn = gr.ClearButton( elem_classes=["icon-button", "popover"], components=[output_images], variant="secondary", elem_id="clear", min_width=0, value="đī¸", ) with gr.TabItem("âī¸ Settings", elem_id="settings"): # Prompt settings gr.HTML("