Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,8 @@ import gradio as gr
|
|
| 2 |
import librosa
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
|
|
|
|
|
|
| 5 |
|
| 6 |
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 7 |
|
|
@@ -22,8 +24,13 @@ speaker_embeddings = {
|
|
| 22 |
|
| 23 |
|
| 24 |
def predict(text, speaker):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
if len(text.strip()) == 0:
|
| 26 |
-
return (16000, np.zeros(0).astype(np.int16))
|
| 27 |
|
| 28 |
inputs = processor(text=text, return_tensors="pt")
|
| 29 |
|
|
@@ -44,8 +51,6 @@ def predict(text, speaker):
|
|
| 44 |
x = (np.random.rand(512) >= 0.5) * 1.0
|
| 45 |
x[x == 0] = -1.0
|
| 46 |
speaker_embedding *= x
|
| 47 |
-
|
| 48 |
-
#speaker_embedding = np.random.rand(512).astype(np.float32) * 0.3 - 0.15
|
| 49 |
else:
|
| 50 |
speaker_embedding = np.load(speaker_embeddings[speaker[:3]])
|
| 51 |
|
|
@@ -54,7 +59,7 @@ def predict(text, speaker):
|
|
| 54 |
speech = model.generate_speech(input_ids, speaker_embedding, vocoder=vocoder)
|
| 55 |
|
| 56 |
speech = (speech.numpy() * 32767).astype(np.int16)
|
| 57 |
-
return (16000, speech)
|
| 58 |
|
| 59 |
|
| 60 |
title = "SpeechT5: Speech Synthesis"
|
|
@@ -111,21 +116,25 @@ gr.Interface(
|
|
| 111 |
fn=predict,
|
| 112 |
inputs=[
|
| 113 |
gr.Text(label="Input Text"),
|
| 114 |
-
gr.Radio(
|
| 115 |
-
"
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
| 123 |
],
|
| 124 |
outputs=[
|
| 125 |
gr.Audio(label="Generated Speech", type="numpy"),
|
|
|
|
| 126 |
],
|
| 127 |
title=title,
|
| 128 |
description=description,
|
| 129 |
article=article,
|
| 130 |
examples=examples,
|
| 131 |
-
).launch()
|
|
|
|
| 2 |
import librosa
|
| 3 |
import numpy as np
|
| 4 |
import torch
|
| 5 |
+
import tempfile
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 9 |
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
def predict(text, speaker):
|
| 27 |
+
# Create downloadable text file
|
| 28 |
+
tmp_txt = tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8")
|
| 29 |
+
tmp_txt.write(text if text else "")
|
| 30 |
+
tmp_txt.close()
|
| 31 |
+
|
| 32 |
if len(text.strip()) == 0:
|
| 33 |
+
return (16000, np.zeros(0).astype(np.int16)), tmp_txt.name
|
| 34 |
|
| 35 |
inputs = processor(text=text, return_tensors="pt")
|
| 36 |
|
|
|
|
| 51 |
x = (np.random.rand(512) >= 0.5) * 1.0
|
| 52 |
x[x == 0] = -1.0
|
| 53 |
speaker_embedding *= x
|
|
|
|
|
|
|
| 54 |
else:
|
| 55 |
speaker_embedding = np.load(speaker_embeddings[speaker[:3]])
|
| 56 |
|
|
|
|
| 59 |
speech = model.generate_speech(input_ids, speaker_embedding, vocoder=vocoder)
|
| 60 |
|
| 61 |
speech = (speech.numpy() * 32767).astype(np.int16)
|
| 62 |
+
return (16000, speech), tmp_txt.name
|
| 63 |
|
| 64 |
|
| 65 |
title = "SpeechT5: Speech Synthesis"
|
|
|
|
| 116 |
fn=predict,
|
| 117 |
inputs=[
|
| 118 |
gr.Text(label="Input Text"),
|
| 119 |
+
gr.Radio(
|
| 120 |
+
label="Speaker",
|
| 121 |
+
choices=[
|
| 122 |
+
"BDL (male)",
|
| 123 |
+
"CLB (female)",
|
| 124 |
+
"KSP (male)",
|
| 125 |
+
"RMS (male)",
|
| 126 |
+
"SLT (female)",
|
| 127 |
+
"Surprise Me!"
|
| 128 |
+
],
|
| 129 |
+
value="BDL (male)"
|
| 130 |
+
),
|
| 131 |
],
|
| 132 |
outputs=[
|
| 133 |
gr.Audio(label="Generated Speech", type="numpy"),
|
| 134 |
+
gr.File(label="Download Generated Text")
|
| 135 |
],
|
| 136 |
title=title,
|
| 137 |
description=description,
|
| 138 |
article=article,
|
| 139 |
examples=examples,
|
| 140 |
+
).launch()
|