import { json, type RequestEvent } from '@sveltejs/kit'; import prisma from '$lib/prisma'; import { tokenIsAvailable } from '$lib/utils'; /** @type {import('./$types').RequestHandler} */ export async function GET(request : RequestEvent) { const token = request.cookies.get('hf_access_token') let IS_ADMIN = !!token if (token) { const user = await tokenIsAvailable(token) if (user) { IS_ADMIN = process?.env?.SECRET_HF_ADMIN ? process?.env?.SECRET_HF_ADMIN.includes(user.sub) : false } } const page = parseInt(request.url.searchParams.get('page') || '0') const filter = request.url.searchParams.get('filter') || 'hotest' const search = request.url.searchParams.get('search') || '' const limit = parseInt(request.url.searchParams.get('limit') || '20') const orderBy: Record = {} if (filter === 'hotest') { orderBy['likes7d'] = 'desc' } else if (filter === 'likes') { orderBy['likes'] = 'desc' } else { orderBy['createdAt'] = 'desc' } const only_not_public = filter === 'staff_only'; const cards = await prisma.model.findMany({ where: { ...( !IS_ADMIN ? { isPublic: true } : only_not_public ? { isPublic: false } : {} ), OR: [ { id: { contains: search } }, ] }, orderBy: orderBy, skip: page * limit, take: limit, }) const total_reposId = await prisma.model.count({ where: { ...(IS_ADMIN ? {} : { isPublic: true }), OR: [ { id: { contains: search } }, ] }, }) return json({ cards, total_items: total_reposId }) } export async function PATCH({ request } : 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 hf_request = await fetch(`https://huggingface.co/api/models?limit=10000&filter=lora%2Cdiffusers&sort=likes7d`) const hf_models = await hf_request.json(); let total_updates = 0; for (const model of hf_models) { await prisma.model.update({ where: { id: model.id }, data: { likes: model.likes, downloads: model.downloads, likes7d: model.likes7d, } }) .then(() => { total_updates++ }) .catch(() => {}) } return json({ message: `Updated ${total_updates} models` }) }