enzostvs HF staff commited on
Commit
9fd8477
β€’
1 Parent(s): d3c7901

add cron to update and scrap

Browse files
package-lock.json CHANGED
@@ -18,9 +18,12 @@
18
  "cookie": "^0.6.0",
19
  "cors": "^2.8.5",
20
  "desm": "^1.3.1",
 
21
  "express": "^4.18.2",
22
  "js-cookie": "^3.0.5",
23
  "moment": "^2.30.1",
 
 
24
  "svelte-infinite-scroll": "^2.0.1"
25
  },
26
  "devDependencies": {
@@ -3801,6 +3804,14 @@
3801
  "node": ">=4"
3802
  }
3803
  },
 
 
 
 
 
 
 
 
3804
  "node_modules/debug": {
3805
  "version": "4.3.4",
3806
  "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
@@ -3933,6 +3944,17 @@
3933
  "node": ">=6.0.0"
3934
  }
3935
  },
 
 
 
 
 
 
 
 
 
 
 
3936
  "node_modules/ee-first": {
3937
  "version": "1.1.1",
3938
  "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
@@ -4380,6 +4402,28 @@
4380
  "reusify": "^1.0.4"
4381
  }
4382
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4383
  "node_modules/file-entry-cache": {
4384
  "version": "6.0.1",
4385
  "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz",
@@ -4478,6 +4522,17 @@
4478
  "is-callable": "^1.1.3"
4479
  }
4480
  },
 
 
 
 
 
 
 
 
 
 
 
4481
  "node_modules/forwarded": {
4482
  "version": "0.2.0",
4483
  "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
@@ -5335,6 +5390,52 @@
5335
  "node": ">= 0.6"
5336
  }
5337
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5338
  "node_modules/node-releases": {
5339
  "version": "2.0.14",
5340
  "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.14.tgz",
@@ -6995,6 +7096,14 @@
6995
  }
6996
  }
6997
  },
 
 
 
 
 
 
 
 
6998
  "node_modules/which": {
6999
  "version": "2.0.2",
7000
  "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
 
18
  "cookie": "^0.6.0",
19
  "cors": "^2.8.5",
20
  "desm": "^1.3.1",
21
+ "dotenv": "^16.4.1",
22
  "express": "^4.18.2",
23
  "js-cookie": "^3.0.5",
24
  "moment": "^2.30.1",
25
+ "node-cron": "^3.0.3",
26
+ "node-fetch": "^3.3.2",
27
  "svelte-infinite-scroll": "^2.0.1"
28
  },
29
  "devDependencies": {
 
3804
  "node": ">=4"
3805
  }
3806
  },
3807
+ "node_modules/data-uri-to-buffer": {
3808
+ "version": "4.0.1",
3809
+ "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz",
3810
+ "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==",
3811
+ "engines": {
3812
+ "node": ">= 12"
3813
+ }
3814
+ },
3815
  "node_modules/debug": {
3816
  "version": "4.3.4",
3817
  "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz",
 
3944
  "node": ">=6.0.0"
3945
  }
3946
  },
3947
+ "node_modules/dotenv": {
3948
+ "version": "16.4.1",
3949
+ "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.1.tgz",
3950
+ "integrity": "sha512-CjA3y+Dr3FyFDOAMnxZEGtnW9KBR2M0JvvUtXNW+dYJL5ROWxP9DUHCwgFqpMk0OXCc0ljhaNTr2w/kutYIcHQ==",
3951
+ "engines": {
3952
+ "node": ">=12"
3953
+ },
3954
+ "funding": {
3955
+ "url": "https://github.com/motdotla/dotenv?sponsor=1"
3956
+ }
3957
+ },
3958
  "node_modules/ee-first": {
3959
  "version": "1.1.1",
3960
  "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz",
 
4402
  "reusify": "^1.0.4"
4403
  }
4404
  },
4405
+ "node_modules/fetch-blob": {
4406
+ "version": "3.2.0",
4407
+ "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz",
4408
+ "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==",
4409
+ "funding": [
4410
+ {
4411
+ "type": "github",
4412
+ "url": "https://github.com/sponsors/jimmywarting"
4413
+ },
4414
+ {
4415
+ "type": "paypal",
4416
+ "url": "https://paypal.me/jimmywarting"
4417
+ }
4418
+ ],
4419
+ "dependencies": {
4420
+ "node-domexception": "^1.0.0",
4421
+ "web-streams-polyfill": "^3.0.3"
4422
+ },
4423
+ "engines": {
4424
+ "node": "^12.20 || >= 14.13"
4425
+ }
4426
+ },
4427
  "node_modules/file-entry-cache": {
4428
  "version": "6.0.1",
4429
  "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz",
 
4522
  "is-callable": "^1.1.3"
4523
  }
4524
  },
4525
+ "node_modules/formdata-polyfill": {
4526
+ "version": "4.0.10",
4527
+ "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
4528
+ "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==",
4529
+ "dependencies": {
4530
+ "fetch-blob": "^3.1.2"
4531
+ },
4532
+ "engines": {
4533
+ "node": ">=12.20.0"
4534
+ }
4535
+ },
4536
  "node_modules/forwarded": {
4537
  "version": "0.2.0",
4538
  "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz",
 
5390
  "node": ">= 0.6"
5391
  }
