Squashed commit of the following:

commit c36b0ec0b374dd8ccbee3a6044ee7e3f1fefe368
Author: Nicholas Koben Kao <[email protected]>
Date:   Thu Jan 25 17:54:54 2024 -0800

    nits on wording and removing link to broken storage/:id page

commit 0777fdcf7b0002244bc713199d3d64eea6b6061e
Author: Nicholas Koben Kao <[email protected]>
Date:   Thu Jan 25 17:23:55 2024 -0800

    builder update config and such

commit 958b795bb2b6ac27ce33c5729ef265b068420e1a
Author: Nicholas Koben Kao <[email protected]>
Date:   Thu Jan 25 17:23:43 2024 -0800

    rename all from checkponit to model

commit 7a9c5636e73bd005499b141a4dd382db5672c962
Author: Nicholas Koben Kao <[email protected]>
Date:   Thu Jan 25 16:51:59 2024 -0800

    rename for consistency

commit 48bebbafab9a95388817df97c15f8ea97e0fea75
Author: Nicholas Koben Kao <[email protected]>
Date:   Thu Jan 25 16:18:36 2024 -0800

    bulider

commit 81dacd9af457886f2f027994d225a7748c738abb
Author: Nicholas Koben Kao <[email protected]>
Date:   Thu Jan 25 16:17:56 2024 -0800

    different types of models
