Spaces:
Sleeping
Sleeping
Delete filtravimas.py
Browse files- filtravimas.py +0 -84
filtravimas.py
DELETED
@@ -1,84 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import torch
|
3 |
-
import torchaudio
|
4 |
-
import torch.nn as nn
|
5 |
-
import torchaudio.transforms as T
|
6 |
-
import noisereduce as nr
|
7 |
-
import numpy as np
|
8 |
-
from asteroid.models import DCCRNet
|
9 |
-
|
10 |
-
TEMP_DIR = "temp_filtered"
|
11 |
-
OUTPUT_PATH = os.path.join(TEMP_DIR, "ivestis.wav")
|
12 |
-
os.makedirs(TEMP_DIR, exist_ok=True)
|
13 |
-
|
14 |
-
class WaveUNet(nn.Module):
|
15 |
-
def __init__(self):
|
16 |
-
super(WaveUNet, self).__init__()
|
17 |
-
self.encoder = nn.Sequential(
|
18 |
-
nn.Conv1d(1, 16, kernel_size=3, stride=1, padding=1),
|
19 |
-
nn.ReLU(),
|
20 |
-
nn.Conv1d(16, 32, kernel_size=3, stride=1, padding=1),
|
21 |
-
nn.ReLU(),
|
22 |
-
nn.Conv1d(32, 64, kernel_size=3, stride=1, padding=1),
|
23 |
-
nn.ReLU(),
|
24 |
-
)
|
25 |
-
self.decoder = nn.Sequential(
|
26 |
-
nn.ConvTranspose1d(64, 32, kernel_size=3, stride=1, padding=1),
|
27 |
-
nn.ReLU(),
|
28 |
-
nn.ConvTranspose1d(32, 16, kernel_size=3, stride=1, padding=1),
|
29 |
-
nn.ReLU(),
|
30 |
-
nn.ConvTranspose1d(16, 1, kernel_size=3, stride=1, padding=1)
|
31 |
-
)
|
32 |
-
|
33 |
-
def forward(self, x):
|
34 |
-
x = self.encoder(x)
|
35 |
-
x = self.decoder(x)
|
36 |
-
return x
|
37 |
-
|
38 |
-
def filtruoti_su_waveunet(input_path, output_path):
|
39 |
-
print("🔧 Wave-U-Net filtravimas...")
|
40 |
-
model = WaveUNet()
|
41 |
-
model.eval()
|
42 |
-
mixture, sr = torchaudio.load(input_path)
|
43 |
-
if sr != 16000:
|
44 |
-
print("🔁 Resample į 16kHz...")
|
45 |
-
mixture = T.Resample(orig_freq=sr, new_freq=16000)(mixture)
|
46 |
-
mixture = mixture.unsqueeze(0)
|
47 |
-
with torch.no_grad():
|
48 |
-
output = model(mixture)
|
49 |
-
output = output.squeeze(0)
|
50 |
-
torchaudio.save(output_path, output, 16000)
|
51 |
-
print(f"✅ Wave-U-Net išsaugota: {output_path}")
|
52 |
-
|
53 |
-
def filtruoti_su_denoiser(input_path, output_path):
|
54 |
-
print("🔧 Denoiser (DCCRNet)...")
|
55 |
-
model = DCCRNet.from_pretrained("JorisCos/DCCRNet_Libri1Mix_enhsingle_16k")
|
56 |
-
mixture, sr = torchaudio.load(input_path)
|
57 |
-
if sr != 16000:
|
58 |
-
print("🔁 Resample į 16kHz...")
|
59 |
-
mixture = T.Resample(orig_freq=sr, new_freq=16000)(mixture)
|
60 |
-
with torch.no_grad():
|
61 |
-
est_source = model.separate(mixture)
|
62 |
-
torchaudio.save(output_path, est_source[0], 16000)
|
63 |
-
print(f"✅ Denoiser išsaugota: {output_path}")
|
64 |
-
|
65 |
-
def filtruoti_su_noisereduce(input_path, output_path):
|
66 |
-
print("🔧 Noisereduce filtravimas...")
|
67 |
-
waveform, sr = torchaudio.load(input_path)
|
68 |
-
audio = waveform.numpy()[0]
|
69 |
-
reduced = nr.reduce_noise(y=audio, sr=sr)
|
70 |
-
reduced_tensor = torch.from_numpy(reduced).unsqueeze(0)
|
71 |
-
torchaudio.save(output_path, reduced_tensor, sr)
|
72 |
-
print(f"✅ Noisereduce išsaugota: {output_path}")
|
73 |
-
|
74 |
-
def filtruoti_audio(input_path: str, metodas: str) -> str:
|
75 |
-
if metodas == "Denoiser":
|
76 |
-
filtruoti_su_denoiser(input_path, OUTPUT_PATH)
|
77 |
-
elif metodas == "Wave-U-Net":
|
78 |
-
filtruoti_su_waveunet(input_path, OUTPUT_PATH)
|
79 |
-
elif metodas == "Noisereduce":
|
80 |
-
filtruoti_su_noisereduce(input_path, OUTPUT_PATH)
|
81 |
-
else:
|
82 |
-
raise ValueError("Nepalaikomas filtravimo metodas")
|
83 |
-
|
84 |
-
return OUTPUT_PATH
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|