Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
import { json } from '@sveltejs/kit'; | |
import prisma from '$lib/prisma'; | |
// import { env } from '$env/dynamic/private' | |
// import jsonData from "$lib/utils/loras.json"; | |
// import type { ModelCard } from '$lib/type'; | |
/** @type {import('./$types').RequestHandler} */ | |
export async function POST({ request }) { | |
const headers = Object.fromEntries(request.headers.entries()); | |
if (headers["x-hf-token"] !== process.env.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}`) | |
const data = await res.json(); | |
const mergedData = { | |
image: model.image, | |
repo: model.repo, | |
title: model.title, | |
likes: data.likes, | |
downloads: data.downloads, | |
} | |
return mergedData | |
} | |
)) | |
for (const model of cards) { | |
await prisma.model.create({ | |
data: { | |
repo: model.repo, | |
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`, | |
}) | |
} | |