// api.js — conexión compartida al backend Gradio (chat / transcribe / speak). import { Client } from "https://cdn.jsdelivr.net/npm/@gradio/client/dist/index.min.js"; let clientPromise = null; export function getClient() { if (!clientPromise) { clientPromise = Client.connect(window.location.origin); } return clientPromise; } export async function transcribe(blob, ext, durationMs) { const client = await getClient(); const audioFile = new File([blob], `audio.${ext}`, { type: blob.type }); const r = await client.predict("/transcribe", { audio: audioFile, duration_ms: durationMs }); return r.data?.[0] ?? {}; } export async function chat(message, childAge = 3) { const client = await getClient(); const r = await client.predict("/chat", { message, child_age: childAge }); return r.data?.[0] ?? {}; } export async function speak(text) { const client = await getClient(); const r = await client.predict("/speak", { text }); // FileData puede venir con .url o con .path (Gradio lo resuelve según versión) const fd = r.data?.[0]; return fd?.url ?? fd?.path ?? ""; }