sam-ezai commited on
Commit
9a8d919
·
verified ·
1 Parent(s): 564119f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -0
app.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import numpy as np
4
+ import io
5
+ import soundfile as sf # To read audio data into NumPy array
6
+
7
+ # Placeholder API URLs (Replace with actual endpoints)
8
+ TRANSLATION_API_URL = "https://twcc2.eztalking.ai/nantrans/inference"
9
+ TTS_API_URL = "http://twcc2.eztalking.ai/mtts/tts"
10
+
11
+
12
+ def fetch_translation(text, target_language):
13
+ # Mock implementation (Replace with actual API call)
14
+ payload = {
15
+ "input_text": text,
16
+ "id": "1",
17
+ "src_lang": "zh",
18
+ "tgt_lang": "tw"
19
+ }
20
+
21
+ response = requests.post(TRANSLATION_API_URL, json=payload)
22
+ if response.status_code == 200:
23
+ return response.text
24
+ return "Translation failed."
25
+
26
+
27
+ def fetch_tts_audio(translated_text, spk):
28
+ # Mock implementation (Replace with actual API call)
29
+ payload = {
30
+ "input_text": translated_text,
31
+ "id": "1",
32
+ "src_lang": "tw",
33
+ "tgt_lang": "tailo"
34
+ }
35
+
36
+ # response = requests.post(TRANSLATION_API_URL, json=payload)
37
+ # if response.status_code == 200:
38
+ # translated_text = response.text
39
+ payload = {
40
+ "text": translated_text, # "tw_convert": False,
41
+ "b64enc": False, "speaker": spk, "speed": 0.9
42
+ }
43
+
44
+ response = requests.post(TTS_API_URL, json=payload)
45
+ if response.status_code == 200:
46
+ # Read the audio data from the response content into a NumPy array
47
+ audio_data, sample_rate = sf.read(io.BytesIO(response.content))
48
+ return audio_data, sample_rate
49
+ return None, None
50
+
51
+
52
+ def translate_and_speak(text, target_language, spk):
53
+ translated_text = fetch_translation(text, target_language)
54
+
55
+ if not translated_text:
56
+ return "Translation failed.", None
57
+
58
+ audio_data, sample_rate = fetch_tts_audio(translated_text, spk)
59
+ if audio_data is not None:
60
+ return translated_text, (sample_rate, audio_data)
61
+ return translated_text, None
62
+
63
+
64
+ spk_list = requests.get("http://twcc2.eztalking.ai/mtts/list_speakers").json()
65
+
66
+ # Gradio Interface
67
+ iface = gr.Interface(
68
+ fn=translate_and_speak,
69
+ inputs=[
70
+ gr.Textbox(label="Enter Text"),
71
+ gr.Dropdown(choices=["en", "es", "fr", "de"], label="Target Language", value="en"),
72
+ gr.Dropdown(choices=spk_list, label="Select Speaker", value=spk_list[0])
73
+ ],
74
+ outputs=[
75
+ gr.Textbox(label="Translated Text"),
76
+ gr.Audio(label="TTS Audio", type="numpy")
77
+ ],
78
+ title="Text Translator with TTS",
79
+ description="Translate text to a selected language and generate TTS audio."
80
+ )
81
+
82
+ iface.launch()