Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,116 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
import librosa
|
| 5 |
+
import noisereduce as nr
|
| 6 |
+
from faster_whisper import WhisperModel
|
| 7 |
+
from TTS.api import TTS
|
| 8 |
+
from demucs import separate
|
| 9 |
+
from pydub import AudioSegment
|
| 10 |
+
from scipy.io.wavfile import write
|
| 11 |
+
import os
|
| 12 |
+
import pyloudnorm as pyln
|
| 13 |
|
| 14 |
+
# --- Setup Models ---
|
| 15 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
|
| 17 |
+
# Load Whisper model for Speech-to-Text
|
| 18 |
+
whisper_model = WhisperModel("base", device=device)
|
| 19 |
+
|
| 20 |
+
# Load TTS model
|
| 21 |
+
tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
|
| 22 |
+
|
| 23 |
+
# --- Helper Functions ---
|
| 24 |
+
|
| 25 |
+
def read_wav_file(path):
|
| 26 |
+
return librosa.load(path, sr=None)
|
| 27 |
+
|
| 28 |
+
def save_wav(data, rate, path):
|
| 29 |
+
write(path, rate, data.astype(np.int16))
|
| 30 |
+
|
| 31 |
+
# 1. Noise Reduction
|
| 32 |
+
def noise_reduction(audio_path):
|
| 33 |
+
rate, data = read_wav_file(audio_path)
|
| 34 |
+
if len(data.shape) > 1:
|
| 35 |
+
data = np.mean(data, axis=1)
|
| 36 |
+
reduced_noise = nr.reduce_noise(y=data, sr=rate)
|
| 37 |
+
out_path = "noise_reduced.wav"
|
| 38 |
+
save_wav(reduced_noise, rate, out_path)
|
| 39 |
+
return out_path
|
| 40 |
+
|
| 41 |
+
# 2. Speech-to-Text
|
| 42 |
+
def speech_to_text(audio_path):
|
| 43 |
+
segments, info = whisper_model.transcribe(audio_path, beam_size=5)
|
| 44 |
+
text = "".join(segment.text for segment in segments)
|
| 45 |
+
return text
|
| 46 |
+
|
| 47 |
+
# 3. Text-to-Speech
|
| 48 |
+
def text_to_speech(text):
|
| 49 |
+
out_path = "tts_output.wav"
|
| 50 |
+
tts.tts_to_file(text=text, file_path=out_path)
|
| 51 |
+
return out_path
|
| 52 |
+
|
| 53 |
+
# 4. Source Separation using Demucs
|
| 54 |
+
def source_separation(audio_path):
|
| 55 |
+
out_dir = "separated"
|
| 56 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 57 |
+
separate.main(["--input", audio_path, "--out", out_dir])
|
| 58 |
+
|
| 59 |
+
# Return the stems directory path and files
|
| 60 |
+
track_name = os.path.splitext(os.path.basename(audio_path))[0]
|
| 61 |
+
stems_path = os.path.join(out_dir, track_name)
|
| 62 |
+
separated_files = [os.path.join(stems_path, f) for f in os.listdir(stems_path)]
|
| 63 |
+
return [(f, os.path.basename(f)) for f in separated_files]
|
| 64 |
+
|
| 65 |
+
# 5. Loudness Normalization
|
| 66 |
+
def loudness_normalize(audio_path):
|
| 67 |
+
data, rate = librosa.load(audio_path, sr=None, mono=False)
|
| 68 |
+
meter = pyln.Meter(rate)
|
| 69 |
+
loudness = meter.integrated_loudness(data.T if data.ndim > 1 else data)
|
| 70 |
+
normalized = pyln.normalize.loudness(data, loudness, -16.0)
|
| 71 |
+
|
| 72 |
+
out_path = "normalized_audio.wav"
|
| 73 |
+
write(out_path, rate, (normalized * 32767).astype(np.int16))
|
| 74 |
+
return out_path
|
| 75 |
+
|
| 76 |
+
# --- Gradio Interface ---
|
| 77 |
+
|
| 78 |
+
title = "🔊 AudioMaster - All-in-One Audio Processor"
|
| 79 |
+
description = "Upload an audio file and apply noise reduction, transcription, TTS, separation, or normalization."
|
| 80 |
+
|
| 81 |
+
with gr.Blocks(title=title) as demo:
|
| 82 |
+
gr.Markdown(f"# {title}")
|
| 83 |
+
gr.Markdown(description)
|
| 84 |
+
|
| 85 |
+
with gr.Tab("🧹 Noise Reduction"):
|
| 86 |
+
with gr.Row():
|
| 87 |
+
noise_input = gr.Audio(label="Upload Noisy Audio", type="filepath")
|
| 88 |
+
noise_output = gr.Audio(label="Cleaned Audio")
|
| 89 |
+
gr.Button("Reduce Noise").click(fn=noise_reduction, inputs=noise_input, outputs=noise_output)
|
| 90 |
+
|
| 91 |
+
with gr.Tab("🎙️ Speech-to-Text"):
|
| 92 |
+
with gr.Row():
|
| 93 |
+
stt_input = gr.Audio(label="Upload Speech Audio", type="filepath")
|
| 94 |
+
stt_output = gr.Textbox(label="Transcribed Text")
|
| 95 |
+
gr.Button("Transcribe").click(fn=speech_to_text, inputs=stt_input, outputs=stt_output)
|
| 96 |
+
|
| 97 |
+
with gr.Tab("🗣️ Text-to-Speech"):
|
| 98 |
+
with gr.Row():
|
| 99 |
+
tts_input = gr.Textbox(label="Enter Text")
|
| 100 |
+
tts_output = gr.Audio(label="Generated Speech")
|
| 101 |
+
gr.Button("Synthesize").click(fn=text_to_speech, inputs=tts_input, outputs=tts_output)
|
| 102 |
+
|
| 103 |
+
with gr.Tab("🎵 Source Separation (Demucs)"):
|
| 104 |
+
with gr.Row():
|
| 105 |
+
sep_input = gr.Audio(label="Upload Music Track", type="filepath")
|
| 106 |
+
sep_output = gr.File(label="Separated Stems")
|
| 107 |
+
gr.Button("Separate Sources").click(fn=source_separation, inputs=sep_input, outputs=sep_output)
|
| 108 |
+
|
| 109 |
+
with gr.Tab("🔊 Loudness Normalization"):
|
| 110 |
+
with gr.Row():
|
| 111 |
+
loud_input = gr.Audio(label="Upload Audio to Normalize", type="filepath")
|
| 112 |
+
loud_output = gr.Audio(label="Normalized Audio")
|
| 113 |
+
gr.Button("Normalize").click(fn=loudness_normalize, inputs=loud_input, outputs=loud_output)
|
| 114 |
+
|
| 115 |
+
# Launch
|
| 116 |
demo.launch()
|