Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -55,18 +55,19 @@ def translate_text(text, target_language):
|
|
55 |
except Exception as e:
|
56 |
return None, f"Translation error: {str(e)}"
|
57 |
|
58 |
-
# Function to convert text to speech using Kokoro or gTTS
|
59 |
-
def text_to_speech(text, language
|
60 |
try:
|
61 |
-
if
|
|
|
62 |
# Use Kokoro TTS
|
63 |
lang_code = KOKORO_LANGUAGES[language]
|
64 |
pipeline = KPipeline(lang_code=lang_code)
|
65 |
generator = pipeline(text, voice="af_heart", speed=1, split_pattern=r'\n+')
|
66 |
audio_data = None
|
67 |
for i, (gs, ps, audio) in enumerate(generator):
|
68 |
-
audio_data = audio # Use the
|
69 |
-
break
|
70 |
if audio_data is None:
|
71 |
raise ValueError("No audio generated by Kokoro")
|
72 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
|
@@ -84,7 +85,7 @@ def text_to_speech(text, language, tts_engine):
|
|
84 |
return None, f"TTS error: {str(e)}"
|
85 |
|
86 |
# Main function to process audio input and return outputs
|
87 |
-
def process_audio(audio_file, target_language
|
88 |
if audio_file is None:
|
89 |
return "Please upload an audio file or record audio.", None, None, None
|
90 |
|
@@ -96,7 +97,7 @@ def process_audio(audio_file, target_language, tts_engine):
|
|
96 |
if error:
|
97 |
return error, transcription, None, None
|
98 |
|
99 |
-
audio_output, error = text_to_speech(translated_text, target_language
|
100 |
if error:
|
101 |
return error, transcription, translated_text, None
|
102 |
|
@@ -105,23 +106,17 @@ def process_audio(audio_file, target_language, tts_engine):
|
|
105 |
# Gradio interface
|
106 |
with gr.Blocks(title="AI Audio Translator") as demo:
|
107 |
gr.Markdown("# AI Audio Translator")
|
108 |
-
gr.Markdown("Upload an audio file or record via microphone, select a target language
|
109 |
|
110 |
supported_langs = list(set(list(KOKORO_LANGUAGES.keys()) + list({v: k for k, v in lang.tts_langs().items()}.keys())))
|
111 |
|
112 |
with gr.Row():
|
113 |
audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Input Audio")
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
)
|
120 |
-
tts_engine = gr.Radio(
|
121 |
-
choices=["Kokoro", "gTTS"],
|
122 |
-
value="gTTS",
|
123 |
-
label="Text-to-Speech Engine"
|
124 |
-
)
|
125 |
|
126 |
submit_btn = gr.Button("Translate")
|
127 |
|
@@ -133,7 +128,7 @@ with gr.Blocks(title="AI Audio Translator") as demo:
|
|
133 |
|
134 |
submit_btn.click(
|
135 |
fn=process_audio,
|
136 |
-
inputs=[audio_input, target_lang
|
137 |
outputs=[error_output, transcription_output, translation_output, audio_output]
|
138 |
)
|
139 |
|
|
|
55 |
except Exception as e:
|
56 |
return None, f"Translation error: {str(e)}"
|
57 |
|
58 |
+
# Function to convert text to speech using Kokoro or gTTS based on language
|
59 |
+
def text_to_speech(text, language):
|
60 |
try:
|
61 |
+
# Check if the language is supported by Kokoro
|
62 |
+
if language in KOKORO_LANGUAGES:
|
63 |
# Use Kokoro TTS
|
64 |
lang_code = KOKORO_LANGUAGES[language]
|
65 |
pipeline = KPipeline(lang_code=lang_code)
|
66 |
generator = pipeline(text, voice="af_heart", speed=1, split_pattern=r'\n+')
|
67 |
audio_data = None
|
68 |
for i, (gs, ps, audio) in enumerate(generator):
|
69 |
+
audio_data = audio # Use the first segment
|
70 |
+
break
|
71 |
if audio_data is None:
|
72 |
raise ValueError("No audio generated by Kokoro")
|
73 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
|
|
|
85 |
return None, f"TTS error: {str(e)}"
|
86 |
|
87 |
# Main function to process audio input and return outputs
|
88 |
+
def process_audio(audio_file, target_language):
|
89 |
if audio_file is None:
|
90 |
return "Please upload an audio file or record audio.", None, None, None
|
91 |
|
|
|
97 |
if error:
|
98 |
return error, transcription, None, None
|
99 |
|
100 |
+
audio_output, error = text_to_speech(translated_text, target_language)
|
101 |
if error:
|
102 |
return error, transcription, translated_text, None
|
103 |
|
|
|
106 |
# Gradio interface
|
107 |
with gr.Blocks(title="AI Audio Translator") as demo:
|
108 |
gr.Markdown("# AI Audio Translator")
|
109 |
+
gr.Markdown("Upload an audio file or record via microphone, select a target language, and get the transcription, translation, and translated audio! Uses Kokoro TTS for supported languages, otherwise gTTS.")
|
110 |
|
111 |
supported_langs = list(set(list(KOKORO_LANGUAGES.keys()) + list({v: k for k, v in lang.tts_langs().items()}.keys())))
|
112 |
|
113 |
with gr.Row():
|
114 |
audio_input = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Input Audio")
|
115 |
+
target_lang = gr.Dropdown(
|
116 |
+
choices=sorted(supported_langs),
|
117 |
+
value="Spanish",
|
118 |
+
label="Target Language"
|
119 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
submit_btn = gr.Button("Translate")
|
122 |
|
|
|
128 |
|
129 |
submit_btn.click(
|
130 |
fn=process_audio,
|
131 |
+
inputs=[audio_input, target_lang],
|
132 |
outputs=[error_output, transcription_output, translation_output, audio_output]
|
133 |
)
|
134 |
|