Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import requests
|
6 |
+
import subprocess
|
7 |
+
from subprocess import getoutput
|
8 |
+
from huggingface_hub import snapshot_download, HfApi, create_repo
|
9 |
+
|
10 |
+
api = HfApi()
|
11 |
+
|
12 |
+
hf_token = os.environ.get("HF_TOKEN_WITH_WRITE_PERMISSION")
|
13 |
+
|
14 |
+
def train_dreambooth_blora_sdxl(instance_data_dir, b_lora_trained_folder, instance_prompt, max_train_steps, checkpoint_steps):
|
15 |
+
|
16 |
+
script_filename = "train_dreambooth_b-lora_sdxl.py" # Assuming it's in the same folder
|
17 |
+
|
18 |
+
command = [
|
19 |
+
"accelerate",
|
20 |
+
"launch",
|
21 |
+
script_filename, # Use the local script
|
22 |
+
"--pretrained_model_name_or_path=stabilityai/stable-diffusion-xl-base-1.0",
|
23 |
+
f"--instance_data_dir={instance_data_dir}",
|
24 |
+
f"--output_dir={b_lora_trained_folder}",
|
25 |
+
f"--instance_prompt={instance_prompt}",
|
26 |
+
"--resolution=1024",
|
27 |
+
"--rank=64",
|
28 |
+
"--train_batch_size=1",
|
29 |
+
"--learning_rate=5e-5",
|
30 |
+
"--lr_scheduler=constant",
|
31 |
+
"--lr_warmup_steps=0",
|
32 |
+
f"--max_train_steps={max_train_steps}",
|
33 |
+
f"--checkpointing_steps={checkpoint_steps}",
|
34 |
+
"--seed=0",
|
35 |
+
"--gradient_checkpointing",
|
36 |
+
"--use_8bit_adam",
|
37 |
+
"--mixed_precision=fp16",
|
38 |
+
"--push_to_hub",
|
39 |
+
f"--hub_token={hf_token}"
|
40 |
+
]
|
41 |
+
|
42 |
+
try:
|
43 |
+
subprocess.run(command, check=True)
|
44 |
+
print("Training is finished!")
|
45 |
+
|
46 |
+
except subprocess.CalledProcessError as e:
|
47 |
+
print(f"An error occurred: {e}")
|
48 |
+
|
49 |
+
def main(image_path, b_lora_trained_folder, instance_prompt):
|
50 |
+
|
51 |
+
local_dir = "image_to_train"
|
52 |
+
# Check if the directory exists and create it if necessary
|
53 |
+
if not os.path.exists(local_dir):
|
54 |
+
os.makedirs(local_dir)
|
55 |
+
|
56 |
+
shutil.copy(image_path, local_dir)
|
57 |
+
print(f"source image has been copied in {local_dir} directory")
|
58 |
+
|
59 |
+
max_train_steps = 1000
|
60 |
+
checkpoint_steps = 500
|
61 |
+
|
62 |
+
train_dreambooth_blora_sdxl(local_dir, b_lora_trained_folder, instance_prompt, max_train_steps, checkpoint_steps)
|
63 |
+
|
64 |
+
your_username = api.whoami(token=hf_token)["name"]
|
65 |
+
|
66 |
+
return f"Done, your trained model has been stored in your models library: {your_username}/{b_lora_trained_folder}"
|
67 |
+
|
68 |
+
with gr.Blocks(css=css) as demo:
|
69 |
+
with gr.Column(elem_id="col-container"):
|
70 |
+
image = gr.Image(sources=[upload], type="filepath")
|
71 |
+
b_lora_name = gr.Textbox(label="b_lora_name", placeholder="b_lora_trained_folder")
|
72 |
+
instance_prompt = gr.Textbox(label="instance prompt", placeholder="[v42]")
|
73 |
+
train_btn = gr.Button("Train B-LoRa")
|
74 |
+
status = gr.Textbox(label="status")
|
75 |
+
|
76 |
+
train_btn.click(
|
77 |
+
fn = main,
|
78 |
+
inputs = [image, b_lora_name, instance_prompt],
|
79 |
+
outputs = [status]
|
80 |
+
)
|
81 |
+
|
82 |
+
demo.launch(debug=True)
|