import { json, type RequestEvent } from '@sveltejs/kit'; import prisma from '$lib/prisma'; import { tokenIsAvailable } from '$lib/utils'; /** @type {import('./$types').RequestHandler} */ export async function GET({ url, params } : RequestEvent) { const id = params.id?.replace("@", "/") // to Booelan const full = Boolean(url.searchParams.get('full')) ?? false const model = await prisma.model.findFirst({ where: { id, }, select: full ? { id: true, likes: true, downloads: true, image: true, instance_prompt: true, gallery: { select: { id: true, prompt: true, image: true, createdAt: true, }, where: { isPublic: true }, orderBy: { createdAt: 'desc' }, take: 10 }, comments: { select: { id: true, createdAt: true, text: true, user: { select: { id: true, name: true, sub: true, picture: true, preferred_username: true, } } } } } : { instance_prompt: true, image: true, id: true, } }) if (!model) { return json({ error: { token: "Model params is required" } }, { status: 401 }) } let infos: Record = {}; if (full) { const hf_model_request = await fetch(`https://huggingface.co/api/models/${id}`, { headers: { "Authorization": `Bearer ${process.env.SECRET_HF_TOKEN}` } }) const hf_model_response = await hf_model_request.json(); infos = { base_model: hf_model_response?.cardData?.base_model, license: hf_model_response?.cardData?.license, tags: hf_model_response?.cardData?.tags, } } return json({ model: { ...model, infos } }) } export async function POST({ params, cookies, fetch } : RequestEvent) { const token = cookies.get('hf_access_token') if (!token) { return json({ error: { token: "Token is required" } }, { status: 401 }) } const user = await tokenIsAvailable(token) if (!user || !process.env.SECRET_HF_ADMIN?.includes(user.sub)) { return json({ error: { token: "Wrong castle fam :^)" } }, { status: 401 }) } const id = params.id?.replace("@", "/") const model = await prisma.model.findFirst({ where: { id, } }) if (!model) { return json({ error: { token: "Model not found" } }, { status: 404 }) } const data = await fetch(`https://huggingface.co/api/models/${model.id}`) const hf_model = await data.json() await prisma.model.update({ where: { id, }, data: { isPublic: true, instance_prompt: hf_model?.cardData?.instance_prompt } }) return json({ success: true }) } export async function DELETE({ params, cookies } : RequestEvent) { const token = cookies.get('hf_access_token') if (!token) { return json({ error: { token: "Token is required" } }, { status: 401 }) } const user = await tokenIsAvailable(token) if (!user || !process.env.SECRET_HF_ADMIN?.includes(user.sub)) { return json({ error: { token: "Wrong castle fam :^)" } }, { status: 401 }) } const id = params.id?.replace("@", "/") const model = await prisma.model.findFirst({ where: { id, } }) if (!model) { return json({ error: { token: "Model not found" } }, { status: 404 }) } await prisma.model.update({ where: { id, }, data: { isPublic: false, } }) return json({ success: true }) }