|
import streamlit as st |
|
import speech_recognition as sr |
|
from gtts import gTTS |
|
import os |
|
|
|
def transcribe_audio(audio_file): |
|
"""Transcribe uploaded audio file to text.""" |
|
recognizer = sr.Recognizer() |
|
try: |
|
|
|
with open("temp_audio.wav", "wb") as f: |
|
f.write(audio_file.read()) |
|
|
|
|
|
with sr.AudioFile("temp_audio.wav") as source: |
|
audio = recognizer.record(source) |
|
text = recognizer.recognize_google(audio) |
|
|
|
|
|
os.remove("temp_audio.wav") |
|
return text |
|
except Exception as e: |
|
st.error(f"Audio transcription failed: {str(e)}") |
|
return "" |
|
|
|
def text_to_speech(text, target_lang): |
|
"""Convert translated text to audio with robust handling.""" |
|
try: |
|
if not text: |
|
st.error("No text to convert to audio.") |
|
return None |
|
|
|
|
|
lang_map = { |
|
"English": "en", |
|
"French": "fr", |
|
"Spanish": "es", |
|
"German": "de", |
|
"Chinese": "zh-cn", |
|
"Arabic": "ar", |
|
"Russian": "ru", |
|
"Hindi": "hi", |
|
"Japanese": "ja", |
|
} |
|
lang_code = lang_map.get(target_lang, "en") |
|
|
|
|
|
st.write(f"Generating audio for language code: {lang_code}") |
|
tts = gTTS(text=text, lang=lang_code, slow=False) |
|
audio_path = "output_audio.mp3" |
|
tts.save(audio_path) |
|
|
|
|
|
if os.path.exists(audio_path): |
|
st.success(f"Audio generated at: {audio_path}") |
|
return audio_path |
|
else: |
|
st.error("Audio file not found after generation. Check server permissions.") |
|
return None |
|
except Exception as e: |
|
st.error(f"Audio generation failed: {str(e)}") |
|
return None |