Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 3,162 Bytes
eb29a95 17aecfb 8fce765 98b0aa6 8fce765 9601c9e 8fce765 b0186cb 8fce765 98b0aa6 b34e9b1 1c0590e b34e9b1 db9bd2d d9ecfe6 17aecfb d9ecfe6 f3dac82 d9ecfe6 17aecfb eb29a95 17aecfb db9bd2d f3ad218 1c0590e 17aecfb b1a4d81 8fce765 b1a4d81 eb29a95 b1a4d81 b34e9b1 98b0aa6 17aecfb 98b0aa6 9fd8477 612d973 9fd8477 b0122ad 58dc3d3 b0122ad fe56e1a 2081a7c 79d0fc0 b0122ad 9fd8477 79d0fc0 fe56e1a 9fd8477 db9bd2d 9fd8477 |
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 |
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 = false
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<string, string> = {}
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 models = await prisma.model.findMany({
where: {
isPublic: true
}
});
let total_updates = 0;
for (const model of models) {
const hugging_face_request = await fetch(`https://huggingface.co/api/models?id=${model.id}&sort=likes7d`)
const hugging_face_model = await hugging_face_request.json()?.catch(() => {})
let hugging_face_model2 = undefined;
if (!model.instance_prompt) {
const hugging_face_request2 = await fetch(`https://huggingface.co/api/models/${model.id}`)
hugging_face_model2 = await hugging_face_request2.json()?.catch(() => {})
}
if (!hugging_face_model?.[0]) {
continue;
}
await prisma.model.update({
where: {
id: model.id
},
data: {
likes: hugging_face_model?.[0]?.likes,
downloads: hugging_face_model?.[0]?.downloads,
likes7d: hugging_face_model?.[0]?.trendingScore,
id: hugging_face_model?.[0]?.id,
...(hugging_face_model2?.cardData?.instance_prompt ? {
instance_prompt: hugging_face_model2?.cardData?.instance_prompt,
} : {}
)
}
})
.then(() => {
total_updates++
})
.catch(() => {})
}
return json({
message: `Updated ${total_updates} models`
})
} |