File size: 5,029 Bytes
f62b8d3
 
1185ec1
f27679f
 
 
 
 
1f1caeb
1185ec1
f62b8d3
 
 
f27679f
f62b8d3
f27679f
 
 
 
 
 
 
 
f62b8d3
f27679f
 
f62b8d3
f27679f
 
f62b8d3
f27679f
f62b8d3
f27679f
f62b8d3
f27679f
f62b8d3
f27679f
 
 
 
 
 
 
 
 
1185ec1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e40bd21
1185ec1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f1caeb
1185ec1
 
 
1f1caeb
1185ec1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f27679f
1185ec1
 
 
f62b8d3
f27679f
f62b8d3
f27679f
 
 
 
 
 
 
f62b8d3
 
 
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"use server"

import { ChannelInfo, VideoRequest } from "@/types/general"
import { getCredentials } from "./getCredentials"
import { listFiles } from "@/huggingface/hub/src"
import { parsePromptFileName } from "../utils/parsePromptFileName"
import { downloadFileAsText } from "./downloadFileAsText"
import { parseDatasetPrompt } from "../utils/parseDatasetPrompt"
import { computeOrientationProjectionWidthHeight } from "../utils/computeOrientationProjectionWidthHeight"
import { downloadClapProject } from "./downloadClapProject"

/**
 * Return all the videos requests created by a user on their channel
 *
 */
export async function getVideoRequestsFromChannel({
  channel,
  apiKey,
  renewCache,
  neverThrow,
}: {
  channel: ChannelInfo
  apiKey?: string
  renewCache?: boolean
  neverThrow?: boolean
}): Promise<VideoRequest[]> {

  try {
    const { credentials } = await getCredentials(apiKey)

    let videos: Record<string, VideoRequest> = {}

    const repo = `datasets/${channel.datasetUser}/${channel.datasetName}`

    // console.log(`scanning ${repo}`)

    for await (const file of listFiles({
      repo,
      // recursive: true,
      // expand: true,
      credentials,
      requestInit: renewCache
        ? { cache: "no-cache" }
        : undefined
    })) {
      try {
        const filePath = file.path.toLowerCase().trim()
        // TODO we should add some safety mechanisms here:
        // skip lists of files that are too long
        // skip files that are too big
        // skip files with file.security.safe !== true

        // console.log("file.path:", file.path)
        /// { type, oid, size, path }
        if (filePath === "readme.md") {
          // console.log("found the README")
          // TODO: read this readme
        } else if (filePath.endsWith(".clap")) {
          const clap = await downloadClapProject({
            path: file.path,
            channel,
            credentials,
          })
          console.log("got a clap file:", clap.clapProject.meta)
          
          // in the frontend UI we want to display everything,
          // we don't filter stuff even if they are incomplete

          videos[clap.videoRequest.id] = clap.videoRequest
        } else if (filePath.startsWith("prompt_") && filePath.endsWith(".md")) {
          
          const id = parsePromptFileName(filePath)

          if (!id) { continue }

          const rawMarkdown = await downloadFileAsText({
            repo,
            path: file.path, // be sure to use the original file.path (with capitalization if any) and not filePath
            apiKey,
            renewCache,
            neverThrow: true,
          })

          if (!rawMarkdown) {
            // console.log(`markdown file is empty, skipping`)
            continue
          }

          const {
            title,
            description,
            tags,
            prompt,
            thumbnail,
            model,
            lora,
            style,
            music,
            voice,
            orientation,
          } = parseDatasetPrompt(rawMarkdown, channel)

          /*
          on ai-tube side (not the ai-tube robot) we are okay with partial video requests,
          ie. drafts
          if (!title || !description || !prompt) {
            // console.log("dataset prompt is incomplete or unparseable")
            // continue
          }
          */

          // console.log("prompt parsed markdown:", { title, description, tags })
          let thumbnailUrl =
            thumbnail.startsWith("http")
              ? thumbnail
              : (thumbnail.endsWith(".webp") || thumbnail.endsWith(".jpg") || thumbnail.endsWith(".jpeg"))
              ? `https://huggingface.co/${repo}/resolve/main/${thumbnail}`
              : ""

          // TODO: the clap file is empty if
          // the video is prompted using Markdown
          const clapUrl = ""

          const video: VideoRequest = {
            id,
            label: title,
            description,
            prompt,
            thumbnailUrl,
            clapUrl,
            model,
            lora,
            style,
            voice,
            music,
            updatedAt: file.lastCommit?.date || new Date().toISOString(),
            tags: Array.isArray(tags) && tags.length ? tags : channel.tags,
            channel,
            duration: 0,
            ...computeOrientationProjectionWidthHeight({
              lora,
              orientation,
              // projection, // <- will be extrapolated from the LoRA for now
            }),
          }

          videos[id] = video

        } else if (filePath.endsWith(".mp4")) {
          // console.log("found a video:", file.path)
        }
      } catch (err) {
        console.error("error while processing a dataset file:")
        console.error(err)
      }
    }

    return Object.values(videos)
  } catch (err) {
    if (neverThrow) {
      console.error(`getVideoRequestsFromChannel():`, err)
      return []
    } else {
      throw err
    }
  }
}