tee342 commited on
Commit
78a7fa2
Β·
verified Β·
1 Parent(s): 5cdf2bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -99
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  from pydub import AudioSegment
 
3
  import numpy as np
4
  import tempfile
5
  import os
@@ -21,6 +22,7 @@ import warnings
21
  from faster_whisper import WhisperModel
22
  from mutagen.mp3 import MP3
23
  from mutagen.id3 import ID3, TIT2, TPE1, TALB, TYER
 
24
 
25
  # Suppress warnings
26
  warnings.filterwarnings("ignore")
@@ -273,8 +275,6 @@ def transcribe_audio(audio_path):
273
  return text
274
 
275
  # === TTS Tab ===
276
- from TTS.api import TTS
277
-
278
  tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
279
 
280
  def generate_tts(text):
@@ -306,46 +306,23 @@ def analyze_audio(audio_path):
306
 
307
  return stats, image
308
 
309
- # === Speaker Diarization Tab ===
310
  try:
311
- from pyannote.audio import Pipeline as DiarizationPipeline
312
-
313
- hf_token = os.getenv("HF_TOKEN")
314
- if hf_token:
315
- diarize_pipeline = DiarizationPipeline.from_pretrained("pyannote/speaker-diarization", use_auth_token=hf_token)
316
- else:
317
- diarize_pipeline = None
318
- print("⚠️ No HF_TOKEN set – speaker diarization disabled")
319
- except ImportError as e:
320
- diarize_pipeline = None
321
- print(f"⚠️ Could not load diarization: {e}")
322
-
323
- def diarize_and_transcribe(audio_path):
324
- if not diarize_pipeline:
325
- return "⚠️ Diarization pipeline not loaded – check HF token or install pyannote.audio"
326
-
327
- # Run diarization
328
- audio = AudioSegment.from_file(audio_path)
329
- temp_wav = os.path.join(tempfile.gettempdir(), "diarize.wav")
330
- audio.export(temp_wav, format="wav")
331
-
332
- # Run diarization
333
- diarization = diarize_pipeline(temp_wav)
334
-
335
- # Run transcription
336
- result = whisper.transcribe(temp_wav)
337
-
338
- segments = []
339
- for turn, _, speaker in diarization.itertracks(yield_label=True):
340
- text = " ".join([seg["text"] for seg in result["segments"] if seg["start"] >= turn.start and seg["end"] <= turn.end])
341
- segments.append({
342
- "speaker": speaker,
343
- "start": turn.start,
344
- "end": turn.end,
345
- "text": text
346
- })
347
-
348
- return segments
349
 
350
  # === Save/Load Project File (.aiproj) ===
351
  def save_project(audio_path, preset_name, effects):
@@ -359,6 +336,24 @@ def save_project(audio_path, preset_name, effects):
359
  pickle.dump(project_data, f)
360
  return out_path
361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
  # === Mix Two Tracks ===
363
  def mix_tracks(track1, track2, volume_offset=0):
364
  a1 = AudioSegment.from_file(track1)
@@ -368,28 +363,24 @@ def mix_tracks(track1, track2, volume_offset=0):
368
  mixed.export(out_path, format="wav")
369
  return out_path
370
 
371
- # === Voice Style Transfer (Dummy) ===
372
- def apply_style_transfer(audio_path, mood="Happy"):
373
- return audio_path
374
-
375
- # === Metadata Tagging ===
376
- def tag_mp3(file_path, title, artist, album, year):
377
  try:
378
- audio = MP3(file_path)
379
- try:
380
- audio.tags = ID3()
381
- except:
382
- audio.add_tags()
383
- audio.tags.add(TIT2(encoding=3, text=title))
384
- audio.tags.add(TPE1(encoding=3, text=artist))
385
- if album:
386
- audio.tags.add(TALB(encoding=3, text=album))
387
- if year:
388
- audio.tags.add(TYER(encoding=3, text=str(year)))
389
- audio.save()
390
- return file_path
391
  except Exception as e:
392
- return None
393
 
394
  # === UI ===
395
  effect_options = [
@@ -478,7 +469,7 @@ with gr.Blocks(title="AI Audio Studio", css="style.css") as demo:
478
  inputs=gr.Audio(label="Upload Audio", type="filepath"),
479
  outputs=gr.Textbox(label="Transcribed Text", lines=10),
480
  title="Transcribe & Edit Spoken Content",
481
- description="Convert voice to text, then edit the script before exporting again."
482
  )
483
 
484
  # --- TTS Voice Generator ---
@@ -491,18 +482,61 @@ with gr.Blocks(title="AI Audio Studio", css="style.css") as demo:
491
  description="Type anything and turn it into natural-sounding speech."
492
  )
493
 
494
- # --- Speaker Diarization ===
495
- if diarize_pipeline:
496
- with gr.Tab("πŸ§β€β™‚οΈ Who Spoke When?"):
497
- gr.Interface(
498
- fn=diarize_and_transcribe,
499
- inputs=gr.Audio(label="Upload Interview/Podcast", type="filepath"),
500
- outputs=gr.JSON(label="Diarized Transcript"),
501
- title="Split By Speaker + Transcribe",
502
- description="Detect speakers and transcribe their speech automatically."
503
- )
504
-
505
- # --- Load/Save Project ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
506
  with gr.Tab("πŸ“ Save/Load Project"):
