forked from Akegarasu/lora-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
206 lines (166 loc) · 7.03 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import asyncio
import json
import os
import sys
from datetime import datetime
from pathlib import Path
from typing import Optional
import starlette.responses as starlette_responses
import toml
from fastapi import BackgroundTasks, FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, Response
from fastapi.staticfiles import StaticFiles
import mikazuki.utils as utils
import mikazuki.process as process
from mikazuki.log import log
from mikazuki.models import TaggerInterrogateRequest
from mikazuki.tagger.interrogator import (available_interrogators,
on_interrogate)
from mikazuki.tasks import tm
app = FastAPI()
avaliable_scripts = [
"networks/extract_lora_from_models.py",
"networks/extract_lora_from_dylora.py"
]
# fix mimetype error in some fucking systems
_origin_guess_type = starlette_responses.guess_type
def _hooked_guess_type(*args, **kwargs):
url = args[0]
r = _origin_guess_type(*args, **kwargs)
if url.endswith(".js"):
r = ("application/javascript", None)
elif url.endswith(".css"):
r = ("text/css", None)
return r
starlette_responses.guess_type = _hooked_guess_type
# cors middleware
if os.environ.get("ENABLE_APP_CORS") == "1":
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:8004"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.middleware("http")
async def add_cache_control_header(request, call_next):
response = await call_next(request)
response.headers["Cache-Control"] = "max-age=0"
# response.headers["Access-Control-Allow-Origin"] = "*"
return response
@app.post("/api/run")
async def create_toml_file(request: Request):
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
toml_file = os.path.join(os.getcwd(), f"config", "autosave", f"{timestamp}.toml")
toml_data = await request.body()
j = json.loads(toml_data.decode("utf-8"))
if not utils.validate_data_dir(j["train_data_dir"]):
return {
"status": "fail",
"detail": "训练数据集路径不存在或没有图片,请检查目录。"
}
suggest_cpu_threads = 8 if len(utils.get_total_images(j["train_data_dir"])) > 100 else 2
trainer_file = "./sd-scripts/train_network.py"
model_train_type = j.pop("model_train_type", "sd-lora")
if model_train_type == "sdxl-lora":
trainer_file = "./sd-scripts/sdxl_train_network.py"
elif model_train_type == "sd-dreambooth":
trainer_file = "./sd-scripts/train_db.py"
elif model_train_type == "sdxl-finetune":
trainer_file = "./sd-scripts/sdxl_train.py"
multi_gpu = j.pop("multi_gpu", False)
def is_promopt_like(s):
for p in ["--n", "--s", "--l", "--d"]:
if p in s:
return True
return False
sample_prompts = j.get("sample_prompts", None)
if sample_prompts is not None and not os.path.exists(sample_prompts) and is_promopt_like(sample_prompts):
sample_prompts_file = os.path.join(os.getcwd(), f"config", "autosave", f"{timestamp}-promopt.txt")
with open(sample_prompts_file, "w", encoding="utf-8") as f:
f.write(sample_prompts)
j["sample_prompts"] = sample_prompts_file
log.info(f"Wrote promopts to file {sample_prompts_file}")
with open(toml_file, "w") as f:
f.write(toml.dumps(j))
coro = asyncio.to_thread(process.run_train, toml_file, trainer_file, multi_gpu, suggest_cpu_threads)
asyncio.create_task(coro)
return {"status": "success"}
@app.post("/api/run_script")
async def run_script(request: Request, background_tasks: BackgroundTasks):
paras = await request.body()
j = json.loads(paras.decode("utf-8"))
script_name = j["script_name"]
if script_name not in avaliable_scripts:
return {"status": "fail"}
del j["script_name"]
result = []
for k, v in j.items():
result.append(f"--{k}")
if not isinstance(v, bool):
value = str(v)
if " " in value:
value = f'"{v}"'
result.append(value)
script_args = " ".join(result)
script_path = Path(os.getcwd()) / "sd-scripts" / script_name
cmd = f"{utils.python_bin} {script_path} {script_args}"
background_tasks.add_task(utils.run, cmd)
return {"status": "success"}
@app.post("/api/interrogate")
async def run_interrogate(req: TaggerInterrogateRequest, background_tasks: BackgroundTasks):
interrogator = available_interrogators.get(req.interrogator_model, available_interrogators["wd14-convnextv2-v2"])
background_tasks.add_task(on_interrogate,
image=None,
batch_input_glob=req.path,
batch_input_recursive=False,
batch_output_dir="",
batch_output_filename_format="[name].[output_extension]",
batch_output_action_on_conflict=req.batch_output_action_on_conflict,
batch_remove_duplicated_tag=True,
batch_output_save_json=False,
interrogator=interrogator,
threshold=req.threshold,
additional_tags=req.additional_tags,
exclude_tags=req.exclude_tags,
sort_by_alphabetical_order=False,
add_confident_as_weight=False,
replace_underscore=req.replace_underscore,
replace_underscore_excludes=req.replace_underscore_excludes,
escape_tag=req.escape_tag,
unload_model_after_running=True
)
return {"status": "success"}
# @app.get("/api/schema/{name}")
# async def get_schema(name: str):
# with open(os.path.join(os.getcwd(), "mikazuki", "schema", name), encoding="utf-8") as f:
# content = f.read()
# return Response(content=content, media_type="text/plain")
@app.get("/api/pick_file")
async def pick_file(picker_type: str):
if picker_type == "folder":
coro = asyncio.to_thread(utils.open_directory_selector, os.getcwd())
elif picker_type == "modelfile":
file_types = [("checkpoints", "*.safetensors;*.ckpt;*.pt"), ("all files", "*.*")]
coro = asyncio.to_thread(utils.open_file_selector, os.getcwd(), "Select file", file_types)
result = await coro
if result == "":
return {
"status": "fail"
}
return {
"status": "success",
"path": result
}
@app.get("/api/tasks")
async def get_tasks():
return tm.dump()
@app.get("/api/tasks/terminate/{task_id}")
async def terminate_task(task_id: str):
tm.terminate_task(task_id)
return {"status": "success"}
@app.get("/")
async def index():
return FileResponse("./frontend/dist/index.html")
app.mount("/", StaticFiles(directory="frontend/dist"), name="static")