This commit is contained in:
bennykok
2024-01-26 10:08:37 +08:00
parent 62a69dba06
commit 85477aba9d
22 changed files with 2919 additions and 235 deletions
-5
View File
@@ -1,5 +0,0 @@
import { insertCivitaiCheckpointSchema } from "@/db/schema";
export const addCivitaiCheckpointSchema = insertCivitaiCheckpointSchema.pick({
civitai_url: true,
});
+5
View File
@@ -0,0 +1,5 @@
import { insertCivitaiModelSchema } from "@/db/schema";
export const addCivitaiModelSchema = insertCivitaiModelSchema.pick({
civitai_url: true,
});
+3 -3
View File
@@ -15,7 +15,7 @@ import { headers } from "next/headers";
import { redirect } from "next/navigation";
import "server-only";
import type { z } from "zod";
import { retrieveCheckpointVolumes } from "./curdCheckpoint";
import { retrieveModelVolumes } from "./curdModel";
export async function getMachines() {
const { userId, orgId } = auth();
@@ -190,7 +190,7 @@ async function _buildMachine(
throw new Error("No domain");
}
const volumes = await retrieveCheckpointVolumes();
const volumes = await retrieveModelVolumes();
// Call remote builder
const result = await fetch(`${process.env.MODAL_BUILDER_URL!}/create`, {
method: "POST",
@@ -204,7 +204,7 @@ async function _buildMachine(
callback_url: `${protocol}://${domain}/api/machine-built`,
models: data.models, //JSON.parse(data.models as string),
gpu: data.gpu && data.gpu.length > 0 ? data.gpu : "T4",
checkpoint_volume_name: volumes[0].volume_name,
model_volume_name: volumes[0].volume_name,
}),
});
@@ -2,92 +2,91 @@
import { auth } from "@clerk/nextjs";
import {
checkpointTable,
CheckpointType,
checkpointVolumeTable,
CheckpointVolumeType,
modelTable,
ModelType,
userVolume,
UserVolumeType,
} from "@/db/schema";
import { withServerPromise } from "./withServerPromise";
import { db } from "@/db/db";
import type { z } from "zod";
import { headers } from "next/headers";
import { addCivitaiCheckpointSchema } from "./addCheckpointSchema";
import { addCivitaiModelSchema } from "./addCivitaiModelSchema";
import { and, eq, isNull } from "drizzle-orm";
import { CivitaiModelResponse } from "@/types/civitai";
import { CivitaiModelResponse, getModelTypeDetails } from "@/types/civitai";
export async function getCheckpoints() {
export async function getModel() {
const { userId, orgId } = auth();
if (!userId) throw new Error("No user id");
const checkpoints = await db
const models = await db
.select()
.from(checkpointTable)
.from(modelTable)
.where(
orgId
? eq(checkpointTable.org_id, orgId)
? eq(modelTable.org_id, orgId)
// make sure org_id is null
: and(
eq(checkpointTable.user_id, userId),
isNull(checkpointTable.org_id),
eq(modelTable.user_id, userId),
isNull(modelTable.org_id),
),
);
return checkpoints;
return models;
}
export async function getCheckpointById(id: string) {
export async function getModelById(id: string) {
const { userId, orgId } = auth();
if (!userId) throw new Error("No user id");
const checkpoint = await db
const model = await db
.select()
.from(checkpointTable)
.from(modelTable)
.where(
and(
orgId ? eq(checkpointTable.org_id, orgId) : and(
eq(checkpointTable.user_id, userId),
isNull(checkpointTable.org_id),
orgId ? eq(modelTable.org_id, orgId) : and(
eq(modelTable.user_id, userId),
isNull(modelTable.org_id),
),
eq(checkpointTable.id, id),
eq(modelTable.id, id),
),
);
return checkpoint[0];
return model[0];
}
export async function getCheckpointVolumes() {
export async function getModelVolumes() {
const { userId, orgId } = auth();
if (!userId) throw new Error("No user id");
const volume = await db
.select()
.from(checkpointVolumeTable)
.from(userVolume)
.where(
and(
orgId
? eq(checkpointVolumeTable.org_id, orgId)
? eq(userVolume.org_id, orgId)
// make sure org_id is null
: and(
eq(checkpointVolumeTable.user_id, userId),
isNull(checkpointVolumeTable.org_id),
eq(userVolume.user_id, userId),
isNull(userVolume.org_id),
),
eq(checkpointVolumeTable.disabled, false),
eq(userVolume.disabled, false),
),
);
return volume;
}
export async function retrieveCheckpointVolumes() {
let volumes = await getCheckpointVolumes();
export async function retrieveModelVolumes() {
let volumes = await getModelVolumes();
if (volumes.length === 0) {
// create volume if not already created
volumes = await addCheckpointVolume();
volumes = await addModelVolume();
}
return volumes;
}
export async function addCheckpointVolume() {
export async function addModelVolume() {
const { userId, orgId } = auth();
if (!userId) throw new Error("No user id");
// Insert the new checkpointVolume into the checkpointVolumeTable
const insertedVolume = await db
.insert(checkpointVolumeTable)
.insert(userVolume)
.values({
user_id: userId,
org_id: orgId,
@@ -111,8 +110,8 @@ function getUrl(civitai_url: string) {
return { url: baseUrl + modelId, modelVersionId };
}
export const addCivitaiCheckpoint = withServerPromise(
async (data: z.infer<typeof addCivitaiCheckpointSchema>) => {
export const addCivitaiModel = withServerPromise(
async (data: z.infer<typeof addCivitaiModelSchema>) => {
const { userId, orgId } = auth();
if (!data.civitai_url) return { error: "no civitai_url" };
@@ -145,17 +144,22 @@ export const addCivitaiCheckpoint = withServerPromise(
selectedModelVersionId = selectedModelVersion?.id.toString();
}
const checkpointVolumes = await getCheckpointVolumes();
const userVolume = await getModelVolumes();
let cVolume;
if (checkpointVolumes.length === 0) {
const volume = await addCheckpointVolume();
if (userVolume.length === 0) {
const volume = await addModelVolume();
cVolume = volume[0];
} else {
cVolume = checkpointVolumes[0];
cVolume = userVolume[0];
}
const model_type = getModelTypeDetails(civitaiModelRes.type);
if (!model_type) {
return
}
const a = await db
.insert(checkpointTable)
.insert(modelTable)
.values({
user_id: userId,
org_id: orgId,
@@ -166,15 +170,15 @@ export const addCivitaiCheckpoint = withServerPromise(
civitai_url: data.civitai_url,
civitai_download_url: selectedModelVersion.files[0].downloadUrl,
civitai_model_response: civitaiModelRes,
checkpoint_volume_id: cVolume.id,
user_volume_id: cVolume.id,
model_type,
updated_at: new Date(),
})
.returning();
const b = a[0];
await uploadCheckpoint(data, b, cVolume);
// redirect(`/checkpoints/${b.id}`);
await uploadModel(data, b, cVolume);
},
);
@@ -213,10 +217,10 @@ export const addCivitaiCheckpoint = withServerPromise(
// },
// );
async function uploadCheckpoint(
data: z.infer<typeof addCivitaiCheckpointSchema>,
c: CheckpointType,
v: CheckpointVolumeType,
async function uploadModel(
data: z.infer<typeof addCivitaiModelSchema>,
c: ModelType,
v: UserVolumeType,
) {
const headersList = headers();
@@ -239,9 +243,9 @@ async function uploadCheckpoint(
download_url: c.civitai_download_url,
volume_name: v.volume_name,
volume_id: v.id,
checkpoint_id: c.id,
model_id: c.id,
callback_url: `${protocol}://${domain}/api/volume-upload`,
upload_type: "checkpoint"
upload_type: c.model_type,
}),
},
);
@@ -249,23 +253,23 @@ async function uploadCheckpoint(
if (!result.ok) {
const error_log = await result.text();
await db
.update(checkpointTable)
.update(modelTable)
.set({
...data,
status: "failed",
error_log: error_log,
})
.where(eq(checkpointTable.id, c.id));
.where(eq(modelTable.id, c.id));
throw new Error(`Error: ${result.statusText} ${error_log}`);
} else {
// setting the build machine id
const json = await result.json();
await db
.update(checkpointTable)
.update(modelTable)
.set({
...data,
upload_machine_id: json.build_machine_instance_id,
})
.where(eq(checkpointTable.id, c.id));
.where(eq(modelTable.id, c.id));
}
}
@@ -1,18 +1,18 @@
import { db } from "@/db/db";
import {
checkpointTable,
modelTable,
} from "@/db/schema";
import { auth } from "@clerk/nextjs";
import { and, desc, eq, isNull } from "drizzle-orm";
export async function getAllUserCheckpoints() {
export async function getAllUserModels() {
const { userId, orgId } = await auth();
if (!userId) {
return null;
}
const checkpoints = await db.query.checkpointTable.findMany({
const models = await db.query.modelTable.findMany({
with: {
user: {
columns: {
@@ -28,14 +28,15 @@ export async function getAllUserCheckpoints() {
civitai_model_response: true,
is_public: true,
upload_type: true,
model_type: true,
status: true,
},
orderBy: desc(checkpointTable.updated_at),
orderBy: desc(modelTable.updated_at),
where:
orgId != undefined
? eq(checkpointTable.org_id, orgId)
: and(eq(checkpointTable.user_id, userId), isNull(checkpointTable.org_id)),
? eq(modelTable.org_id, orgId)
: and(eq(modelTable.user_id, userId), isNull(modelTable.org_id)),
});
return checkpoints;
return models;
}