Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -46,27 +46,19 @@ def prepare_default_embedding(example):
|
|
| 46 |
default_embedding = prepare_default_embedding(default_example)
|
| 47 |
|
| 48 |
replacements = [
|
| 49 |
-
("â", "a"),
|
| 50 |
-
("
|
| 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",
|
| 63 |
-
|
| 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
|
| 70 |
elif number < 100:
|
| 71 |
tens, unit = divmod(number, 10)
|
| 72 |
return number_words[tens * 10] + (" " + number_words[unit] if unit else "")
|
|
@@ -76,60 +68,40 @@ def number_to_words(number):
|
|
| 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 |
-
|
| 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 |
-
# Prepare the input for the model
|
| 120 |
inputs = processor(text=normalized_text, return_tensors="pt").to(device)
|
| 121 |
-
|
| 122 |
-
# Use the default speaker embedding
|
| 123 |
speaker_embeddings = default_embedding
|
| 124 |
|
| 125 |
-
# Generate speech
|
| 126 |
with torch.no_grad():
|
| 127 |
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings.unsqueeze(0), vocoder=vocoder)
|
| 128 |
|
| 129 |
speech_np = speech.cpu().numpy()
|
| 130 |
-
|
| 131 |
return (16000, speech_np)
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
iface = gr.Interface(
|
| 134 |
fn=text_to_speech,
|
| 135 |
inputs=[
|
|
@@ -138,8 +110,12 @@ iface = gr.Interface(
|
|
| 138 |
outputs=[
|
| 139 |
gr.Audio(label="Generated Speech", type="numpy")
|
| 140 |
],
|
| 141 |
-
title="
|
| 142 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
)
|
| 144 |
|
| 145 |
-
iface.launch(share=True)
|
|
|
|
| 46 |
default_embedding = prepare_default_embedding(default_example)
|
| 47 |
|
| 48 |
replacements = [
|
| 49 |
+
("â", "a"), ("ç", "ch"), ("ğ", "gh"), ("ı", "i"), ("î", "i"),
|
| 50 |
+
("ö", "oe"), ("ş", "sh"), ("ü", "ue"), ("û", "u"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
]
|
| 52 |
|
| 53 |
number_words = {
|
| 54 |
0: "sıfır", 1: "bir", 2: "iki", 3: "üç", 4: "dört", 5: "beş", 6: "altı", 7: "yedi", 8: "sekiz", 9: "dokuz",
|
| 55 |
+
10: "on", 20: "yirmi", 30: "otuz", 40: "kırk", 50: "elli", 60: "altmış", 70: "yetmiş", 80: "seksen", 90: "doksan",
|
| 56 |
+
100: "yüz", 1000: "bin"
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
def number_to_words(number):
|
| 60 |
if number < 20:
|
| 61 |
+
return number_words.get(number, str(number))
|
| 62 |
elif number < 100:
|
| 63 |
tens, unit = divmod(number, 10)
|
| 64 |
return number_words[tens * 10] + (" " + number_words[unit] if unit else "")
|
|
|
|
| 68 |
elif number < 1000000:
|
| 69 |
thousands, remainder = divmod(number, 1000)
|
| 70 |
return (number_to_words(thousands) + " bin" if thousands > 1 else "bin") + (" " + number_to_words(remainder) if remainder else "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
else:
|
| 72 |
+
return str(number) # For very large numbers, return as is
|
| 73 |
|
| 74 |
def replace_numbers_with_words(text):
|
| 75 |
+
return re.sub(r'\b\d+\b', lambda m: number_to_words(int(m.group())), text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
def normalize_text(text):
|
|
|
|
| 78 |
text = text.lower()
|
|
|
|
|
|
|
| 79 |
text = replace_numbers_with_words(text)
|
|
|
|
|
|
|
| 80 |
for old, new in replacements:
|
| 81 |
text = text.replace(old, new)
|
|
|
|
|
|
|
| 82 |
text = re.sub(r'[^\w\s]', '', text)
|
|
|
|
| 83 |
return text
|
| 84 |
|
| 85 |
@spaces.GPU(duration=60)
|
| 86 |
def text_to_speech(text, audio_file=None):
|
|
|
|
| 87 |
normalized_text = normalize_text(text)
|
|
|
|
|
|
|
| 88 |
inputs = processor(text=normalized_text, return_tensors="pt").to(device)
|
|
|
|
|
|
|
| 89 |
speaker_embeddings = default_embedding
|
| 90 |
|
|
|
|
| 91 |
with torch.no_grad():
|
| 92 |
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings.unsqueeze(0), vocoder=vocoder)
|
| 93 |
|
| 94 |
speech_np = speech.cpu().numpy()
|
|
|
|
| 95 |
return (16000, speech_np)
|
| 96 |
+
|
| 97 |
+
# Add example Turkish sentences
|
| 98 |
+
example_sentences = [
|
| 99 |
+
"Merhaba, nasılsın?",
|
| 100 |
+
"Bugün hava çok güzel. Merhaba, yapay zeka ve makine öğrenmesi konularında bilgisayar donanımı ve kodlama kullanarak veri bilimi ve algoritmalar üzerinde çalışıyorum, ayrıca CUDA teknolojisini de öğreniyorum, teşekkürler.",
|
| 101 |
+
"Türk kahvesi içmeyi seviyorum.",
|
| 102 |
+
"İstanbul Boğazı'nda yürüyüş yapmak harika."
|
| 103 |
+
]
|
| 104 |
+
|
| 105 |
iface = gr.Interface(
|
| 106 |
fn=text_to_speech,
|
| 107 |
inputs=[
|
|
|
|
| 110 |
outputs=[
|
| 111 |
gr.Audio(label="Generated Speech", type="numpy")
|
| 112 |
],
|
| 113 |
+
title="Fine-tuned Turkish SpeechT5 Text-to-Speech Demo",
|
| 114 |
+
description="This demo uses a fine-tuned model based on microsoft/speecht5_tts for Turkish text-to-speech. Enter Turkish text and listen to the generated speech."
|
| 115 |
+
Note:- This report was prepared as a task given by the IIT Roorkee PARIMAL intern program
|
| 116 |
+
|
| 117 |
+
This space demonstrates the demo version of Omarrran/turkish_finetuned_speecht5_tts version for the turkish language.,
|
| 118 |
+
examples=example_sentences
|
| 119 |
)
|
| 120 |
|
| 121 |
+
iface.launch(share=True)
|