File size: 997 Bytes
f27679f
 
 
f42b4a1
f27679f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

// safe way to get the credentials

import { Credentials, WhoAmIUser, whoAmI } from "@/lib/huggingface/hub/src"

import { adminCredentials, adminUsername } from "../config"

export async function getCredentials(apiKey?: string): Promise<{
  username: string
  avatarUrl: string
  credentials: Credentials
}> {
  let username = adminUsername
  let credentials: Credentials = adminCredentials
  let avatarUrl = ""

  if (apiKey) {
    try {
      credentials = { accessToken: apiKey }
      const user = await whoAmI({ credentials }) as unknown as WhoAmIUser
      if (!user.name) {
        throw new Error(`couldn't get the username`)
      }
      username = user.name
      avatarUrl = user.avatarUrl || ""
    } catch (err) {
      console.error(err)
      // important: we throw an error if an apiKey was explicitely given but is empty
      throw new Error(`the provided Hugging Face API key is invalid or has expired`)
    }
  }

  return {
    username,
    avatarUrl,
    credentials
  }
}