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]