-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
71 lines (61 loc) · 2.45 KB
/
Copy pathapi.py
File metadata and controls
71 lines (61 loc) · 2.45 KB
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
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import numpy as np
import librosa # Ensure librosa and any dependencies are installed
import uvicorn
from fastapi.middleware.cors import CORSMiddleware
from python import melody_hash
from python import generate_stored_sequences
# --- DTW, knn_classify, and compute_dtw_distance functions here ---
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000", "http://localhost:3001"], # Add your React app's URL here
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def compute_dtw_distance(seq1, seq2):
seq1 = seq1.reshape(1, -1)
seq2 = seq2.reshape(1, -1)
D, wp = librosa.sequence.dtw(X=seq1, Y=seq2, metric='euclidean')
dtw_distance = D[-1, -1]
return dtw_distance
def knn_classify(test_sequence, stored_sequences, k=3):
"""
Compare test_sequence to each stored sequence and return an array of predictions.
Each stored sequence is a tuple: ([interval1, interval2, interval3], metadata)
where metadata is a list of (song_name, splice_order) tuples.
"""
distances = []
for stored_seq, metadata in stored_sequences:
dtw_distance = compute_dtw_distance(np.array(stored_seq), np.array(test_sequence))
distances.append((dtw_distance, metadata))
distances.sort(key=lambda x: x[0])
# Get the top k nearest neighbors
nearest = distances[:k]
predictions = []
for dist, meta in nearest:
# Extend predictions with all metadata from this neighbor
predictions.extend(meta)
return predictions
# Generate stored sequences using the external module.
stored_sequences = generate_stored_sequences()
# Dummy stored_sequences (replace with your actual stored data)
#stored_sequences = [
#([4, 3, 5], "Beethoven Symphony No. 5"),
# ([3, 2, 4], "Mozart Eine kleine Nachtmusik"),
# ... additional sequences
#]
# Define request model for input JSON
class HummingSequence(BaseModel):
sequence: list # list of intervals (e.g., [4, 3, 5])
@app.post("/api/classify")
async def classify_humming(data: HummingSequence):
test_sequence = np.array(data.sequence)
if test_sequence.size == 0:
raise HTTPException(status_code=400, detail="Empty sequence provided")
predictions = knn_classify(test_sequence, stored_sequences, k=3)
return {"predictions": predictions}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)