Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
/** @type {import('./$types').RequestHandler} */ | |
import { json } from '@sveltejs/kit'; | |
import prisma from '$lib/prisma'; | |
import { env } from '$env/dynamic/private' | |
export async function POST({ request }) { | |
const headers = Object.fromEntries(request.headers.entries()); | |
if (headers["x-hf-token"] !== env.SECRET_HF_TOKEN) { | |
return Response.json({ | |
message: "Wrong castle fam :^)" | |
}, { status: 401 }); | |
} | |
const { models } = await request.json(); | |
const cards = await Promise.all(models.map(async (model: Record<string, string>) => { | |
const res = await fetch(`https://huggingface.co/api/models/${model.repo}`, { | |
headers: { | |
"Authorization": `Bearer ${env.SECRET_HF_TOKEN}` | |
} | |
}) | |
const data = await res.json(); | |
const mergedData = { | |
image: model.image, | |
id: model.repo, | |
title: model.title, | |
likes: data.likes, | |
downloads: data.downloads, | |
} | |
return mergedData | |
} | |
)) | |
for (const model of cards) { | |
await prisma.model.create({ | |
data: { | |
id: model.id, | |
image: model.image, | |
title: model.title, | |
likes: model.likes, | |
downloads: model.downloads, | |
isPublic: true, | |
} | |
}) | |
} | |
const total_items = await prisma.model.count() | |
return json({ | |
message: `Successfully added ${total_items} models`, | |
}) | |
} | |