Import models that are already in the models folder
The worker scans MODELS_DIR (startup, every 2 min, on "Rescan") for <org>/<name> folders that no model uses and stores them in local_folders. The library lists them; Import opens Add model prefilled with the folder, the commit hf recorded and the files on disk preselected. The import job runs hf download into the existing folder: hf 2.1.1 hashes files it didn't download itself, keeps matching ones and re-downloads the rest (verified in Docker, including a corrupted file being replaced). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3603742637
commit
145818f149
16 files changed
+1418
-25
No files matched your search
@@ -2,7 +2,7 @@
|
||||
|
||||
import { XIcon } from "lucide-react";
|
||||
import Link from "next/link";
|
||||
import { useMemo, useState } from "react";
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { FileRoleBadge } from "@/components/labels";
|
||||
import { TagBadge } from "@/components/tag-badge";
|
||||
@@ -32,15 +32,21 @@ async function postJson<T>(url: string, body: unknown): Promise<T> {
|
||||
return json;
|
||||
}
|
||||
|
||||
/** A folder from the models folder to import (found by the worker's scan). */
|
||||
export type ImportFolder = { path: string; commit: string | null; files: { path: string; size: number }[]; fileCount: number };
|
||||
|
||||
export function AddModelForm({
|
||||
freeBytes,
|
||||
onAdded,
|
||||
importFolder,
|
||||
}: {
|
||||
freeBytes: number | null;
|
||||
onAdded: (modelId: string) => void;
|
||||
/** Import mode: look up this folder's repo and preselect the files that are already there. */
|
||||
importFolder?: ImportFolder;
|
||||
}) {
|
||||
const [repo, setRepo] = useState("");
|
||||
const [revision, setRevision] = useState("main");
|
||||
const [repo, setRepo] = useState(importFolder?.path ?? "");
|
||||
const [revision, setRevision] = useState(importFolder?.commit ?? "main");
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [result, setResult] = useState<LookupResult | null>(null);
|
||||
@@ -51,15 +57,19 @@ export function AddModelForm({
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [tags, setTags] = useState<{ name: string; color?: string | null }[]>([]);
|
||||
|
||||
async function lookup(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
// Local files of the imported folder by path.
|
||||
const onDisk = useMemo(() => new Map((importFolder?.files ?? []).map((f) => [f.path, f.size])), [importFolder]);
|
||||
|
||||
async function lookup(e?: React.FormEvent) {
|
||||
e?.preventDefault();
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
const r = await postJson<LookupResult>("/api/hub/lookup", { repo, revision });
|
||||
setResult(r);
|
||||
setRevision(r.revision);
|
||||
setChecked(new Set());
|
||||
// Import: preselect every repo file that's already in the folder.
|
||||
setChecked(new Set(r.files.filter((f) => onDisk.has(f.path)).map((f) => f.path)));
|
||||
setIncludeText("");
|
||||
setExcludeText("");
|
||||
setTags([]);
|
||||
@@ -71,6 +81,15 @@ export function AddModelForm({
|
||||
}
|
||||
}
|
||||
|
||||
const autoLookup = useRef(false);
|
||||
useEffect(() => {
|
||||
if (importFolder && !autoLookup.current) {
|
||||
autoLookup.current = true;
|
||||
void lookup();
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps -- run once for the folder being imported
|
||||
}, [importFolder]);
|
||||
|
||||
const includePatterns = useMemo(() => parsePatternList(includeText), [includeText]);
|
||||
const excludePatterns = useMemo(() => parsePatternList(excludeText), [excludeText]);
|
||||
|
||||
@@ -106,7 +125,13 @@ export function AddModelForm({
|
||||
const entries = useMemo(() => groupSplitFiles(files), [files]);
|
||||
const selectedEntries = entries.filter((e) => e.parts.every((f) => f.selected)).length;
|
||||
const totalBytes = selected.reduce((s, f) => s + f.size, 0);
|
||||
const notEnoughSpace = freeBytes != null && totalBytes > freeBytes;
|
||||
// Files already on disk with the right size are verified by hf, not downloaded again.
|
||||
const present = selected.filter((f) => onDisk.get(f.path) === f.size);
|
||||
const toDownload = totalBytes - present.reduce((s, f) => s + f.size, 0);
|
||||
const notEnoughSpace = freeBytes != null && toDownload > freeBytes;
|
||||
const repoPaths = useMemo(() => new Set(result?.files.map((f) => f.path)), [result]);
|
||||
const sizeDiffers = selected.filter((f) => onDisk.has(f.path) && onDisk.get(f.path) !== f.size).length;
|
||||
const notInRepo = importFolder ? importFolder.files.filter((f) => !repoPaths.has(f.path)).length : 0;
|
||||
|
||||
function toggle(paths: string[], on: boolean) {
|
||||
setChecked((prev) => {
|
||||
@@ -142,8 +167,9 @@ export function AddModelForm({
|
||||
include: [...includePatterns, ...[...checked].sort().map(escapeGlob)],
|
||||
exclude: excludePatterns,
|
||||
tags: tags.map((t) => t.name),
|
||||
localPath: importFolder?.path,
|
||||
});
|
||||
toast.success(`${result.repoId} added, download queued`);
|
||||
toast.success(importFolder ? `${result.repoId} imported, verifying files` : `${result.repoId} added, download queued`);
|
||||
onAdded(id);
|
||||
} catch (err) {
|
||||
toast.error(err instanceof Error ? err.message : String(err));
|
||||
@@ -156,8 +182,12 @@ export function AddModelForm({
|
||||
<div className="flex flex-col gap-4">
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Hugging Face repository</CardTitle>
|
||||
<CardDescription>Repo ID like unsloth/Qwen3-8B-GGUF, or paste the repo URL.</CardDescription>
|
||||
<CardTitle>{importFolder ? `Import ${importFolder.path}` : "Hugging Face repository"}</CardTitle>
|
||||
<CardDescription>
|
||||
{importFolder
|
||||
? `The folder's files are matched against this Hub repo${importFolder.commit ? " at the commit hf recorded when it downloaded them" : ""}. Change the repo or revision if they don't fit.`
|
||||
: "Repo ID like unsloth/Qwen3-8B-GGUF, or paste the repo URL."}
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<form onSubmit={lookup} className="flex flex-col gap-3 sm:flex-row sm:items-end">
|
||||
@@ -220,6 +250,23 @@ export function AddModelForm({
|
||||
</CardDescription>
|
||||
</CardHeader>
|
||||
<CardContent className="flex flex-col gap-4">
|
||||
{importFolder && !result.existingModelId && (
|
||||
<div className="rounded-md bg-muted px-3 py-2 text-sm">
|
||||
<p>
|
||||
<span className="font-medium">
|
||||
{present.length} of {selected.length} selected files are already on disk
|
||||
</span>{" "}
|
||||
and are verified by hash (hf only downloads files that are missing or different).
|
||||
{sizeDiffers > 0 && ` ${sizeDiffers} differ in size and will be downloaded again.`}
|
||||
</p>
|
||||
{notInRepo > 0 && (
|
||||
<p className="mt-1 text-muted-foreground">
|
||||
{notInRepo} file{notInRepo === 1 ? " in the folder isn't" : "s in the folder aren't"} part of the repo at this
|
||||
commit; {notInRepo === 1 ? "it stays" : "they stay"} untouched.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{result.existingModelId && (
|
||||
<p className="rounded-md bg-muted px-3 py-2 text-sm">
|
||||
This repo is already in the library.{" "}
|
||||
@@ -353,6 +400,7 @@ export function AddModelForm({
|
||||
</span>
|
||||
)}
|
||||
{excluded && <span className="text-xs text-muted-foreground">(excluded)</span>}
|
||||
{importFolder && <DiskState parts={parts} onDisk={onDisk} />}
|
||||
</span>
|
||||
</TableCell>
|
||||
<TableCell className="text-right tabular-nums">{formatBytes(e.size)}</TableCell>
|
||||
@@ -373,6 +421,7 @@ export function AddModelForm({
|
||||
{selectedEntries} file{selectedEntries === 1 ? "" : "s"} · {formatBytes(totalBytes)}
|
||||
</span>{" "}
|
||||
selected
|
||||
{importFolder && <span className="text-muted-foreground"> · {formatBytes(toDownload)} to download</span>}
|
||||
{freeBytes != null && (
|
||||
<span className={notEnoughSpace ? "ml-2 text-destructive" : "ml-2 text-muted-foreground"}>
|
||||
({formatBytes(freeBytes)} free on the models volume)
|
||||
@@ -388,7 +437,7 @@ export function AddModelForm({
|
||||
onClick={addToLibrary}
|
||||
disabled={selected.length === 0 || submitting || !!result.existingModelId}
|
||||
>
|
||||
{submitting ? "Adding…" : "Add & download"}
|
||||
{submitting ? "Adding…" : importFolder ? "Import & verify" : "Add & download"}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
@@ -398,3 +447,21 @@ export function AddModelForm({
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** Import mode: is this entry already in the folder (and the right size)? */
|
||||
function DiskState({ parts, onDisk }: { parts: { path: string; size: number }[]; onDisk: Map<string, number> }) {
|
||||
const here = parts.filter((f) => onDisk.has(f.path));
|
||||
if (here.length === 0) return null;
|
||||
const ok = here.every((f) => onDisk.get(f.path) === f.size) && here.length === parts.length;
|
||||
return (
|
||||
<span
|
||||
className={
|
||||
ok
|
||||
? "shrink-0 rounded border border-emerald-500/30 bg-emerald-500/10 px-1 text-[10px] leading-4 text-emerald-700 dark:text-emerald-300"
|
||||
: "shrink-0 rounded border border-amber-500/30 bg-amber-500/10 px-1 text-[10px] leading-4 text-amber-700 dark:text-amber-300"
|
||||
}
|
||||
>
|
||||
{ok ? "on disk" : here.length < parts.length ? `${here.length} of ${parts.length} parts on disk` : "on disk, size differs"}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user