Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import soundfile as sf
|
|
| 4 |
import spaces
|
| 5 |
import os
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 8 |
from speechbrain.pretrained import EncoderClassifier
|
| 9 |
from datasets import load_dataset
|
|
@@ -44,22 +45,94 @@ def prepare_default_embedding(example):
|
|
| 44 |
|
| 45 |
default_embedding = prepare_default_embedding(default_example)
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
@spaces.GPU(duration = 60)
|
| 48 |
def text_to_speech(text, audio_file=None):
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
speaker_embeddings = default_embedding
|
| 52 |
|
| 53 |
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings.unsqueeze(0), vocoder=vocoder)
|
| 54 |
sf.write("output.wav", speech.cpu().numpy(), samplerate=16000)
|
| 55 |
-
return "output.wav"
|
| 56 |
|
| 57 |
iface = gr.Interface(
|
| 58 |
fn=text_to_speech,
|
| 59 |
inputs=[
|
| 60 |
gr.Textbox(label="Enter Turkish text to convert to speech")
|
| 61 |
],
|
| 62 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 63 |
title="Turkish SpeechT5 Text-to-Speech Demo with Optional Custom Voice",
|
| 64 |
description="Enter Turkish text, optionally upload a short audio sample of the target speaker, and listen to the generated speech using the fine-tuned SpeechT5 model."
|
| 65 |
)
|
|
|
|
| 4 |
import spaces
|
| 5 |
import os
|
| 6 |
import numpy as np
|
| 7 |
+
import re
|
| 8 |
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
|
| 9 |
from speechbrain.pretrained import EncoderClassifier
|
| 10 |
from datasets import load_dataset
|
|
|
|
| 45 |
|
| 46 |
default_embedding = prepare_default_embedding(default_example)
|
| 47 |
|
| 48 |
+
replacements = [
|
| 49 |
+
("â", "a"), # Long a
|
| 50 |
+
("ç", "ch"), # Ch as in "chair"
|
| 51 |
+
("ğ", "gh"), # Silent g or slight elongation of the preceding vowel
|
| 52 |
+
("ı", "i"), # Dotless i
|
| 53 |
+
("î", "i"), # Long i
|
| 54 |
+
("ö", "oe"), # Similar to German ö
|
| 55 |
+
("ş", "sh"), # Sh as in "shoe"
|
| 56 |
+
("ü", "ue"), # Similar to German ü
|
| 57 |
+
("û", "u"), # Long u
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
number_words = {
|
| 61 |
+
0: "sıfır", 1: "bir", 2: "iki", 3: "üç", 4: "dört", 5: "beş", 6: "altı", 7: "yedi", 8: "sekiz", 9: "dokuz",
|
| 62 |
+
10: "on", 11: "on bir", 12: "on iki", 13: "on üç", 14: "on dört", 15: "on beş", 16: "on altı", 17: "on yedi",
|
| 63 |
+
18: "on sekiz", 19: "on dokuz", 20: "yirmi", 30: "otuz", 40: "kırk", 50: "elli", 60: "altmış", 70: "yetmiş",
|
| 64 |
+
80: "seksen", 90: "doksan", 100: "yüz", 1000: "bin"
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
def number_to_words(number):
|
| 68 |
+
if number < 20:
|
| 69 |
+
return number_words[number]
|
| 70 |
+
elif number < 100:
|
| 71 |
+
tens, unit = divmod(number, 10)
|
| 72 |
+
return number_words[tens * 10] + (" " + number_words[unit] if unit else "")
|
| 73 |
+
elif number < 1000:
|
| 74 |
+
hundreds, remainder = divmod(number, 100)
|
| 75 |
+
return (number_words[hundreds] + " yüz" if hundreds > 1 else "yüz") + (" " + number_to_words(remainder) if remainder else "")
|
| 76 |
+
elif number < 1000000:
|
| 77 |
+
thousands, remainder = divmod(number, 1000)
|
| 78 |
+
return (number_to_words(thousands) + " bin" if thousands > 1 else "bin") + (" " + number_to_words(remainder) if remainder else "")
|
| 79 |
+
elif number < 1000000000:
|
| 80 |
+
millions, remainder = divmod(number, 1000000)
|
| 81 |
+
return number_to_words(millions) + " milyon" + (" " + number_to_words(remainder) if remainder else "")
|
| 82 |
+
elif number < 1000000000000:
|
| 83 |
+
billions, remainder = divmod(number, 1000000000)
|
| 84 |
+
return number_to_words(billions) + " milyar" + (" " + number_to_words(remainder) if remainder else "")
|
| 85 |
+
else:
|
| 86 |
+
return str(number)
|
| 87 |
+
|
| 88 |
+
def replace_numbers_with_words(text):
|
| 89 |
+
def replace(match):
|
| 90 |
+
number = int(match.group())
|
| 91 |
+
return number_to_words(number)
|
| 92 |
+
|
| 93 |
+
# Find the numbers and change with words.
|
| 94 |
+
result = re.sub(r'\b\d+\b', replace, text)
|
| 95 |
+
|
| 96 |
+
return result
|
| 97 |
+
|
| 98 |
+
def normalize_text(text):
|
| 99 |
+
# Convert to lowercase
|
| 100 |
+
text = text.lower()
|
| 101 |
+
|
| 102 |
+
# Replace numbers with words
|
| 103 |
+
text = replace_numbers_with_words(text)
|
| 104 |
+
|
| 105 |
+
# Apply character replacements
|
| 106 |
+
for old, new in replacements:
|
| 107 |
+
text = text.replace(old, new)
|
| 108 |
+
|
| 109 |
+
# Remove punctuation
|
| 110 |
+
text = re.sub(r'[^\w\s]', '', text)
|
| 111 |
+
|
| 112 |
+
return text
|
| 113 |
+
|
| 114 |
@spaces.GPU(duration = 60)
|
| 115 |
def text_to_speech(text, audio_file=None):
|
| 116 |
+
# Normalize the input text
|
| 117 |
+
normalized_text = normalize_text(text)
|
| 118 |
+
|
| 119 |
+
inputs = processor(text=normalized_text, return_tensors="pt").to(device)
|
| 120 |
|
| 121 |
speaker_embeddings = default_embedding
|
| 122 |
|
| 123 |
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings.unsqueeze(0), vocoder=vocoder)
|
| 124 |
sf.write("output.wav", speech.cpu().numpy(), samplerate=16000)
|
| 125 |
+
return "output.wav", normalized_text
|
| 126 |
|
| 127 |
iface = gr.Interface(
|
| 128 |
fn=text_to_speech,
|
| 129 |
inputs=[
|
| 130 |
gr.Textbox(label="Enter Turkish text to convert to speech")
|
| 131 |
],
|
| 132 |
+
outputs=[
|
| 133 |
+
gr.Audio(label="Generated Speech"),
|
| 134 |
+
gr.Textbox(label="Normalized Text")
|
| 135 |
+
],
|
| 136 |
title="Turkish SpeechT5 Text-to-Speech Demo with Optional Custom Voice",
|
| 137 |
description="Enter Turkish text, optionally upload a short audio sample of the target speaker, and listen to the generated speech using the fine-tuned SpeechT5 model."
|
| 138 |
)
|