lora-ease-helper / script.py
multimodalart's picture
Update script.py
6a50ac3
raw
history blame
4.31 kB
import sys
import subprocess
from safetensors.torch import load_file
from diffusers import AutoPipelineForText2Image
from datasets import load_dataset
from huggingface_hub.repocard import RepoCard
import torch
import re
import argparse
def parse_arguments():
parser = argparse.ArgumentParser(description="Process script arguments.")
parser.add_argument('--dataset_name', required=True, help='Name of the dataset.')
parser.add_argument('--output_dir', required=True, help='Output directory.')
parser.add_argument('--num_new_tokens_per_abstraction', type=int, default=0, help='Number of new tokens per abstraction.')
parser.add_argument('--train_text_encoder_ti', action='store_true', help='Flag to train text encoder TI.')
return parser.parse_args()
def do_train(script_args):
# Pass all arguments to trainer.py
print("Starting training...")
subprocess.run(['python', 'trainer.py'] + script_args)
def do_inference(dataset_name, output_dir, num_tokens):
try:
print("Starting inference to generate example images...")
dataset = load_dataset(dataset_name)
pipe = AutoPipelineForText2Image.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
)
pipe = pipe.to("cuda")
pipe.load_lora_weights(f'{output_dir}/pytorch_lora_weights.safetensors')
prompts = dataset["train"]["prompt"]
widget_content = []
if(num_tokens > 0):
tokens_sequence = ''.join(f'<s{i}>' for i in range(num_tokens))
tokens_list = [f'<s{i}>' for i in range(num_tokens)]
state_dict = load_file(f"{output_dir}/embeddings.safetensors")
pipe.load_textual_inversion(state_dict["clip_l"], token=tokens_list, text_encoder=pipe.text_encoder, tokenizer=pipe.tokenizer)
pipe.load_textual_inversion(state_dict["clip_g"], token=tokens_list, text_encoder=pipe.text_encoder_2, tokenizer=pipe.tokenizer_2)
prompts = [prompt.replace("TOK", tokens_sequence) for prompt in prompts]
for i, prompt in enumerate(prompts):
image = pipe(prompt, num_inference_steps=25, guidance_scale=7.5).images[0]
filename = f"image-{i}.png"
image.save(f"{output_dir}/{filename}")
card_dict = {
"text": prompt,
"output": {
"url": filename
}
}
widget_content.append(card_dict)
repo_id = api.create_repo(f"{username}/{output_dir}", exist_ok=True).repo_id
with open(f'{output_dir}/README.md', 'r') as file:
readme_content = file.read()
readme_content = readme_content.replace(f'{output_dir}', f'{username}/{output_dir}')
card = RepoCard(readme_content)
card.data["widget"] = widget_content
card.save(f'{output_dir}/README.md')
except Exception as e:
print("Something went wrong with generating images, specifically: ", e)
from huggingface_hub import HfApi
api = HfApi()
username = api.whoami()["name"]
print("Starting upload...")
api.upload_folder(
folder_path=output_dir,
repo_id=f"{username}/{output_dir}",
repo_type="model",
)
print("Upload finished!")
import sys
import argparse
def main():
# Capture all arguments except the script name
script_args = sys.argv[1:]
# Create the argument parser
parser = argparse.ArgumentParser()
parser.add_argument('--dataset_name', required=True)
parser.add_argument('--output_dir', required=True)
parser.add_argument('--num_new_tokens_per_abstraction', type=int, default=0)
parser.add_argument('--train_text_encoder_ti', action='store_true')
# Parse known arguments
args, _ = parser.parse_known_args(script_args)
# Set num_tokens to 0 if '--train_text_encoder_ti' is not present
if not args.train_text_encoder_ti:
args.num_new_tokens_per_abstraction = 0
# Proceed with training and inference
do_train(script_args)
print("Training finished!")
do_inference(args.dataset_name, args.output_dir, args.num_new_tokens_per_abstraction)
print("All finished!")
if __name__ == "__main__":
main()