File size: 1,592 Bytes
d69917c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
import os
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 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='en', to=2)

    return translated_text, audio_path

if __name__ == "__main__":
    # print(audio_streaming("hello world"))
    # os.system(f"start {audio_streaming('hello world!')}")
    translate = set(GoogleTranslator().get_supported_languages(as_dict=True))
    speak = set(gtts.lang.tts_langs())
    not_speak = translate - speak
    print(not_speak, len(not_speak))