File size: 916 Bytes
c1f12bf
ed9e9d0
c1f12bf
 
 
 
ed9e9d0
 
 
 
 
 
c1f12bf
 
 
 
 
 
ed9e9d0
 
c1f12bf
 
 
 
ed9e9d0
 
 
 
c1f12bf
ed9e9d0
 
 
 
 
 
 
 
 
c1f12bf
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
import { HFHubCategory } from './types'

export function parseHuggingFaceHubId(
  input?: any,
  defaultCategory: HFHubCategory = 'models'
): {
  category: HFHubCategory
  owner: string
  id: string
  categoryAndOwnerAndId: string
  ownerAndId: string
} {
  let inputStr = `${input || ''}`

  if (inputStr.includes('.co/')) {
    inputStr = inputStr.split('.co/').pop() || ''
  } else if (inputStr.includes('.com/')) {
    inputStr = inputStr.split('.com/').pop() || ''
  }

  let parts = inputStr.split('/')
  if (parts.length < 2 || parts.length > 3) {
    throw new Error(`input seems invalid, cannot extract chunks`)
  }

  if (parts.length === 2) {
    parts = [defaultCategory, parts[0], parts[1]]
  }

  const [category, owner, id] = parts

  return {
    category: category as HFHubCategory,
    owner,
    id,
    categoryAndOwnerAndId: `${category}/${owner}/${id}`,
    ownerAndId: `${owner}/${id}`,
  }
}