File size: 2,781 Bytes
73380e1
e369286
73380e1
 
 
 
c858ef3
73380e1
e369286
73380e1
94bde3d
 
39e7de4
 
 
9080c96
 
 
39e7de4
9080c96
94bde3d
39e7de4
 
9080c96
94bde3d
 
 
 
 
 
 
73380e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e369286
 
c858ef3
e369286
 
73380e1
 
e369286
c858ef3
f37b677
c858ef3
f37b677
73380e1
e369286
02c24b9
73380e1
e369286
c858ef3
73380e1
e369286
 
 
f37b677
02c24b9
f37b677
73380e1
f37b677
3d7df6e
 
e369286
c858ef3
 
e369286
 
 
 
 
 
c858ef3
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
"""
Snare Scout API - Minimal Gradio
"""

import os
import sys
import gradio as gr

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

# Download PANNs checkpoint to current directory
PANNS_CHECKPOINT = "Cnn14_mAP=0.431.pth"

if not os.path.exists(PANNS_CHECKPOINT):
    print("πŸ“₯ Downloading PANNs checkpoint (~320MB)...")
    import urllib.request
    urllib.request.urlretrieve(
        "https://zenodo.org/record/3987831/files/Cnn14_mAP%3D0.431.pth?download=1",
        PANNS_CHECKPOINT
    )
    print(f"βœ… PANNs checkpoint ready! Size: {os.path.getsize(PANNS_CHECKPOINT)}")
else:
    print(f"βœ… PANNs checkpoint exists: {os.path.getsize(PANNS_CHECKPOINT)} bytes")

# Also copy to where panns_inference might look
os.makedirs("/root/panns_data", exist_ok=True)
if not os.path.exists("/root/panns_data/Cnn14_mAP=0.431.pth"):
    import shutil
    shutil.copy(PANNS_CHECKPOINT, "/root/panns_data/Cnn14_mAP=0.431.pth")
    print("βœ… Copied to /root/panns_data/")

DB_PATH = "library/snare_scout.sqlite"
if not os.path.exists(DB_PATH):
    import requests
    os.makedirs("library", exist_ok=True)
    print("πŸ“₯ Downloading database...")
    url = "https://www.dropbox.com/scl/fi/x6ybzdfak4m6q30k3xkec/snare_scout.sqlite?rlkey=cku45y0c7q06hf7y0ca0ktv3e&dl=1"
    response = requests.get(url, stream=True)
    with open(DB_PATH, 'wb') as f:
        for chunk in response.iter_content(chunk_size=8192):
            f.write(chunk)
    print("βœ… Database ready!")

import scout
scout.DEFAULT_DB_PATH = DB_PATH

print("πŸ”„ Loading AI models...")
embedder = scout.get_embedder()
print("πŸ”„ Loading library...")
lib = scout.load_library_matrices(DB_PATH, False)
print(f"βœ… Ready! {len(lib['ids'])} clips loaded")


def search(audio_file):
    print(f"[API] Got request: {audio_file}")
    
    if audio_file is None:
        return "No audio uploaded"
    
    try:
        with open(audio_file, 'rb') as f:
            audio_bytes = f.read()
        
        print(f"[API] Audio size: {len(audio_bytes)} bytes")
        
        results = scout.search_library(
            embedder, audio_bytes, lib, 
            top_k=10, debug=True, db_path=DB_PATH
        )
        
        print(f"[API] Found {len(results)} results")
        
        lines = []
        for r in results:
            lines.append(f"{r['id']}|{r['url']}|{r['t0']}|{r['title']}")
        
        return "\n".join(lines) if lines else "No matches found"
        
    except Exception as e:
        print(f"[API] ERROR: {e}")
        import traceback
        traceback.print_exc()
        return f"Error: {e}"


demo = gr.Interface(
    fn=search,
    inputs=gr.Audio(type="filepath"),
    outputs=gr.Textbox(),
    title="Snare Scout"
)

demo.launch(server_name="0.0.0.0", server_port=7860)