Spaces:
Running
Running
Commit ·
e369286
1
Parent(s): a3087a5
Simplify Gradio
Browse files
api.py
CHANGED
|
@@ -1,13 +1,12 @@
|
|
| 1 |
"""
|
| 2 |
-
Snare Scout API -
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os
|
| 6 |
import sys
|
| 7 |
-
import json
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
-
|
| 11 |
|
| 12 |
DB_PATH = "library/snare_scout.sqlite"
|
| 13 |
if not os.path.exists(DB_PATH):
|
|
@@ -31,65 +30,44 @@ lib = scout.load_library_matrices(DB_PATH, False)
|
|
| 31 |
print(f"✅ Ready! {len(lib['ids'])} clips loaded")
|
| 32 |
|
| 33 |
|
| 34 |
-
def search(
|
| 35 |
-
print(f"[API]
|
| 36 |
-
print(f"[API] audio type: {type(audio)}")
|
| 37 |
-
print(f"[API] audio value: {audio}")
|
| 38 |
-
print(f"[API] top_k: {top_k}")
|
| 39 |
|
| 40 |
-
if
|
| 41 |
-
|
| 42 |
-
return json.dumps({"error": "No audio", "results": []})
|
| 43 |
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
with open(audio, 'rb') as f:
|
| 47 |
audio_bytes = f.read()
|
| 48 |
|
| 49 |
print(f"[API] Audio size: {len(audio_bytes)} bytes")
|
| 50 |
|
| 51 |
results = scout.search_library(
|
| 52 |
-
embedder,
|
| 53 |
-
|
| 54 |
-
lib,
|
| 55 |
-
top_k=int(top_k),
|
| 56 |
-
debug=False,
|
| 57 |
-
db_path=DB_PATH
|
| 58 |
)
|
|
|
|
| 59 |
print(f"[API] Found {len(results)} results")
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
"url": r["url"],
|
| 66 |
-
"t0": r["t0"],
|
| 67 |
-
"title": r["title"],
|
| 68 |
-
"score": float(r.get("score", 0))
|
| 69 |
-
}
|
| 70 |
-
for r in results
|
| 71 |
-
]
|
| 72 |
-
}
|
| 73 |
|
| 74 |
-
return
|
| 75 |
|
| 76 |
except Exception as e:
|
| 77 |
print(f"[API] ERROR: {e}")
|
| 78 |
import traceback
|
| 79 |
traceback.print_exc()
|
| 80 |
-
return
|
| 81 |
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
submit_btn = gr.Button("Search")
|
| 91 |
-
output = gr.Textbox(label="Results JSON", lines=10)
|
| 92 |
-
|
| 93 |
-
submit_btn.click(fn=search, inputs=[audio_input, top_k_input], outputs=output)
|
| 94 |
|
| 95 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
"""
|
| 2 |
+
Snare Scout API - Minimal Gradio
|
| 3 |
"""
|
| 4 |
|
| 5 |
import os
|
| 6 |
import sys
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
+
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
| 10 |
|
| 11 |
DB_PATH = "library/snare_scout.sqlite"
|
| 12 |
if not os.path.exists(DB_PATH):
|
|
|
|
| 30 |
print(f"✅ Ready! {len(lib['ids'])} clips loaded")
|
| 31 |
|
| 32 |
|
| 33 |
+
def search(audio_file):
|
| 34 |
+
print(f"[API] Got request: {audio_file}")
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
if audio_file is None:
|
| 37 |
+
return "No audio uploaded"
|
|
|
|
| 38 |
|
| 39 |
try:
|
| 40 |
+
with open(audio_file, 'rb') as f:
|
|
|
|
| 41 |
audio_bytes = f.read()
|
| 42 |
|
| 43 |
print(f"[API] Audio size: {len(audio_bytes)} bytes")
|
| 44 |
|
| 45 |
results = scout.search_library(
|
| 46 |
+
embedder, audio_bytes, lib,
|
| 47 |
+
top_k=10, debug=False, db_path=DB_PATH
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
)
|
| 49 |
+
|
| 50 |
print(f"[API] Found {len(results)} results")
|
| 51 |
|
| 52 |
+
# Return as plain text
|
| 53 |
+
lines = []
|
| 54 |
+
for r in results:
|
| 55 |
+
lines.append(f"{r['id']}|{r['url']}|{r['t0']}|{r['title']}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
return "\n".join(lines)
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
print(f"[API] ERROR: {e}")
|
| 61 |
import traceback
|
| 62 |
traceback.print_exc()
|
| 63 |
+
return f"Error: {e}"
|
| 64 |
|
| 65 |
|
| 66 |
+
demo = gr.Interface(
|
| 67 |
+
fn=search,
|
| 68 |
+
inputs=gr.Audio(type="filepath"),
|
| 69 |
+
outputs=gr.Textbox(),
|
| 70 |
+
title="Snare Scout"
|
| 71 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|