enzostvs HF staff commited on
Commit
a1f821c
·
1 Parent(s): e88b579

add route to manually update models from specific author

Browse files
src/routes/api/scrap-models/[slug]/+server.ts CHANGED
@@ -1,7 +1,7 @@
1
  // latent-consistency/lcm-lora-sdv1-5
2
  /** @type {import('./$types').RequestHandler} */
3
 
4
- import { json } from '@sveltejs/kit';
5
  // import moment from 'moment';
6
  import prisma from '$lib/prisma';
7
 
@@ -18,30 +18,34 @@ export async function POST({ request, params }) {
18
  const response = await fetch(`https://huggingface.co/api/models/${slug}`)
19
  const model = await response.json();
20
 
21
- const hasImages = model?.siblings?.filter((sibling: Record<string, string>) => sibling?.rfilename.endsWith(".png") || sibling?.rfilename.endsWith(".jpeg") || sibling?.rfilename.endsWith(".jpg"))
22
- if (hasImages.length > 0) {
23
- model.image = hasImages[1]?.rfilename ? `https://huggingface.co/${model.id}/resolve/main/${hasImages[1]?.rfilename}` : `https://huggingface.co/${model.id}/resolve/main/${hasImages[0]?.rfilename}`
24
  } else {
25
- const hasReadme = model?.siblings?.find((sibling: Record<string, string>) => sibling?.rfilename === "README.md")
26
- if (hasReadme) {
27
- const readmeRes = await fetch(`https://huggingface.co/${model.id}/raw/main/README.md`)
28
- const readme = await readmeRes.text().catch(() => null)
29
- if (!readme) {
30
- return json({
31
- message: "No readme"
32
- }, { status: 404 })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  }
34
- const imageRegex = /!\[.*\]\((.*)\)/
35
- let image = readme.match(imageRegex)?.[1]
36
-
37
- if (!image) {
38
- return json({
39
- message: "No image found"
40
- }, { status: 404 })
41
- }
42
- image = image.replace(/&#x2F;/g, "/")
43
- if (image.startsWith("http")) model.image = image
44
- else model.image = `https://huggingface.co/${model.id}/resolve/main/${image.replace("./", "")}`
45
  }
46
  }
47
 
@@ -60,3 +64,51 @@ export async function POST({ request, params }) {
60
  message: `Successfully added the model ${model.id}`,
61
  })
62
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  // latent-consistency/lcm-lora-sdv1-5
2
  /** @type {import('./$types').RequestHandler} */
3
 
4
+ import { json, type RequestEvent } from '@sveltejs/kit';
5
  // import moment from 'moment';
6
  import prisma from '$lib/prisma';
7
 
 
18
  const response = await fetch(`https://huggingface.co/api/models/${slug}`)
19
  const model = await response.json();
20
 
21
+ if (model?.cardData?.thumbnail) {
22
+ model.image = `https://huggingface.co/${model.id}/resolve/main/${model?.cardData?.thumbnail}`
 
23
  } else {
24
+ const hasImages = model?.siblings?.filter((sibling: Record<string, string>) => sibling?.rfilename.endsWith(".png") || sibling?.rfilename.endsWith(".jpeg") || sibling?.rfilename.endsWith(".jpg"))
25
+ if (hasImages.length > 0) {
26
+ model.image = hasImages[1]?.rfilename ? `https://huggingface.co/${model.id}/resolve/main/${hasImages[1]?.rfilename}` : `https://huggingface.co/${model.id}/resolve/main/${hasImages[0]?.rfilename}`
27
+ } else {
28
+ const hasReadme = model?.siblings?.find((sibling: Record<string, string>) => sibling?.rfilename === "README.md")
29
+ if (hasReadme) {
30
+ const readmeRes = await fetch(`https://huggingface.co/${model.id}/raw/main/README.md`)
31
+ const readme = await readmeRes.text().catch(() => null)
32
+ if (!readme) {
33
+ return json({
34
+ message: "No readme"
35
+ }, { status: 404 })
36
+ }
37
+ const imageRegex = /!\[.*\]\((.*)\)/
38
+ let image = readme.match(imageRegex)?.[1]
39
+
40
+ if (!image) {
41
+ return json({
42
+ message: "No image found"
43
+ }, { status: 404 })
44
+ }
45
+ image = image.replace(/&#x2F;/g, "/")
46
+ if (image.startsWith("http")) model.image = image
47
+ else model.image = `https://huggingface.co/${model.id}/resolve/main/${image.replace("./", "")}`
48
  }
 
 
 
 
 
 
 
 
 
 
 
49
  }
50
  }
51
 
 
64
  message: `Successfully added the model ${model.id}`,
65
  })
66
  }
67
+ export async function PATCH({ request, params } : RequestEvent) {
68
+ const headers = Object.fromEntries(request.headers.entries());
69
+
70
+ if (headers["x-hf-token"] !== process.env.SECRET_HF_TOKEN) {
71
+ return Response.json({
72
+ message: "Wrong castle fam :^)"
73
+ }, { status: 401 });
74
+ }
75
+
76
+ const slug = params?.slug?.replace("@", "/")
77
+
78
+ const models = await prisma.model.findMany({
79
+ where: {
80
+ id: slug
81
+ }
82
+ })
83
+
84
+ for (const model of models) {
85
+ const hf_model = await fetch(`https://huggingface.co/api/models/${slug}`)
86
+ const new_model = await hf_model.json();
87
+ const update_data = {
88
+ instance_prompt: model.instance_prompt,
89
+ image: model.image,
90
+ }
91
+
92
+ if (new_model?.cardData?.instance_prompt) {
93
+ update_data.instance_prompt = new_model?.cardData?.instance_prompt
94
+ }
95
+ if (new_model?.cardData?.thumbnail) {
96
+ update_data.image = `https://huggingface.co/${model.id}/resolve/main/${new_model?.cardData?.thumbnail}`
97
+ }
98
+
99
+ await prisma.model.update({
100
+ where: {
101
+ id: model.id
102
+ },
103
+ data: {
104
+ ...update_data,
105
+ likes: new_model.likes,
106
+ downloads: new_model.downloads,
107
+ }
108
+ })
109
+ }
110
+
111
+ return json({
112
+ message: true
113
+ })
114
+ }