Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
add script.py
Browse files
script.py
CHANGED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from huggingface_hub import snapshot_download, delete_repo, metadata_update
|
3 |
+
import uuid
|
4 |
+
import json
|
5 |
+
import yaml
|
6 |
+
import subprocess
|
7 |
+
|
8 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
9 |
+
HF_DATASET = os.environ.get("DATA_PATH")
|
10 |
+
|
11 |
+
|
12 |
+
def download_dataset(hf_dataset_path: str):
|
13 |
+
random_id = str(uuid.uuid4())
|
14 |
+
snapshot_download(
|
15 |
+
repo_id=hf_dataset_path,
|
16 |
+
token=HF_TOKEN,
|
17 |
+
local_dir=f"/tmp/{random_id}",
|
18 |
+
repo_type="dataset",
|
19 |
+
)
|
20 |
+
return f"/tmp/{random_id}"
|
21 |
+
|
22 |
+
|
23 |
+
def process_dataset(dataset_dir: str):
|
24 |
+
# dataset dir consists of images, config.yaml and a metadata.jsonl (optional) with fields: file_name, prompt
|
25 |
+
# generate .txt files with the same name as the images with the prompt as the content
|
26 |
+
# remove metadata.jsonl
|
27 |
+
# return the path to the processed dataset
|
28 |
+
|
29 |
+
# check if config.yaml exists
|
30 |
+
if not os.path.exists(os.path.join(dataset_dir, "config.yaml")):
|
31 |
+
raise ValueError("config.yaml does not exist")
|
32 |
+
|
33 |
+
# check if metadata.jsonl exists
|
34 |
+
if os.path.exists(os.path.join(dataset_dir, "metadata.jsonl")):
|
35 |
+
metadata = json.load(open(os.path.join(dataset_dir, "metadata.jsonl")))
|
36 |
+
for item in metadata:
|
37 |
+
txt_path = os.path.join(dataset_dir, item["file_name"])
|
38 |
+
txt_path = txt_path.rsplit(".", 1)[0] + ".txt"
|
39 |
+
with open(txt_path, "w") as f:
|
40 |
+
f.write(item["prompt"])
|
41 |
+
|
42 |
+
# remove metadata.jsonl
|
43 |
+
os.remove(os.path.join(dataset_dir, "metadata.jsonl"))
|
44 |
+
|
45 |
+
with open(os.path.join(dataset_dir, "config.yaml"), "r") as f:
|
46 |
+
config = yaml.safe_load(f)
|
47 |
+
|
48 |
+
# update config with new dataset
|
49 |
+
config["config"]["process"][0]["datasets"][0]["folder_path"] = dataset_dir
|
50 |
+
|
51 |
+
with open(os.path.join(dataset_dir, "config.yaml"), "w") as f:
|
52 |
+
yaml.dump(config, f)
|
53 |
+
|
54 |
+
return dataset_dir
|
55 |
+
|
56 |
+
|
57 |
+
def run_training(hf_dataset_path: str):
|
58 |
+
|
59 |
+
dataset_dir = download_dataset(hf_dataset_path)
|
60 |
+
dataset_dir = process_dataset(dataset_dir)
|
61 |
+
|
62 |
+
# run training
|
63 |
+
commands = "git clone https://github.com/ostris/ai-toolkit.git ai-toolkit && cd ai-toolkit && git submodule update --init --recursive"
|
64 |
+
subprocess.run(commands, shell=True)
|
65 |
+
|
66 |
+
commands = f"python run.py {os.path.join(dataset_dir, 'config.yaml')}"
|
67 |
+
process = subprocess.Popen(commands, shell=True, cwd="ai-toolkit", env=os.environ)
|
68 |
+
|
69 |
+
return process, dataset_dir
|
70 |
+
|
71 |
+
|
72 |
+
if __name__ == "__main__":
|
73 |
+
process, dataset_dir = run_training(HF_DATASET)
|
74 |
+
process.wait() # Wait for the training process to finish
|
75 |
+
|
76 |
+
with open(os.path.join(dataset_dir, "config.yaml"), "r") as f:
|
77 |
+
config = yaml.safe_load(f)
|
78 |
+
repo_id = config["config"]["process"][0]["save"]["hf_repo_id"]
|
79 |
+
|
80 |
+
metadata = {
|
81 |
+
"tags": [
|
82 |
+
"autotrain",
|
83 |
+
"spacerunner",
|
84 |
+
"text-to-image",
|
85 |
+
"flux",
|
86 |
+
"lora",
|
87 |
+
"diffusers",
|
88 |
+
"template:sd-lora",
|
89 |
+
]
|
90 |
+
}
|
91 |
+
metadata_update(repo_id, metadata, token=HF_TOKEN, repo_type="model", overwrite=True)
|
92 |
+
delete_repo(HF_DATASET, token=HF_TOKEN, repo_type="dataset", missing_ok=True)
|