Spaces:
Running
Running
Update text_speech_utils.py
Browse files- text_speech_utils.py +25 -12
text_speech_utils.py
CHANGED
@@ -5,6 +5,7 @@ import soundfile as sf
|
|
5 |
from gtts import gTTS
|
6 |
import os
|
7 |
import tempfile
|
|
|
8 |
|
9 |
# Load Whisper model
|
10 |
model = whisper.load_model("base") # You can also try "small", "medium", or "large"
|
@@ -36,20 +37,32 @@ def transcribe_audio(filename):
|
|
36 |
# Function to save text as an audio file using gTTS (Google Text-to-Speech)
|
37 |
def save_text_as_audio(text, audio_filename):
|
38 |
print("Converting text to speech...")
|
39 |
-
tts = gTTS(text=text, lang='en', slow=False)
|
40 |
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
53 |
|
54 |
# Function to play audio using pydub's playback
|
55 |
def play_audio(filename):
|
|
|
5 |
from gtts import gTTS
|
6 |
import os
|
7 |
import tempfile
|
8 |
+
import time
|
9 |
|
10 |
# Load Whisper model
|
11 |
model = whisper.load_model("base") # You can also try "small", "medium", or "large"
|
|
|
37 |
# Function to save text as an audio file using gTTS (Google Text-to-Speech)
|
38 |
def save_text_as_audio(text, audio_filename):
|
39 |
print("Converting text to speech...")
|
|
|
40 |
|
41 |
+
try:
|
42 |
+
tts = gTTS(text=text, lang='en', slow=False)
|
43 |
+
|
44 |
+
# Save as mp3 file
|
45 |
+
mp3_filename = audio_filename.replace('.wav', '.mp3')
|
46 |
+
tts.save(mp3_filename)
|
47 |
+
print(f"Audio saved as {mp3_filename}")
|
48 |
+
|
49 |
+
# Convert mp3 to wav using pydub
|
50 |
+
audio = AudioSegment.from_mp3(mp3_filename)
|
51 |
+
audio.export(audio_filename, format="wav")
|
52 |
+
|
53 |
+
# Delete the temporary mp3 file
|
54 |
+
os.remove(mp3_filename)
|
55 |
+
print(f"Audio converted and saved as {audio_filename}")
|
56 |
|
57 |
+
except Exception as e:
|
58 |
+
print(f"Error occurred during text-to-speech conversion: {e}")
|
59 |
+
|
60 |
+
# In case of error (like hitting the rate limit), wait and retry
|
61 |
+
print("Waiting for 60 seconds before retrying...")
|
62 |
+
time.sleep(60) # wait for a minute before retrying
|
63 |
+
|
64 |
+
# Retry the conversion
|
65 |
+
save_text_as_audio(text, audio_filename) # Retry recursively
|
66 |
|
67 |
# Function to play audio using pydub's playback
|
68 |
def play_audio(filename):
|