from gtts import gTTS from deep_translator import GoogleTranslator import soundfile as sf import tempfile import numpy as np import gtts output_path = 'Audio/output.wav' translate_path = 'Audio/translate.wav' def get_lang(lang): translate = GoogleTranslator().get_supported_languages() speak = list(gtts.lang.tts_langs().values()) speak = list(map(lambda x: x.lower(), speak)) speak.sort() set_lang = list(set(speak) & set(translate)) set_lang.sort() if lang in set_lang: for key, value in gtts.lang.tts_langs().items(): if value.lower() == lang: return key else: return 'en' else: return 'en' def audio_streaming(txt=None, lang='en', to=None): # If an audio file is provided as input, use it; otherwise, use the direct file path speak = gTTS(text=txt, lang=lang, slow=False) if to == 1: audio = output_path else: audio = translate_path speak.save(audio) # Load the audio file data, samplerate = sf.read(audio) # Ensure data is in float32 format data = np.array(data, dtype=np.float32) # Save to a temporary file that Gradio can use for audio playback with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file: sf.write(tmp_file.name, data, samplerate) temp_audio_path = tmp_file.name # Return the file path to Gradio return temp_audio_path def translate_txt(lang, text): translator = GoogleTranslator(source="en", target=lang) translated_text = translator.translate(text) audio_path = audio_streaming(translated_text, lang=get_lang(lang), to=2) return translated_text, audio_path if __name__ == "__main__": # print(audio_streaming("hello world")) # os.system(f"start {audio_streaming('hello world!')}") translate = GoogleTranslator().get_supported_languages() # print(f"Can translate: {translate}", len(translate)) # print() speak = list(gtts.lang.tts_langs().values()) speak = list(map(lambda x: x.lower(), speak)) speak.sort() # print(f"Can speak: {speak}", len(speak)) # print() set_lang = list(set(speak)&set(translate)) set_lang.sort() # print(f"Set of lang: {set_lang}", len(set_lang)) not_in_speak = list(set(translate) - set(speak)) print(not_in_speak)