507
  gr.Interface(
508
  fn=save_project,
@@ -516,7 +550,7 @@ with gr.Blocks(title="AI Audio Studio", css="style.css") as demo:
516
  description="Save your session, effects, and settings in one file to reuse later."
517
  )
518
 
519
- # --- Mix Two Tracks ---
520
  with gr.Tab("πŸ”€ Mix Two Tracks"):
521
  gr.Interface(
522
  fn=mix_tracks,
@@ -530,33 +564,17 @@ with gr.Blocks(title="AI Audio Studio", css="style.css") as demo:
530
  description="Mix or subtract two audio files."
531
  )
532
 
533
- # --- Voice Style Transfer ---
534
- with gr.Tab("🧠 Voice Style Transfer"):
535
- gr.Interface(
536
- fn=apply_style_transfer,
537
- inputs=[
538
- gr.Audio(label="Upload Voice Clip", type="filepath"),
539
- gr.Radio(["Happy", "Sad", "Angry", "Calm"], label="Choose Tone")
540
- ],
541
- outputs=gr.Audio(label="Stylized Output", type="filepath"),
542
- title="Change Emotional Tone of Voice",
543
- description="Shift the emotional style of any voice clip."
544
- )
545
-
546
- # --- Metadata Tagging ---
547
- with gr.Tab("πŸ—‚ Add MP3 Tags"):
548
  gr.Interface(
549
- fn=tag_mp3,
550
  inputs=[
551
- gr.File(label="Upload MP3/WAV"),
552
- gr.Textbox(label="Title"),
553
- gr.Textbox(label="Artist"),
554
- gr.Textbox(label="Album"),
555
- gr.Number(label="Year")
556
  ],
557
- outputs=gr.File(label="Tagged Audio File"),
558
- title="Add Title, Artist, Album, Year to MP3",
559
- description="Enhance your exported files with metadata tags"
560
  )
561
 
562
  demo.launch()
 
1
  import gradio as gr
2
  from pydub import AudioSegment
3
+ from pydub.silence import detect_nonsilent
4
  import numpy as np
5
  import tempfile
6
  import os
 
22
  from faster_whisper import WhisperModel
23
  from mutagen.mp3 import MP3
24
  from mutagen.id3 import ID3, TIT2, TPE1, TALB, TYER
25
+ from TTS.api import TTS
26
 
27
  # Suppress warnings
28
  warnings.filterwarnings("ignore")
 
275
  return text
276
 
277
  # === TTS Tab ===
 
 
278
  tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False)
279
 
280
  def generate_tts(text):
 
306
 
307
  return stats, image
308
 
309
+ # === Voiceprint Matching (Optional – requires Python 3.9) ===
310
  try:
311
+ from resemblyzer import preprocess_wav, VoiceEncoder
312
+
313
+ encoder = VoiceEncoder()
314
+
315
+ def match_speakers(clip1, clip2):
316
+ wav1 = preprocess_wav(clip1)
317
+ wav2 = preprocess_wav(clip2)
318
+ embed1 = encoder.embed_utterance(wav1)
319
+ embed2 = encoder.embed_utterance(wav2)
320
+ similarity = np.inner(embed1, embed2)
321
+ return f"Speaker Match Score: {similarity:.2f}"
322
+
323
+ except ImportError:
324
+ def match_speakers(*args):
325
+ return "⚠️ Speaker matching requires Python 3.9 or below"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
 
327
  # === Save/Load Project File (.aiproj) ===
328
  def save_project(audio_path, preset_name, effects):
 
336
  pickle.dump(project_data, f)
337
  return out_path
338
 
339
+ # === VAD – Detect & Trim Silence ===
340
+ def detect_silence(audio_file, silence_threshold=-50.0, min_silence_len=1000):
341
+ audio = AudioSegment.from_file(audio_file)
342
+
343
+ nonsilent_ranges = detect_nonsilent(
344
+ audio,
345
+ min_silence_len=int(min_silence_len),
346
+ silence_thresh=silence_threshold
347
+ )
348
+
349
+ if not nonsilent_ranges:
350
+ return audio.export(os.path.join(tempfile.gettempdir(), "trimmed.wav"), format="wav")
351
+
352
+ trimmed = audio[nonsilent_ranges[0][0]:nonsilent_ranges[-1][1]]
353
+ out_path = os.path.join(tempfile.gettempdir(), "trimmed.wav")
354
+ trimmed.export(out_path, format="wav")
355
+ return out_path
356
+
357
  # === Mix Two Tracks ===
358
  def mix_tracks(track1, track2, volume_offset=0):
359
  a1 = AudioSegment.from_file(track1)
 
363
  mixed.export(out_path, format="wav")
364
  return out_path
365
 
