File size: 1,468 Bytes
3f9a435 5085eea d04ec9a 3f9a435 a977cc2 3f9a435 d04ec9a 3f9a435 42c91bd b44ad5c 3f9a435 d04ec9a 769175a d04ec9a 769175a 3f9a435 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import streamlit as st
import speech_recognition as sr
from gtts import gTTS
import io
import time
def transcribe_audio(audio_file):
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)
return text
except Exception as e:
st.error(f"Transcription failed: {str(e)}")
return ""
def text_to_speech(text, target_lang, max_retries=2):
try:
if not text:
return None
lang_map = {
"English": "en", "French": "fr", "Spanish": "es", "German": "de",
"Hindi": "hi", "Chinese": "zh-cn", "Arabic": "ar", "Russian": "ru", "Japanese": "ja"
}
lang_code = lang_map.get(target_lang, "en")
for attempt in range(max_retries):
try:
tts = gTTS(text=text[:200], lang=lang_code, slow=False)
audio_buffer = io.BytesIO()
tts.write_to_fp(audio_buffer)
audio_buffer.seek(0)
if audio_buffer.getbuffer().nbytes > 0:
return audio_buffer
time.sleep(2 ** attempt)
except Exception:
if attempt == max_retries - 1:
return None
return None
except Exception:
return None |