Spaces:
Running
on
Zero
Running
on
Zero
import os | |
import soundfile as sf | |
from kokoro import KPipeline | |
import random | |
import spaces | |
pipeline = KPipeline(lang_code='a') # 'a' for English | |
ENGLISH_VOICES = [ | |
"af_bella", | |
"af_nicole", | |
"am_michael", | |
"am_fenrir" | |
] | |
def generate_voice(text: str, path: str): | |
for voice in random.sample(ENGLISH_VOICES, len(ENGLISH_VOICES)): | |
try: | |
print(f"π Trying voice: {voice}") | |
generator = pipeline(text, voice=voice) | |
for i, (gs, ps, audio) in enumerate(generator): | |
if i == 0: | |
sf.write(path, audio, 24000) | |
print(f"β Audio saved with {voice} at: {path}") | |
return True | |
except Exception as e: | |
print(f"β Failed with {voice}: {e}") | |
continue | |
print("π All voices failed.") | |
if os.path.exists(path): | |
os.remove(path) | |
print("ποΈ Removed broken file.") | |
return False | |