366
+ # === Load Custom Plugins ===
367
+ def load_plugin(plugin_file, audio_file):
 
 
 
 
368
  try:
369
+ from importlib.util import spec_from_file_location, module_from_spec
370
+ spec = spec_from_file_location("plugin", plugin_file.name)
371
+ plugin = module_from_spec(spec)
372
+ spec.loader.exec_module(plugin)
373
+
374
+ # Run the plugin
375
+ audio = AudioSegment.from_file(audio_file)
376
+ processed = plugin.process(audio)
377
+
378
+ # Save output
379
+ out_path = os.path.join(tempfile.gettempdir(), "plugin_output.wav")
380
+ processed.export(out_path, format="wav")
381
+ return out_path
382
  except Exception as e:
383
+ return f"⚠️ Plugin error: {str(e)}"
384
 
385
  # === UI ===
386
  effect_options = [
 
469
  inputs=gr.Audio(label="Upload Audio", type="filepath"),
470
  outputs=gr.Textbox(label="Transcribed Text", lines=10),
471
  title="Transcribe & Edit Spoken Content",
472
+ description="Convert voice to text and edit it before exporting again."
473
  )
474
 
475
  # --- TTS Voice Generator ---
 
482
  description="Type anything and turn it into natural-sounding speech."
483
  )
484
 
485
+ # --- Speaker Matching (Voiceprint) ===
486
+ with gr.Tab("πŸ§β€β™‚οΈ Match Speakers"):
487
+ gr.Interface(
488
+ fn=match_speakers,
489
+ inputs=[
490
+ gr.File(label="Clip 1"),
491
+ gr.File(label="Clip 2")
492
+ ],
493
+ outputs=gr.Textbox(label="Match Score", lines=1),
494
+ title="Are These the Same Person?",
495
+ description="Detect speaker similarity using AI."
496
+ )
497
+
498
+ # --- Voice Cloning (Dummy) ===
499
+ with gr.Tab("🎭 Voice Cloning (AI Dubbing)"):
500
+ gr.Interface(
501
+ fn=apply_style_transfer,
502
+ inputs=[
503
+ gr.Audio(label="Upload Voice Clip", type="filepath"),
504
+ gr.Radio(["Happy", "Sad", "Angry", "Calm"], label="Choose Tone")
505
+ ],
506
+ outputs=gr.Audio(label="Stylized Output", type="filepath"),
507
+ title="Change Emotional Tone of Voice",
508
+ description="Shift the emotional style of any voice clip."
509
+ )
510
+
511
+ # --- Auto-Save Sessions ===
512
+ with gr.Tab("🧾 Shareable Preset"):
513
+ gr.Interface(
514
+ fn=encode_preset,
515
+ inputs=[
516
+ gr.CheckboxGroup(choices=effect_options, label="Effects"),
517
+ gr.Dropdown(choices=preset_names, label="Preset"),
518
+ gr.Dropdown(choices=["MP3", "WAV"], label="Format")
519
+ ],
520
+ outputs=gr.Textbox(label="Shareable Link", lines=1),
521
+ title="Save Your Settings and Share Them",
522
+ description="Generate a link to share your effect chain with others."
523
+ )
524
+
525
+ # --- VAD – Trim Silence Automatically ===
526
+ with gr.Tab("βœ‚οΈ Trim Silence Automatically"):
527
+ gr.Interface(
528
+ fn=detect_silence,
529
+ inputs=[
530
+ gr.File(label="Upload Track"),
531
+ gr.Slider(minimum=-100, maximum=-10, value=-50, label="Silence Threshold (dB)"),
532
+ gr.Number(label="Min Silence Length (ms)", value=1000)
533
+ ],
534
+ outputs=gr.File(label="Trimmed Output"),
535
+ title="Auto-Detect & Remove Silence",
536
+ description="Detect and trim silence at start/end or between words"
537
+ )
538
+
539
+ # --- Load/Save Project ===
540
  with gr.Tab("πŸ“ Save/Load Project"):
541
  gr.Interface(
542
  fn=save_project,
 
550
  description="Save your session, effects, and settings in one file to reuse later."
551
  )
552
 
553
+ # --- Mix Two Tracks ===
554
  with gr.Tab("πŸ”€ Mix Two Tracks"):
555
  gr.Interface(
556
  fn=mix_tracks,
 
564
  description="Mix or subtract two audio files."
565
  )
566
 
567
+ # --- Custom Effect Plugin System ===
568
+ with gr.Tab("🧩 Custom Effect Plugin"):
 
 
 
 
 
 
 
 
 
 
 
 
 
569
  gr.Interface(
570
+ fn=load_plugin,
571
  inputs=[
572
+ gr.File(label="Upload .py plugin"),
573
+ gr.Audio(label="Upload Audio", type="filepath")
 
 
 
574
  ],
575
+ outputs=gr.Audio(label="Processed Output", type="filepath"),
576
+ title="Run Your Own Python Effect",
577
+ description="Upload a .py file with a 'process' function"
578
  )
579
 
580
  demo.launch()