File size: 11,566 Bytes
e3f0358 814308b e3f0358 1519c55 e3f0358 c8e18fb 5519ee4 e3f0358 7dd36aa 5519ee4 d7c2bd8 e3f0358 c8e18fb e3f0358 c8e18fb e3f0358 c8e18fb d7c2bd8 e3f0358 0973a1e c8e18fb 5519ee4 e3f0358 107a3c2 e3f0358 107a3c2 e3f0358 c8e18fb 5519ee4 c8e18fb e3f0358 8aae1ef e3f0358 8fad399 c8e18fb e3f0358 8fad399 e3f0358 8aae1ef e3f0358 5519ee4 e3f0358 c8e18fb e3f0358 c8e18fb e3f0358 c8e18fb d7c2bd8 e3f0358 0973a1e e3f0358 0973a1e c8e18fb e3f0358 c8e18fb e3f0358 c8e18fb e3f0358 c8e18fb e3f0358 c8e18fb e3f0358 c8e18fb e3f0358 c8e18fb e3f0358 6f9caf2 c8e18fb e3f0358 c8e18fb e3f0358 c8e18fb e3f0358 c8e18fb e3f0358 d7c2bd8 5519ee4 c8e18fb e3f0358 814308b e3f0358 c8e18fb e3f0358 c8e18fb e3f0358 c8e18fb e3f0358 5519ee4 c8e18fb e3f0358 c8e18fb e3f0358 d7c2bd8 c8e18fb e3f0358 d7c2bd8 e3f0358 5519ee4 c8e18fb | 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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 | # ==========================================
# 0. HUGGING FACE ZERO-GPU INITIALIZATION
# ==========================================
try:
import spaces
except ImportError:
class spaces:
@staticmethod
def GPU(fn):
return fn
# ==========================================
# 1. IMPORTS
# ==========================================
import os
import re
import tempfile
import numpy as np
import whisper
import gradio as gr
from groq import Groq
from f5_tts.api import F5TTS
# ==========================================
# 2. CONFIGURATION
# ==========================================
# IMPORTANT:
# Set your Groq API key as an environment variable.
# Do NOT put your real API key directly in the code.
GROQ_API_KEY = os.environ.get("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")
# Your reference voice
REFERENCE_VOICE_FILE = "my_voice_fixed.wav"
# Text spoken in your reference voice file
REFERENCE_TEXT = (
"Hi, my name is Rabbani. "
"I am a software engineer at Gamana Solutions."
)
if not os.path.exists(REFERENCE_VOICE_FILE):
raise FileNotFoundError(
f"Reference voice file '{REFERENCE_VOICE_FILE}' not found."
)
# ==========================================
# 3. INITIALIZE MODELS
# ==========================================
groq_client = None
whisper_model = None
f5tts = None
# ==========================================
# 4. GROQ CLIENT
# ==========================================
def get_groq_client():
global groq_client
if not GROQ_API_KEY:
raise ValueError(
"GROQ_API_KEY is not configured."
)
if groq_client is None:
groq_client = Groq(
api_key=GROQ_API_KEY
)
return groq_client
# ==========================================
# 5. REMOVE FILLER WORDS
# ==========================================
def remove_filler_words(text):
if not text:
return ""
# Convert multiple spaces into one
text = re.sub(r"\s+", " ", text).strip()
filler_patterns = [
r"\bumm+\b",
r"\bum+\b",
r"\buh+\b",
r"\bah+\b",
r"\byou know\b",
r"\bi mean\b",
r"\bactually\b",
r"\bbasically\b",
r"\blike\b",
r"\bsort of\b",
r"\bkind of\b",
r"\bwell\b",
]
for pattern in filler_patterns:
text = re.sub(
pattern,
"",
text,
flags=re.IGNORECASE
)
# Remove repeated spaces
text = re.sub(r"\s+", " ", text)
# Fix spaces before punctuation
text = re.sub(r"\s+([,.!?])", r"\1", text)
return text.strip()
# ==========================================
# 6. GRAMMAR CORRECTION
# ==========================================
def correct_grammar(text):
if not text:
return ""
try:
client = get_groq_client()
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{
"role": "system",
"content": """
You are a speech-cleaning and grammar-correction system.
Your task is ONLY to clean the user's spoken sentence.
Rules:
1. Remove filler words.
2. Correct grammar.
3. Correct sentence structure.
4. Remove unnecessary repetitions.
5. Keep the original meaning.
6. Do not add new information.
7. Do not answer the question.
8. Do not explain the correction.
9. Return ONLY the corrected sentence.
10. Keep the sentence natural and conversational.
Example:
Input:
"Umm, I want to know like how Salesforce LWC works, you know?"
Output:
"I want to know how Salesforce LWC works."
Input:
"Actually umm I am working on Salesforce and I want to create ten records."
Output:
"I am working on Salesforce, and I want to create ten records."
"""
},
{
"role": "user",
"content": text
}
],
max_tokens=200,
temperature=0.1,
timeout=15
)
cleaned_text = (
response
.choices[0]
.message
.content
.strip()
)
return cleaned_text
except Exception as error:
print(
f"Grammar correction error: {error}"
)
# Fallback:
# Return filler-cleaned text
return remove_filler_words(text)
# ==========================================
# 7. WHISPER TRANSCRIPTION
# ==========================================
def transcribe_audio(audio_file):
global whisper_model
if whisper_model is None:
print("Loading Whisper tiny model...")
whisper_model = whisper.load_model(
"tiny"
)
result = whisper_model.transcribe(
audio_file,
fp16=False
)
text = result["text"].strip()
return text
# ==========================================
# 8. F5-TTS VOICE CLONING
# ==========================================
def clone_voice(text):
global f5tts
if not text:
return None
try:
if f5tts is None:
print("Loading F5-TTS...")
f5tts = F5TTS()
# Temporary output file
with tempfile.TemporaryDirectory() as temp_dir:
output_file = os.path.join(
temp_dir,
"generated.wav"
)
result = f5tts.infer(
ref_file=REFERENCE_VOICE_FILE,
ref_text=REFERENCE_TEXT,
gen_text=text,
file_wave=output_file
)
# ==================================
# Get audio from F5-TTS
# ==================================
if isinstance(result, tuple):
audio_data = result[0]
sample_rate = result[1]
else:
audio_data = result
sample_rate = 24000
# ==================================
# Convert Tensor β NumPy
# ==================================
if hasattr(
audio_data,
"cpu"
):
audio_data = (
audio_data
.cpu()
.numpy()
)
elif hasattr(
audio_data,
"numpy"
):
audio_data = (
audio_data
.numpy()
)
audio_data = np.asarray(
audio_data
)
audio_data = np.squeeze(
audio_data
)
# Make independent copy
audio_data = audio_data.copy()
# Temporary file is deleted here
return (
sample_rate,
audio_data
)
except Exception as error:
print(
f"F5-TTS error: {error}"
)
return None
# ==========================================
# 9. MAIN VOICE PIPELINE
# ==========================================
@spaces.GPU
def voice_pipeline(audio_input):
if audio_input is None:
return (
"Please record your voice.",
"",
None
)
try:
# ==================================
# STEP 1: Whisper
# ==================================
print(
"\nSTEP 1: Transcribing audio..."
)
original_text = transcribe_audio(
audio_input
)
print(
"Original:",
original_text
)
# ==================================
# STEP 2: Remove filler words
# ==================================
print(
"\nSTEP 2: Removing filler words..."
)
filler_removed_text = (
remove_filler_words(
original_text
)
)
print(
"Filler removed:",
filler_removed_text
)
# ==================================
# STEP 3: Grammar correction
# ==================================
print(
"\nSTEP 3: Correcting grammar..."
)
corrected_text = (
correct_grammar(
filler_removed_text
)
)
print(
"Corrected:",
corrected_text
)
# ==================================
# STEP 4: Voice cloning
# ==================================
print(
"\nSTEP 4: Cloning voice..."
)
generated_audio = clone_voice(
corrected_text
)
if generated_audio is None:
return (
original_text,
corrected_text,
None
)
# ==================================
# STEP 5: Return everything
# ==================================
return (
original_text,
corrected_text,
generated_audio
)
except Exception as error:
print(
f"Pipeline error: {error}"
)
return (
f"Error: {error}",
"",
None
)
# ==========================================
# 10. GRADIO UI
# ==========================================
with gr.Blocks(
title="Rabbani Voice Cleaner & Cloner"
) as interface:
gr.Markdown(
"# ποΈ Voice Cleaner + Grammar Correction + Voice Cloning"
)
gr.Markdown(
"""
Speak naturally into the microphone.
The system will:
**Your Voice β Whisper β Remove Fillers β Correct Grammar β Clone Your Voice**
"""
)
# ======================================
# INPUT
# ======================================
gr.Markdown(
"## π€ Step 1 β Speak"
)
audio_input = gr.Audio(
sources=["microphone"],
type="filepath",
label="Record Your Voice"
)
process_button = gr.Button(
"Process Voice",
variant="primary"
)
# ======================================
# ORIGINAL TRANSCRIPTION
# ======================================
gr.Markdown(
"## π Step 2 β Whisper Transcription"
)
original_output = gr.Textbox(
label="Original Speech",
interactive=False
)
# ======================================
# CORRECTED TEXT
# ======================================
gr.Markdown(
"## β¨ Step 3 β Corrected Sentence"
)
corrected_output = gr.Textbox(
label="Clean + Grammatically Correct Sentence",
interactive=False
)
# ======================================
# CLONED AUDIO
# ======================================
gr.Markdown(
"## π Step 4 β Cloned Voice"
)
cloned_audio = gr.Audio(
label="Your Cloned Voice",
type="numpy",
interactive=False
)
# ======================================
# BUTTON EVENT
# ======================================
process_button.click(
fn=voice_pipeline,
inputs=[
audio_input
],
outputs=[
original_output,
corrected_output,
cloned_audio
]
)
# ==========================================
# 11. START APPLICATION
# ==========================================
interface.queue().launch() |