import gradio as gr from huggingface_hub import login import os is_shared_ui = True if "fffiloni/sd-xl-custom-model" in os.environ['SPACE_ID'] else False hf_token = os.environ.get("HF_TOKEN") login(token=hf_token) import torch from diffusers import DiffusionPipeline, AutoencoderKL vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16) pipe = DiffusionPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True ) device="cuda" if torch.cuda.is_available() else "cpu" pipe.to(device) def load_model(custom_model, weight_name): if custom_model == "": gr.Warning("If you want to use a private model, you need to duplicate this space on your personal account.") raise gr.Error("You forgot to define Model ID.") # This is where you load your trained weights pipe.load_lora_weights(custom_model, weight_name=weight_name, use_auth_token=True) return "Model loaded!" def infer (prompt, inf_steps, guidance_scale, seed, lora_weight, progress=gr.Progress(track_tqdm=True)): generator = torch.Generator(device="cuda").manual_seed(seed) image = pipe( prompt=prompt, num_inference_steps=inf_steps, guidance_scale = guidance_scale, generator=generator, cross_attention_kwargs={"scale": lora_weight} ).images[0] return image css=""" #col-container{ margin: 0 auto; max-width: 680px; text-align: left; } div#warning-duplicate { background-color: #ebf5ff; padding: 0 10px 5px; margin: 20px 0; } div#warning-duplicate > .gr-prose > h2, div#warning-duplicate > .gr-prose > p { color: #0f4592!important; } div#warning-duplicate strong { color: #0f4592; } p.actions { display: flex; align-items: center; margin: 20px 0; } div#warning-duplicate .actions a { display: inline-block; margin-right: 10px; } """ with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): if is_shared_ui: top_description = gr.HTML(f'''
Use this demo to check results from your previously trained LoRa model.
""") with gr.Row(): with gr.Column(): custom_model = gr.Textbox(label="Your custom model ID", placeholder="your_username/your_trained_model_name", info="Make sure your model is set to PUBLIC ") weight_name = gr.Textbox(label="Safetensors file", value="pytorch_lora_weights.safetensors", info="specify which one if model has several .safetensors files") with gr.Column(): load_model_btn = gr.Button("Load my model") model_status = gr.Textbox(label="model status", interactive=False) prompt_in = gr.Textbox(label="Prompt") with gr.Row(): inf_steps = gr.Slider( label="Inference steps", minimum=12, maximum=50, step=1, value=25 ) guidance_scale = gr.Slider( label="Guidance scale", minimum=0.0, maximum=50.0, step=0.1, value=7.5 ) seed = gr.Slider( label="Seed", minimum=0, maximum=500000, step=1, value=42 ) lora_weight = gr.Slider( label="LoRa weigth", minimum=0.0, maximum=1.0, step=0.01, value=0.9 ) submit_btn = gr.Button("Submit") image_out = gr.Image(label="Image output") load_model_btn.click( fn = load_model, inputs=[custom_model, weight_name], outputs = [model_status] ) submit_btn.click( fn = infer, inputs = [prompt_in, inf_steps, guidance_scale, seed, lora_weight], outputs = [image_out] ) demo.queue().launch()