mgbam commited on
Commit
1071494
·
verified ·
1 Parent(s): 87c5f3b

Update genesis/narration.py

Browse files
Files changed (1) hide show
  1. genesis/narration.py +36 -29
genesis/narration.py CHANGED
@@ -1,40 +1,47 @@
1
  # genesis/narration.py
 
 
 
 
 
2
  import os
3
  import requests
4
- from typing import Optional
5
 
6
  ELEVEN_LABS_API_KEY = os.getenv("ELEVEN_LABS_API_KEY")
7
- VOICE_ID = "EXAVITQu4vr4xnSDxMaL" # Default voice; change in ElevenLabs dashboard if needed
8
-
9
- def narrate_text(text: str) -> Optional[str]:
10
- """Convert text to speech using ElevenLabs API and return audio URL."""
11
- if not ELEVEN_LABS_API_KEY:
12
- print("[Narration] No ElevenLabs API key provided.")
13
- return None
14
 
 
 
 
 
 
 
 
15
  try:
16
- url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}"
17
- headers = {
18
- "xi-api-key": ELEVEN_LABS_API_KEY,
19
- "Content-Type": "application/json"
20
- }
21
- payload = {
22
- "text": text,
23
- "voice_settings": {
24
- "stability": 0.4,
25
- "similarity_boost": 0.8
26
- }
27
- }
28
- r = requests.post(url, headers=headers, json=payload, timeout=30)
29
  r.raise_for_status()
30
-
31
- # Save audio locally
32
- audio_file = "narration.mp3"
33
- with open(audio_file, "wb") as f:
34
  f.write(r.content)
 
 
 
35
 
36
- # In Spaces, you can serve local files directly
37
- return audio_file
 
 
 
 
 
38
  except Exception as e:
39
- print(f"[Narration] Failed: {e}")
40
- return None
 
 
 
 
 
 
 
 
1
  # genesis/narration.py
2
+ """
3
+ Narration Module for GENESIS-AI
4
+ Generates audio narration from text using ElevenLabs, with gTTS fallback.
5
+ """
6
+
7
  import os
8
  import requests
9
+ from gtts import gTTS
10
 
11
  ELEVEN_LABS_API_KEY = os.getenv("ELEVEN_LABS_API_KEY")
12
+ ELEVEN_TTS_URL = "https://api.elevenlabs.io/v1/text-to-speech"
 
 
 
 
 
 
13
 
14
+ def narrate_text_elevenlabs(text, voice="Rachel"):
15
+ """Generate narration using ElevenLabs API."""
16
+ headers = {
17
+ "xi-api-key": ELEVEN_LABS_API_KEY,
18
+ "Content-Type": "application/json"
19
+ }
20
+ payload = {"text": text, "voice_settings": {"stability": 0.75, "similarity_boost": 0.75}}
21
  try:
22
+ r = requests.post(f"{ELEVEN_TTS_URL}/{voice}", json=payload, headers=headers)
 
 
 
 
 
 
 
 
 
 
 
 
23
  r.raise_for_status()
24
+ output_path = "narration.mp3"
25
+ with open(output_path, "wb") as f:
 
 
26
  f.write(r.content)
27
+ return output_path
28
+ except Exception as e:
29
+ return {"error": str(e)}
30
 
31
+ def narrate_text_gtts(text, lang="en"):
32
+ """Fallback narration with gTTS."""
33
+ try:
34
+ tts = gTTS(text=text, lang=lang)
35
+ output_path = "narration_fallback.mp3"
36
+ tts.save(output_path)
37
+ return output_path
38
  except Exception as e:
39
+ return {"error": str(e)}
40
+
41
+ def narrate_text(text):
42
+ """Try ElevenLabs first, else fallback."""
43
+ if ELEVEN_LABS_API_KEY:
44
+ result = narrate_text_elevenlabs(text)
45
+ if isinstance(result, str):
46
+ return result
47
+ return narrate_text_gtts(text)