siddhartharya commited on
Commit
ef260c8
1 Parent(s): 772260c

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +11 -22
utils.py CHANGED
@@ -8,12 +8,10 @@ import re
8
  import tempfile
9
  import requests
10
  from bs4 import BeautifulSoup
11
- import subprocess
12
- import pyttsx3
13
- from pydub import AudioSegment
14
 
15
  groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])
16
  tokenizer = tiktoken.get_encoding("cl100k_base")
 
17
 
18
  class DialogueItem(BaseModel):
19
  speaker: Literal["Maria", "Sarah"]
@@ -96,23 +94,14 @@ def generate_script(system_prompt: str, input_text: str, tone: str, target_lengt
96
 
97
  return dialogue
98
 
99
- def generate_audio_espeak(text: str, speaker: str) -> str:
100
- voice = "en-us+f3" if speaker == "Maria" else "en-gb+f3"
101
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
102
- subprocess.call(['espeak-ng', '-v', voice, '-w', temp_audio.name, text])
103
- return temp_audio.name
104
-
105
- def generate_audio_pyttsx3(text: str, speaker: str) -> str:
106
- engine = pyttsx3.init()
107
- voices = engine.getProperty('voices')
108
- engine.setProperty('voice', voices[1].id if speaker == "Maria" else voices[0].id)
109
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
110
- engine.save_to_file(text, temp_audio.name)
111
- engine.runAndWait()
112
- return temp_audio.name
113
-
114
  def generate_audio(text: str, speaker: str) -> str:
115
- try:
116
- return generate_audio_espeak(text, speaker)
117
- except Exception:
118
- return generate_audio_pyttsx3(text, speaker)
 
 
 
 
 
 
 
8
  import tempfile
9
  import requests
10
  from bs4 import BeautifulSoup
 
 
 
11
 
12
  groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])
13
  tokenizer = tiktoken.get_encoding("cl100k_base")
14
+ VOICERSS_API_KEY = os.environ["VOICERSS_API_KEY"]
15
 
16
  class DialogueItem(BaseModel):
17
  speaker: Literal["Maria", "Sarah"]
 
94
 
95
  return dialogue
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  def generate_audio(text: str, speaker: str) -> str:
98
+ voice = "en-us" if speaker == "Maria" else "en-gb"
99
+ url = f"http://api.voicerss.org/?key={VOICERSS_API_KEY}&hl={voice}&src={text}"
100
+
101
+ response = requests.get(url)
102
+ if response.status_code != 200:
103
+ raise Exception(f"Error generating audio: {response.text}")
104
+
105
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
106
+ temp_audio.write(response.content)
107
+ return temp_audio.name