Create inference.py
Browse files- inference.py +35 -0
inference.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import numpy as np
|
| 3 |
+
import re
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# Pitch class mapping
|
| 7 |
+
NAME_TO_PC = {
|
| 8 |
+
"C":0,"C#":1,"Db":1,"D":2,"D#":3,"Eb":3,"E":4,"F":5,"F#":6,"Gb":6,
|
| 9 |
+
"G":7,"G#":8,"Ab":8,"A":9,"A#":10,"Bb":10,"B":11
|
| 10 |
+
}
|
| 11 |
+
NOTE_TOKEN_RE = re.compile(r"[A-Ga-g](?:#|b)?")
|
| 12 |
+
|
| 13 |
+
# Load model at startup
|
| 14 |
+
MODEL_PATH = os.path.join(os.path.dirname(__file__), "chord_classifier.pkl")
|
| 15 |
+
clf = joblib.load(MODEL_PATH)
|
| 16 |
+
|
| 17 |
+
def notes_to_vector(notes_str: str):
|
| 18 |
+
tokens = NOTE_TOKEN_RE.findall(notes_str)
|
| 19 |
+
pcs = [NAME_TO_PC.get(t.upper(), None) for t in tokens]
|
| 20 |
+
pcs = [p for p in pcs if p is not None]
|
| 21 |
+
vec = np.zeros(12)
|
| 22 |
+
for p in pcs:
|
| 23 |
+
vec[p] = 1
|
| 24 |
+
return vec
|
| 25 |
+
|
| 26 |
+
def predict(notes: str):
|
| 27 |
+
"""
|
| 28 |
+
Main inference function.
|
| 29 |
+
Hugging Face will call this with {"inputs": "C E G"}.
|
| 30 |
+
"""
|
| 31 |
+
vec = notes_to_vector(notes)
|
| 32 |
+
if np.sum(vec) < 2:
|
| 33 |
+
return {"label": "Invalid input (need 2+ notes)"}
|
| 34 |
+
label = clf.predict([vec])[0]
|
| 35 |
+
return {"label": label}
|