from diffusers import StableDiffusionPipeline from lora_diffusion import monkeypatch_lora, tune_lora_scale import torch import os import gradio as gr import subprocess MODEL_NAME="stabilityai/stable-diffusion-2-1-base" INSTANCE_DIR="./data_example" OUTPUT_DIR="./output_example" model_id = "stabilityai/stable-diffusion-2-1-base" pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda") #prompt = "style of sks, baby lion" torch.manual_seed(1) #image = pipe(prompt, num_inference_steps=50, guidance_scale= 7).images[0] #no need #image # nice. diffusers are cool. #no need #finetuned_lora_weights = "./lora_weight.pt" #global var counter = 0 #Getting Lora fine-tuned weights def monkeypatching(alpha, in_prompt, wt): #, prompt, pipe): finetuned_lora_weights print("****** inside monkeypatching *******") print(f"in_prompt is - {str(in_prompt)}") global counter if counter == 0 : #if wt == "./lora_playgroundai_wt.pt" : monkeypatch_lora(pipe.unet, torch.load(wt)) #finetuned_lora_weights tune_lora_scale(pipe.unet, alpha) #1.00) counter +=1 #else: #monkeypatch_lora(pipe.unet, torch.load("./output_example/lora_weight.pt")) #finetuned_lora_weights #tune_lora_scale(pipe.unet, alpha) #1.00) #counter +=1 else : tune_lora_scale(pipe.unet, alpha) #1.00) prompt = "style of hclu, " + str(in_prompt) #"baby lion" image = pipe(prompt, num_inference_steps=50, guidance_scale=7).images[0] image.save("./illust_lora.jpg") #"./contents/illust_lora.jpg") return image def accelerate_train_lora(steps): print("*********** inside accelerate_train_lora ***********") #subprocess.run(accelerate launch {"./train_lora_dreambooth.py"} \ #subprocess.Popen(f'accelerate launch {"./train_lora_dreambooth.py"} \ os.system( f'accelerate launch {"./train_lora_dreambooth.py"} \ --pretrained_model_name_or_path={MODEL_NAME} \ --instance_data_dir={INSTANCE_DIR} \ --output_dir={OUTPUT_DIR} \ --instance_prompt="style of hclu" \ --resolution=512 \ --train_batch_size=1 \ --gradient_accumulation_steps=1 \ --learning_rate=1e-4 \ --lr_scheduler="constant" \ --lr_warmup_steps=0 \ --max_train_steps={int(steps)}') #,shell=True) #30000 print("*********** completing accelerate_train_lora ***********") lora_trained_weights = "./output_example/lora_weight.pt" return lora_trained_weights #"./output_example/lora_weight.pt" with gr.Blocks() as demo: gr.Markdown("""
You can skip the queue by duplicating this space and upgrading to gpu in settings:
") gr.Markdown("""NEW!! : I have fine-tuned the SD model for 15,000 steps using 100 PlaygroundAI images and LORA. You can load this trained model using the example component. Load the weight and start using the Space with the Inference button. Feel free to toggle the Alpha value.""") gr.Markdown( """**Main Features**