File size: 1,416 Bytes
0565a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51cef45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0565a93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
import gradio as gr
import requests
import os
import tempfile
from scipy.io import wavfile


demo_keys = os.environ['DEMO_KEYS']
tts_url = "https://translation-api.ghananlp.org/tts/v1/tts"
translation_url = "https://translation-api.ghananlp.org/v1/translate"
headers ={
    # Request headers
    'Content-Type': 'application/json',
    'Cache-Control': 'no-cache',
    'Ocp-Apim-Subscription-Key': demo_keys
    }

def synthesize(text, language):
    data = {
        "text": text,
        "language": language
    }
    audio = requests.post(tts_url, headers=headers, json=data).content

    # Write bytes to a temporary file
    with tempfile.NamedTemporaryFile(delete=True) as temp_wav:
        temp_wav.write(audio)
        temp_wav.flush()

        # Read the wav file
        rate, audio_array = wavfile.read(temp_wav.name)

    return (rate, audio_array)


def translate(text, source_language, target_language):
    data = {
        "in": text,
        "lang": source_language + "-" + target_language
    }
    response = requests.post(translation_url, headers=headers, json=data).text
    return response

gr.Interface(
    fn=synthesize,
    inputs=[
        gr.Text(label="Input Text"),
        gr.Radio(label="Language", choices=[
            "tw",
        ],
        value="tw"),
    ],
    outputs=[
        gr.Audio(label="Generated Speech", type="numpy"),
    ],
    title="ACity TTS Demo",
).launch()