Spaces:
Sleeping
Sleeping
Download web/src/api.ts from saim256/swipe-tts-preview: direct link, hf CLI and curl.
- Browser
- Download file 3.66 kB
-
https://huggingface.co/spaces/saim256/swipe-tts-preview/resolve/main/web/src/api.ts
- Command line
-
hf download hf://spaces/saim256/swipe-tts-preview/web/src/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/saim256/swipe-tts-preview/resolve/main/web/src/api.ts
3.66 kB
| import type { Bootstrap, BulkScript, GenerationSettings, Job, SavedOutput, Voice } from "./types"; | |
| async function parseResponse<T>(response: Response): Promise<T> { | |
| const text = await response.text(); | |
| const data = text ? JSON.parse(text) : {}; | |
| if (!response.ok) { | |
| const message = data.detail || data.error || "Request failed."; | |
| throw new Error(typeof message === "string" ? message : JSON.stringify(message)); | |
| } | |
| return data as T; | |
| } | |
| async function jsonRequest<T>(url: string, body?: unknown, init?: RequestInit): Promise<T> { | |
| const response = await fetch(url, { | |
| method: body === undefined ? "GET" : "POST", | |
| credentials: "same-origin", | |
| headers: body === undefined ? undefined : { "content-type": "application/json" }, | |
| body: body === undefined ? undefined : JSON.stringify(body), | |
| ...init | |
| }); | |
| return parseResponse<T>(response); | |
| } | |
| export async function login(username: string, password: string) { | |
| return jsonRequest<{ ok: boolean; user: { username: string } }>("/api/login", { username, password }); | |
| } | |
| export async function logout() { | |
| return jsonRequest<{ ok: boolean }>("/api/logout", {}); | |
| } | |
| export async function session() { | |
| return jsonRequest<{ ok: boolean; user: { username: string } }>("/api/session"); | |
| } | |
| export async function bootstrap() { | |
| return jsonRequest<Bootstrap & { ok: boolean }>("/api/bootstrap"); | |
| } | |
| export async function refreshJobs() { | |
| return jsonRequest<{ ok: boolean; jobs: Job[]; saved: SavedOutput[] }>("/api/jobs"); | |
| } | |
| export async function refreshSaved() { | |
| return jsonRequest<{ ok: boolean; saved: SavedOutput[] }>("/api/saved"); | |
| } | |
| export async function refreshVoices() { | |
| return jsonRequest<{ ok: boolean; voices: Voice[] }>("/api/voices"); | |
| } | |
| export async function createJob(input: { | |
| title: string; | |
| text: string; | |
| voice: string; | |
| language: string; | |
| settings: GenerationSettings; | |
| }) { | |
| return jsonRequest<{ ok: boolean; job: Job; worker: { id?: string; created?: boolean; started?: boolean } }>("/api/jobs", input); | |
| } | |
| export async function createBatch(items: BulkScript[], settings: GenerationSettings, parallelWorkers = 1) { | |
| return jsonRequest<{ ok: boolean; jobs: Job[]; worker: { id?: string; created?: boolean; started?: boolean }; workers?: Array<{ id?: string; slot?: number; created?: boolean; started?: boolean }> }>("/api/batches", { items, settings, parallel_workers: parallelWorkers }); | |
| } | |
| export async function resumeBatch() { | |
| return jsonRequest<{ ok: boolean; jobs: Job[]; message?: string; worker?: { id?: string; created?: boolean; started?: boolean } }>("/api/batches/resume", {}); | |
| } | |
| export async function cloneVoice(input: { name: string; language: string; refText: string; audio: File }) { | |
| const form = new FormData(); | |
| form.set("name", input.name); | |
| form.set("language", input.language); | |
| form.set("ref_text", input.refText); | |
| form.set("audio", input.audio); | |
| const response = await fetch("/api/voices/clone", { | |
| method: "POST", | |
| credentials: "same-origin", | |
| body: form | |
| }); | |
| return parseResponse<{ ok: boolean; message: string; voices: Voice[] }>(response); | |
| } | |
| export async function designVoice(input: { | |
| title: string; | |
| text: string; | |
| language: string; | |
| description: string; | |
| settings: GenerationSettings; | |
| }) { | |
| return jsonRequest<{ ok: boolean; job: Job; worker: { id?: string; created?: boolean; started?: boolean } }>("/api/voices/design", input); | |
| } | |
| export async function saveDesignedVoice(input: { | |
| name: string; | |
| language: string; | |
| ref_text: string; | |
| job_id?: string; | |
| output_path?: string; | |
| }) { | |
| return jsonRequest<{ ok: boolean; message: string; voices: Voice[] }>("/api/voices/save-designed", input); | |
| } | |