// latent-consistency/lcm-lora-sdv1-5 /** @type {import('./$types').RequestHandler} */ import { json, type RequestEvent } from '@sveltejs/kit'; // import moment from 'moment'; import prisma from '$lib/prisma'; export async function POST({ request, params }) { const headers = Object.fromEntries(request.headers.entries()); const slug= params?.slug?.replace("@", "/") if (headers["x-hf-token"] !== process.env.SECRET_HF_TOKEN) { return Response.json({ message: "Wrong castle fam :^)" }, { status: 401 }); } const response = await fetch(`https://huggingface.co/api/models/${slug}`) const model = await response.json(); if (model?.cardData?.thumbnail) { const filename = model?.cardData?.thumbnail?.split("/").pop() const hasImage = model?.siblings?.find((sibling: Record) => sibling?.rfilename.includes(filename)) if (hasImage?.rfilename) { model.image = `https://huggingface.co/${model.id}/resolve/main/${hasImage?.rfilename}` } } else { const hasImages = model?.siblings?.filter((sibling: Record) => sibling?.rfilename.endsWith(".png") || sibling?.rfilename.endsWith(".jpeg") || sibling?.rfilename.endsWith(".jpg")) if (hasImages.length > 0) { model.image = hasImages[1]?.rfilename ? `https://huggingface.co/${model.id}/resolve/main/${hasImages[1]?.rfilename}` : `https://huggingface.co/${model.id}/resolve/main/${hasImages[0]?.rfilename}` } else { const hasReadme = model?.siblings?.find((sibling: Record) => sibling?.rfilename === "README.md") if (hasReadme) { const readmeRes = await fetch(`https://huggingface.co/${model.id}/raw/main/README.md`) const readme = await readmeRes.text().catch(() => null) if (!readme) { return json({ message: "No readme" }, { status: 404 }) } const imageRegex = /!\[.*\]\((.*)\)/ let image = readme.match(imageRegex)?.[1] if (image) { image = image.replace(///g, "/") if (image.startsWith("http")) model.image = image else model.image = `https://huggingface.co/${model.id}/resolve/main/${image.replace("./", "")}` } } } } const base_model = model?.tags?.find((tag: string) => tag.startsWith("base_model:"))?.split(":")[1] ?? null await prisma.model.create({ data: { id: model.id, ...( model?.image?.startsWith("http") ? { image: model.image } : { image: undefined, } ), likes: model.likes, downloads: model.downloads, isPublic: true, instance_prompt: model?.cardData?.instance_prompt, ...(base_model ? { base_model: base_model } : {} ) } }).catch((err) => { console.log(err.message) return json({ message: err.message }, { status: 500 }) }) return json({ message: `Successfully added the model ${model.id}`, }) } export async function PATCH({ request, params } : RequestEvent) { const headers = Object.fromEntries(request.headers.entries()); if (headers["x-hf-token"] !== process.env.SECRET_HF_TOKEN) { return Response.json({ message: "Wrong castle fam :^)" }, { status: 401 }); } const slug = params?.slug?.replace("@", "/") const models = await prisma.model.findMany({ where: { id: { startsWith: slug } }, }) for (const model of models) { const hf_model = await fetch(`https://huggingface.co/api/models?id=${model.id}&sort=likes7d`) const new_model = await hf_model.json(); if (!new_model?.[0]) { continue; } const update_data = { instance_prompt: model.instance_prompt, image: model.image, } if (new_model?.cardData?.instance_prompt) { update_data.instance_prompt = new_model?.cardData?.instance_prompt } if (new_model?.cardData?.thumbnail) { const filename = new_model?.cardData?.thumbnail?.split("/").pop() const hasImage = new_model?.siblings?.find((sibling: Record) => sibling?.rfilename.includes(filename)) if (hasImage?.rfilename) { update_data.image = `https://huggingface.co/${model.id}/resolve/main/${hasImage?.rfilename}` } } await prisma.model.update({ where: { id: model.id }, data: { ...update_data, likes: new_model?.[0]?.likes, downloads: new_model?.[0]?.downloads, likes7d: new_model?.[0]?.trendingScore, id: new_model?.[0]?.id, } }) } return json({ message: true }) }