CivitAI: add models from civitai.com / civitai.red links
Paste a CivitAI model link (civitai.com, .red or .green), a download link or an AIR id into "Add model": pick a version and its files, and whether to follow new versions. The model is stored as source "civitai" with repoId civitai/<modelId>, pinned to the version id, in the folder civitai/<id>-<name>; files keep CivitAI's SHA-256 for deep verify. Early access and creator-required logins are caught before anything is queued. Migration 0027 adds models.display_name, nsfw and source_card, and model_files.civitai (file id and attributes). The model page links to CivitAI instead of the Hub, and Hub checks, updates and adding files are refused for CivitAI models for now. The worker fails CivitAI download jobs until it can run them (next step). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
765dac2ed2
commit
a1acd8c0a5
19 files changed
+2276
-16
No files matched your search
@@ -0,0 +1,21 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { withAuth } from "@/lib/api-auth";
|
||||
import { handleApiError } from "@/lib/api-errors";
|
||||
import { lookupCivitai } from "@/lib/civitai/lookup";
|
||||
|
||||
const Body = z.object({
|
||||
input: z.string().trim().min(1).max(2048),
|
||||
/** Another version of the same model (the version picker). */
|
||||
versionId: z.number().int().positive().optional(),
|
||||
});
|
||||
|
||||
/** Look up a CivitAI model link or id: the model, its versions and one version's files. */
|
||||
export const POST = withAuth(async (req) => {
|
||||
try {
|
||||
const body = Body.parse(await req.json());
|
||||
return NextResponse.json(await lookupCivitai(body.input, body.versionId));
|
||||
} catch (err) {
|
||||
return handleApiError(err);
|
||||
}
|
||||
});
|
||||
@@ -9,5 +9,7 @@ export const POST = withAuth<RouteContext<"/api/models/[id]/hub-check">>(async (
|
||||
const model = await getCheckedModel(id);
|
||||
if (!model) return apiError(404, "not_found", "Model not found.");
|
||||
if (model.source === "local") return apiError(409, "local_model", "This is a local model without a Hugging Face repo.");
|
||||
// Checking CivitAI for new versions comes with CivitAI updates; the Hub would report it missing.
|
||||
if (model.source === "civitai") return apiError(409, "civitai_model", "Checks for new CivitAI versions aren't available yet.");
|
||||
return NextResponse.json(await checkHub(model));
|
||||
});
|
||||
@@ -4,7 +4,7 @@ import { withAuth } from "@/lib/api-auth";
|
||||
import { apiError, handleApiError } from "@/lib/api-errors";
|
||||
import { REPO_ID_RE, SHA_RE } from "@/lib/hub/client";
|
||||
import { queueDownload } from "@/lib/jobs";
|
||||
import { addLocalModel, addModel, ModelError } from "@/lib/models";
|
||||
import { addCivitaiModel, addLocalModel, addModel, ModelError } from "@/lib/models";
|
||||
import { addTag, normalizeTagName, TagError } from "@/lib/tags";
|
||||
|
||||
const Body = z.object({
|
||||
@@ -26,13 +26,33 @@ const LocalBody = z.object({
|
||||
tags: z.array(z.string()).max(50).default([]),
|
||||
});
|
||||
|
||||
/** A CivitAI model at the version the user reviewed. */
|
||||
const CivitaiBody = z.object({
|
||||
source: z.literal("civitai"),
|
||||
modelId: z.number().int().positive(),
|
||||
versionId: z.number().int().positive(),
|
||||
track: z.enum(["latest", "pinned"]).default("latest"),
|
||||
fileIds: z.array(z.number().int().positive()).min(1).max(1_000),
|
||||
download: z.boolean().default(true),
|
||||
tags: z.array(z.string()).max(50).default([]),
|
||||
});
|
||||
|
||||
/**
|
||||
* Add a model to the library, pinned to the SHA the user reviewed, and queue its download. With
|
||||
* `source: "local"`, add a folder from the models folder that has no Hub repo instead (nothing queued).
|
||||
* `source: "civitai"`, a CivitAI model pinned to a version. With `source: "local"`, add a folder
|
||||
* from the models folder that has no Hub repo instead (nothing queued).
|
||||
*/
|
||||
export const POST = withAuth(async (req) => {
|
||||
try {
|
||||
const json = await req.json();
|
||||
if (json?.source === "civitai") {
|
||||
const body = CivitaiBody.parse(json);
|
||||
body.tags.forEach(normalizeTagName);
|
||||
const { id } = await addCivitaiModel(body);
|
||||
for (const tag of body.tags) await addTag(id, tag);
|
||||
const job = body.download ? await queueDownload(id, "initial") : null;
|
||||
return NextResponse.json({ id, jobId: job?.jobId ?? null }, { status: 201 });
|
||||
}
|
||||
if (json?.source === "local") {
|
||||
const body = LocalBody.parse(json);
|
||||
body.tags.forEach(normalizeTagName);
|
||||
|
||||
Reference in new issue
Block a user