Import folders without a Hub repo as local models
The Import form gets "Add as local model": every file in the folder, kept as it is, named after the folder. A new models.source column (hub | local) marks them; local models skip Hub checks, metadata, downloads, updates and deep verify, and Verify checks existence and size only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
90d6007b55
commit
80ccf800df
21 files changed
+1747
-39
No files matched your search
+51
-2
@@ -7,14 +7,14 @@ import { hubMissingMessage } from "@/lib/hub/availability";
|
||||
import { checkFileAccess, getAuthorAvatar, getRepoInfo, listFiles } from "@/lib/hub/client";
|
||||
import { metadataColumns } from "@/lib/hub/refresh";
|
||||
import { hasOpenJob, queueDownload } from "@/lib/jobs";
|
||||
import { localFolderExists, requestFolderScan } from "@/lib/local-folders";
|
||||
import { getLocalFolder, localFolderExists, requestFolderScan } from "@/lib/local-folders";
|
||||
import { purgeModelRows } from "@/lib/model-purge";
|
||||
import { contentId } from "@/lib/update-diff";
|
||||
import { escapeGlob, isSelected, matchesAny, type SelectionRule } from "@/lib/selection";
|
||||
|
||||
export class ModelError extends Error {
|
||||
constructor(
|
||||
public code: "already_exists" | "empty_selection" | "not_found" | "busy" | "hub_missing" | "no_update",
|
||||
public code: "already_exists" | "empty_selection" | "not_found" | "busy" | "hub_missing" | "no_update" | "local_model",
|
||||
message: string,
|
||||
public status: number,
|
||||
) {
|
||||
@@ -91,6 +91,53 @@ export async function addModel(input: {
|
||||
return { id: modelId };
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a folder from the models folder that has no Hub repo as a local model: every file the scan
|
||||
* found, kept as it is. No pinned commit, so no Hub checks, updates, downloads or deep verify; its
|
||||
* name is the folder's path.
|
||||
*/
|
||||
export async function addLocalModel(input: { localPath: string }): Promise<{ id: string }> {
|
||||
const folder = await getLocalFolder(input.localPath);
|
||||
if (!folder) throw new ModelError("not_found", `Folder ${input.localPath} was not found in the models folder. Rescan and try again.`, 404);
|
||||
if (folder.files.length === 0) throw new ModelError("empty_selection", "This folder has no files.", 400);
|
||||
if (folder.files.length < folder.fileCount) {
|
||||
throw new ModelError("empty_selection", `This folder has ${folder.fileCount} files, more than a local model can list (${folder.files.length}).`, 400);
|
||||
}
|
||||
const usedBy = await db().query.models.findFirst({ columns: { repoId: true }, where: eq(models.localPath, folder.path) });
|
||||
if (usedBy) throw new ModelError("already_exists", `The folder ${folder.path} already belongs to ${usedBy.repoId}.`, 409);
|
||||
const existing = await db().query.models.findFirst({
|
||||
columns: { id: true },
|
||||
where: and(eq(models.repoType, "model"), eq(sql`lower(${models.repoId})`, folder.path.toLowerCase())),
|
||||
});
|
||||
if (existing) throw new ModelError("already_exists", `${folder.path} is already in the library.`, 409);
|
||||
|
||||
const d = db();
|
||||
const modelId = ulid();
|
||||
const now = new Date();
|
||||
const fileRows = folder.files.map((f) => ({ modelId, path: f.path, size: f.size, selected: true, state: "present" as const, checkedAt: folder.scannedAt }));
|
||||
await d.batch([
|
||||
d.insert(models).values({
|
||||
id: modelId,
|
||||
repoId: folder.path,
|
||||
source: "local",
|
||||
trackedRevision: "",
|
||||
pinnedSha: "",
|
||||
localPath: folder.path,
|
||||
status: "ready",
|
||||
// Nothing to fetch from the Hub.
|
||||
metadataFetchedAt: now,
|
||||
}),
|
||||
...chunk(fileRows, INSERT_CHUNK).map((c) => d.insert(modelFiles).values(c)),
|
||||
d.delete(localFolders).where(eq(localFolders.path, folder.path)), // no longer an import candidate
|
||||
]);
|
||||
return { id: modelId };
|
||||
}
|
||||
|
||||
/** Refuse Hub actions (download, update, adding files) for a local model. */
|
||||
export function assertHubModel(model: { source: string }): void {
|
||||
if (model.source === "local") throw new ModelError("local_model", "This is a local model without a Hugging Face repo.", 409);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a model. With `deleteFiles` the worker removes its files from disk first (a "delete" job;
|
||||
* files that aren't part of the repo are kept); without, only the library entry goes and the
|
||||
@@ -127,6 +174,7 @@ export async function applyUpdate(id: string): Promise<{ jobId: string; sha: str
|
||||
const d = db();
|
||||
const model = await d.query.models.findFirst({ where: eq(models.id, id) });
|
||||
if (!model) throw new ModelError("not_found", "Model not found.", 404);
|
||||
assertHubModel(model);
|
||||
const missing = hubMissingMessage(model);
|
||||
if (missing) throw new ModelError("hub_missing", missing, 409);
|
||||
if (await hasOpenJob(id)) {
|
||||
@@ -202,6 +250,7 @@ export async function addFilesToModel(modelId: string, paths: string[]): Promise
|
||||
const d = db();
|
||||
const model = await d.query.models.findFirst({ where: eq(models.id, modelId) });
|
||||
if (!model) throw new ModelError("not_found", "Model not found.", 404);
|
||||
assertHubModel(model);
|
||||
const missing = hubMissingMessage(model);
|
||||
if (missing) throw new ModelError("hub_missing", missing, 409);
|
||||
|
||||
|
||||
Reference in new issue
Block a user