import {z} from 'zod' export type BaseEngine = 'text/llama-2-7b' | 'text/llama-2-13b' | 'text/llama-2-70b' | 'text/codellama-7b' | 'text/codellama-13b' | 'text/codellama-34b' | 'text/codellama-70b' | 'text/mistral-7b' | 'text/mixtral-8x7b' | 'image/controlnet-sd15' | 'image/controlnet-sdxl' | 'image/stable-diffusion-v1-5' | 'image/stable-diffusion-xl-v1-0' | 'image/stable-diffusion-ssd' | 'audio/tortoise-tts' | 'audio/rvc'; export type LoraTuneCheckpoint = {checkpoint_id: string, engine?: (BaseEngine | null) | undefined, name?: (string | null) | undefined}; export type LoraTuneFile = {caption?: (string | null) | undefined, file_id: string}; export type LoraTuneInput = {base_checkpoint: LoraTuneCheckpoint, files: Array, resize_images?: (boolean | null) | undefined, seed?: (number | null) | undefined, steps: number, trigger_words: Array, tune_type?: 'lora_tune' | undefined}; export type TextToSpeechLatentTune = {asset_ids: Array, inline_assets?: (boolean | null) | undefined, tune_type?: 'text_to_speech_latent_tune' | undefined}; export type CreateTuneRequest = {continue_on_rejection?: (boolean | null) | undefined, description?: (string | null) | undefined, details: LoraTuneInput | TextToSpeechLatentTune, name: string}; export type CreateApiV1TuneArgs = {body: CreateTuneRequest}; export type CreateApiV1TuneOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const baseEngine = z.enum(["text/llama-2-7b", "text/llama-2-13b", "text/llama-2-70b", "text/codellama-7b", "text/codellama-13b", "text/codellama-34b", "text/codellama-70b", "text/mistral-7b", "text/mixtral-8x7b", "image/controlnet-sd15", "image/controlnet-sdxl", "image/stable-diffusion-v1-5", "image/stable-diffusion-xl-v1-0", "image/stable-diffusion-ssd", "audio/tortoise-tts", "audio/rvc"]); export const loraTuneCheckpoint = z.object({checkpoint_id: z.string(), engine: baseEngine.nullable().optional(), name: z.string().nullable().optional()}); export const loraTuneFile = z.object({caption: z.string().nullable().optional(), file_id: z.string()}); export const loraTuneOutput = z.object({base_checkpoint: loraTuneCheckpoint, files: z.array(loraTuneFile), resize_images: z.boolean().nullable().optional(), seed: z.number().int().nullable().optional(), steps: z.number().int(), trigger_words: z.array(z.string()), tune_type: z.literal("lora_tune").optional()}); export const textToSpeechLatentTune = z.object({asset_ids: z.array(z.string()), inline_assets: z.boolean().nullable().optional(), tune_type: z.literal("text_to_speech_latent_tune").optional()}); export const tuneResult = z.object({number_of_available_assets: z.number().int().nullable().optional(), output_assets: z.array(z.string()).nullable().optional(), rejected_assets: z.array(z.string()).nullable().optional(), unready_assets: z.array(z.string()).nullable().optional()}); export const tuneStatus = z.enum(["pending", "running", "succeeded", "cancelled", "failed"]); export const tuneType = z.enum(["lora_tune", "text_to_speech_latent_tune", "text_to_speech_rvc_tune", "text_to_speech_tortoise_tune"]); export const tune = z.object({created_at: z.string(), deleted_at: z.string().nullable().optional(), description: z.string(), details: z.discriminatedUnion("tune_type", [loraTuneOutput, textToSpeechLatentTune]), id: z.string(), name: z.string(), output_lora_ids: z.array(z.string()), result: tuneResult.nullable().optional(), status: tuneStatus, status_details: z.string(), succeeded_at: z.string().nullable().optional(), tenant_id: z.string(), tune_type: tuneType, updated_at: z.string()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create Tune * * POST /v1/tune */ export const createApiV1Tune = async (args: CreateApiV1TuneArgs, options: CreateApiV1TuneOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/tune${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(tune, await res.json()) };