File size: 3,956 Bytes
a3f1817
 
f42b4a1
f24ad59
1185ec1
a3f1817
 
0f35d4c
a3f1817
 
 
 
 
 
 
 
f276512
a3f1817
 
 
 
f276512
 
a3f1817
 
 
 
f276512
 
 
 
a3f1817
 
 
f276512
 
a3f1817
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac7030c
a3f1817
 
 
 
 
 
 
 
 
ac7030c
a3f1817
 
 
 
0f35d4c
a3f1817
 
 
 
 
 
4c34e70
a3f1817
0f35d4c
a3f1817
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f35d4c
a3f1817
 
 
4c34e70
0f35d4c
a3f1817
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f276512
a3f1817
 
 
 
 
 
 
 
 
4c34e70
a3f1817
 
 
 
0f35d4c
 
a3f1817
 
 
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"use server"

import { Credentials, downloadFile, whoAmI } from "@/lib/huggingface/hub/src"
import { parseDatasetReadme } from "@/app/api/parsers/parseDatasetReadme"
import { ChannelInfo, VideoGenerationModel, VideoOrientation } from "@/types/general"

import { adminCredentials } from "../config"
import { defaultVideoModel, defaultVideoOrientation } from "@/app/config"

export async function parseChannel(options: {
  id: string
  name: string
  likes: number
  updatedAt: Date
  apiKey?: string
  owner?: string
  // ownerId?: string
  renewCache?: boolean
}): Promise<ChannelInfo> {
  // console.log("getChannels")
  let credentials: Credentials = adminCredentials
  let owner = options.owner || ""
  // let ownerId = options.ownerId || ""

  if (options.apiKey) {
    try {
      credentials = { accessToken: options.apiKey }
      const { id: userId, name: username } = await whoAmI({ credentials })
      if (!userId) {
        throw new Error(`couldn't get the userId`)
      }
      if (!username) {
        throw new Error(`couldn't get the username`)
      }
      // everything is in order
      // ownerId = userId
      owner = username
    } catch (err) {
      console.error(err)
      throw err
    }
  }

  const prefix = "ai-tube-"

  const name = options.name

  const chunks = name.split("/")
  const [datasetUser, datasetName] = chunks.length === 2
    ? chunks
    : [name, name]

  // console.log(`found a candidate dataset "${datasetName}" owned by @${datasetUser}`)
  
  // ignore channels which don't start with ai-tube
  if (!datasetName.startsWith(prefix)) {
    throw new Error("this is not an AiTube channel")
  }

  // ignore the video index
  if (datasetName === "ai-tube-index") {
    throw new Error("cannot get channel of ai-tube-index: time-space continuum broken!")
  }

  const slug = datasetName.replaceAll(prefix, "")
  
  // console.log(`found an AiTube channel: "${slug}"`)

  // TODO parse the README to get the proper label
  let label = slug.replaceAll("-", " ")

  let model: VideoGenerationModel = defaultVideoModel
  let lora = ""
  let style = ""
  let thumbnail = ""
  let prompt = ""
  let description = ""
  let voice = ""
  let music = ""
  let tags: string[] = []
  let orientation: VideoOrientation = defaultVideoOrientation

  // console.log(`going to read datasets/${name}`)
  try {
    const response = await downloadFile({
      repo: `datasets/${name}`,
      path: "README.md",
      credentials
    })
    const readme = await response?.text()

    const parsedDatasetReadme = parseDatasetReadme(readme)
    
    // console.log("parsedDatasetReadme: ", parsedDatasetReadme)

    prompt = parsedDatasetReadme.prompt
    label = parsedDatasetReadme.pretty_name
    description = parsedDatasetReadme.description
    thumbnail = parsedDatasetReadme.thumbnail || "thumbnail.jpg"
    model = parsedDatasetReadme.model || defaultVideoModel
    lora = parsedDatasetReadme.lora || ""
    style = parsedDatasetReadme.style || ""
    voice = parsedDatasetReadme.voice || ""
    music = parsedDatasetReadme.music || ""
    orientation = parsedDatasetReadme.orientation || defaultVideoOrientation

    thumbnail =
      thumbnail.startsWith("http")
        ? thumbnail
        : (thumbnail.endsWith(".jpg") || thumbnail.endsWith(".jpeg"))
        ? `https://huggingface.co/datasets/${name}/resolve/main/${thumbnail}`
        : ""

    tags = parsedDatasetReadme.tags
      .map(tag => tag.trim()) // clean them up
      .filter(tag => tag) // remove empty tags
    
  } catch (err) {
    // console.log("failed to read the readme:", err)
  }

  const channel: ChannelInfo = {
    id: options.id,
    // datasetUserId: ownerId,
    datasetUser,
    datasetName,
    slug,
    label,
    description,
    model,
    lora,
    style,
    voice,
    music,
    thumbnail,
    prompt,
    likes: options.likes,
    tags,
    updatedAt: options.updatedAt.toISOString(),
    orientation,
  }

  return channel
}