File size: 1,451 Bytes
f817b5c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47

const get_space_info = async (space_id) => {
    try {
        const response = await fetch(`https://huggingface.co/api/spaces/${space_id}`)
        const json = await response.json()

        if (json.error) {
            return null
        }

        const dayjs = require('dayjs')
        const relativeTime = require('dayjs/plugin/relativeTime')
        dayjs.extend(relativeTime)
        const lastModified = dayjs(json.lastModified).fromNow()

        const author = json.author
        const title = json.cardData.title || 'Untitled'
        const emoji = json.cardData.emoji
        let colorFrom = json.cardData.colorFrom || 'pink'
        let colorTo = json.cardData.colorTo || 'purple'
        const likes = json.likes
        const sdk = json.sdk
        const runtime_stage = json.runtime.stage
        const current_hardware = json.runtime.hardware.current

        const colors = ['red', 'yellow', 'green', 'blue', 'indigo', 'purple', 'pink', 'gray']
        if (!colors.includes(colorFrom)) {
            colorFrom = 'pink'
        }
        if (!colors.includes(colorTo)) {
            colorTo = 'purple'
        }

        const result = { space_id, author, title, emoji, lastModified, colorFrom, colorTo, likes, sdk, runtime_stage, current_hardware }

        console.debug("API response: ", result)

        return result

    } catch (error) {
        console.error(error)
        throw error
    }
}

export { get_space_info }