Spaces:
Sleeping
Sleeping
Julian Bilcke
commited on
Commit
·
3942037
1
Parent(s):
f27679f
performance improvement for the index getter
Browse files
src/app/server/actions/ai-tube-hf/getIndex.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
-
import { downloadFile } from "@/huggingface/hub/src"
|
| 2 |
import { VideoInfo, VideoStatus } from "@/types"
|
| 3 |
|
| 4 |
-
import {
|
| 5 |
|
| 6 |
export async function getIndex({
|
| 7 |
status,
|
|
@@ -9,40 +8,30 @@ export async function getIndex({
|
|
| 9 |
}: {
|
| 10 |
status: VideoStatus
|
| 11 |
|
| 12 |
-
/**
|
| 13 |
-
* Renew the cache
|
| 14 |
-
*
|
| 15 |
-
* This is was the batch job daemon will use, as in normal time
|
| 16 |
-
* we will want to use the cache since the file might be large
|
| 17 |
-
*
|
| 18 |
-
* it is also possible that we decide to *never* renew the cache from a user's perspective,
|
| 19 |
-
* and only renew it manually when a video changes status
|
| 20 |
-
*
|
| 21 |
-
* that way user requests will always be snappy!
|
| 22 |
-
*/
|
| 23 |
renewCache?: boolean
|
| 24 |
}): Promise<Record<string, VideoInfo>> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
const response = await downloadFile({
|
| 28 |
-
credentials: adminCredentials,
|
| 29 |
-
repo: `datasets/${adminUsername}/ai-tube-index`,
|
| 30 |
-
path: `${status}.json`,
|
| 31 |
-
|
| 32 |
-
})
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
jsonResponse
|
| 43 |
-
throw new Error("index is not an object, admin repair needed")
|
| 44 |
-
}
|
| 45 |
-
const videos = jsonResponse as Record<string, VideoInfo>
|
| 46 |
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
}
|
|
|
|
|
|
|
| 1 |
import { VideoInfo, VideoStatus } from "@/types"
|
| 2 |
|
| 3 |
+
import { adminUsername } from "../config"
|
| 4 |
|
| 5 |
export async function getIndex({
|
| 6 |
status,
|
|
|
|
| 8 |
}: {
|
| 9 |
status: VideoStatus
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
renewCache?: boolean
|
| 12 |
}): Promise<Record<string, VideoInfo>> {
|
| 13 |
+
try {
|
| 14 |
+
const response = await fetch(
|
| 15 |
+
`https://huggingface.co/datasets/${adminUsername}/ai-tube-index/raw/main/${status}.json`
|
| 16 |
+
, {
|
| 17 |
+
cache: "no-store"
|
| 18 |
+
})
|
| 19 |
|
| 20 |
+
const jsonResponse = await response?.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
if (
|
| 23 |
+
typeof jsonResponse === "undefined" &&
|
| 24 |
+
typeof jsonResponse !== "object" &&
|
| 25 |
+
Array.isArray(jsonResponse) ||
|
| 26 |
+
jsonResponse === null) {
|
| 27 |
+
throw new Error("index is not an object, admin repair needed")
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
const videos = jsonResponse as Record<string, VideoInfo>
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
return videos
|
| 33 |
+
} catch (err) {
|
| 34 |
+
console.error(`failed to get index ${status}:`, err)
|
| 35 |
+
return {}
|
| 36 |
+
}
|
| 37 |
}
|