Spaces:
Sleeping
Sleeping
File size: 1,834 Bytes
d86f106 00269a9 |
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 |
import json
import os
import requests
import gradio as gr
import numpy as np
base_url = "https://api.sandbox.deepgram.com/nlu"
token_str = os.environ['DG_TOKEN']
def tts_fn(text, speed, pitch_steps, variability):
texts = [text]
response = requests.post(
f'{base_url}',
files=[('texts', ('texts', json.dumps(texts), 'application/json'))],
params={'synthesize': 'true', 'speed': speed, 'pitch_steps': int(pitch_steps), 'variability': variability},
headers={
'Authorization': f'Token {token_str}'
},
).json()
sample_rate = int(response['results'][0]['sample_rate'])
audio = np.array(response['results'][0]['audio'], dtype=np.float32)
return (sample_rate, audio)
app = gr.Blocks()
with app:
with gr.Tab("TTS MVP"):
with gr.Row():
with gr.Column():
pangram = "The beige hue on the waters of the loch impressed all, including the French queen, before she heard that symphony again, just as young Arthur wanted."
cherry = "Good evening. I'm a text to speech bot. Please allow me to assist you."
textbox = gr.TextArea(label="Text", placeholder="Type a sentence here", value=cherry)
speed = gr.Slider(minimum=0.0, maximum=2.0, value=1.1, step=0.1, label="Speed")
pitch_steps = gr.Slider(minimum=-24, maximum=24, value=0, step=1, label="Pitch Steps: 12 to an octave")
variability = gr.Slider(minimum=0.0, maximum=1.0, value=0.7, step=0.1, label="Variability")
with gr.Column():
audio_output = gr.Audio(label="Output Audio", elem_id='tts-audio')
btn = gr.Button("Generate")
btn.click(tts_fn, inputs=[textbox, speed, pitch_steps, variability], outputs=[audio_output])
app.launch()
|