import gradio as gr from huggingface_hub import login, HfFileSystem, HfApi, ModelCard import os import random import spaces 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) fs = HfFileSystem(token=hf_token) api = HfApi() import torch from diffusers import StableDiffusionXLPipeline, AutoencoderKL device="cuda" if torch.cuda.is_available() else "cpu" def get_files(file_paths): last_files = {} # Dictionary to store the last file for each path for file_path in file_paths: # Split the file path into directory and file components directory, file_name = file_path.rsplit('/', 1) # Update the last file for the current path last_files[directory] = file_name # Extract the last files from the dictionary result = list(last_files.values()) return result def load_model(custom_model): 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.") # Get instance_prompt a.k.a trigger word card = ModelCard.load(custom_model) repo_data = card.data.to_dict() instance_prompt = repo_data.get("instance_prompt") if instance_prompt is not None: print(f"Trigger word: {instance_prompt}") else: instance_prompt = "no trigger word needed" print(f"Trigger word: no trigger word needed") # List all ".safetensors" files in repo sfts_available_files = fs.glob(f"{custom_model}/*safetensors") sfts_available_files = get_files(sfts_available_files) if sfts_available_files == []: sfts_available_files = ["NO SAFETENSORS FILE"] print(f"Safetensors available: {sfts_available_files}") return custom_model, "Model Ready", gr.update(choices=sfts_available_files, value=sfts_available_files[0], visible=True), gr.update(value=instance_prompt, visible=True) def custom_model_changed(custom_model, previous_model): if custom_model == "" and previous_model == "" : status_message = "" elif custom_model != previous_model: status_message = "model changed, please reload before any new run" else: status_message = "model ready" return status_message @spaces.GPU def infer (custom_model, weight_name, prompt, inf_steps, guidance_scale, width, height, seed, lora_weight, progress=gr.Progress(track_tqdm=True)): vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16) pipe = StableDiffusionXLPipeline.from_pretrained( "stabilityai/stable-diffusion-xl-base-1.0", vae=vae, torch_dtype=torch.float16, variant="fp16", use_safetensors=True ) pipe.to(device) if weight_name == "NO SAFETENSORS FILE": pipe.load_lora_weights( custom_model, low_cpu_mem_usage = True, use_auth_token = True ) else: pipe.load_lora_weights( custom_model, weight_name = weight_name, low_cpu_mem_usage = True, use_auth_token = True ) pipe.fuse_lora() if seed < 0 : seed = random.randint(0, 423538377342) generator = torch.Generator(device="cuda").manual_seed(seed) image = pipe( prompt=prompt, num_inference_steps=inf_steps, width=width, height=height, guidance_scale = guidance_scale, generator=generator, cross_attention_kwargs={"scale": lora_weight} ).images[0] pipe.unfuse_lora() return image, seed css=""" #col-container{ margin: 0 auto; max-width: 720px; text-align: left; } div#warning-duplicate { background-color: #ebf5ff; padding: 0 16px 16px; 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; } button#load_model_btn{ height: 46px; } #status_info{ font-size: 0.9em; } .custom-color { color: #030303 !important; } """ with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): if is_shared_ui: top_description = gr.HTML(f'''

Note: you might want to use a private custom LoRa model

To do so, duplicate the Space and run it on your own profile using your own access token and eventually a GPU (T4-small or A10G-small) for faster inference without waiting in the queue.

Duplicate this Space to start using private models and skip the queue

''', elem_id="warning-duplicate") gr.HTML("""

SD-XL Custom Model Inference

Use this demo to check results from your previously trained LoRa model.

""") with gr.Group(): with gr.Row(): with gr.Column(): if not is_shared_ui: your_username = api.whoami()["name"] my_models = api.list_models(author=your_username, filter=["diffusers", "stable-diffusion-xl", 'lora']) model_names = [item.modelId for item in my_models] if not is_shared_ui: custom_model = gr.Dropdown( label = "Your custom model ID", info="You can pick one of your private models", choices = model_names, allow_custom_value = True #placeholder = "username/model_id" ) else: 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.Dropdown( label="Safetensors file", #value="pytorch_lora_weights.safetensors", info="specify which one if model has several .safetensors files", allow_custom_value=True, visible = False ) with gr.Column(): with gr.Group(): load_model_btn = gr.Button("Load my model", elem_id="load_model_btn") previous_model = gr.Textbox( visible = False ) model_status = gr.Textbox( label = "model status", show_label = False, elem_id = "status_info" ) trigger_word = gr.Textbox(label="Trigger word", interactive=False, visible=False) prompt_in = gr.Textbox( label="Your Prompt", info = "Dont' forget to include your trigger word if necessary" ) with gr.Accordion("Advanced Settings", open=False): 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 ) with gr.Row(): width = gr.Slider( label="Width", minimum=256, maximum=1024, step=32, value=1024, ) height = gr.Slider( label="Height", minimum=256, maximum=1024, step=32, value=1024, ) with gr.Row(): seed = gr.Slider( label="Seed", info = "-1 denotes a random seed", minimum=-1, maximum=423538377342, step=1, value=-1 ) last_used_seed = gr.Number( label = "Last used seed", info = "the seed used in the last generation", ) lora_weight = gr.Slider( label="LoRa weigth", minimum=0.0, maximum=1.0, step=0.01, value=1.0 ) submit_btn = gr.Button("Submit") image_out = gr.Image(label="Image output") custom_model.blur( fn=custom_model_changed, inputs = [custom_model, previous_model], outputs = [model_status], queue = False ) load_model_btn.click( fn = load_model, inputs=[custom_model], outputs = [previous_model, model_status, weight_name, trigger_word], queue = False ) submit_btn.click( fn = infer, inputs = [custom_model, weight_name, prompt_in, inf_steps, guidance_scale, width, height, seed, lora_weight], outputs = [image_out, last_used_seed] ) demo.queue().launch()