File size: 1,026 Bytes
8b05224
 
 
 
454765e
 
8b05224
 
 
 
046681b
 
 
 
8b05224
 
9afa8dd
8b05224
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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"     
]

@spaces.GPU()
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