File size: 7,453 Bytes
909e414 |
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 |
import gradio as gr
import torch
import torchaudio
import numpy as np
from pathlib import Path
from huggingface_hub import hf_hub_download
from omegaconf import DictConfig
from miipher_2.model.feature_cleaner import FeatureCleaner
from miipher_2.lightning_vocoders.lightning_module import HiFiGANLightningModule
# Model configuration
MODEL_REPO_ID = "Atotti/miipher-2-HuBERT-HiFi-GAN-v0.1"
ADAPTER_FILENAME = "checkpoint_199k_fixed.pt"
VOCODER_FILENAME = "epoch=77-step=137108.ckpt"
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
SAMPLE_RATE_INPUT = 16000
SAMPLE_RATE_OUTPUT = 22050
# Cache for models
models_cache = {}
def download_models():
"""Download models from Hugging Face Hub"""
print("Downloading models from Hugging Face Hub...")
adapter_path = hf_hub_download(
repo_id=MODEL_REPO_ID,
filename=ADAPTER_FILENAME,
cache_dir="./models"
)
vocoder_path = hf_hub_download(
repo_id=MODEL_REPO_ID,
filename=VOCODER_FILENAME,
cache_dir="./models"
)
return adapter_path, vocoder_path
def load_models():
"""Load models into memory"""
if "cleaner" in models_cache and "vocoder" in models_cache:
return models_cache["cleaner"], models_cache["vocoder"]
adapter_path, vocoder_path = download_models()
# Model configuration
model_config = DictConfig({
"hubert_model_name": "utter-project/mHuBERT-147",
"hubert_layer": 6,
"adapter_hidden_dim": 768
})
# Initialize FeatureCleaner
print("Loading FeatureCleaner...")
cleaner = FeatureCleaner(model_config).to(DEVICE).eval()
# Load adapter weights
adapter_checkpoint = torch.load(adapter_path, map_location=DEVICE, weights_only=False)
cleaner.load_state_dict(adapter_checkpoint["model_state_dict"])
# Load vocoder
print("Loading vocoder...")
vocoder = HiFiGANLightningModule.load_from_checkpoint(
vocoder_path, map_location=DEVICE
).to(DEVICE).eval()
# Cache models
models_cache["cleaner"] = cleaner
models_cache["vocoder"] = vocoder
return cleaner, vocoder
@torch.inference_mode()
def enhance_audio(audio_path, progress=gr.Progress()):
"""Enhance audio using Miipher-2 model"""
try:
progress(0, desc="Loading models...")
cleaner, vocoder = load_models()
progress(0.2, desc="Loading audio...")
# Load audio
waveform, sr = torchaudio.load(audio_path)
# Resample to 16kHz if needed
if sr != SAMPLE_RATE_INPUT:
waveform = torchaudio.functional.resample(waveform, sr, SAMPLE_RATE_INPUT)
# Convert to mono if stereo
waveform = waveform.mean(0, keepdim=True)
# Move to device
waveform = waveform.to(DEVICE)
progress(0.4, desc="Extracting features...")
# Extract features using FeatureCleaner
with torch.no_grad(), torch.autocast(device_type=DEVICE.type, dtype=torch.float16, enabled=(DEVICE.type == "cuda")):
features = cleaner(waveform)
# Ensure correct shape for vocoder
if features.dim() == 2:
features = features.unsqueeze(0)
progress(0.7, desc="Generating enhanced audio...")
# Generate audio using vocoder
# Lightning SSL-Vocoderの入力形式に合わせる (batch, seq_len, input_channels)
batch = {"input_feature": features.transpose(1, 2)}
enhanced_audio = vocoder.generator_forward(batch)
# Convert to numpy
enhanced_audio = enhanced_audio.squeeze(0).cpu().to(torch.float32).detach().numpy()
progress(1.0, desc="Enhancement complete!")
# Save audio using torchaudio to avoid Gradio format issues
enhanced_audio = np.clip(enhanced_audio, -1.0, 1.0)
enhanced_audio_tensor = torch.from_numpy(enhanced_audio)
# Ensure 2D tensor: (channels, samples)
if enhanced_audio_tensor.dim() == 1:
enhanced_audio_tensor = enhanced_audio_tensor.unsqueeze(0)
# Save to temporary file using torchaudio
import tempfile
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp_file:
torchaudio.save(tmp_file.name, enhanced_audio_tensor, SAMPLE_RATE_OUTPUT)
return tmp_file.name
except Exception as e:
raise gr.Error(f"Error during enhancement: {str(e)}")
# Create Gradio interface
def create_interface():
title = "🎤 Miipher-2 Speech Enhancement"
description = """
<div style="text-align: center;">
<p>High-quality speech enhancement using <b>Miipher-2</b> (HuBERT + Parallel Adapter + HiFi-GAN)</p>
<p>📄 <a href="https://arxiv.org/abs/2505.04457">Paper</a> |
🤗 <a href="https://huggingface.co/Atotti/miipher-2-HuBERT-HiFi-GAN-v0.1">Model</a> |
💻 <a href="https://github.com/your-repo/open-miipher-2">GitHub</a></p>
</div>
"""
article = """
## How it works
1. **Upload** a noisy or degraded audio file
2. **Process** using Miipher-2 model
3. **Download** the enhanced audio
### Model Details
- **SSL Backbone**: mHuBERT-147 (Multilingual)
- **Adapter**: Parallel adapters at layer 6
- **Vocoder**: HiFi-GAN trained on SSL features
- **Input**: Any sample rate (automatically resampled to 16kHz)
- **Output**: 22.05kHz high-quality audio
### Tips
- Works best with speech audio
- Supports various noise types (background noise, reverb, etc.)
- Processing time depends on audio length and hardware
"""
examples = [
["examples/noisy_speech_1.wav"],
["examples/noisy_speech_2.wav"],
["examples/reverb_speech.wav"],
]
with gr.Blocks(title=title, theme=gr.themes.Soft()) as demo:
gr.Markdown(f"# {title}")
gr.Markdown(description)
with gr.Row():
with gr.Column():
input_audio = gr.Audio(
label="Input Audio (Noisy/Degraded)",
type="filepath",
sources=["upload", "microphone"]
)
enhance_btn = gr.Button("🚀 Enhance Audio", variant="primary")
with gr.Column():
output_audio = gr.Audio(
label="Enhanced Audio",
type="filepath",
interactive=False
)
# Add examples if they exist
examples_dir = Path("examples")
if examples_dir.exists():
example_files = list(examples_dir.glob("*.wav")) + list(examples_dir.glob("*.mp3"))
if example_files:
gr.Examples(
examples=[[str(f)] for f in example_files[:3]],
inputs=input_audio,
outputs=output_audio,
fn=enhance_audio,
cache_examples=True
)
gr.Markdown(article)
# Connect the enhancement function
enhance_btn.click(
fn=enhance_audio,
inputs=input_audio,
outputs=output_audio,
show_progress=True
)
return demo
# Launch the app
if __name__ == "__main__":
# Pre-load models
print("Pre-loading models...")
load_models()
print("Models loaded successfully!")
# Create and launch interface
demo = create_interface()
demo.launch()
|