Spaces:
Running
Running
Commit Β·
f37b677
1
Parent(s): d647e74
Add debug logging
Browse files
api.py
CHANGED
|
@@ -1,16 +1,14 @@
|
|
| 1 |
"""
|
| 2 |
Snare Scout API - Runs on Hugging Face Spaces
|
| 3 |
-
Handles the heavy AI search, returns results (no audio bytes)
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
| 7 |
import sys
|
| 8 |
import json
|
|
|
|
| 9 |
|
| 10 |
-
# Add src to path for imports
|
| 11 |
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "src"))
|
| 12 |
|
| 13 |
-
# Download database on startup
|
| 14 |
DB_PATH = "library/snare_scout.sqlite"
|
| 15 |
if not os.path.exists(DB_PATH):
|
| 16 |
import requests
|
|
@@ -28,10 +26,10 @@ scout.DEFAULT_DB_PATH = DB_PATH
|
|
| 28 |
|
| 29 |
from fastapi import FastAPI, UploadFile, File
|
| 30 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 31 |
|
| 32 |
app = FastAPI(title="Snare Scout API")
|
| 33 |
|
| 34 |
-
# Allow requests from anywhere
|
| 35 |
app.add_middleware(
|
| 36 |
CORSMiddleware,
|
| 37 |
allow_origins=["*"],
|
|
@@ -40,7 +38,6 @@ app.add_middleware(
|
|
| 40 |
allow_headers=["*"],
|
| 41 |
)
|
| 42 |
|
| 43 |
-
# Load models and library on startup
|
| 44 |
print("π Loading AI models...")
|
| 45 |
embedder = scout.get_embedder()
|
| 46 |
print("π Loading library...")
|
|
@@ -60,14 +57,17 @@ def health():
|
|
| 60 |
|
| 61 |
@app.post("/search")
|
| 62 |
async def search(audio: UploadFile = File(...), top_k: int = 15):
|
| 63 |
-
""
|
| 64 |
-
Search for similar sounds.
|
| 65 |
-
Returns metadata only (url, t0, title, score) - no audio bytes.
|
| 66 |
-
Client fetches audio from YouTube directly.
|
| 67 |
-
"""
|
| 68 |
-
audio_bytes = await audio.read()
|
| 69 |
|
| 70 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
results = scout.search_library(
|
| 72 |
embedder,
|
| 73 |
audio_bytes,
|
|
@@ -76,9 +76,9 @@ async def search(audio: UploadFile = File(...), top_k: int = 15):
|
|
| 76 |
debug=False,
|
| 77 |
db_path=DB_PATH
|
| 78 |
)
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
return {
|
| 82 |
"results": [
|
| 83 |
{
|
| 84 |
"id": r["id"],
|
|
@@ -90,5 +90,11 @@ async def search(audio: UploadFile = File(...), top_k: int = 15):
|
|
| 90 |
for r in results
|
| 91 |
]
|
| 92 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
except Exception as e:
|
| 94 |
-
|
|
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Snare Scout API - Runs on Hugging Face Spaces
|
|
|
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os
|
| 6 |
import sys
|
| 7 |
import json
|
| 8 |
+
import traceback
|
| 9 |
|
|
|
|
| 10 |
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "src"))
|
| 11 |
|
|
|
|
| 12 |
DB_PATH = "library/snare_scout.sqlite"
|
| 13 |
if not os.path.exists(DB_PATH):
|
| 14 |
import requests
|
|
|
|
| 26 |
|
| 27 |
from fastapi import FastAPI, UploadFile, File
|
| 28 |
from fastapi.middleware.cors import CORSMiddleware
|
| 29 |
+
from fastapi.responses import JSONResponse
|
| 30 |
|
| 31 |
app = FastAPI(title="Snare Scout API")
|
| 32 |
|
|
|
|
| 33 |
app.add_middleware(
|
| 34 |
CORSMiddleware,
|
| 35 |
allow_origins=["*"],
|
|
|
|
| 38 |
allow_headers=["*"],
|
| 39 |
)
|
| 40 |
|
|
|
|
| 41 |
print("π Loading AI models...")
|
| 42 |
embedder = scout.get_embedder()
|
| 43 |
print("π Loading library...")
|
|
|
|
| 57 |
|
| 58 |
@app.post("/search")
|
| 59 |
async def search(audio: UploadFile = File(...), top_k: int = 15):
|
| 60 |
+
print(f"[API] Received search request, top_k={top_k}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
try:
|
| 63 |
+
audio_bytes = await audio.read()
|
| 64 |
+
print(f"[API] Audio size: {len(audio_bytes)} bytes")
|
| 65 |
+
|
| 66 |
+
if len(audio_bytes) < 100:
|
| 67 |
+
print("[API] File too small, returning empty")
|
| 68 |
+
return JSONResponse(content={"results": []})
|
| 69 |
+
|
| 70 |
+
print("[API] Starting search...")
|
| 71 |
results = scout.search_library(
|
| 72 |
embedder,
|
| 73 |
audio_bytes,
|
|
|
|
| 76 |
debug=False,
|
| 77 |
db_path=DB_PATH
|
| 78 |
)
|
| 79 |
+
print(f"[API] Search complete, {len(results)} results")
|
| 80 |
|
| 81 |
+
output = {
|
|
|
|
| 82 |
"results": [
|
| 83 |
{
|
| 84 |
"id": r["id"],
|
|
|
|
| 90 |
for r in results
|
| 91 |
]
|
| 92 |
}
|
| 93 |
+
|
| 94 |
+
print(f"[API] Returning response...")
|
| 95 |
+
return JSONResponse(content=output)
|
| 96 |
+
|
| 97 |
except Exception as e:
|
| 98 |
+
print(f"[API] ERROR: {e}")
|
| 99 |
+
print(traceback.format_exc())
|
| 100 |
+
return JSONResponse(content={"error": str(e), "results": []})
|