Spaces:
Running
Running
Download frontend/src/components/ScriptAudio.tsx from userIdc2024/creative-brain-os: direct link, hf CLI and curl.
- Browser
- Download file 4.27 kB
-
https://huggingface.co/spaces/userIdc2024/creative-brain-os/resolve/main/frontend/src/components/ScriptAudio.tsx
- Command line
-
hf download hf://spaces/userIdc2024/creative-brain-os/frontend/src/components/ScriptAudio.tsx
-
curl -L -o ScriptAudio.tsx https://huggingface.co/spaces/userIdc2024/creative-brain-os/resolve/main/frontend/src/components/ScriptAudio.tsx
4.27 kB
| import { useEffect, useRef, useState } from "react"; | |
| import { api, ApiError } from "../api/client"; | |
| import type { Voice } from "../api/types"; | |
| // Shown only if GET /tts/voices fails, so the picker is never empty. Must be a | |
| // voice the speech model actually has (see app/services/speech.py VOICES). | |
| const FALLBACK_VOICES: Voice[] = [ | |
| { name: "Roger", characteristic: "Confident, persuasive (male)" }, | |
| ]; | |
| /** Empty value = let the server cast the voice from the presenter description. | |
| * It is the default because a hand-picked voice is how an animated woman ended up | |
| * narrated by a man: the pipeline had no idea who was on screen. */ | |
| const AUTO_VOICE = ""; | |
| export function ScriptAudio({ | |
| assetId, | |
| candidateNumber, | |
| }: { | |
| assetId: string; | |
| candidateNumber: number; | |
| }) { | |
| const [voices, setVoices] = useState<Voice[] | null>(null); | |
| const [voice, setVoice] = useState(AUTO_VOICE); | |
| const [audioUrl, setAudioUrl] = useState<string | null>(null); | |
| const [loading, setLoading] = useState(false); | |
| const [error, setError] = useState<string | null>(null); | |
| const audioUrlRef = useRef<string | null>(null); | |
| useEffect(() => { | |
| api | |
| .listVoices() | |
| .then((rows) => { | |
| setVoices(rows); | |
| setVoice((current) => | |
| rows.some((v) => v.name === current) ? current : (rows[0]?.name ?? current), | |
| ); | |
| }) | |
| .catch(() => setVoices(FALLBACK_VOICES)); | |
| }, []); | |
| useEffect(() => { | |
| return () => { | |
| if (audioUrlRef.current) URL.revokeObjectURL(audioUrlRef.current); | |
| }; | |
| }, []); | |
| async function handleGenerate() { | |
| setLoading(true); | |
| setError(null); | |
| try { | |
| const blob = await api.generateCandidateAudio(assetId, candidateNumber, voice || null); | |
| if (audioUrlRef.current) URL.revokeObjectURL(audioUrlRef.current); | |
| const url = URL.createObjectURL(blob); | |
| audioUrlRef.current = url; | |
| setAudioUrl(url); | |
| } catch (err) { | |
| setError(err instanceof ApiError ? err.message : "Failed to generate audio"); | |
| } finally { | |
| setLoading(false); | |
| } | |
| } | |
| const options = voices ?? FALLBACK_VOICES; | |
| const selected = options.find((v) => v.name === voice); | |
| return ( | |
| <div className="border border-white/10 bg-slate-2 px-4 py-3"> | |
| <div className="flex flex-wrap items-center gap-2.5"> | |
| <span className="font-display text-xs tracking-wider text-chalk-dim uppercase"> | |
| Voiceover | |
| </span> | |
| <select | |
| value={voice} | |
| onChange={(e) => setVoice(e.target.value)} | |
| disabled={!voices} | |
| className="border border-white/15 bg-ink px-2 py-1 font-script text-xs text-chalk focus:border-gaffer focus:outline-none" | |
| > | |
| <option value={AUTO_VOICE}>Auto — match the presenter (recommended)</option> | |
| {options.map((v) => ( | |
| <option key={v.name} value={v.name}> | |
| {v.name} — {v.characteristic} | |
| </option> | |
| ))} | |
| </select> | |
| {selected ? ( | |
| <span className="font-script text-[11px] text-chalk-dim"> | |
| Tone: <span className="text-viewfinder">{selected.characteristic}</span> | |
| </span> | |
| ) : ( | |
| <span className="font-script text-[11px] text-chalk-dim"> | |
| Cast from the shot list — a woman on camera gets a woman’s voice. | |
| </span> | |
| )} | |
| <button | |
| type="button" | |
| onClick={handleGenerate} | |
| disabled={loading} | |
| className="border border-viewfinder/50 px-3 py-1 font-display text-xs tracking-wider text-viewfinder uppercase transition hover:bg-viewfinder hover:text-ink disabled:cursor-not-allowed disabled:opacity-50" | |
| > | |
| {loading ? "Generating…" : "▶ Generate audio"} | |
| </button> | |
| {error && <span className="text-xs text-tally">{error}</span>} | |
| </div> | |
| {audioUrl && ( | |
| <div className="mt-2.5 flex items-center gap-3"> | |
| <audio controls src={audioUrl} className="h-9 max-w-full" /> | |
| <a | |
| href={audioUrl} | |
| download={`candidate-${candidateNumber}-${voice || "auto"}.wav`} | |
| className="font-script text-xs text-viewfinder hover:underline" | |
| > | |
| Download | |
| </a> | |
| </div> | |
| )} | |
| </div> | |
| ); | |
| } | |