5392
  },
5393
+ "node_modules/node-cron": {
5394
+ "version": "3.0.3",
5395
+ "resolved": "https://registry.npmjs.org/node-cron/-/node-cron-3.0.3.tgz",
5396
+ "integrity": "sha512-dOal67//nohNgYWb+nWmg5dkFdIwDm8EpeGYMekPMrngV3637lqnX0lbUcCtgibHTz6SEz7DAIjKvKDFYCnO1A==",
5397
+ "dependencies": {
5398
+ "uuid": "8.3.2"
5399
+ },
5400
+ "engines": {
5401
+ "node": ">=6.0.0"
5402
+ }
5403
+ },
5404
+ "node_modules/node-domexception": {
5405
+ "version": "1.0.0",
5406
+ "resolved": "https://registry.npmjs.org/node-domexception/-/node-domexception-1.0.0.tgz",
5407
+ "integrity": "sha512-/jKZoMpw0F8GRwl4/eLROPA3cfcXtLApP0QzLmUT/HuPCZWyB7IY9ZrMeKw2O/nFIqPQB3PVM9aYm0F312AXDQ==",
5408
+ "funding": [
5409
+ {
5410
+ "type": "github",
5411
+ "url": "https://github.com/sponsors/jimmywarting"
5412
+ },
5413
+ {
5414
+ "type": "github",
5415
+ "url": "https://paypal.me/jimmywarting"
5416
+ }
5417
+ ],
5418
+ "engines": {
5419
+ "node": ">=10.5.0"
5420
+ }
5421
+ },
5422
+ "node_modules/node-fetch": {
5423
+ "version": "3.3.2",
5424
+ "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz",
5425
+ "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==",
5426
+ "dependencies": {
5427
+ "data-uri-to-buffer": "^4.0.0",
5428
+ "fetch-blob": "^3.1.4",
5429
+ "formdata-polyfill": "^4.0.10"
5430
+ },
5431
+ "engines": {
5432
+ "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
5433
+ },
5434
+ "funding": {
5435
+ "type": "opencollective",
5436
+ "url": "https://opencollective.com/node-fetch"
5437
+ }
5438
+ },
5439
  "node_modules/node-releases": {
5440
  "version": "2.0.14",
5441
  "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.14.tgz",
 
7096
  }
7097
  }
7098
  },
7099
+ "node_modules/web-streams-polyfill": {
7100
+ "version": "3.3.2",
7101
+ "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.2.tgz",
7102
+ "integrity": "sha512-3pRGuxRF5gpuZc0W+EpwQRmCD7gRqcDOMt688KmdlDAgAyaB1XlN0zq2njfDNm44XVdIouE7pZ6GzbdyH47uIQ==",
7103
+ "engines": {
7104
+ "node": ">= 8"
7105
+ }
7106
+ },
7107
  "node_modules/which": {
7108
  "version": "2.0.2",
7109
  "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz",
package.json CHANGED
@@ -51,9 +51,12 @@
51
  "cookie": "^0.6.0",
52
  "cors": "^2.8.5",
53
  "desm": "^1.3.1",
 
54
  "express": "^4.18.2",
55
  "js-cookie": "^3.0.5",
56
  "moment": "^2.30.1",
 
 
57
  "svelte-infinite-scroll": "^2.0.1"
58
  }
59
  }
 
51
  "cookie": "^0.6.0",
52
  "cors": "^2.8.5",
53
  "desm": "^1.3.1",
54
+ "dotenv": "^16.4.1",
55
  "express": "^4.18.2",
56
  "js-cookie": "^3.0.5",
57
  "moment": "^2.30.1",
58
+ "node-cron": "^3.0.3",
59
+ "node-fetch": "^3.3.2",
60
  "svelte-infinite-scroll": "^2.0.1"
61
  }
62
  }
server.js CHANGED
@@ -1,5 +1,10 @@
1
  import { handler } from './build/handler.js';
2
  import express from 'express';
 
 
 
 
 
3
 
4
  const app = express();
5
 
@@ -11,4 +16,29 @@ app.use(handler);
11
 
12
  app.listen(3000, () => {
13
  console.log('listening on port 3000');
14
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import { handler } from './build/handler.js';
2
  import express from 'express';
3
+ import cron from 'node-cron';
4
+ import fetch from 'node-fetch';
5
+ import dotenv from 'dotenv';
6
+
7
+ dotenv.config();
8
 
9
  const app = express();
10
 
 
16
 
17
  app.listen(3000, () => {
18
  console.log('listening on port 3000');
19
+ });
20
+
21
+ cron.schedule("0 * * * *", async () => {
22
+ const request = await fetch(`${process.env.SPACE_HOST}/api/scrap-models`, {
23
+ method: 'POST',
24
+ headers: {
25
+ 'Content-Type': 'application/json',
26
+ 'x-hf-token': process.env.SECRET_HF_TOKEN
27
+ },
28
+ });
29
+ const response = await request.json();
30
+ console.info(response?.message)
31
+ });
32
+
33
+ cron.schedule("0 0 * * *", async () => {
34
+ const request = await fetch(`${process.env.SPACE_HOST}/api/models`, {
35
+ method: 'PATCH',
36
+ headers: {
37
+ 'Content-Type': 'application/json',
38
+ 'x-hf-token': process.env.SECRET_HF_TOKEN
39
+ },
40
+ });
41
+ const response = await request.json();
42
+ console.info(response?.message)
43
+ });
44
+
src/routes/api/models/+server.ts CHANGED
@@ -49,3 +49,41 @@ export async function GET(request : RequestEvent) {
49
  total_items: total_reposId
50
  })
51
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  total_items: total_reposId
50
  })
