File size: 1,323 Bytes
3f9a435 5085eea 3f9a435 a977cc2 3f9a435 42c91bd a977cc2 42c91bd a977cc2 3f9a435 a977cc2 5085eea a977cc2 5085eea 3f9a435 a977cc2 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 |
import streamlit as st
import speech_recognition as sr
from gtts import gTTS
import io
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):
try:
if not text:
st.error("No text to convert.")
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"Attempting audio for {lang_code}")
tts = gTTS(text=text[:200], lang=lang_code, slow=False) # Limit to 200 chars
audio_buffer = io.BytesIO()
tts.write_to_fp(audio_buffer)
audio_buffer.seek(0)
st.success(f"Audio generated for {target_lang}")
return audio_buffer
except Exception as e:
st.error(f"Audio failed: {str(e)}")
return None |