acity-Demo / app.py
Lagyamfi's picture
Update app.py
51cef45 verified
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()