diff --git a/batdetect2/api.py b/batdetect2/api.py index 1a5f14c..8978b63 100644 --- a/batdetect2/api.py +++ b/batdetect2/api.py @@ -123,6 +123,8 @@ from batdetect2.utils.detector_utils import list_audio_files, load_model import audioread import os import soundfile as sf +import requests +import io # Remove warnings from torch warnings.filterwarnings("ignore", category=UserWarning, module="torch") @@ -279,6 +281,49 @@ def process_file( file_id ) +def process_url( + url: str, + model: DetectionModel = MODEL, + config: Optional[ProcessingConfiguration] = None, + device: torch.device = DEVICE, + file_id: str | None = None +) -> du.RunResults: + """Process audio file with model. + + Parameters + ---------- + url : str + HTTP URL to load the audio data from + model : DetectionModel, optional + Detection model. Uses default model if not specified. + config : Optional[ProcessingConfiguration], optional + Processing configuration, by default None (uses default parameters). + device : torch.device, optional + Device to use, by default tries to use GPU if available. + file_id: Optional[str], + Give the data an id. Defaults to the URL + """ + if config is None: + config = CONFIG + + if file_id is None: + file_id = url + + response = requests.get(url) + + # Raise exception on HTTP error + response.raise_for_status() + + # Retrieve body as raw bytes + raw_audio_data = response.content + + return du.process_file( + io.BytesIO(raw_audio_data), + model, + config, + device, + file_id + ) def process_spectrogram( spec: torch.Tensor,