Spaces:
Sleeping
Sleeping
File size: 9,463 Bytes
1185ec1 3d4392e f42b4a1 09a7c47 1185ec1 09a7c47 1185ec1 09a7c47 1185ec1 09a7c47 1185ec1 09a7c47 1185ec1 09a7c47 1185ec1 09a7c47 1185ec1 09a7c47 1185ec1 09a7c47 1185ec1 09a7c47 1185ec1 e40bd21 09a7c47 1185ec1 09a7c47 1185ec1 e40bd21 1185ec1 566d763 3d4392e 1185ec1 e40bd21 637b219 e40bd21 637b219 e40bd21 09a7c47 e40bd21 637b219 09a7c47 637b219 09a7c47 e40bd21 e02a62b e40bd21 e02a62b e40bd21 637b219 e40bd21 1185ec1 637b219 1185ec1 3adb71a 1185ec1 3adb71a 637b219 1185ec1 637b219 1185ec1 3adb71a 1185ec1 3adb71a 637b219 1185ec1 e40bd21 1185ec1 09a7c47 1185ec1 e40bd21 637b219 1185ec1 |
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
import YAML from "yaml"
import { v4 as uuidv4 } from "uuid"
import { ClapHeader, ClapMeta, ClapModel, ClapProject, ClapScene, ClapSegment, ClapStreamType } from "./types"
import { getValidNumber } from "@/lib/utils/getValidNumber"
import { dataUriToBlob } from "@/app/api/utils/dataUriToBlob"
type StringOrBlob = string | Blob
/**
* Import a clap file from various data sources into an ClapProject
*
* Inputs can be:
* - a Clap project (which is an object)
* - an URL to a remote .clap file
* - a string containing a YAML array
* - a data uri containing a gzipped YAML array
* - a Blob containing a gzipped YAML array
*
* note: it is not really async, because for some reason YAML.parse is a blocking call like for JSON,
* there is no async version although we are now in the 20s not 90s
*/
export async function parseClap(src?: ClapProject | string | Blob, debug = false): Promise<ClapProject> {
try {
if (typeof src === "object" && Array.isArray(src?.scenes) && Array.isArray(src?.models)) {
if (debug) {
console.log("parseClap: input is already a Clap file, nothing to do:", src)
}
// we can skip verification
return src as ClapProject
}
} catch (err) {
// well, this is not a clap project
}
let stringOrBlob = (src || "") as StringOrBlob
// both should work
const dataUriHeader1 = "data:application/x-gzip;base64,"
const dataUriHeader2 = "data:application/octet-stream;base64,"
const inputIsString = typeof stringOrBlob === "string"
const inputIsDataUri = typeof stringOrBlob === "string" ? stringOrBlob.startsWith(dataUriHeader1) || stringOrBlob.startsWith(dataUriHeader2) : false
const inputIsRemoteFile = typeof stringOrBlob === "string" ? (stringOrBlob.startsWith("http://") || stringOrBlob.startsWith("https://")) : false
let inputIsBlob = typeof stringOrBlob !== "string"
let inputYamlArrayString = ""
if (debug) {
console.log(`parseClap: pre-analysis: ${JSON.stringify({
inputIsString,
inputIsBlob,
inputIsDataUri,
inputIsRemoteFile
}, null, 2)}`)
}
if (typeof stringOrBlob === "string") {
if (debug) {
console.log("parseClap: input is a string ", stringOrBlob.slice(0, 120))
}
if (inputIsDataUri) {
if (debug) {
console.log(`parseClap: input is a data uri archive`)
}
stringOrBlob = dataUriToBlob(stringOrBlob, "application/x-gzip")
if (debug) {
console.log(`parseClap: inputBlob = `, stringOrBlob)
}
inputIsBlob = true
} else if (inputIsRemoteFile) {
try {
if (debug) {
console.log(`parseClap: input is a remote .clap file`)
}
const res = await fetch(stringOrBlob)
stringOrBlob = await res.blob()
if (!stringOrBlob) { throw new Error("blob is empty") }
inputIsBlob = true
} catch (err) {
// url seems invalid
throw new Error(`failed to download the .clap file (${err})`)
}
} else {
if (debug) {
console.log("parseClap: input is a text string containing a YAML array")
}
inputYamlArrayString = stringOrBlob
inputIsBlob = false
}
}
if (typeof stringOrBlob !== "string" && stringOrBlob) {
if (debug) {
console.log("parseClap: decompressing the blob..")
}
// Decompress the input blob using gzip
const decompressedStream = stringOrBlob.stream().pipeThrough(new DecompressionStream('gzip'))
try {
// Convert the stream to text using a Response object
const decompressedOutput = new Response(decompressedStream)
// decompressedOutput.headers.set("Content-Type", "application/x-gzip")
if (debug) {
console.log("parseClap: decompressedOutput: ", decompressedOutput)
}
// const blobAgain = await decompressedOutput.blob()
inputYamlArrayString = await decompressedOutput.text()
if (debug && inputYamlArrayString) {
console.log("parseClap: successfully decompressed the blob!")
}
} catch (err) {
const message = `parseClap: failed to decompress (${err})`
console.error(message)
throw new Error(message)
}
}
// we don't need this anymore I think
// new Blob([inputStringOrBlob], { type: "application/x-yaml" })
let maybeArray: any = {}
try {
if (debug) {
console.log("parseClap: parsing the YAML array..")
}
// Parse YAML string to raw data
maybeArray = YAML.parse(inputYamlArrayString)
} catch (err) {
throw new Error("invalid clap file (input string is not YAML)")
}
if (!Array.isArray(maybeArray) || maybeArray.length < 2) {
throw new Error("invalid clap file (need a clap format header block and project metadata block)")
}
if (debug) {
console.log("parseClap: the YAML seems okay, continuing decoding..")
}
const maybeClapHeader = maybeArray[0] as ClapHeader
if (maybeClapHeader.format !== "clap-0") {
throw new Error("invalid clap file (sorry, but you can't make up version numbers like that)")
}
const maybeClapMeta = maybeArray[1] as ClapMeta
const clapMeta: ClapMeta = {
id: typeof maybeClapMeta.title === "string" ? maybeClapMeta.id : uuidv4(),
title: typeof maybeClapMeta.title === "string" ? maybeClapMeta.title : "",
description: typeof maybeClapMeta.description === "string" ? maybeClapMeta.description : "",
synopsis: typeof maybeClapMeta.synopsis === "string" ? maybeClapMeta.synopsis : "",
licence: typeof maybeClapMeta.licence === "string" ? maybeClapMeta.licence : "",
orientation: maybeClapMeta.orientation === "portrait" ? "portrait" : maybeClapMeta.orientation === "square" ? "square" : "landscape",
width: getValidNumber(maybeClapMeta.width, 256, 8192, 1024),
height: getValidNumber(maybeClapMeta.height, 256, 8192, 576),
defaultVideoModel: typeof maybeClapMeta.defaultVideoModel === "string" ? maybeClapMeta.defaultVideoModel : "SVD",
extraPositivePrompt: Array.isArray(maybeClapMeta.extraPositivePrompt) ? maybeClapMeta.extraPositivePrompt : [],
screenplay: typeof maybeClapMeta.screenplay === "string" ? maybeClapMeta.screenplay : "",
streamType: (typeof maybeClapMeta.streamType == "string" ? maybeClapMeta.streamType : "static") as ClapStreamType,
}
/*
in case we want to support streaming (mix of models and segments etc), we could do it this way:
const maybeModelsOrSegments = rawData.slice(2)
maybeModelsOrSegments.forEach((unknownElement: any) => {
if (isValidNumber(unknownElement?.track)) {
maybeSegments.push(unknownElement as ClapSegment)
} else {
maybeModels.push(unknownElement as ClapModel)
}
})
*/
const expectedNumberOfModels = maybeClapHeader.numberOfModels || 0
const expectedNumberOfScenes = maybeClapHeader.numberOfScenes || 0
const expectedNumberOfSegments = maybeClapHeader.numberOfSegments || 0
// note: we assume the order is strictly enforced!
// if you implement streaming (mix of models and segments) you will have to rewrite this!
const afterTheHeaders = 2
const afterTheModels = afterTheHeaders + expectedNumberOfModels
const afterTheScenes = afterTheModels + expectedNumberOfScenes
// note: if there are no expected models, maybeModels will be empty
const maybeModels = maybeArray.slice(afterTheHeaders, afterTheModels) as ClapModel[]
// note: if there are no expected scenes, maybeScenes will be empty
const maybeScenes = maybeArray.slice(afterTheModels, afterTheScenes) as ClapScene[]
const maybeSegments = maybeArray.slice(afterTheScenes) as ClapSegment[]
const clapModels: ClapModel[] = maybeModels.map(({
id,
category,
triggerName,
label,
description,
author,
thumbnailUrl,
seed,
assetSourceType,
assetUrl,
age,
gender,
region,
appearance,
voiceVendor,
voiceId,
}) => ({
// TODO: we should verify each of those, probably
id,
category,
triggerName,
label,
description,
author,
thumbnailUrl,
seed,
assetSourceType,
assetUrl,
age,
gender,
region,
appearance,
voiceVendor,
voiceId,
}))
const clapScenes: ClapScene[] = maybeScenes.map(({
id,
scene,
line,
rawLine,
sequenceFullText,
sequenceStartAtLine,
sequenceEndAtLine,
startAtLine,
endAtLine,
events,
}) => ({
id,
scene,
line,
rawLine,
sequenceFullText,
sequenceStartAtLine,
sequenceEndAtLine,
startAtLine,
endAtLine,
events: events.map(e => e)
}))
const clapSegments: ClapSegment[] = maybeSegments.map(({
id,
track,
startTimeInMs,
endTimeInMs,
category,
modelId,
sceneId,
prompt,
label,
outputType,
renderId,
status,
assetUrl,
assetDurationInMs,
createdBy,
editedBy,
outputGain,
seed,
}) => ({
// TODO: we should verify each of those, probably
id,
track,
startTimeInMs,
endTimeInMs,
category,
modelId,
sceneId,
prompt,
label,
outputType,
renderId,
status,
assetUrl,
assetDurationInMs,
createdBy,
editedBy,
outputGain,
seed,
}))
if (debug) {
console.log(`parseClap: successfully parsed ${clapModels.length} models, ${clapScenes.length} scenes and ${clapSegments.length} segments`)
}
return {
meta: clapMeta,
models: clapModels,
scenes: clapScenes,
segments: clapSegments
}
}
|