Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 5,362 Bytes
1185ec1 637b219 e02a62b 1185ec1 e40bd21 1185ec1 e40bd21 1185ec1 e02a62b 1185ec1 e40bd21 637b219 e40bd21 637b219 e40bd21 637b219 e40bd21 e02a62b e40bd21 e02a62b e40bd21 637b219 e40bd21 1185ec1 637b219 1185ec1 3adb71a 1185ec1 3adb71a 637b219 1185ec1 637b219 1185ec1 3adb71a 1185ec1 3adb71a 637b219 1185ec1 e40bd21 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 |
import YAML from "yaml"
import { v4 as uuidv4 } from "uuid"
import { ClapHeader, ClapMeta, ClapModel, ClapProject, ClapScene, ClapSegment } from "./types"
import { getValidNumber } from "@/lib/getValidNumber"
/**
* import a Clap file (from a plain text string)
*
* note: it is not really async, because for some reason YAML.parse is a blocking call like for JSON,
* they is no async version although we are now in the 20s not 90s
*/
export async function parseClap(inputStringOrBlob: string | Blob): Promise<ClapProject> {
// Decompress the input blob using gzip
const decompressor = new DecompressionStream('gzip');
const inputBlob =
typeof inputStringOrBlob === "string"
? new Blob([inputStringOrBlob], { type: "application/x-yaml" })
: inputStringOrBlob;
const decompressedStream = inputBlob.stream().pipeThrough(decompressor);
// Convert the stream to text using a Response object
const text = await new Response(decompressedStream).text();
// Parse YAML string to raw data
const rawData = YAML.parse(text);
if (!Array.isArray(rawData) || rawData.length < 2) {
throw new Error("invalid clap file (need a clap format header block and project metadata block)")
}
const maybeClapHeader = rawData[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 = rawData[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 : "",
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 : []
}
/*
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 = rawData.slice(afterTheHeaders, afterTheModels) as ClapModel[]
// note: if there are no expected scenes, maybeScenes will be empty
const maybeScenes = rawData.slice(afterTheModels, afterTheScenes) as ClapScene[]
const maybeSegments = rawData.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,
}))
return {
meta: clapMeta,
models: clapModels,
scenes: clapScenes,
segments: clapSegments
}
}
|