File size: 1,461 Bytes
eb29a95
 
17aecfb
 
eb29a95
17aecfb
 
 
 
eb29a95
17aecfb
 
 
 
 
 
 
 
56137d7
 
 
 
 
17aecfb
 
 
eb29a95
17aecfb
 
db78a09
17aecfb
 
 
 
 
 
 
 
 
eb29a95
17aecfb
 
 
db78a09
17aecfb
 
 
 
 
 
 
 
 
 
 
 
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
/** @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,
      instance_prompt: data?.cardData?.instance_prompt ?? null,
      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,
        instance_prompt: model.instance_prompt,
        downloads: model.downloads,
        isPublic: true,
      }
    })
  }

  const total_items = await prisma.model.count()

  return json({
    message: `Successfully added ${total_items} models`,
  })
}