Spaces:
Sleeping
Sleeping
Download theme_generation/parser_adapter.py from pachet/theme_generator: direct link, hf CLI and curl.
- Browser
- Download file 2.14 kB
-
https://huggingface.co/spaces/pachet/theme_generator/resolve/main/theme_generation/parser_adapter.py
- Command line
-
hf download hf://spaces/pachet/theme_generator/theme_generation/parser_adapter.py
-
curl -L -o parser_adapter.py https://huggingface.co/spaces/pachet/theme_generator/resolve/main/theme_generation/parser_adapter.py
2.14 kB
| """Optional adapter from theme-realization-v1 into the sibling melody parser.""" | |
| from pathlib import Path | |
| import os | |
| import sys | |
| from .common import PITCH_CLASS | |
| from .io import ensure_muses_import_path | |
| def analyze_realization(payload, source: Path | None = None): | |
| if payload.get("format") != "theme-realization-v1": | |
| raise ValueError("Expected theme-realization-v1 payload") | |
| source = source or Path(os.environ.get("INTENTION_COMPOSITION_SRC", Path(__file__).resolve().parents[2] / "intention_based_composition" / "src")) | |
| if not source.exists(): | |
| raise RuntimeError("Sibling intention parser source is unavailable") | |
| ensure_muses_import_path() | |
| if str(source) not in sys.path: | |
| sys.path.insert(0, str(source)) | |
| from muses.base.temporals import TemporalCollection | |
| from intention_composition.melody.corpus import MelodyNote, ThemeMelody, metric_strength | |
| from intention_composition.melody.parser import parse_theme | |
| collection = TemporalCollection(name="intention realization") | |
| events = [event for event in payload["events"] if event["pitch"] is not None] | |
| for event in events: | |
| collection.insert_note(event["pitch"], event["start_beat"], event["duration_beats"]) | |
| notes = tuple(MelodyNote(note, events[i]["rpc"], events[i]["duration"], metric_strength(note.start_beat, 4), i) | |
| for i, note in enumerate(collection.iter_notes())) | |
| theme = ThemeMelody("generated", "", "Intention realization", payload["key"], PITCH_CLASS[payload["key"]], "4/4", 4, | |
| collection, notes) | |
| analysis = parse_theme(theme).to_dict(include_notes=True) | |
| agreement = [{"name": span["name"], "start": span["start"], "end": span["end"], | |
| "exact_parser_match": any(match["name"] == span["name"] and match["start"] == span["start"] and match["end"] == span["end"] | |
| for match in analysis["matches"])} for span in payload["spans"]] | |
| return {"interpretation": "parser roundtrip diagnostic, not independent musical validation", "analysis": analysis, | |
| "planned_span_agreement": agreement} | |