jeevzz commited on
Commit
c08931b
·
verified ·
1 Parent(s): e1b96a4

Update voice.py

Browse files
Files changed (1) hide show
  1. voice.py +12 -6
voice.py CHANGED
@@ -29,7 +29,7 @@ async def generate_audio(text: str, voice: str) -> str:
29
 
30
  # Fallback 1: Hugging Face Inference API
31
  try:
32
- from huggingface_hub import InferenceClient
33
  from dotenv import load_dotenv
34
  from pathlib import Path
35
  import traceback
@@ -44,19 +44,25 @@ async def generate_audio(text: str, voice: str) -> str:
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
- # Use direct POST to bypass client-side provider lookup issues
58
- # The API returns raw audio bytes for TTS models
59
- audio_bytes = client.post(json={"inputs": text}, model=model)
 
 
 
 
 
 
60
  print(f"HF Inference successful with {model}, received {len(audio_bytes)} bytes")
61
 
62
  fd, path = tempfile.mkstemp(suffix=".flac")
 
29
 
30
  # Fallback 1: Hugging Face Inference API
31
  try:
32
+ import requests
33
  from dotenv import load_dotenv
34
  from pathlib import Path
35
  import traceback
 
44
  else:
45
  print("WARNING: HF_TOKEN is MISSING in environment variables!")
46
 
 
 
47
  # List of models to try (in order of preference)
48
  # 1. Facebook MMS (Reliable, standard)
49
  # 2. ESPnet LJSpeech (High quality female voice)
50
  models = ["facebook/mms-tts-eng", "espnet/kan-bayashi_ljspeech_vits"]
51
 
52
+ headers = {"Authorization": f"Bearer {hf_token}"}
53
+
54
  for model in models:
55
  try:
56
  print(f"Attempting HF Inference with model: {model}")
57
+ api_url = f"https://api-inference.huggingface.co/models/{model}"
58
+
59
+ # Use direct requests to bypass library version issues
60
+ response = requests.post(api_url, headers=headers, json={"inputs": text}, timeout=30)
61
+
62
+ if response.status_code != 200:
63
+ raise Exception(f"API returned status {response.status_code}: {response.text}")
64
+
65
+ audio_bytes = response.content
66
  print(f"HF Inference successful with {model}, received {len(audio_bytes)} bytes")
67
 
68
  fd, path = tempfile.mkstemp(suffix=".flac")