Spaces:
Running
Running
File size: 9,803 Bytes
c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 c0f5b55 c56a4e0 08c841b c0f5b55 08c841b c56a4e0 08c841b c56a4e0 08c841b c56a4e0 c0f5b55 |
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
import torch
import soundfile as sf
import os
import re
from transformers import SpeechT5Processor, SpeechT5ForTextToSpeech, SpeechT5HifiGan
from speechbrain.pretrained import EncoderClassifier
# Define paths and device
model_path = "HAMMALE/speecht5-darija" # Path to your model on HF Hub
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
# Load models
processor = SpeechT5Processor.from_pretrained(model_path)
model = SpeechT5ForTextToSpeech.from_pretrained(model_path).to(device)
vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan").to(device)
# Load speaker embedding model
speaker_model = EncoderClassifier.from_hparams(
source="speechbrain/spkrec-xvect-voxceleb",
run_opts={"device": device},
savedir=os.path.join("/tmp", "spkrec-xvect-voxceleb"),
)
# Load pre-computed speaker embeddings
male_embedding = torch.load("male_embedding.pt") if os.path.exists("male_embedding.pt") else torch.randn(1, 512)
female_embedding = torch.load("female_embedding.pt") if os.path.exists("female_embedding.pt") else torch.randn(1, 512)
# Text normalization function
def normalize_text(text):
"""Normalize text for TTS processing"""
text = text.lower()
# Keep letters, numbers, spaces and apostrophes - fixed regex
text = re.sub(r'[^\w\s\'\u0600-\u06FF]', '', text)
text = ' '.join(text.split())
return text
# Function to synthesize speech
def synthesize_speech(text, voice_type="male", speed=1.0):
"""Generate speech from text using the specified voice type"""
try:
# Select speaker embedding based on voice type
if voice_type == "male":
speaker_embeddings = male_embedding.to(device)
else:
speaker_embeddings = female_embedding.to(device)
# Normalize and tokenize input text
normalized_text = normalize_text(text)
inputs = processor(text=normalized_text, return_tensors="pt").to(device)
# Generate speech
with torch.no_grad():
speech = model.generate_speech(
inputs["input_ids"],
speaker_embeddings,
vocoder=vocoder
)
# Convert to numpy array and adjust speed if needed
speech_np = speech.cpu().numpy()
# Apply speed adjustment (simple resampling)
if speed != 1.0:
# This is a simple approach - for production use a proper resampling library
import numpy as np
from scipy import signal
sample_rate = 16000
new_length = int(len(speech_np) / speed)
speech_np = signal.resample(speech_np, new_length)
# Save temporary audio file
output_file = "output_speech.wav"
sf.write(output_file, speech_np, 16000)
return output_file, None
except Exception as e:
return None, f"Error generating speech: {str(e)}"
# Gradio imports need to be added
import gradio as gr
# Custom CSS for a full-screen, modern design
custom_css = """
body, html {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
overflow-x: hidden;
}
.gradio-container {
font-family: 'Montserrat', 'Arial', sans-serif !important;
height: 100vh;
width: 100vw;
background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
display: flex;
flex-direction: column;
padding: 0;
margin: 0;
overflow-y: auto;
}
.main-header {
background: linear-gradient(90deg, #d32f2f, #1976d2);
color: white;
padding: 2em;
text-align: center;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.15);
border-bottom: 4px solid #ffffff33;
}
.main-header h1 {
font-size: 2.8em;
margin: 0;
font-weight: 700;
letter-spacing: 1px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}
.main-header p {
font-size: 1.2em;
margin: 0.5em 0 0;
opacity: 0.9;
font-weight: 300;
}
.container {
max-width: 1200px;
margin: 2em auto;
padding: 0 1em;
flex: 1;
}
.row {
display: flex;
gap: 2em;
background: white;
border-radius: 15px;
padding: 2em;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
margin-bottom: 2em;
}
.column {
flex: 1;
padding: 1em;
}
.info-box {
background: #fef6f6;
border-left: 5px solid #d32f2f;
padding: 1.5em;
border-radius: 8px;
margin-bottom: 1.5em;
font-size: 1em;
line-height: 1.6;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
.textbox textarea {
border: 2px solid #e0e0e0 !important;
border-radius: 10px !important;
padding: 1em !important;
font-size: 1.1em !important;
transition: border-color 0.3s ease !important;
}
.textbox textarea:focus {
border-color: #d32f2f !important;
box-shadow: 0 0 8px rgba(211, 47, 47, 0.2) !important;
}
.radio {
display: flex;
justify-content: center;
gap: 1.5em;
margin: 1em 0;
}
.radio label {
background: #f5f5f5;
padding: 0.8em 1.5em;
border-radius: 25px;
border: 2px solid #e0e0e0;
cursor: pointer;
transition: all 0.3s ease;
}
.radio input:checked + label {
background: #d32f2f;
color: white;
border-color: #d32f2f;
box-shadow: 0 4px 8px rgba(211, 47, 47, 0.2);
}
.slider {
margin: 1.5em 0;
}
.slider input {
accent-color: #d32f2f !important;
}
.button {
background: linear-gradient(90deg, #d32f2f, #1976d2) !important;
color: white !important;
padding: 1em 2em !important;
border-radius: 25px !important;
border: none !important;
font-size: 1.1em !important;
font-weight: 600 !important;
transition: transform 0.2s ease, box-shadow 0.3s ease !important;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15) !important;
}
.button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.25) !important;
}
.audio {
margin-top: 1em;
}
.audio audio {
width: 100%;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}
.example-header {
font-weight: 600;
color: #d32f2f;
margin: 1.5em 0 0.5em;
font-size: 1.2em;
}
ul {
padding-left: 1.5em;
color: #333;
}
li {
margin: 0.5em 0;
font-size: 1em;
}
.examples {
margin-top: 1.5em;
padding: 1em;
background: #f9f9f9;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
}
footer {
text-align: center;
padding: 1.5em;
background: #ffffff;
color: #666;
font-size: 0.95em;
border-top: 1px solid #e0e0e0;
margin-top: auto;
}
.flag-icon {
width: 30px;
height: 30px;
vertical-align: middle;
margin-right: 10px;
}
"""
# Create Gradio interface with enhanced design
with gr.Blocks(css=custom_css) as demo:
gr.HTML(
"""
<div class="main-header">
<h1>๐ฒ๐ฆ Moroccan Darija Text-to-Speech ๐๏ธ</h1>
<p>Transform your Darija text into lifelike speech with ease</p>
</div>
"""
)
with gr.Row(elem_classes="row"):
with gr.Column(elem_classes="column"):
gr.HTML(
"""
<div class="info-box">
<p>Experience high-quality Darija speech synthesis powered by the SpeechT5 model, fine-tuned on the DODa audio dataset. Customize the voice and speed to suit your needs.</p>
</div>
"""
)
text_input = gr.Textbox(
label="Enter Darija Text",
placeholder="Kteb chi jomla b darija hna, bhal 'Salam, kifach nta?'...",
lines=3,
elem_classes="textbox"
)
with gr.Row(elem_classes="radio"):
voice_type = gr.Radio(
["male", "female"],
label="Voice Type",
value="male"
)
speed = gr.Slider(
minimum=0.5,
maximum=2.0,
value=1.0,
step=0.1,
label="Speech Speed",
elem_classes="slider"
)
generate_btn = gr.Button("Generate Speech", variant="primary", elem_classes="button")
gr.HTML(
"""
<div class="example-header">Try These Phrases:</div>
<ul>
<li>"Ana Nadi Bezzaaf hhh"</li>
<li>"Lyoum ajwaa zwina bezzaaf."</li>
<li>"Lmaghrib ahssan blad fi l3alam"</li>
<li>"Chukran bzzaf 3la lmosanada!"</li>
</ul>
"""
)
with gr.Column(elem_classes="column"):
audio_output = gr.Audio(label="Generated Speech", elem_classes="audio")
error_output = gr.Textbox(label="Error (if any)", visible=False)
gr.Examples(
examples=[
["Ana Nadi Bezzaaf hhh", "male", 1.0],
["Lyoum ajwaa zwina bezzaaf.", "female", 1.0],
["Lmaghrib ahssan blad fi l3alam", "male", 1.0],
["Filistine horaa mina lbari ila lbarri", "female", 0.8],
],
inputs=[text_input, voice_type, speed],
outputs=[audio_output, error_output],
fn=synthesize_speech
)
gr.HTML(
"""
<footer>
<p>Developed by HAMMALE | Data: DODa Audio Dataset</p>
</footer>
"""
)
# Set button click action
generate_btn.click(
fn=synthesize_speech,
inputs=[text_input, voice_type, speed],
outputs=[audio_output, error_output]
)
# Launch the demo
if __name__ == "__main__":
demo.launch() |