"""LilyScript — a symbolic-music AIGC app built on the LilyletNotaGen model. Left column: (1) generation parameter panel (2) streaming run log (3) .lyl file list (session outputs + built-in examples) (4) editable lyl editor Right column: sheet-music panel (placeholder for now; later a lyl->MEI->Verovio SVG renderer reusing the lilylet-live-editor pipeline). Generation streams patch-by-patch: raw decoded text (with `[r:x/y]` stream markers) goes to the run log, while the measure-segmented postprocessed text fills the editor. The backend is the int8 + two-level KV-cache ONNX generator (see lilyscript/generator.py); models load from a local dir for now. """ import os import time import gradio as gr from lilyscript.generator import StreamingLilyletGenerator HERE = os.path.dirname(os.path.abspath(__file__)) # TODO: swap for huggingface_hub.snapshot_download(repo_id=...) to pull the int8 # ONNX weights from the hub instead of a local dir. MODEL_DIR = os.environ.get('LILYSCRIPT_MODEL_DIR', os.path.join(HERE, 'models')) ASSET_DIR = os.path.join(HERE, 'assets') EXAMPLES_DIR = os.path.join(HERE, 'examples') OUTPUT_DIR = os.path.join(HERE, 'outputs') EXAMPLE_PREFIX = '\U0001F4C4 ' # 📄 examples OUTPUT_PREFIX = '✨ ' # ✨ session outputs _GEN = None def get_generator (): '''Lazily build the (heavy) ONNX generator on first use.''' global _GEN if _GEN is None: _GEN = StreamingLilyletGenerator(MODEL_DIR, ASSET_DIR) return _GEN def load_examples (): '''Read built-in example .lyl files into a {label: text} dict.''' store = {} if os.path.isdir(EXAMPLES_DIR): for name in sorted(os.listdir(EXAMPLES_DIR)): if name.endswith('.lyl'): with open(os.path.join(EXAMPLES_DIR, name), encoding='utf-8') as f: store[EXAMPLE_PREFIX + name] = f.read() return store def run_generation (prompt, measures, temperature, top_k, top_p, max_patches, seed, store): '''Streaming generate callback. Yields updates for (log, editor, file_list, store). store: {label: lyl_text} dict held in gr.State; the produced document is added to it under a timestamped label once generation finishes. ''' gen = get_generator() meas = int(measures) if measures and int(measures) > 0 else None store = dict(store or {}) raw = pretty = '' for raw, pretty, done in gen.generate_stream( prompt_text=prompt or '', max_patches=int(max_patches), temperature=float(temperature), top_k=int(top_k), top_p=float(top_p), measures=meas, seed=int(seed)): # stream: update log + editor, keep file list / store unchanged yield raw, pretty, gr.update(), store # finished: persist the document, refresh the file list, select the new entry label = OUTPUT_PREFIX + time.strftime('%H%M%S') + ('_m%d' % meas if meas else '') + '_s%d' % int(seed) store[label] = pretty os.makedirs(OUTPUT_DIR, exist_ok=True) with open(os.path.join(OUTPUT_DIR, label.replace(OUTPUT_PREFIX, '') + '.lyl'), 'w', encoding='utf-8') as f: f.write(pretty) yield raw, pretty, gr.update(choices=list(store.keys()), value=label), store def load_file (label, store): '''File-list selection -> load that document into the editor.''' return (store or {}).get(label, '') SHEET_PLACEHOLDER = '''