|
import os |
|
from transformers import pipeline |
|
from huggingface_hub import InferenceClient |
|
import gradio as gr |
|
|
|
|
|
|
|
translation_pipeline = pipeline("translation", model="facebook/nllb-200-distilled-600M", src_lang="zh", tgt_lang="en") |
|
|
|
|
|
tts_pipeline = pipeline("text-to-speech", model="facebook/mms-tts-eng") |
|
|
|
|
|
def translate_and_speak(chinese_text): |
|
""" |
|
Translates Chinese text to English and generates speech. |
|
""" |
|
|
|
|
|
translated_text = translation_pipeline([chinese_text])[0]['translation_text'] |
|
|
|
|
|
|
|
try: |
|
|
|
audio_output = tts_pipeline(translated_text) |
|
|
|
audio_bytes = audio_output['audio'] |
|
sampling_rate = audio_output['sampling_rate'] |
|
return translated_text, (sampling_rate, audio_bytes) |
|
|
|
except Exception as e: |
|
return translated_text, None, f"Error generating speech: {e}" |
|
|
|
|
|
|
|
iface = gr.Interface( |
|
fn=translate_and_speak, |
|
inputs=gr.Textbox(label="Enter Chinese Text"), |
|
outputs=[ |
|
gr.Textbox(label="Translated English Text"), |
|
gr.Audio(label="Generated Speech", format="wav") |
|
], |
|
title="Chinese to English Translation and Text-to-Speech", |
|
description="Translate Chinese text to English and listen to the English translation." |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |