Spaces:
Running
Running
File size: 4,309 Bytes
c9a7b70 a1f821c c9a7b70 a1f821c 08ea78f 549506b a1f821c c9a7b70 549506b c9a7b70 549506b c9a7b70 a1f821c 08ea78f a1f821c 58dc3d3 a1f821c 79d0fc0 a1f821c 08ea78f a1f821c 79d0fc0 a1f821c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
// 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<string, string>) => 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<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}`,
})
}
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<string, string>) => 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
})
} |