loracraft 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. loracraft/__init__.py +1 -0
  2. loracraft/__main__.py +6 -0
  3. loracraft/imaging.py +15 -0
  4. loracraft/server/__init__.py +0 -0
  5. loracraft/server/app.py +110 -0
  6. loracraft/server/datasets.py +312 -0
  7. loracraft/server/db.py +136 -0
  8. loracraft/server/runs.py +425 -0
  9. loracraft/server/system.py +123 -0
  10. loracraft/server/tagging.py +367 -0
  11. loracraft/server/tagging_prompt.py +114 -0
  12. loracraft/trainer/__init__.py +7 -0
  13. loracraft/trainer/__main__.py +16 -0
  14. loracraft/trainer/bucketing.py +46 -0
  15. loracraft/trainer/config.py +85 -0
  16. loracraft/trainer/data.py +113 -0
  17. loracraft/trainer/device.py +29 -0
  18. loracraft/trainer/events.py +21 -0
  19. loracraft/trainer/loop.py +384 -0
  20. loracraft/trainer/sdxl.py +135 -0
  21. loracraft/vendor/__init__.py +0 -0
  22. loracraft/vendor/kohya/LICENSE +201 -0
  23. loracraft/vendor/kohya/README.md +17 -0
  24. loracraft/vendor/kohya/__init__.py +1 -0
  25. loracraft/vendor/kohya/hashing.py +48 -0
  26. loracraft/vendor/kohya/lora.py +306 -0
  27. loracraft/vendor/kohya/sdxl_keys.py +121 -0
  28. loracraft/vendor/kohya/train_functions.py +119 -0
  29. loracraft/web_dist/assets/bricolage-grotesque-latin-ext-wght-normal-CcLUaPy7.woff2 +0 -0
  30. loracraft/web_dist/assets/bricolage-grotesque-latin-wght-normal-DLoelf7F.woff2 +0 -0
  31. loracraft/web_dist/assets/bricolage-grotesque-vietnamese-wght-normal-BUzh504Q.woff2 +0 -0
  32. loracraft/web_dist/assets/index-Cc11kEew.css +1 -0
  33. loracraft/web_dist/assets/index-Chx5XfHC.js +6 -0
  34. loracraft/web_dist/assets/inter-cyrillic-ext-wght-normal-BOeWTOD4.woff2 +0 -0
  35. loracraft/web_dist/assets/inter-cyrillic-wght-normal-DqGufNeO.woff2 +0 -0
  36. loracraft/web_dist/assets/inter-greek-ext-wght-normal-DlzME5K_.woff2 +0 -0
  37. loracraft/web_dist/assets/inter-greek-wght-normal-CkhJZR-_.woff2 +0 -0
  38. loracraft/web_dist/assets/inter-latin-ext-wght-normal-DO1Apj_S.woff2 +0 -0
  39. loracraft/web_dist/assets/inter-latin-wght-normal-Dx4kXJAl.woff2 +0 -0
  40. loracraft/web_dist/assets/inter-vietnamese-wght-normal-CBcvBZtf.woff2 +0 -0
  41. loracraft/web_dist/assets/jetbrains-mono-cyrillic-wght-normal-D73BlboJ.woff2 +0 -0
  42. loracraft/web_dist/assets/jetbrains-mono-greek-wght-normal-Bw9x6K1M.woff2 +0 -0
  43. loracraft/web_dist/assets/jetbrains-mono-latin-ext-wght-normal-DBQx-q_a.woff2 +0 -0
  44. loracraft/web_dist/assets/jetbrains-mono-latin-wght-normal-B9CIFXIH.woff2 +0 -0
  45. loracraft/web_dist/assets/jetbrains-mono-vietnamese-wght-normal-Bt-aOZkq.woff2 +0 -0
  46. loracraft/web_dist/index.html +13 -0
  47. loracraft-0.1.0.dist-info/METADATA +160 -0
  48. loracraft-0.1.0.dist-info/RECORD +52 -0
  49. loracraft-0.1.0.dist-info/WHEEL +4 -0
  50. loracraft-0.1.0.dist-info/entry_points.txt +2 -0
  51. loracraft-0.1.0.dist-info/licenses/LICENSE +201 -0
  52. loracraft-0.1.0.dist-info/licenses/src/loracraft/vendor/kohya/LICENSE +201 -0
