fix: TTS voice upload updates voice_to_speaker.yaml + language selector + v0.8.3
- compose.yaml: mount gpu/tts/config → /app/config for voice_to_speaker.yaml - /api/tts/upload-voice: creates/updates voice_to_speaker.yaml with new voice entry - TTSTab: language selector (default Russian), passed to API - /api/tts/generate: arrayBuffer instead of streaming for reliability
This commit is contained in:
@@ -120,7 +120,7 @@ export async function GET() {
|
||||
ok: envError === null,
|
||||
error: envError,
|
||||
},
|
||||
version: "0.8.2",
|
||||
version: "0.8.3",
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { promises as fs } from "node:fs";
|
||||
import { promises as fs, existsSync } from "node:fs";
|
||||
import path from "node:path";
|
||||
import { PROJECT_DIR } from "@/lib/docker";
|
||||
|
||||
@@ -7,13 +7,15 @@ export const dynamic = "force-dynamic";
|
||||
export const runtime = "nodejs";
|
||||
|
||||
const TTS_VOICES_DIR = path.join(PROJECT_DIR, "gpu", "tts", "voices");
|
||||
const TTS_CONFIG_DIR = path.join(PROJECT_DIR, "gpu", "tts", "config");
|
||||
const VOICE_CONFIG_PATH = path.join(TTS_CONFIG_DIR, "voice_to_speaker.yaml");
|
||||
|
||||
/**
|
||||
* POST /api/tts/upload-voice
|
||||
*
|
||||
* Uploads a custom voice sample (WAV file) for XTTSv2 voice cloning.
|
||||
* The file is saved to gpu/tts/voices/ which is mounted into the TTS
|
||||
* container at /app/voices/.
|
||||
* The file is saved to gpu/tts/voices/ AND the voice_to_speaker.yaml config
|
||||
* is updated so openedai-speech picks it up after restart.
|
||||
*
|
||||
* For XTTS voice samples:
|
||||
* - Mono (single channel) 22050 Hz WAV file
|
||||
@@ -34,6 +36,7 @@ export async function POST(req: NextRequest) {
|
||||
const form = await req.formData();
|
||||
const file = form.get("file");
|
||||
const name = form.get("name") as string | null;
|
||||
const language = (form.get("language") as string) || null;
|
||||
|
||||
if (!file || !(file instanceof File)) {
|
||||
return NextResponse.json(
|
||||
@@ -54,18 +57,24 @@ export async function POST(req: NextRequest) {
|
||||
const filename = `${voiceName}.wav`;
|
||||
const dest = path.join(TTS_VOICES_DIR, filename);
|
||||
|
||||
// Create directory if it doesn't exist
|
||||
// Create directories if they don't exist
|
||||
await fs.mkdir(TTS_VOICES_DIR, { recursive: true });
|
||||
await fs.mkdir(TTS_CONFIG_DIR, { recursive: true });
|
||||
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
await fs.writeFile(dest, buffer);
|
||||
|
||||
// Now update voice_to_speaker.yaml to include the new voice
|
||||
// openedai-speech reads this config at startup — without it, the voice
|
||||
// won't appear in the voices list even though the WAV file exists.
|
||||
await updateVoiceConfig(voiceName, filename, language);
|
||||
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
voice: voiceName,
|
||||
path: filename,
|
||||
size: buffer.length,
|
||||
hint: "Voice uploaded. Restart TTS container for it to pick up the new voice, or it may be available immediately depending on the backend.",
|
||||
hint: "Voice uploaded and config updated. Restart TTS container to apply.",
|
||||
});
|
||||
} catch (err) {
|
||||
return NextResponse.json(
|
||||
@@ -83,9 +92,108 @@ export async function POST(req: NextRequest) {
|
||||
export async function GET() {
|
||||
try {
|
||||
const entries = await fs.readdir(TTS_VOICES_DIR);
|
||||
const voices = entries.filter((f) => f.endsWith(".wav") || f.endsWith(".mp3") || f.endsWith(".flac"));
|
||||
const voices = entries.filter(
|
||||
(f) => f.endsWith(".wav") || f.endsWith(".mp3") || f.endsWith(".flac")
|
||||
);
|
||||
return NextResponse.json({ ok: true, voices });
|
||||
} catch {
|
||||
return NextResponse.json({ ok: true, voices: [] });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update voice_to_speaker.yaml to add a new voice entry.
|
||||
* If the file doesn't exist, create it with the default built-in voices
|
||||
* plus the new custom voice.
|
||||
*/
|
||||
async function updateVoiceConfig(
|
||||
voiceName: string,
|
||||
filename: string,
|
||||
language: string | null
|
||||
): Promise<void> {
|
||||
let existing = "";
|
||||
try {
|
||||
existing = await fs.readFile(VOICE_CONFIG_PATH, "utf-8");
|
||||
} catch {
|
||||
// File doesn't exist — create from scratch with default voices
|
||||
existing = createDefaultVoiceConfig();
|
||||
}
|
||||
|
||||
// Check if voice already exists in config
|
||||
if (existing.includes(` ${voiceName}:`)) {
|
||||
// Voice already in config — update the speaker file path
|
||||
existing = existing.replace(
|
||||
new RegExp(
|
||||
` ${voiceName}:\\n(?: [^\\n]+\\n)*`,
|
||||
"m"
|
||||
),
|
||||
formatVoiceEntry(voiceName, filename, language)
|
||||
);
|
||||
} else {
|
||||
// Add new voice under tts-1-hd section
|
||||
// Find the tts-1-hd: section and append
|
||||
if (existing.includes("tts-1-hd:")) {
|
||||
existing = existing.replace(
|
||||
/(tts-1-hd:\n)/,
|
||||
`$1${formatVoiceEntry(voiceName, filename, language)}`
|
||||
);
|
||||
} else {
|
||||
// No tts-1-hd section — add it
|
||||
existing += `\ntts-1-hd:\n${formatVoiceEntry(voiceName, filename, language)}`;
|
||||
}
|
||||
}
|
||||
|
||||
await fs.writeFile(VOICE_CONFIG_PATH, existing, "utf-8");
|
||||
}
|
||||
|
||||
function formatVoiceEntry(
|
||||
name: string,
|
||||
filename: string,
|
||||
language: string | null
|
||||
): string {
|
||||
let entry = ` ${name}:\n model: xtts\n speaker: voices/${filename}\n`;
|
||||
if (language) {
|
||||
entry += ` language: ${language}\n`;
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
function createDefaultVoiceConfig(): string {
|
||||
return `# voice_to_speaker.yaml — openedai-speech voice configuration
|
||||
# Custom voices are added here. Restart TTS container after changes.
|
||||
|
||||
tts-1:
|
||||
alloy:
|
||||
model: voices/en_US-amy-medium.onnx
|
||||
echo:
|
||||
model: voices/en_US-libritts-high.onnx
|
||||
fable:
|
||||
model: voices/en_US-ryan-high.onnx
|
||||
onyx:
|
||||
model: voices/en_US-daniel-low.onnx
|
||||
nova:
|
||||
model: voices/en_US-lessac-medium.onnx
|
||||
shimmer:
|
||||
model: voices/en_US-nora-medium.onnx
|
||||
|
||||
tts-1-hd:
|
||||
alloy:
|
||||
model: xtts
|
||||
speaker: voices/alloy.wav
|
||||
echo:
|
||||
model: xtts
|
||||
speaker: voices/echo.wav
|
||||
fable:
|
||||
model: xtts
|
||||
speaker: voices/fable.wav
|
||||
onyx:
|
||||
model: xtts
|
||||
speaker: voices/onyx.wav
|
||||
nova:
|
||||
model: xtts
|
||||
speaker: voices/nova.wav
|
||||
shimmer:
|
||||
model: xtts
|
||||
speaker: voices/shimmer.wav
|
||||
`;
|
||||
}
|
||||
|
||||
@@ -4439,6 +4439,7 @@ function TTSTab({
|
||||
const [audioUrl, setAudioUrl] = React.useState<string | null>(null);
|
||||
const [error, setError] = React.useState<string>("");
|
||||
const [uploadProgress, setUploadProgress] = React.useState<string | null>(null);
|
||||
const [voiceLanguage, setVoiceLanguage] = React.useState<string>("ru");
|
||||
const fileInputRef = React.useRef<HTMLInputElement | null>(null);
|
||||
|
||||
const refreshVoices = React.useCallback(async () => {
|
||||
@@ -4496,7 +4497,7 @@ function TTSTab({
|
||||
}
|
||||
}
|
||||
|
||||
async function handleUploadVoice(file: File) {
|
||||
async function handleUploadVoice(file: File, language: string = "ru") {
|
||||
if (!file.name.match(/\.(wav|mp3|flac)$/i)) {
|
||||
setError("Voice sample must be WAV/MP3/FLAC");
|
||||
return;
|
||||
@@ -4508,13 +4509,14 @@ function TTSTab({
|
||||
formData.append("file", file);
|
||||
const name = file.name.replace(/\.[^.]+$/, "").replace(/[^\w\-]/g, "_");
|
||||
formData.append("name", name);
|
||||
formData.append("language", language);
|
||||
const res = await fetch("/api/tts/upload-voice", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
const data = await res.json();
|
||||
if (data.ok) {
|
||||
setError(`Voice "${data.voice}" uploaded. Restart TTS to apply.`);
|
||||
setError(`Voice "${data.voice}" uploaded (${language}). Restart TTS to apply.`);
|
||||
refreshVoices();
|
||||
} else {
|
||||
setError(data.error || "Upload failed");
|
||||
@@ -4707,8 +4709,8 @@ function TTSTab({
|
||||
|
||||
{/* Voice upload (for XTTS custom voices) */}
|
||||
<div className="rounded-md border p-3 space-y-2 bg-muted/20">
|
||||
<div className="flex items-center justify-between">
|
||||
<Label>Custom voices (XTTSv2)</Label>
|
||||
<Label>Custom voices (XTTSv2)</Label>
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<Button
|
||||
size="sm"
|
||||
variant="outline"
|
||||
@@ -4725,9 +4727,32 @@ function TTSTab({
|
||||
className="hidden"
|
||||
onChange={(e) => {
|
||||
const f = e.target.files?.[0];
|
||||
if (f) handleUploadVoice(f);
|
||||
if (f) handleUploadVoice(f, voiceLanguage);
|
||||
}}
|
||||
/>
|
||||
<Select value={voiceLanguage} onValueChange={setVoiceLanguage}>
|
||||
<SelectTrigger className="w-32">
|
||||
<SelectValue />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="ru">Russian (ru)</SelectItem>
|
||||
<SelectItem value="en">English (en)</SelectItem>
|
||||
<SelectItem value="es">Spanish (es)</SelectItem>
|
||||
<SelectItem value="fr">French (fr)</SelectItem>
|
||||
<SelectItem value="de">German (de)</SelectItem>
|
||||
<SelectItem value="it">Italian (it)</SelectItem>
|
||||
<SelectItem value="pt">Portuguese (pt)</SelectItem>
|
||||
<SelectItem value="pl">Polish (pl)</SelectItem>
|
||||
<SelectItem value="tr">Turkish (tr)</SelectItem>
|
||||
<SelectItem value="nl">Dutch (nl)</SelectItem>
|
||||
<SelectItem value="cs">Czech (cs)</SelectItem>
|
||||
<SelectItem value="ar">Arabic (ar)</SelectItem>
|
||||
<SelectItem value="zh-cn">Chinese (zh-cn)</SelectItem>
|
||||
<SelectItem value="ja">Japanese (ja)</SelectItem>
|
||||
<SelectItem value="ko">Korean (ko)</SelectItem>
|
||||
<SelectItem value="hi">Hindi (hi)</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
</div>
|
||||
{uploadProgress && (
|
||||
<p className="text-xs text-muted-foreground">
|
||||
@@ -4738,8 +4763,8 @@ function TTSTab({
|
||||
<strong className="text-foreground">For natural Russian:</strong>{" "}
|
||||
built-in voices (alloy, nova, shimmer) are English-trained and will
|
||||
speak Russian with an accent. Upload a clean WAV sample of a Russian
|
||||
speaker (10-20s, mono 22050 Hz, no noise) → restart TTS → select
|
||||
the new voice. XTTSv2 supports Russian natively.
|
||||
speaker (10-20s, mono 22050 Hz, no noise), select the language, and
|
||||
restart TTS. The new voice will appear in the dropdown above.
|
||||
<br />
|
||||
<span className="mt-1 block">
|
||||
Good sources: audiobook samples (lib.ru), news clips, or record
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
* Displayed in the footer and available via /api/debug so the user can
|
||||
* verify the update actually took effect.
|
||||
*/
|
||||
export const DASHBOARD_VERSION = "0.8.2";
|
||||
export const DASHBOARD_VERSION = "0.8.3";
|
||||
|
||||
/**
|
||||
* Build timestamp — set at build time by Next.js.
|
||||
|
||||
Reference in New Issue
Block a user