File size: 960 Bytes
eb29a95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { uploadFile } from "@huggingface/hub";
import type { RepoDesignation, Credentials } from "@huggingface/hub";
import { env } from '$env/dynamic/private'

const TOTAL_FOLDERS = 10
// const MAX_IMAGES_PER_FOLDER = 10_000

export const UploaderDataset = async (blob: Blob, name: string) => {
  const repo: RepoDesignation = { type: "dataset", name: "enzostvs/loras-studio" };
  const credentials: Credentials = { accessToken: env.SECRET_HF_TOKEN as string };
  
  const folder_key = Math.floor(Math.random() * TOTAL_FOLDERS)
  const formatted_name = name.replace(/[^a-zA-Z0-9]/g, "-")
  const path = `images-${folder_key}/${formatted_name}.png`

  try {
    await uploadFile({
      repo,
      credentials,
      file:
        {
          path,
          content: blob,
        },
    })
    return { path, ok: true }
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  } catch (e: any) {
    console.error(e)
    return { ok: false }
  }
}