Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
// latent-consistency/lcm-lora-sdv1-5 | |
/** @type {import('./$types').RequestHandler} */ | |
import { json } 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(); | |
const hasImages = model?.siblings?.filter((sibling: Record<string, string>) => 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<string, string>) => 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) { | |
return json({ | |
message: "No image found" | |
}, { status: 404 }) | |
} | |
image = image.replace(///g, "/") | |
if (image.startsWith("http")) model.image = image | |
else model.image = `https://huggingface.co/${model.id}/resolve/main/${image.replace("./", "")}` | |
} | |
} | |
await prisma.model.create({ | |
data: { | |
id: model.id, | |
image: model.image, | |
likes: model.likes, | |
downloads: model.downloads, | |
isPublic: true, | |
instance_prompt: model?.cardData?.instance_prompt, | |
} | |
}).catch(() => {}) | |
return json({ | |
message: `Successfully added the model ${model.id}`, | |
}) | |
} | |