File size: 1,881 Bytes
e1837eb
 
 
fbf9193
404cb8e
fbf9193
404cb8e
 
 
 
 
36f495e
404cb8e
36f495e
404cb8e
 
 
 
 
 
e1837eb
285d5b7
 
 
e1837eb
 
 
 
 
 
36f495e
 
 
e1837eb
 
 
 
 
 
 
5553918
e1837eb
 
 
 
 
 
 
 
 
 
 
 
 
36f495e
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
import pandas as pd
import re
import ast
import os
from huggingface_hub import hf_hub_download

# Configuration
REPO_ID = "theodoredc/MusicChordTransformer"
CSV_FILENAME = "chords_mapping.csv"

# Try to determine if we're running from a local repo or need to download
current_dir = os.path.dirname(os.path.abspath(__file__))
local_csv_path = os.path.join(current_dir, CSV_FILENAME)

if os.path.exists(local_csv_path):
    # Running locally (development)
    csv_path = local_csv_path
else:
    # Download from HuggingFace Hub
    csv_path = hf_hub_download(repo_id=REPO_ID, filename=CSV_FILENAME)


chord_relations = pd.read_csv(csv_path)

# Create a dictionary with keys the "chords" and values the "degrees"
chord_degrees = dict(zip(chord_relations['Chords'], chord_relations['Degrees']))
for key, value in chord_degrees.items():
    chord_degrees[key] = ast.literal_eval(value)

NOTE_TO_IDX = {
    'C': 0, 'Bs': 0, 'Cs': 1, 'Db': 1, 'D': 2, 'Ds': 3, 'Eb': 3, 'E': 4,
    'Fb': 4, 'Es': 5, 'F': 5, 'Fs': 6, 'Gb': 6, 'G': 7, 'Gs': 8, 'Ab': 8,
    'A': 9, 'As': 10, 'Bb': 10, 'B': 11, 'Cb': 11
}

def chord_to_array(chord_str):
    chord = chord_str.split('/')
    root_chord = chord[0]
    if root_chord not in chord_degrees:
        raise ValueError(f"Root chord '{root_chord}' not found in chord_degrees.")
    degree = list(chord_degrees[root_chord])
    if len(chord) > 1:
        bass_note = chord[1]
        if bass_note:
            if bass_note in NOTE_TO_IDX:
                degree[NOTE_TO_IDX[bass_note]] = 1
            else:
                print(f"Warning: Bass note '{bass_note}' not found in NOTE_TO_IDX for chord '{chord_str}'.")
    return degree

def process_chord_sequence(data):
    """Process full chord string into list of vectors"""
    clean = re.sub(r'<.*?>', '', data)
    chords = clean.strip().split()
    return [chord_to_array(ch) for ch in chords]