Upload pipeline.py with huggingface_hub
Browse files- pipeline.py +3 -51
pipeline.py
CHANGED
|
@@ -1,53 +1,5 @@
|
|
| 1 |
-
import
|
| 2 |
-
import pandas as pd
|
| 3 |
-
import torchaudio
|
| 4 |
-
import torchaudio.transforms as T
|
| 5 |
|
| 6 |
class BioacousticEngine:
|
| 7 |
-
def __init__(self
|
| 8 |
-
self.device =
|
| 9 |
-
self.repo_dir = repo_dir
|
| 10 |
-
|
| 11 |
-
# Dynamic path loading
|
| 12 |
-
sys.path.append(os.path.abspath(repo_dir))
|
| 13 |
-
from cvt import cvt13
|
| 14 |
-
|
| 15 |
-
# Standardized audio processor loading
|
| 16 |
-
mel_module = importlib.import_module("mel_spectrogram")
|
| 17 |
-
self.preprocessor = mel_module.MelSpectrogramProcessor(device=self.device)
|
| 18 |
-
|
| 19 |
-
# Initialize architecture and map weights
|
| 20 |
-
self.model = cvt13()
|
| 21 |
-
self.model.load_state_dict(torch.load(f"{repo_dir}/protoclr.pth", map_location="cpu"))
|
| 22 |
-
self.model = self.model.to(self.device).eval()
|
| 23 |
-
|
| 24 |
-
# Load compressed spatial boundaries
|
| 25 |
-
brain_data = joblib.load(f"{repo_dir}/trained_cluster_brain.joblib")
|
| 26 |
-
self.reducer = brain_data['umap']
|
| 27 |
-
self.df = pd.read_csv(f"{repo_dir}/acoustic_atlas_metadata.csv")
|
| 28 |
-
|
| 29 |
-
def process_waveform(self, waveform, sample_rate):
|
| 30 |
-
"""Standardizes incoming audio channels and samples."""
|
| 31 |
-
if sample_rate != 16000:
|
| 32 |
-
waveform = T.Resample(orig_freq=sample_rate, new_freq=16000)(waveform)
|
| 33 |
-
if waveform.shape[0] > 1:
|
| 34 |
-
waveform = torch.mean(waveform, dim=0, keepdim=True)
|
| 35 |
-
|
| 36 |
-
# 3-Second peak activity selection
|
| 37 |
-
total_samples = waveform.shape[-1]
|
| 38 |
-
target_samples = 3 * 16000
|
| 39 |
-
if total_samples > target_samples:
|
| 40 |
-
step = 4000
|
| 41 |
-
max_energy = -1
|
| 42 |
-
best_start = 0
|
| 43 |
-
for start in range(0, total_samples - target_samples + 1, step):
|
| 44 |
-
chunk = waveform[:, start:start + target_samples]
|
| 45 |
-
energy = chunk.abs().mean().item()
|
| 46 |
-
if energy > max_energy:
|
| 47 |
-
max_energy = energy
|
| 48 |
-
best_start = start
|
| 49 |
-
waveform = waveform[:, best_start:best_start + target_samples]
|
| 50 |
-
|
| 51 |
-
if waveform.abs().max() > 0.02:
|
| 52 |
-
waveform = waveform / waveform.abs().max()
|
| 53 |
-
return waveform.to(self.device)
|
|
|
|
| 1 |
+
import torch
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
class BioacousticEngine:
|
| 4 |
+
def __init__(self):
|
| 5 |
+
self.device = "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|