51
  }
52
+
53
+ export async function PATCH({ request } : RequestEvent) {
54
+ const headers = Object.fromEntries(request.headers.entries());
55
+
56
+ if (headers["x-hf-token"] !== process.env.SECRET_HF_TOKEN) {
57
+ return Response.json({
58
+ message: "Wrong castle fam :^)"
59
+ }, { status: 401 });
60
+ }
61
+
62
+ const models = await prisma.model.findMany({
63
+ where: {
64
+ isPublic: true,
65
+ }
66
+ })
67
+
68
+ let total_updates = 0;
69
+
70
+ for (const model of models) {
71
+ const response = await fetch(`https://huggingface.co/api/models/${model.id}`)
72
+ const hf_model = await response.json();
73
+
74
+ await prisma.model.update({
75
+ where: {
76
+ id: model.id
77
+ },
78
+ data: {
79
+ likes: hf_model.likes,
80
+ downloads: hf_model.downloads,
81
+ }
82
+ })
83
+ total_updates++
84
+ }
85
+
86
+ return json({
87
+ message: `Updated ${total_updates} models`
88
+ })
89
+ }
src/routes/api/scrap-models/+server.ts CHANGED
@@ -1,7 +1,7 @@
1
  /** @type {import('./$types').RequestHandler} */
2
 
3
  import { json } from '@sveltejs/kit';
4
- // import moment from 'moment';
5
  import prisma from '$lib/prisma';
6
 
7
  export async function POST({ request }) {
@@ -13,15 +13,20 @@ export async function POST({ request }) {
13
  }, { status: 401 });
14
  }
15
 
16
- const response = await fetch(`https://huggingface.co/api/models?limit=5000&filter=lora%2Cdiffusers&sort=createdAt&full=true&config=true`)
17
- const responseData = await response.json();
18
-
19
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
  let index = 0;
21
- for (const model of responseData) {
 
 
22
  if (index % 50 === 0) {
23
  await new Promise(resolve => setTimeout(resolve, 2000));
24
  }
 
 
 
 
25
 
26
  const hasImages = model?.siblings?.filter((sibling: Record<string, string>) => sibling?.rfilename.endsWith(".png") || sibling?.rfilename.endsWith(".jpeg") || sibling?.rfilename.endsWith(".jpg"))
27
  if (hasImages.length > 0) {
@@ -52,16 +57,13 @@ export async function POST({ request }) {
52
  image: model.image,
53
  likes: model.likes,
54
  downloads: model.downloads,
55
- isPublic: true,
56
  }
57
  }).catch(() => {})
58
-
59
- index++
60
  }
61
 
62
- const total_items = await prisma.model.count()
63
-
64
  return json({
65
- message: `Successfully added ${total_items} models`,
66
  })
67
  }
 
1
  /** @type {import('./$types').RequestHandler} */
2
 
3
  import { json } from '@sveltejs/kit';
4
+ import moment from 'moment';
5
  import prisma from '$lib/prisma';
6
 
7
  export async function POST({ request }) {
 
13
  }, { status: 401 });
14
  }
15
 
16
+ const response_models = await fetch(`https://huggingface.co/api/models?limit=200&filter=lora%2Cdiffusers&sort=createdAt&full=true&config=true`)
17
+ const new_models = await response_models.json();
 
18
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
  let index = 0;
20
+ let model_saved = 0;
21
+
22
+ for (const model of new_models) {
23
  if (index % 50 === 0) {
24
  await new Promise(resolve => setTimeout(resolve, 2000));
25
  }
26
+ index++
27
+ if (moment(model.createdAt).isBefore(moment().subtract(1, "hour"))) {
28
+ continue;
29
+ }
30
 
31
  const hasImages = model?.siblings?.filter((sibling: Record<string, string>) => sibling?.rfilename.endsWith(".png") || sibling?.rfilename.endsWith(".jpeg") || sibling?.rfilename.endsWith(".jpg"))
32
  if (hasImages.length > 0) {
 
57
  image: model.image,
58
  likes: model.likes,
59
  downloads: model.downloads,
60
+ isPublic: false,
61
  }
62
  }).catch(() => {})
63
+ model_saved++
 
64
  }
65
 
 
 
66
  return json({
67
+ message: `Successfully added ${model_saved} models`,
68
  })
69
  }