File size: 11,935 Bytes
87e044e 9dcc975 c9c6cc6 9dcc975 c9c6cc6 9dcc975 c9c6cc6 9dcc975 c9c6cc6 9dcc975 c9c6cc6 9dcc975 5aa1563 9dcc975 a2d6668 9dcc975 87e044e c9c6cc6 87e044e c9c6cc6 87e044e a2d6668 87e044e a2d6668 87e044e c9c6cc6 87e044e c9c6cc6 87e044e 9dcc975 |
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 |
import gradio as gr
import torch
import numpy as np
import torchaudio
import tempfile
import os
# Patch LinaCodec to work on CPU
print("Setting up LinaCodec for CPU...")
# Import and patch before initializing
from linacodec.tokenizer import LinaCodecModel
from huggingface_hub import hf_hub_download
import torch.nn as nn
class CPULinaCodec:
"""CPU-compatible wrapper for LinaCodec"""
def __init__(self):
print("Loading LinaCodec model on CPU...")
# Download model files
repo_id = "YatharthS/LinaCodec"
config_path = hf_hub_download(repo_id=repo_id, filename="config.yaml")
weights_path = hf_hub_download(repo_id=repo_id, filename="model.safetensors")
# Load model on CPU instead of CUDA
self.device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {self.device}")
self.model = LinaCodecModel.from_pretrained(
config_path=config_path,
weights_path=weights_path
).eval()
# Move to appropriate device
self.model = self.model.to(self.device)
self.sample_rate = 48000
print(f"Model loaded successfully on {self.device}!")
def encode(self, audio_path):
"""Encode audio file to tokens and embeddings"""
import torchaudio
# Load audio
wav, sr = torchaudio.load(audio_path)
wav = wav.to(self.device)
# Resample if needed
if sr != 24000:
resampler = torchaudio.transforms.Resample(sr, 24000).to(self.device)
wav = resampler(wav)
# Ensure mono
if wav.shape[0] > 1:
wav = wav.mean(dim=0, keepdim=True)
# Encode
with torch.no_grad():
codes, embedding = self.model.encode(wav.unsqueeze(0))
return codes, embedding
def decode(self, codes, embedding):
"""Decode tokens and embeddings back to audio"""
with torch.no_grad():
wav = self.model.decode(codes, embedding)
return wav.squeeze(0)
def convert_voice(self, source_path, reference_path):
"""Convert voice using source content and reference timbre"""
import torchaudio
# Load source audio
source_wav, source_sr = torchaudio.load(source_path)
source_wav = source_wav.to(self.device)
if source_sr != 24000:
resampler = torchaudio.transforms.Resample(source_sr, 24000).to(self.device)
source_wav = resampler(source_wav)
if source_wav.shape[0] > 1:
source_wav = source_wav.mean(dim=0, keepdim=True)
# Load reference audio
ref_wav, ref_sr = torchaudio.load(reference_path)
ref_wav = ref_wav.to(self.device)
if ref_sr != 24000:
resampler = torchaudio.transforms.Resample(ref_sr, 24000).to(self.device)
ref_wav = resampler(ref_wav)
if ref_wav.shape[0] > 1:
ref_wav = ref_wav.mean(dim=0, keepdim=True)
# Encode source for content
with torch.no_grad():
source_codes, _ = self.model.encode(source_wav.unsqueeze(0))
# Encode reference for timbre
_, ref_embedding = self.model.encode(ref_wav.unsqueeze(0))
# Decode with source codes but reference embedding
converted_wav = self.model.decode(source_codes, ref_embedding)
return converted_wav.squeeze(0)
# Initialize the CPU-compatible model
lina_tokenizer = CPULinaCodec()
def encode_decode_audio(audio_input):
"""Encode and decode audio to demonstrate compression."""
try:
if audio_input is None:
return None, "Please upload an audio file."
# audio_input is a tuple (sample_rate, audio_data)
sr, audio_data = audio_input
# Save temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as tmp:
temp_path = tmp.name
# Convert to tensor and save
if audio_data.dtype == np.int16:
audio_data = audio_data.astype(np.float32) / 32768.0
elif audio_data.dtype == np.int32:
audio_data = audio_data.astype(np.float32) / 2147483648.0
# Handle mono/stereo
if len(audio_data.shape) == 1:
audio_tensor = torch.FloatTensor(audio_data).unsqueeze(0)
else:
audio_tensor = torch.FloatTensor(audio_data.T)
# Save as wav
torchaudio.save(temp_path, audio_tensor, sr)
# Encode
speech_tokens, global_embedding = lina_tokenizer.encode(temp_path)
# Decode
decoded_audio = lina_tokenizer.decode(speech_tokens, global_embedding)
# Clean up
os.unlink(temp_path)
# Convert to numpy for Gradio
decoded_audio = decoded_audio.cpu().squeeze().numpy()
device_info = "GPU (CUDA)" if torch.cuda.is_available() else "CPU"
info = f"β
Success!\n"
info += f"Device: {device_info}\n"
info += f"Original sample rate: {sr} Hz\n"
info += f"Output sample rate: 48000 Hz\n"
info += f"Speech tokens shape: {speech_tokens.shape}\n"
info += f"Global embedding shape: {global_embedding.shape}"
return (48000, decoded_audio), info
except Exception as e:
return None, f"β Error: {str(e)}"
def voice_conversion(source_audio, reference_audio):
"""Convert voice using source content and reference timbre."""
try:
if source_audio is None or reference_audio is None:
return None, "Please upload both source and reference audio files."
# Save source audio
sr_source, audio_source = source_audio
with tempfile.NamedTemporaryFile(delete=False, suffix='_source.wav') as tmp:
source_path = tmp.name
if audio_source.dtype == np.int16:
audio_source = audio_source.astype(np.float32) / 32768.0
elif audio_source.dtype == np.int32:
audio_source = audio_source.astype(np.float32) / 2147483648.0
if len(audio_source.shape) == 1:
audio_tensor = torch.FloatTensor(audio_source).unsqueeze(0)
else:
audio_tensor = torch.FloatTensor(audio_source.T)
torchaudio.save(source_path, audio_tensor, sr_source)
# Save reference audio
sr_ref, audio_ref = reference_audio
with tempfile.NamedTemporaryFile(delete=False, suffix='_ref.wav') as tmp:
ref_path = tmp.name
if audio_ref.dtype == np.int16:
audio_ref = audio_ref.astype(np.float32) / 32768.0
elif audio_ref.dtype == np.int32:
audio_ref = audio_ref.astype(np.float32) / 2147483648.0
if len(audio_ref.shape) == 1:
audio_tensor = torch.FloatTensor(audio_ref).unsqueeze(0)
else:
audio_tensor = torch.FloatTensor(audio_ref.T)
torchaudio.save(ref_path, audio_tensor, sr_ref)
# Convert voice
converted_audio = lina_tokenizer.convert_voice(source_path, ref_path)
# Clean up
os.unlink(source_path)
os.unlink(ref_path)
# Convert to numpy
converted_audio = converted_audio.cpu().squeeze().numpy()
device_info = "GPU (CUDA)" if torch.cuda.is_available() else "CPU"
info = f"β
Voice conversion successful!\n"
info += f"Device: {device_info}\n"
info += f"Source sample rate: {sr_source} Hz\n"
info += f"Reference sample rate: {sr_ref} Hz\n"
info += f"Output sample rate: 48000 Hz\n"
info += f"Content taken from source, timbre/style from reference"
return (48000, converted_audio), info
except Exception as e:
return None, f"β Error: {str(e)}"
# Create Gradio interface
with gr.Blocks(title="LinaCodec Audio Tool", theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# π΅ LinaCodec Audio Tool
**LinaCodec** is a neural audio codec for high-quality speech compression and voice conversion.
### Features:
- π **Encode & Decode**: Compress and reconstruct audio at 48kHz
- π **Voice Conversion**: Transfer timbre/style from one speaker to another
- π» **CPU Compatible**: Works on both CPU and GPU
""")
with gr.Tabs():
# Tab 1: Encode/Decode
with gr.Tab("π Encode & Decode"):
gr.Markdown("""
Upload an audio file to encode it into speech tokens and then decode it back.
This demonstrates the codec's compression and reconstruction capabilities.
""")
with gr.Row():
with gr.Column():
audio_input = gr.Audio(
label="Upload Audio",
type="numpy",
sources=["upload", "microphone"]
)
encode_btn = gr.Button("π Encode & Decode", variant="primary")
with gr.Column():
audio_output = gr.Audio(label="Decoded Audio")
info_output = gr.Textbox(label="Info", lines=6)
encode_btn.click(
fn=encode_decode_audio,
inputs=[audio_input],
outputs=[audio_output, info_output]
)
gr.Examples(
examples=[],
inputs=[audio_input],
label="Examples (upload your own audio)"
)
# Tab 2: Voice Conversion
with gr.Tab("π Voice Conversion"):
gr.Markdown("""
Convert voice by taking content from **source audio** and timbre/style from **reference audio**.
- **Source**: The speech content you want to keep
- **Reference**: The voice style/timbre you want to apply
""")
with gr.Row():
with gr.Column():
source_input = gr.Audio(
label="Source Audio (Content)",
type="numpy",
sources=["upload", "microphone"]
)
reference_input = gr.Audio(
label="Reference Audio (Timbre/Style)",
type="numpy",
sources=["upload", "microphone"]
)
convert_btn = gr.Button("β¨ Convert Voice", variant="primary")
with gr.Column():
converted_output = gr.Audio(label="Converted Audio")
convert_info = gr.Textbox(label="Info", lines=6)
convert_btn.click(
fn=voice_conversion,
inputs=[source_input, reference_input],
outputs=[converted_output, convert_info]
)
gr.Markdown("""
---
### π About LinaCodec
LinaCodec is a neural audio codec designed for high-quality speech compression and voice conversion.
It encodes audio into discrete tokens and a global embedding, enabling efficient storage and manipulation of speech.
**Model**: [YatharthS/LinaCodec](https://huggingface.co/YatharthS/LinaCodec)
### βοΈ Technical Details
- Output sample rate: 48 kHz
- Supports various input formats
- Neural compression with high reconstruction quality
- Works on both CPU and GPU (GPU recommended for faster processing)
""")
# Launch the app
if __name__ == "__main__":
demo.launch() |