File size: 1,355 Bytes
2d0e212
3cd2d15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2b8990
5a36461
a2b8990
3cd2d15
 
a2b8990
2d0e212
 
5a36461
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
import gradio as gr
import joblib
import numpy as np
import os
import re
import subprocess

NAME_TO_PC = {
    "C":0,"C#":1,"Db":1,"D":2,"D#":3,"Eb":3,"E":4,"F":5,"F#":6,"Gb":6,
    "G":7,"G#":8,"Ab":8,"A":9,"A#":10,"Bb":10,"B":11
}
NOTE_TOKEN_RE = re.compile(r"[A-Ga-g](?:#|b)?")

def notes_to_vector(notes_str: str):
    tokens = NOTE_TOKEN_RE.findall(notes_str)
    pcs = [NAME_TO_PC.get(t.upper(), None) for t in tokens]
    pcs = [p for p in pcs if p is not None]
    vec = np.zeros(12)
    for p in pcs:
        vec[p] = 1
    return vec

MODEL_PATH = "chord_classifier.pkl"

def load_model():
    if not os.path.exists(MODEL_PATH):
        print("⚠️ chord_classifier.pkl not found. Training model...")
        subprocess.run(["python", "train_chord_model.py"], check=True)
    return joblib.load(MODEL_PATH)

clf = load_model()

def chord_bot(message: str, history: list[tuple[str,str]]):
    vec = notes_to_vector(message)
    if np.sum(vec) < 2:
        return "⚠️ Please enter at least 2 distinct notes (e.g., C E G)"
    label = clf.predict([vec])[0]
    return f"🎵 Identified chord: **{label}**"

chatbot = gr.ChatInterface(
    fn=chord_bot,
    title="🎶 ML Chord Bot",
    description="Enter 2+ notes (e.g., C E G or Db F Ab C). Powered by a trained RandomForest classifier."
)

if __name__ == "__main__":
    chatbot.launch()