SmilingTree commited on
Commit
a01445b
·
verified ·
1 Parent(s): 7aae4a7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from transformers import pipeline
3
+ from huggingface_hub import InferenceClient
4
+ import gradio as gr
5
+
6
+ # Initialize the translation pipeline
7
+ translation_pipeline = pipeline("translation", model="facebook/nllb-200-distilled-600M")
8
+
9
+ # Initialize the text-to-speech client
10
+ # Make sure you have HF_TOKEN set as a secret in your Hugging Face Space
11
+ client = InferenceClient(
12
+ provider="fal-ai",
13
+ api_key=os.environ.get("HF_TOKEN"),
14
+ )
15
+
16
+ def translate_and_speak(chinese_text):
17
+ """
18
+ Translates Chinese text to English and generates speech.
19
+ """
20
+ # Translate the text
21
+ translated_text = translation_pipeline(chinese_text)[0]['translation_text']
22
+
23
+ # Generate speech from the translated text
24
+ # Use a try-except block to handle potential errors during speech generation
25
+ try:
26
+ audio_bytes = client.text_to_speech(
27
+ translated_text,
28
+ model="hexgrad/Kokoro-82M",
29
+ )
30
+ return translated_text, audio_bytes
31
+ except Exception as e:
32
+ return translated_text, f"Error generating speech: {e}"
33
+
34
+
35
+ # Create the Gradio interface
36
+ iface = gr.Interface(
37
+ fn=translate_and_speak,
38
+ inputs=gr.Textbox(label="Enter Chinese Text"),
39
+ outputs=[
40
+ gr.Textbox(label="Translated English Text"),
41
+ gr.Audio(label="Generated Speech", format="wav")
42
+ ],
43
+ title="Chinese to English Translation and Text-to-Speech",
44
+ description="Translate Chinese text to English and listen to the English translation."
45
+ )
46
+
47
+ # Launch the Gradio app
48
+ if __name__ == "__main__":
49
+ iface.launch()