Spaces:
Running
Running
Update voice.py
Browse files
voice.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import edge_tts
|
| 2 |
import tempfile
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
# Use Microsoft Edge TTS - High quality neural voices
|
| 6 |
# Includes sexy, hot female voices with various accents
|
|
@@ -26,40 +27,53 @@ async def generate_audio(text: str, voice: str) -> str:
|
|
| 26 |
|
| 27 |
except Exception as e:
|
| 28 |
print(f"Edge TTS Failed: {e}. Attempting fallback to HF Inference API...")
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
-
# Fallback 1: Hugging Face Inference API (Realistic Female Voice)
|
| 31 |
-
# using facebook/mms-tts-eng (Very reliable)
|
| 32 |
from huggingface_hub import InferenceClient
|
| 33 |
from dotenv import load_dotenv
|
| 34 |
from pathlib import Path
|
| 35 |
|
| 36 |
-
# Load env vars
|
| 37 |
env_path = Path(__file__).parent / '.env'
|
| 38 |
load_dotenv(dotenv_path=env_path)
|
| 39 |
|
| 40 |
hf_token = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
client = InferenceClient(token=hf_token)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
#
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
print("WARNING: HF_TOKEN is missing! HF Inference API might fail or be rate limited.")
|
| 48 |
-
|
| 49 |
-
audio_bytes = client.text_to_speech(text, model="facebook/mms-tts-eng")
|
| 50 |
-
print(f"HF Inference successful, received {len(audio_bytes)} bytes")
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
print("Successfully generated audio using HF Inference API")
|
| 59 |
-
return path
|
| 60 |
|
| 61 |
except Exception as e2:
|
| 62 |
-
print(f"HF API Fallback failed
|
| 63 |
try:
|
| 64 |
# Fallback 2: gTTS (Google Text-to-Speech)
|
| 65 |
from gtts import gTTS
|
|
@@ -79,33 +93,7 @@ async def generate_audio(text: str, voice: str) -> str:
|
|
| 79 |
raise e
|
| 80 |
|
| 81 |
def get_voices():
|
| 82 |
-
|
| 83 |
-
Return curated list of sexy, hot female voices
|
| 84 |
-
Featuring Microsoft's best neural voices with various styles
|
| 85 |
-
"""
|
| 86 |
return [
|
| 87 |
-
#
|
| 88 |
-
{"name": "๐ Aria (Sexy US) - HOTTEST", "id": "en-US-AriaNeural"},
|
| 89 |
-
{"name": "๐ Jenny (Seductive US)", "id": "en-US-JennyNeural"},
|
| 90 |
-
{"name": "โจ Michelle (Flirty US)", "id": "en-US-MichelleNeural"},
|
| 91 |
-
{"name": "๐น Ashley (Sweet US)", "id": "en-US-AshleyNeural"},
|
| 92 |
-
{"name": "๐ Sara (Warm US)", "id": "en-US-SaraNeural"},
|
| 93 |
-
|
| 94 |
-
# ๐ฌ๐ง British Accent - Elegant & Sophisticated
|
| 95 |
-
{"name": "๐ Sonia (Sexy British)", "id": "en-GB-SoniaNeural"},
|
| 96 |
-
{"name": "๐ Libby (Cute British)", "id": "en-GB-LibbyNeural"},
|
| 97 |
-
{"name": "๐ Mia (Sweet British)", "id": "en-GB-MiaNeural"},
|
| 98 |
-
|
| 99 |
-
# ๐ฆ๐บ Australian Accent - Fun & Playful
|
| 100 |
-
{"name": "๐ด Natasha (Aussie Babe)", "id": "en-AU-NatashaNeural"},
|
| 101 |
-
{"name": "โ๏ธ Freya (Aussie Darling)", "id": "en-AU-FreyaNeural"},
|
| 102 |
-
|
| 103 |
-
# ๐ฎ๐ณ Indian Accent - Exotic & Beautiful
|
| 104 |
-
{"name": "๐บ Neerja (Indian Beauty)", "id": "en-IN-NeerjaNeural"},
|
| 105 |
-
|
| 106 |
-
# ๐จ๐ฆ Canadian - Friendly & Approachable
|
| 107 |
-
{"name": "๐ Clara (Canadian Cutie)", "id": "en-CA-ClaraNeural"},
|
| 108 |
-
|
| 109 |
-
# ๐ฎ๐ช Irish Accent - Charming
|
| 110 |
-
{"name": "โ๏ธ Emily (Irish Charm)", "id": "en-IE-EmilyNeural"},
|
| 111 |
]
|
|
|
|
| 1 |
import edge_tts
|
| 2 |
import tempfile
|
| 3 |
import os
|
| 4 |
+
import traceback
|
| 5 |
|
| 6 |
# Use Microsoft Edge TTS - High quality neural voices
|
| 7 |
# Includes sexy, hot female voices with various accents
|
|
|
|
| 27 |
|
| 28 |
except Exception as e:
|
| 29 |
print(f"Edge TTS Failed: {e}. Attempting fallback to HF Inference API...")
|
| 30 |
+
|
| 31 |
+
# Fallback 1: Hugging Face Inference API
|
| 32 |
try:
|
|
|
|
|
|
|
| 33 |
from huggingface_hub import InferenceClient
|
| 34 |
from dotenv import load_dotenv
|
| 35 |
from pathlib import Path
|
| 36 |
|
| 37 |
+
# Load env vars
|
| 38 |
env_path = Path(__file__).parent / '.env'
|
| 39 |
load_dotenv(dotenv_path=env_path)
|
| 40 |
|
| 41 |
hf_token = os.getenv("HF_TOKEN")
|
| 42 |
+
if hf_token:
|
| 43 |
+
print(f"HF_TOKEN found (starts with): {hf_token[:4]}...")
|
| 44 |
+
else:
|
| 45 |
+
print("WARNING: HF_TOKEN is MISSING in environment variables!")
|
| 46 |
+
|
| 47 |
client = InferenceClient(token=hf_token)
|
| 48 |
|
| 49 |
+
# List of models to try (in order of preference)
|
| 50 |
+
# 1. Facebook MMS (Reliable, standard)
|
| 51 |
+
# 2. ESPnet LJSpeech (High quality female voice)
|
| 52 |
+
models = ["facebook/mms-tts-eng", "espnet/kan-bayashi_ljspeech_vits"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
for model in models:
|
| 55 |
+
try:
|
| 56 |
+
print(f"Attempting HF Inference with model: {model}")
|
| 57 |
+
audio_bytes = client.text_to_speech(text, model=model)
|
| 58 |
+
print(f"HF Inference successful with {model}, received {len(audio_bytes)} bytes")
|
| 59 |
+
|
| 60 |
+
fd, path = tempfile.mkstemp(suffix=".flac")
|
| 61 |
+
os.close(fd)
|
| 62 |
+
|
| 63 |
+
with open(path, "wb") as f:
|
| 64 |
+
f.write(audio_bytes)
|
| 65 |
+
|
| 66 |
+
return path
|
| 67 |
+
except Exception as model_err:
|
| 68 |
+
print(f"HF Model {model} failed: {model_err}")
|
| 69 |
+
traceback.print_exc() # Print the full error stack
|
| 70 |
+
continue # Try next model
|
| 71 |
|
| 72 |
+
print("All HF models failed. Moving to gTTS...")
|
| 73 |
+
raise Exception("All HF models failed")
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
except Exception as e2:
|
| 76 |
+
print(f"HF API Fallback failed completely. Attempting fallback to gTTS...")
|
| 77 |
try:
|
| 78 |
# Fallback 2: gTTS (Google Text-to-Speech)
|
| 79 |
from gtts import gTTS
|
|
|
|
| 93 |
raise e
|
| 94 |
|
| 95 |
def get_voices():
|
| 96 |
+
# ... (Keep the rest of the file the same)
|
|
|
|
|
|
|
|
|
|
| 97 |
return [
|
| 98 |
+
# ...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
]
|