Update audio_processor.py
Browse files- audio_processor.py +12 -11
audio_processor.py
CHANGED
@@ -2,6 +2,7 @@ import streamlit as st
|
|
2 |
import speech_recognition as sr
|
3 |
from gtts import gTTS
|
4 |
import os
|
|
|
5 |
|
6 |
def transcribe_audio(audio_file):
|
7 |
"""Transcribe uploaded audio file to text."""
|
@@ -24,7 +25,7 @@ def transcribe_audio(audio_file):
|
|
24 |
return ""
|
25 |
|
26 |
def text_to_speech(text, target_lang):
|
27 |
-
"""Convert translated text to audio with robust handling."""
|
28 |
try:
|
29 |
if not text:
|
30 |
st.error("No text to convert to audio.")
|
@@ -44,19 +45,19 @@ def text_to_speech(text, target_lang):
|
|
44 |
}
|
45 |
lang_code = lang_map.get(target_lang, "en")
|
46 |
|
47 |
-
# Generate audio
|
48 |
st.write(f"Generating audio for language code: {lang_code}") # Debug output
|
49 |
tts = gTTS(text=text, lang=lang_code, slow=False)
|
50 |
-
|
51 |
-
tts.
|
|
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
return None
|
60 |
except Exception as e:
|
61 |
st.error(f"Audio generation failed: {str(e)}")
|
62 |
return None
|
|
|
2 |
import speech_recognition as sr
|
3 |
from gtts import gTTS
|
4 |
import os
|
5 |
+
import io
|
6 |
|
7 |
def transcribe_audio(audio_file):
|
8 |
"""Transcribe uploaded audio file to text."""
|
|
|
25 |
return ""
|
26 |
|
27 |
def text_to_speech(text, target_lang):
|
28 |
+
"""Convert translated text to audio with robust handling using memory buffer."""
|
29 |
try:
|
30 |
if not text:
|
31 |
st.error("No text to convert to audio.")
|
|
|
45 |
}
|
46 |
lang_code = lang_map.get(target_lang, "en")
|
47 |
|
48 |
+
# Generate audio in memory
|
49 |
st.write(f"Generating audio for language code: {lang_code}") # Debug output
|
50 |
tts = gTTS(text=text, lang=lang_code, slow=False)
|
51 |
+
audio_buffer = io.BytesIO()
|
52 |
+
tts.write_to_fp(audio_buffer)
|
53 |
+
audio_buffer.seek(0)
|
54 |
|
55 |
+
# Save temporarily for debugging (optional)
|
56 |
+
with open("output_audio.mp3", "wb") as f:
|
57 |
+
f.write(audio_buffer.getbuffer())
|
58 |
+
|
59 |
+
st.success(f"Audio generated in memory for {target_lang}")
|
60 |
+
return audio_buffer
|
|
|
61 |
except Exception as e:
|
62 |
st.error(f"Audio generation failed: {str(e)}")
|
63 |
return None
|