Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
|
@@ -174,14 +174,58 @@ for lang, voices in YOURVOIC_VOICE_MAP.items():
|
|
| 174 |
|
| 175 |
def get_yourvoic_voice_for_language(language, selected_voice):
|
| 176 |
"""Get a valid voice name for the given language.
|
| 177 |
-
|
| 178 |
-
Otherwise, fall back to the first voice for that language."""
|
| 179 |
voice_name = get_voice_name(selected_voice)
|
| 180 |
-
valid_voices = YOURVOIC_VOICE_MAP.get(language,
|
|
|
|
|
|
|
| 181 |
if voice_name in valid_voices:
|
| 182 |
return voice_name
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
YOURVOIC_MODELS = [
|
| 187 |
"aura-prime -- Balanced quality and speed (recommended)",
|
|
|
|
| 174 |
|
| 175 |
def get_yourvoic_voice_for_language(language, selected_voice):
|
| 176 |
"""Get a valid voice name for the given language.
|
| 177 |
+
First tries the selected voice, then hardcoded map, then queries the API."""
|
|
|
|
| 178 |
voice_name = get_voice_name(selected_voice)
|
| 179 |
+
valid_voices = YOURVOIC_VOICE_MAP.get(language, [])
|
| 180 |
+
|
| 181 |
+
# If selected voice is in the valid list, use it
|
| 182 |
if voice_name in valid_voices:
|
| 183 |
return voice_name
|
| 184 |
+
|
| 185 |
+
# Try first valid voice from hardcoded map
|
| 186 |
+
if valid_voices:
|
| 187 |
+
return valid_voices[0]
|
| 188 |
+
|
| 189 |
+
# Fallback: query the YourVoic API for valid voices
|
| 190 |
+
yourvoic_lang = LANGUAGES.get(language, {}).get("yourvoic", "en-US")
|
| 191 |
+
return _fetch_yourvoic_voice(yourvoic_lang)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
# Cache for API-fetched voices
|
| 195 |
+
_yourvoic_voice_cache = {}
|
| 196 |
+
|
| 197 |
+
def _fetch_yourvoic_voice(yourvoic_lang):
|
| 198 |
+
"""Query YourVoic /v1/voices endpoint to get a valid voice for a language."""
|
| 199 |
+
if yourvoic_lang in _yourvoic_voice_cache:
|
| 200 |
+
return _yourvoic_voice_cache[yourvoic_lang]
|
| 201 |
+
|
| 202 |
+
yv_key = os.environ.get("YOURVOIC_API_KEY", "")
|
| 203 |
+
if not yv_key:
|
| 204 |
+
return "Peter" # fallback
|
| 205 |
+
|
| 206 |
+
try:
|
| 207 |
+
resp = http_requests.get(
|
| 208 |
+
f"{YOURVOIC_VOICES_URL}?language={yourvoic_lang}",
|
| 209 |
+
headers={"X-API-Key": yv_key},
|
| 210 |
+
timeout=15,
|
| 211 |
+
)
|
| 212 |
+
print(f"[YourVoic] Voices API for {yourvoic_lang}: status={resp.status_code}")
|
| 213 |
+
if resp.status_code == 200:
|
| 214 |
+
data = resp.json()
|
| 215 |
+
voices = data if isinstance(data, list) else data.get("voices", data.get("data", []))
|
| 216 |
+
if voices:
|
| 217 |
+
# Get first voice name
|
| 218 |
+
if isinstance(voices[0], dict):
|
| 219 |
+
voice_name = voices[0].get("name", voices[0].get("voice_id", "Peter"))
|
| 220 |
+
else:
|
| 221 |
+
voice_name = str(voices[0])
|
| 222 |
+
print(f"[YourVoic] Found voice for {yourvoic_lang}: {voice_name}")
|
| 223 |
+
_yourvoic_voice_cache[yourvoic_lang] = voice_name
|
| 224 |
+
return voice_name
|
| 225 |
+
except Exception as e:
|
| 226 |
+
print(f"[YourVoic] Voice lookup failed for {yourvoic_lang}: {e}")
|
| 227 |
+
|
| 228 |
+
return "Peter" # ultimate fallback
|
| 229 |
|
| 230 |
YOURVOIC_MODELS = [
|
| 231 |
"aura-prime -- Balanced quality and speed (recommended)",
|