File size: 967 Bytes
5382ee4
 
 
 
 
88b2903
5382ee4
 
 
88b2903
5382ee4
 
88b2903
5382ee4
 
 
 
 
 
 
 
f8434c9
5382ee4
 
 
 
 
 
88b2903
5382ee4
 
 
 
 
 
 
88b2903
 
 
5382ee4
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
import { Blob } from "buffer"

import { v4 as uuidv4 } from "uuid"
import { uploadFile } from "@huggingface/hub"

import { hfApiKey, hfUsername } from "./config.mts"

export async function uploadMarkdownPrompt({
  markdown,
  dataset,
}: {
  markdown: string
  dataset: string
}): Promise<string> {
  if (!markdown) {
    throw new Error(`the markdown cannot be empty`)
  }

  const credentials = { accessToken: hfApiKey }
  
  // Convert base64 string a Buffer
  const blob = new Blob([markdown])

  const id = uuidv4()
  const uploadFilePath = `prompt_${id}.md`

  await uploadFile({
	  credentials,
    repo: `datasets/${hfUsername}/${dataset}`,
    file: {
      path: uploadFilePath,
      content: blob as any,
    },
    commitTitle: `[robot] Upload markdown prompt ${id}`,
  })

  console.log(`successfully uploaded the file to ${hfUsername}/${dataset}`)
  
  return `https://huggingface.co/datasets/${hfUsername}/${dataset}/resolve/main/${uploadFilePath}`
}