jeevzz commited on
Commit
ea7c4ee
·
verified ·
1 Parent(s): b2932a3

Update voice.py

Browse files
Files changed (1) hide show
  1. voice.py +48 -6
voice.py CHANGED
@@ -25,11 +25,53 @@ async def generate_audio(text: str, voice: str) -> str:
25
  return path
26
 
27
  except Exception as e:
28
- print(f"TTS Failed: {e}")
29
- # Print full traceback for debugging
30
- import traceback
31
- traceback.print_exc()
32
- raise e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  def get_voices():
35
  """
@@ -61,4 +103,4 @@ def get_voices():
61
 
62
  # 🇮🇪 Irish Accent - Charming
63
  {"name": "☘️ Emily (Irish Charm)", "id": "en-IE-EmilyNeural"},
64
- ]
 
25
  return path
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 espnet/kan-bayashi_ljspeech_vits (High quality single speaker)
32
+ from huggingface_hub import InferenceClient
33
+ from dotenv import load_dotenv
34
+ from pathlib import Path
35
+
36
+ # Load env vars if not already loaded
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
+ # Generate audio using HF API
44
+ # This model is a high quality female voice
45
+ audio_bytes = client.text_to_speech(text, model="espnet/kan-bayashi_ljspeech_vits")
46
+
47
+ fd, path = tempfile.mkstemp(suffix=".flac") # Model returns flac usually
48
+ os.close(fd)
49
+
50
+ with open(path, "wb") as f:
51
+ f.write(audio_bytes)
52
+
53
+ print("Successfully generated audio using HF Inference API")
54
+ return path
55
+
56
+ except Exception as e2:
57
+ print(f"HF API Fallback failed: {e2}. Attempting fallback to gTTS...")
58
+ try:
59
+ # Fallback 2: gTTS (Google Text-to-Speech)
60
+ from gtts import gTTS
61
+
62
+ fd, path = tempfile.mkstemp(suffix=".mp3")
63
+ os.close(fd)
64
+
65
+ # Use default English voice for fallback
66
+ tts = gTTS(text=text, lang='en')
67
+ tts.save(path)
68
+ print("Successfully generated audio using gTTS fallback")
69
+ return path
70
+ except Exception as e3:
71
+ print(f"gTTS Fallback also failed: {e3}")
72
+ import traceback
73
+ traceback.print_exc()
74
+ raise e
75
 
76
  def get_voices():
77
  """
 
103
 
104
  # 🇮🇪 Irish Accent - Charming
105
  {"name": "☘️ Emily (Irish Charm)", "id": "en-IE-EmilyNeural"},
106
+ ]