Spaces:
Configuration error
Configuration error
create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#https://github.com/huggingface/diffusers/tree/main/examples/dreambooth
|
| 2 |
+
#export MODEL_NAME="stabilityai/stable-diffusion-2-1-base"
|
| 3 |
+
#export INSTANCE_DIR="./data_example"
|
| 4 |
+
#export OUTPUT_DIR="./output_example"
|
| 5 |
+
|
| 6 |
+
#accelerate launch train_lora_dreambooth.py \
|
| 7 |
+
# --pretrained_model_name_or_path=$MODEL_NAME \
|
| 8 |
+
# --instance_data_dir=$INSTANCE_DIR \
|
| 9 |
+
# --output_dir=$OUTPUT_DIR \
|
| 10 |
+
# --instance_prompt="style of sks" \
|
| 11 |
+
# --resolution=512 \
|
| 12 |
+
# --train_batch_size=1 \
|
| 13 |
+
# --gradient_accumulation_steps=1 \
|
| 14 |
+
# --learning_rate=1e-4 \
|
| 15 |
+
# --lr_scheduler="constant" \
|
| 16 |
+
# --lr_warmup_steps=0 \
|
| 17 |
+
# --max_train_steps=30000
|
| 18 |
+
|
| 19 |
+
from diffusers import StableDiffusionPipeline
|
| 20 |
+
from lora_diffusion import monkeypatch_lora, tune_lora_scale
|
| 21 |
+
import torch
|
| 22 |
+
import os
|
| 23 |
+
#os.system('python file.py')
|
| 24 |
+
import subprocess
|
| 25 |
+
# If your shell script has shebang,
|
| 26 |
+
# you can omit shell=True argument.
|
| 27 |
+
subprocess.run("./run_lora_db.sh", shell=True)
|
| 28 |
+
|
| 29 |
+
#####
|
| 30 |
+
model_id = "stabilityai/stable-diffusion-2-1-base"
|
| 31 |
+
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16).to("cuda")
|
| 32 |
+
prompt = "style of sks, baby lion"
|
| 33 |
+
torch.manual_seed(1)
|
| 34 |
+
#image = pipe(prompt, num_inference_steps=50, guidance_scale= 7).images[0] #no need
|
| 35 |
+
#image # nice. diffusers are cool. #no need
|
| 36 |
+
finetuned_lora_weights = "./lora_weight.pt"
|
| 37 |
+
|
| 38 |
+
#####
|
| 39 |
+
#my fine tuned weights
|
| 40 |
+
def monkeypatching( alpha): #, prompt, pipe): finetuned_lora_weights
|
| 41 |
+
monkeypatch_lora(pipe.unet, torch.load(finetuned_lora_weights)) #"./lora_weight.pt"))
|
| 42 |
+
tune_lora_scale(pipe.unet, alpha) #1.00)
|
| 43 |
+
image = pipe(prompt, num_inference_steps=50, guidance_scale=7).images[0]
|
| 44 |
+
image.save("./illust_lora.jpg") #"./contents/illust_lora.jpg")
|
| 45 |
+
return image
|
| 46 |
+
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
with gr.Row():
|
| 49 |
+
in_images = gr.Image(label="Upload images to fine-tune for LORA")
|
| 50 |
+
#in_prompt = gr.Textbox(label="Enter a ")
|
| 51 |
+
in_steps = gr.Number(label="Enter number of steps")
|
| 52 |
+
in_alpha = gr.Slider(0.1,1.0, step=0.01, label="Set Alpha level - higher value has more chances to overfit")
|
| 53 |
+
b1 = gr.Button(value="Create LORA model")
|
| 54 |
+
with gr.Row():
|
| 55 |
+
out_image = gr.Image(label="Image generated by LORA model")
|
| 56 |
+
b1.click(fn = monkeypatching, inputs=in_alpha, outputs=out_image)
|
| 57 |
+
|
| 58 |
+
demo.launch(debug=True, show_error=True)
|