loracraft/__init__.py ADDED
@@ -0,0 +1 @@
1
+ """LoraCraft: LoRA training workbench."""
loracraft/__main__.py ADDED
@@ -0,0 +1,6 @@
1
+ """`python -m loracraft` starts the server, like the `loracraft` command."""
2
+
3
+ from .server.app import main
4
+
5
+ if __name__ == "__main__":
6
+ main()
loracraft/imaging.py ADDED
@@ -0,0 +1,15 @@
1
+ """Image helpers shared by the server and the trainer. Keep this free of heavy imports."""
2
+
3
+ from PIL import Image, ImageOps
4
+
5
+
6
+ def to_rgb(image: Image.Image) -> Image.Image:
7
+ """Upright RGB copy of `image`, with transparency flattened onto white.
8
+
9
+ A plain `convert("RGB")` drops the alpha channel, which turns transparent areas black.
10
+ """
11
+ image = ImageOps.exif_transpose(image)
12
+ if image.mode in ("RGBA", "LA", "PA") or (image.mode == "P" and "transparency" in image.info):
13
+ image = image.convert("RGBA")
14
+ return Image.alpha_composite(Image.new("RGBA", image.size, "white"), image).convert("RGB")
15
+ return image.convert("RGB")
File without changes
@@ -0,0 +1,110 @@
1
+ """FastAPI application: the JSON API under /api plus the built web UI."""
2
+
3
+ import sys
4
+ from contextlib import asynccontextmanager
5
+ from pathlib import Path
6
+ from urllib.parse import urlsplit
7
+
8
+ from fastapi import FastAPI, HTTPException, Request
9
+ from fastapi.responses import FileResponse, PlainTextResponse
10
+ from fastapi.staticfiles import StaticFiles
11
+
12
+ from . import datasets, db, runs, system, tagging
13
+
14
+ # Built by `npm run build` in web/ and shipped inside the package.
15
+ WEB_DIST = Path(__file__).resolve().parents[1] / "web_dist"
16
+ LOCAL_HOSTS = {"127.0.0.1", "localhost"}
17
+
18
+
19
+ @asynccontextmanager
20
+ async def lifespan(app: FastAPI):
21
+ db.init()
22
+ runs.queue.start()
23
+ yield
24
+ runs.queue.stop()
25
+
26
+
27
+ app = FastAPI(title="LoraCraft", lifespan=lifespan)
28
+
29
+
30
+ @app.middleware("http")
31
+ async def local_only(request: Request, call_next):
32
+ """Serve this machine's own pages only.
33
+
34
+ The API has no login, so a web page elsewhere must not reach it: neither by pointing its own
35
+ domain at 127.0.0.1 (a foreign Host header) nor by posting across origins (a foreign Origin).
36
+ """
37
+ origin = request.headers.get("origin")
38
+ if request.url.hostname not in LOCAL_HOSTS or (origin and urlsplit(origin).hostname not in LOCAL_HOSTS):
39
+ return PlainTextResponse("Forbidden", status_code=403)
40
+ return await call_next(request)
41
+
42
+
43
+ app.include_router(datasets.router)
44
+ app.include_router(runs.router)
45
+ app.include_router(system.router)
46
+ app.include_router(tagging.router)
47
+
48
+ if WEB_DIST.is_dir():
49
+ app.mount("/assets", StaticFiles(directory=WEB_DIST / "assets"), name="assets")
50
+
51
+ @app.get("/{path:path}", include_in_schema=False)
52
+ def spa(path: str):
53
+ """Serve the single-page app for every non-API route."""
54
+ if path.startswith("api/"):
55
+ raise HTTPException(404, "Not found")
56
+ # The path comes from the URL: never let it leave the build directory ("..", absolute paths).
57
+ file = (WEB_DIST / path).resolve()
58
+ inside = file.is_relative_to(WEB_DIST)
59
+ return FileResponse(file if path and inside and file.is_file() else WEB_DIST / "index.html")
60
+
61
+
62
+ def main() -> None:
63
+ import argparse
64
+
65
+ import uvicorn
66
+
67
+ parser = argparse.ArgumentParser(prog="loracraft", description="LoRA training workbench")
68
+ parser.add_argument("--port", type=int, default=8600, help="port to listen on (default: %(default)s)")
69
+ parser.add_argument(
70
+ "--window", action="store_true", help="open in a window of its own instead of serving a browser"
71
+ )
72
+ args = parser.parse_args()
73
+
74
+ if not WEB_DIST.is_dir():
75
+ print(f"The web UI is not built ({WEB_DIST} is missing): only the API will be served.", file=sys.stderr)
76
+ print(f"Data directory: {db.workspace()}")
77
+ if args.window:
78
+ run_in_window(args.port)
79
+ else:
80
+ uvicorn.run("loracraft.server.app:app", host="127.0.0.1", port=args.port)
81
+
82
+
83
+ def run_in_window(port: int) -> None:
84
+ """Serve from a background thread and show the UI in the system's web view until it is closed.
85
+
86
+ Closing the window stops the server only. A training run in progress is its own process and
87
+ carries on; the next start picks it up, and starts whatever was still queued.
88
+ """
89
+ import threading
90
+ import time
91
+
92
+ import uvicorn
93
+
94
+ try:
95
+ import webview
96
+ except ImportError:
97
+ sys.exit('--window needs pywebview: pip install "loracraft[app]"')
98
+
99
+ server = uvicorn.Server(uvicorn.Config("loracraft.server.app:app", host="127.0.0.1", port=port))
100
+ thread = threading.Thread(target=server.run, daemon=True)
101
+ thread.start()
102
+ while not server.started:
103
+ if not thread.is_alive():
104
+ sys.exit(1) # the server could not start, e.g. the port is taken; it has said why
105
+ time.sleep(0.05)
106
+
107
+ webview.create_window("LoraCraft", f"http://127.0.0.1:{port}", width=1440, height=900, min_size=(960, 600))
108
+ webview.start() # returns when the window is closed
109
+ server.should_exit = True
110
+ thread.join()
@@ -0,0 +1,312 @@
1
+ """Projects and their dataset images: import, captions, tag operations, thumbnails."""
2
+
3
+ import hashlib
4
+ import shutil
5
+ import sqlite3
6
+ import uuid
7
+ from pathlib import Path
8
+
9
+ from fastapi import APIRouter, Depends, HTTPException, UploadFile
10
+ from fastapi.responses import FileResponse
11
+ from PIL import Image, ImageOps
12
+ from pydantic import BaseModel
13
+
14
+ from ..imaging import to_rgb
15
+ from . import db
16
+
17
+ router = APIRouter(prefix="/api")
18
+
19
+ IMAGE_SUFFIXES = {".png", ".jpg", ".jpeg", ".webp", ".bmp"}
20
+ THUMB_SIZE = 384
21
+
22
+
23
+ # --- Tag helpers -----------------------------------------------------------
24
+
25
+
26
+ def split_tags(caption: str) -> list[str]:
27
+ return [t.strip() for t in caption.split(",") if t.strip()]
28
+
29
+
30
+ def join_tags(tags: list[str]) -> str:
31
+ seen = set()
32
+ unique = [t for t in tags if not (t in seen or seen.add(t))]
33
+ return ", ".join(unique)
34
+
35
+
36
+ # --- Projects --------------------------------------------------------------
37
+
38
+
39
+ class ProjectIn(BaseModel):
40
+ name: str
41
+ kind: str = "character"
42
+ trigger: str = ""
43
+ notes: str = ""
44
+
45
+
46
+ class ProjectPatch(BaseModel):
47
+ name: str | None = None
48
+ kind: str | None = None
49
+ trigger: str | None = None
50
+ notes: str | None = None
51
+
52
+
53
+ def _project(conn: sqlite3.Connection, project_id: int) -> sqlite3.Row:
54
+ row = conn.execute("SELECT * FROM projects WHERE id = ?", (project_id,)).fetchone()
55
+ if row is None:
56
+ raise HTTPException(404, "Project not found")
57
+ return row
58
+
59
+
60
+ @router.get("/projects")
61
+ def list_projects(conn: sqlite3.Connection = Depends(db.get_db)):
62
+ rows = conn.execute(
63
+ """SELECT p.*,
64
+ (SELECT COUNT(*) FROM images i WHERE i.project_id = p.id) AS image_count,
65
+ (SELECT COUNT(*) FROM runs r WHERE r.project_id = p.id) AS run_count,
66
+ (SELECT COUNT(*) FROM images i WHERE i.project_id = p.id AND i.caption = '') AS uncaptioned_count,
67
+ (SELECT COUNT(*) FROM runs r WHERE r.project_id = p.id AND r.status IN ('queued', 'running'))
68
+ AS active_runs,
69
+ (SELECT id FROM images i WHERE i.project_id = p.id ORDER BY id LIMIT 1) AS cover_image_id
70
+ FROM projects p ORDER BY p.id DESC"""
71
+ ).fetchall()
72
+ return [dict(r) for r in rows]
73
+
74
+
75
+ @router.post("/projects")
76
+ def create_project(body: ProjectIn, conn: sqlite3.Connection = Depends(db.get_db)):
77
+ cur = conn.execute(
78
+ "INSERT INTO projects (name, kind, trigger, notes) VALUES (?, ?, ?, ?)",
79
+ (body.name, body.kind, body.trigger, body.notes),
80
+ )
81
+ db.project_dir(cur.lastrowid)
82
+ return dict(_project(conn, cur.lastrowid))
83
+
84
+
85
+ @router.get("/projects/{project_id}")
86
+ def get_project(project_id: int, conn: sqlite3.Connection = Depends(db.get_db)):
87
+ return dict(_project(conn, project_id))
88
+
89
+
90
+ @router.patch("/projects/{project_id}")
91
+ def update_project(project_id: int, body: ProjectPatch, conn: sqlite3.Connection = Depends(db.get_db)):
92
+ _project(conn, project_id)
93
+ for field, value in body.model_dump(exclude_none=True).items():
94
+ conn.execute(f"UPDATE projects SET {field} = ? WHERE id = ?", (value, project_id))
95
+ return dict(_project(conn, project_id))
96
+
97
+
98
+ @router.delete("/projects/{project_id}")
99
+ def delete_project(project_id: int, conn: sqlite3.Connection = Depends(db.get_db)):
100
+ _project(conn, project_id)
101
+ _refuse_while_training(conn, project_id)
102
+ conn.execute("DELETE FROM projects WHERE id = ?", (project_id,))
103
+ shutil.rmtree(db.workspace() / "projects" / str(project_id), ignore_errors=True)
104
+ return {"ok": True}
105
+
106
+
107
+ def _refuse_while_training(conn: sqlite3.Connection, project_id: int) -> None:
108
+ """A queued or running training reads the project's image files; they must not vanish under it."""
109
+ active = conn.execute(
110
+ "SELECT 1 FROM runs WHERE project_id = ? AND status IN ('queued', 'running')", (project_id,)
111
+ ).fetchone()
112
+ if active:
113
+ raise HTTPException(409, "这个项目有训练正在进行或排队,先等它结束或停止它")
114
+
115
+
116
+ # --- Images ----------------------------------------------------------------
117
+
118
+
119
+ def _image(conn: sqlite3.Connection, image_id: int) -> sqlite3.Row:
120
+ row = conn.execute("SELECT * FROM images WHERE id = ?", (image_id,)).fetchone()
121
+ if row is None:
122
+ raise HTTPException(404, "Image not found")
123
+ return row
124
+
125
+
126
+ def image_path(row: sqlite3.Row) -> Path:
127
+ return db.project_dir(row["project_id"]) / "images" / row["filename"]
128
+
129
+
130
+ def _add_image(conn: sqlite3.Connection, project_id: int, name: str, data: bytes, caption: str) -> dict | None:
131
+ """Store one image in the project. Returns None when the same file is already there."""
132
+ digest = hashlib.sha256(data).hexdigest()
133
+ if conn.execute("SELECT 1 FROM images WHERE project_id = ? AND sha256 = ?", (project_id, digest)).fetchone():
134
+ return None
135
+
136
+ images_dir = db.project_dir(project_id) / "images"
137
+ stem, suffix = Path(name).stem, Path(name).suffix.lower()
138
+ target = images_dir / f"{stem}{suffix}"
139
+ counter = 1
140
+ while target.exists():
141
+ target = images_dir / f"{stem}_{counter}{suffix}"
142
+ counter += 1
143
+ target.write_bytes(data)
144
+
145
+ try:
146
+ with Image.open(target) as image:
147
+ width, height = ImageOps.exif_transpose(image).size
148
+ except Exception:
149
+ target.unlink(missing_ok=True)
150
+ raise HTTPException(400, f"Not a readable image: {name}") from None
151
+
152
+ cur = conn.execute(
153
+ "INSERT INTO images (project_id, filename, width, height, sha256, caption) VALUES (?, ?, ?, ?, ?, ?)",
154
+ (project_id, target.name, width, height, digest, join_tags(split_tags(caption))),
155
+ )
156
+ return dict(_image(conn, cur.lastrowid))
157
+
158
+
159
+ class _ImportResult:
160
+ """Outcome of importing several files. Each file is committed on its own, so a file that fails
161
+ later never leaves earlier ones on disk without a database row."""
162
+
163
+ def __init__(self) -> None:
164
+ self.added: list[dict] = []
165
+ self.duplicates = 0
166
+ self.unreadable: list[str] = []
167
+
168
+ def add(self, conn: sqlite3.Connection, project_id: int, name: str, data: bytes, caption: str) -> None:
169
+ try:
170
+ image = _add_image(conn, project_id, name, data, caption)
171
+ except HTTPException:
172
+ self.unreadable.append(name)
173
+ return
174
+ conn.commit()
175
+ if image is None:
176
+ self.duplicates += 1
177
+ else:
178
+ self.added.append(image)
179
+
180
+ def as_dict(self) -> dict:
181
+ return {"added": self.added, "skipped_duplicates": self.duplicates, "unreadable": self.unreadable}
182
+
183
+
184
+ @router.get("/projects/{project_id}/images")
185
+ def list_images(project_id: int, conn: sqlite3.Connection = Depends(db.get_db)):
186
+ _project(conn, project_id)
187
+ rows = conn.execute("SELECT * FROM images WHERE project_id = ? ORDER BY id", (project_id,)).fetchall()
188
+ return [dict(r) for r in rows]
189
+
190
+
191
+ @router.post("/projects/{project_id}/images")
192
+ def upload_images(project_id: int, files: list[UploadFile], conn: sqlite3.Connection = Depends(db.get_db)):
193
+ """Upload images. A .txt file with the same stem as an image is taken as its caption."""
194
+ # Not async: decoding and storing images is blocking work, which belongs in the thread pool.
195
+ _project(conn, project_id)
196
+ named = [(Path(f.filename).name, f) for f in files if f.filename]
197
+ captions = {
198
+ Path(name).stem: f.file.read().decode("utf-8", errors="replace")
199
+ for name, f in named
200
+ if Path(name).suffix.lower() == ".txt"
201
+ }
202
+
203
+ result = _ImportResult()
204
+ for name, f in named:
205
+ if Path(name).suffix.lower() in IMAGE_SUFFIXES:
206
+ # One file in memory at a time; the rest stay in the upload's temporary files.
207
+ result.add(conn, project_id, name, f.file.read(), captions.get(Path(name).stem, ""))
208
+ return result.as_dict()
209
+
210
+
211
+ class ImportFolder(BaseModel):
212
+ path: str
213
+
214
+
215
+ @router.post("/projects/{project_id}/images/import")
216
+ def import_folder(project_id: int, body: ImportFolder, conn: sqlite3.Connection = Depends(db.get_db)):
217
+ """Copy images (and sidecar .txt captions) from a folder on this machine into the project."""
218
+ _project(conn, project_id)
219
+ folder = Path(body.path)
220
+ if not folder.is_dir():
221
+ raise HTTPException(400, f"Not a folder: {body.path}")
222
+
223
+ result = _ImportResult()
224
+ for file in sorted(folder.iterdir()):
225
+ if file.suffix.lower() not in IMAGE_SUFFIXES or not file.is_file():
226
+ continue
227
+ sidecar = file.with_suffix(".txt")
228
+ caption = sidecar.read_text(encoding="utf-8", errors="replace") if sidecar.exists() else ""
229
+ result.add(conn, project_id, file.name, file.read_bytes(), caption)
230
+ return result.as_dict()
231
+
232
+
233
+ class ImagePatch(BaseModel):
234
+ caption: str
235
+
236
+
237
+ @router.patch("/images/{image_id}")
238
+ def update_image(image_id: int, body: ImagePatch, conn: sqlite3.Connection = Depends(db.get_db)):
239
+ _image(conn, image_id)
240
+ conn.execute("UPDATE images SET caption = ? WHERE id = ?", (join_tags(split_tags(body.caption)), image_id))
241
+ return dict(_image(conn, image_id))
242
+
243
+
244
+ class DeleteImages(BaseModel):
245
+ image_ids: list[int]
246
+
247
+
248
+ @router.post("/images/delete")
249
+ def delete_images(body: DeleteImages, conn: sqlite3.Connection = Depends(db.get_db)):
250
+ marks = ",".join("?" * len(body.image_ids))
251
+ rows = conn.execute(f"SELECT * FROM images WHERE id IN ({marks})", body.image_ids).fetchall()
252
+ for project_id in {row["project_id"] for row in rows}:
253
+ _refuse_while_training(conn, project_id)
254
+ conn.execute(f"DELETE FROM images WHERE id IN ({marks})", body.image_ids)
255
+ conn.commit() # rows first: a leftover file is harmless, a row without its file is not
256
+ for row in rows:
257
+ image_path(row).unlink(missing_ok=True)
258
+ _thumb_path(row).unlink(missing_ok=True)
259
+ return {"ok": True}
260
+
261
+
262
+ class TagEdit(BaseModel):
263
+ image_ids: list[int]
264
+ add: list[str] = []
265
+ add_position: str = "end" # start | end
266
+ remove: list[str] = []
267
+ replace: dict[str, str] = {}
268
+
269
+
270
+ @router.post("/tags/edit")
271
+ def edit_tags(body: TagEdit, conn: sqlite3.Connection = Depends(db.get_db)):
272
+ """Batch tag edit over a set of images: replace first, then remove, then add."""
273
+ updated = []
274
+ for image_id in body.image_ids:
275
+ row = _image(conn, image_id)
276
+ tags = [body.replace.get(t, t) for t in split_tags(row["caption"])]
277
+ tags = [t for t in tags if t not in body.remove]
278
+ new = [t.strip() for t in body.add if t.strip() and t.strip() not in tags]
279
+ tags = new + tags if body.add_position == "start" else tags + new
280
+ conn.execute("UPDATE images SET caption = ? WHERE id = ?", (join_tags(tags), image_id))
281
+ updated.append(dict(_image(conn, image_id)))
282
+ return updated
283
+
284
+
285
+ # --- Files -----------------------------------------------------------------
286
+
287
+
288
+ def _thumb_path(row: sqlite3.Row) -> Path:
289
+ return db.project_dir(row["project_id"]) / "thumbs" / f"{row['sha256'][:16]}.webp"
290
+
291
+
292
+ @router.get("/images/{image_id}/thumb")
293
+ def get_thumb(image_id: int, conn: sqlite3.Connection = Depends(db.get_db)):
294
+ row = _image(conn, image_id)
295
+ thumb = _thumb_path(row)
296
+ if not thumb.exists():
297
+ # Written under another name first: a request arriving meanwhile must not be served half a file.
298
+ partial = thumb.with_name(f"{thumb.name}.{uuid.uuid4().hex}.tmp")
299
+ with Image.open(image_path(row)) as image:
300
+ image = to_rgb(image)
301
+ image.thumbnail((THUMB_SIZE, THUMB_SIZE), Image.LANCZOS)
302
+ image.save(partial, "WEBP", quality=82)
303
+ try:
304
+ partial.replace(thumb)
305
+ except OSError: # another request finished the same thumbnail first
306
+ partial.unlink(missing_ok=True)
307
+ return FileResponse(thumb, media_type="image/webp", headers={"Cache-Control": "max-age=31536000, immutable"})
308
+
309
+
310
+ @router.get("/images/{image_id}/file")
311
+ def get_file(image_id: int, conn: sqlite3.Connection = Depends(db.get_db)):
312
+ return FileResponse(image_path(_image(conn, image_id)))
loracraft/server/db.py ADDED
@@ -0,0 +1,136 @@
1
+ """SQLite storage. Plain sqlite3, one connection per request."""
2
+
3
+ import os
4
+ import sqlite3
5
+ import sys
6
+ from contextlib import contextmanager
7
+ from pathlib import Path
8
+
9
+ SCHEMA = """
10
+ CREATE TABLE IF NOT EXISTS projects (
11
+ id INTEGER PRIMARY KEY AUTOINCREMENT, -- ids name files and URLs, so never reuse one
12
+ name TEXT NOT NULL,
13
+ kind TEXT NOT NULL DEFAULT 'character', -- character | style | concept
14
+ trigger TEXT NOT NULL DEFAULT '',
15
+ notes TEXT NOT NULL DEFAULT '',
16
+ tagging_profile TEXT NOT NULL DEFAULT '', -- TaggingProfile JSON, last used for auto-captioning
17
+ created_at TEXT NOT NULL DEFAULT (datetime('now'))
18
+ );
19
+
20
+ CREATE TABLE IF NOT EXISTS images (
21
+ id INTEGER PRIMARY KEY AUTOINCREMENT, -- ids name files and URLs, so never reuse one
22
+ project_id INTEGER NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
23
+ filename TEXT NOT NULL, -- file name inside the project's images directory
24
+ width INTEGER NOT NULL,
25
+ height INTEGER NOT NULL,
26
+ sha256 TEXT NOT NULL,
27
+ caption TEXT NOT NULL DEFAULT '',
28
+ caption_source TEXT NOT NULL DEFAULT '', -- which model and goal produced the caption, if any
29
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
30
+ UNIQUE (project_id, filename)
31
+ );
32
+ CREATE INDEX IF NOT EXISTS images_project ON images(project_id);
33
+
34
+ -- Captions proposed by a vision model, waiting for the user to accept or discard.
35
+ CREATE TABLE IF NOT EXISTS tag_suggestions (
36
+ image_id INTEGER PRIMARY KEY REFERENCES images(id) ON DELETE CASCADE,
37
+ caption TEXT NOT NULL,
38
+ groups TEXT NOT NULL, -- JSON: tags by category
39
+ issues TEXT NOT NULL, -- JSON: tags that were dropped, corrected or not recognised
40
+ model TEXT NOT NULL,
41
+ goal TEXT NOT NULL
42
+ );
43
+
44
+ CREATE TABLE IF NOT EXISTS runs (
45
+ id INTEGER PRIMARY KEY AUTOINCREMENT, -- ids name files and URLs, so never reuse one
46
+ project_id INTEGER NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
47
+ name TEXT NOT NULL,
48
+ status TEXT NOT NULL DEFAULT 'queued', -- queued | running | done | stopped | failed
49
+ config TEXT NOT NULL, -- TrainConfig JSON, including the dataset snapshot
50
+ notes TEXT NOT NULL DEFAULT '',
51
+ rating INTEGER NOT NULL DEFAULT 0,
52
+ best_checkpoint TEXT NOT NULL DEFAULT '', -- file name of the checkpoint the user picked
53
+ created_at TEXT NOT NULL DEFAULT (datetime('now')),
54
+ started_at TEXT,
55
+ finished_at TEXT
56
+ );
57
+ CREATE INDEX IF NOT EXISTS runs_project ON runs(project_id);
58
+
59
+ CREATE TABLE IF NOT EXISTS settings (
60
+ key TEXT PRIMARY KEY,
61
+ value TEXT NOT NULL
62
+ );
63
+ """
64
+
65
+
66
+ # Statements that bring a database created by an earlier version up to SCHEMA, oldest first.
67
+ # A database's `user_version` is the number of entries already applied to it; append, never edit.
68
+ MIGRATIONS: list[str] = []
69
+
70
+
71
+ def default_workspace() -> Path:
72
+ """The per-user data directory of this platform."""
73
+ if sys.platform == "win32":
74
+ base = os.environ.get("LOCALAPPDATA") or Path.home() / "AppData" / "Local"
75
+ elif sys.platform == "darwin":
76
+ base = Path.home() / "Library" / "Application Support"
77
+ else:
78
+ base = os.environ.get("XDG_DATA_HOME") or Path.home() / ".local" / "share"
79
+ return Path(base) / "loracraft"
80
+
81
+
82
+ def workspace() -> Path:
83
+ """Root directory for all user data: the database, images, runs."""
84
+ home = os.environ.get("LORACRAFT_HOME")
85
+ root = Path(home).resolve() if home else default_workspace()
86
+ root.mkdir(parents=True, exist_ok=True)
87
+ return root
88
+
89
+
90
+ def project_dir(project_id: int) -> Path:
91
+ path = workspace() / "projects" / str(project_id)
92
+ (path / "images").mkdir(parents=True, exist_ok=True)
93
+ (path / "thumbs").mkdir(exist_ok=True)
94
+ (path / "runs").mkdir(exist_ok=True)
95
+ return path
96
+
97
+
98
+ def connect() -> sqlite3.Connection:
99
+ # One connection per request; FastAPI may hop threads within a request.
100
+ conn = sqlite3.connect(workspace() / "loracraft.db", check_same_thread=False, timeout=60)
101
+ conn.row_factory = sqlite3.Row
102
+ conn.execute("PRAGMA foreign_keys = ON")
103
+ conn.execute("PRAGMA journal_mode = WAL")
104
+ return conn
105
+
106
+
107
+ @contextmanager
108
+ def session():
109
+ """A connection for use outside a request: commits on success, rolls back on error, always closes."""
110
+ conn = connect()
111
+ try:
112
+ with conn:
113
+ yield conn
114
+ finally:
115
+ conn.close()
116
+
117
+
118
+ def init() -> None:
119
+ with session() as conn:
120
+ fresh = conn.execute("SELECT 1 FROM sqlite_master WHERE name = 'projects'").fetchone() is None
121
+ conn.executescript(SCHEMA)
122
+ # A fresh database is created from SCHEMA, which already includes every migration.
123
+ version = len(MIGRATIONS) if fresh else conn.execute("PRAGMA user_version").fetchone()[0]
124
+ for statement in MIGRATIONS[version:]:
125
+ conn.executescript(statement)
126
+ conn.execute(f"PRAGMA user_version = {len(MIGRATIONS)}")
127
+
128
+
129
+ def get_db():
130
+ """FastAPI dependency: a connection that commits on success."""
131
+ conn = connect()
132
+ try:
133
+ yield conn
134
+ conn.commit()
135
+ finally:
136
+ conn.close()