Spaces:
Running
Running
Update utils/audio_utils.py
Browse files- utils/audio_utils.py +10 -5
utils/audio_utils.py
CHANGED
|
@@ -24,7 +24,7 @@ async def text_to_speech(text: str, output_path: str = None) -> str:
|
|
| 24 |
client = AsyncElevenLabs(api_key=os.getenv('ELEVENLABS_API_KEY'))
|
| 25 |
|
| 26 |
# Generate audio
|
| 27 |
-
audio_generator =
|
| 28 |
text=text,
|
| 29 |
voice_id="21m00Tcm4TlvDq8ikWAM", # Rachel voice
|
| 30 |
model_id="eleven_monolingual_v1",
|
|
@@ -42,12 +42,17 @@ async def text_to_speech(text: str, output_path: str = None) -> str:
|
|
| 42 |
|
| 43 |
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
|
| 44 |
|
| 45 |
-
# Write audio chunks
|
| 46 |
with open(output_path, 'wb') as f:
|
| 47 |
async for chunk in audio_generator:
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
print(f"TTS Error: {e}")
|
|
@@ -109,4 +114,4 @@ async def process_audio_input(audio_data: bytes) -> str:
|
|
| 109 |
if os.path.exists(temp_path):
|
| 110 |
os.remove(temp_path)
|
| 111 |
|
| 112 |
-
return text
|
|
|
|
| 24 |
client = AsyncElevenLabs(api_key=os.getenv('ELEVENLABS_API_KEY'))
|
| 25 |
|
| 26 |
# Generate audio
|
| 27 |
+
audio_generator = client.text_to_speech.convert(
|
| 28 |
text=text,
|
| 29 |
voice_id="21m00Tcm4TlvDq8ikWAM", # Rachel voice
|
| 30 |
model_id="eleven_monolingual_v1",
|
|
|
|
| 42 |
|
| 43 |
Path(output_path).parent.mkdir(parents=True, exist_ok=True)
|
| 44 |
|
| 45 |
+
# Write audio chunks - FIXED: properly handle async generator
|
| 46 |
with open(output_path, 'wb') as f:
|
| 47 |
async for chunk in audio_generator:
|
| 48 |
+
if chunk:
|
| 49 |
+
f.write(chunk)
|
| 50 |
|
| 51 |
+
# Verify file was created and is not empty
|
| 52 |
+
if Path(output_path).is_file() and Path(output_path).stat().st_size > 0:
|
| 53 |
+
return output_path
|
| 54 |
+
else:
|
| 55 |
+
return ""
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
print(f"TTS Error: {e}")
|
|
|
|
| 114 |
if os.path.exists(temp_path):
|
| 115 |
os.remove(temp_path)
|
| 116 |
|
| 117 |
+
return text
|