Spaces:
Runtime error
Runtime error
Download app.py from laurentthomas/sonaria: direct link, hf CLI and curl.
- Browser
- Download file 1.56 kB
-
https://huggingface.co/spaces/laurentthomas/sonaria/resolve/main/app.py
- Command line
-
hf download hf://spaces/laurentthomas/sonaria/app.py
-
curl -L -o app.py https://huggingface.co/spaces/laurentthomas/sonaria/resolve/main/app.py
1.56 kB
| import base64, tempfile, os | |
| from fastapi import FastAPI | |
| from pydantic import BaseModel | |
| from birdnet_analyzer.analyze.core import analyze | |
| app = FastAPI() | |
| class Req(BaseModel): | |
| audio_base64: str | |
| mime: str = "audio/webm" | |
| overlap: float = 0.0 | |
| min_conf: float = 0.25 | |
| lat: float | None = None | |
| lon: float | None = None | |
| week: int | None = -1 | |
| species_list: list[str] | None = None | |
| def run(r: Req): | |
| ext = ".webm" if "webm" in r.mime else ".wav" if "wav" in r.mime else ".mp3" | |
| with tempfile.NamedTemporaryFile(suffix=ext, delete=False) as f: | |
| f.write(base64.b64decode(r.audio_base64)) | |
| path = f.name | |
| outdir = tempfile.mkdtemp() | |
| try: | |
| analyze( | |
| input=path, output=outdir, min_conf=r.min_conf, overlap=r.overlap, | |
| lat=r.lat or -1, lon=r.lon or -1, week=r.week or -1, rtype="csv", | |
| ) | |
| # parse résultat csv → detections[] | |
| import csv, glob | |
| dets = [] | |
| for csv_path in glob.glob(f"{outdir}/*.csv"): | |
| with open(csv_path) as fh: | |
| for row in csv.DictReader(fh): | |
| dets.append({ | |
| "startSec": float(row["Start (s)"]), | |
| "endSec": float(row["End (s)"]), | |
| "scientific": row["Scientific name"], | |
| "common": row["Common name"], | |
| "confidence": float(row["Confidence"]), | |
| }) | |
| return {"duration": 0, "detections": dets} | |
| finally: | |
| os.unlink(path) | |