forked from MakazhanAlpamys/Soup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
840 lines (735 loc) · 25.9 KB
/
Copy pathdeploy.py
File metadata and controls
840 lines (735 loc) · 25.9 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
"""soup deploy — deploy models to inference runtimes (Ollama, HF Spaces)."""
from __future__ import annotations
import os
from pathlib import Path
from typing import TYPE_CHECKING, List, Optional
import typer
if TYPE_CHECKING:
from soup_cli.utils.deploy_autopilot import DeployProfile
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
console = Console()
app = typer.Typer(no_args_is_help=True)
# ---------------------------------------------------------------------------
# HF Space templates (Part F of v0.29.0)
# ---------------------------------------------------------------------------
_GRADIO_APP_PY = '''"""Soup CLI-generated Gradio Chat Space."""
import os
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
MODEL_ID = "{MODEL_REPO}"
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, device_map="auto")
def respond(message, history):
messages = []
for user, assistant in history:
messages.append({{"role": "user", "content": user}})
messages.append({{"role": "assistant", "content": assistant}})
messages.append({{"role": "user", "content": message}})
prompt = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True,
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs, max_new_tokens=512, do_sample=True,
temperature=0.7, top_p=0.9,
)
reply = tokenizer.decode(
outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True,
)
return reply
chat = gr.ChatInterface(respond, title="Soup CLI Fine-tuned Chat")
chat.launch()
'''
_GRADIO_REQS = """gradio>=4.0.0
transformers>=4.40.0
torch>=2.1.0
accelerate>=0.27.0
"""
_GRADIO_README = """---
title: Soup Chat
emoji: 🍲
colorFrom: purple
colorTo: cyan
sdk: gradio
sdk_version: 4.0.0
app_file: app.py
pinned: false
---
# Soup Chat
Generated with [Soup CLI](https://github.com/MakazhanAlpamys/Soup) — model: `{MODEL_REPO}`
"""
_STREAMLIT_APP_PY = '''"""Soup CLI-generated Streamlit Chat Space."""
import streamlit as st
from transformers import AutoModelForCausalLM, AutoTokenizer
MODEL_ID = "{MODEL_REPO}"
st.set_page_config(page_title="Soup Chat", page_icon="🍲")
st.title("Soup Chat")
@st.cache_resource
def load_model():
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, device_map="auto")
return tokenizer, model
tokenizer, model = load_model()
if "messages" not in st.session_state:
st.session_state["messages"] = []
for msg in st.session_state["messages"]:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
prompt = st.chat_input("Ask me anything")
if prompt:
st.session_state["messages"].append({{"role": "user", "content": prompt}})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
messages = [
{{"role": m["role"], "content": m["content"]}}
for m in st.session_state["messages"]
]
chat_prompt = tokenizer.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True,
)
inputs = tokenizer(chat_prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
**inputs, max_new_tokens=512, do_sample=True,
temperature=0.7, top_p=0.9,
)
reply = tokenizer.decode(
outputs[0][inputs["input_ids"].shape[-1]:], skip_special_tokens=True,
)
st.markdown(reply)
st.session_state["messages"].append(
{{"role": "assistant", "content": reply}}
)
'''
_STREAMLIT_REQS = """streamlit>=1.30.0
transformers>=4.40.0
torch>=2.1.0
accelerate>=0.27.0
"""
_STREAMLIT_README = """---
title: Soup Chat
emoji: 🍲
colorFrom: purple
colorTo: cyan
sdk: streamlit
sdk_version: 1.30.0
app_file: app.py
pinned: false
---
# Soup Chat
Generated with [Soup CLI](https://github.com/MakazhanAlpamys/Soup) — model: `{MODEL_REPO}`
"""
HF_SPACE_TEMPLATES = {
"gradio-chat": {
"sdk": "gradio",
"app.py": _GRADIO_APP_PY,
"requirements.txt": _GRADIO_REQS,
"README.md": _GRADIO_README,
},
"streamlit-chat": {
"sdk": "streamlit",
"app.py": _STREAMLIT_APP_PY,
"requirements.txt": _STREAMLIT_REQS,
"README.md": _STREAMLIT_README,
},
}
def render_space_template(template: str, model_repo: str) -> dict[str, str]:
"""Render the Space template files with ``model_repo`` substituted.
Raises ``ValueError`` when the template is unknown or the model repo id
fails validation — injection-proof since repo ids are already a
restrictive alphanumeric subset after validation.
"""
from soup_cli.utils.hf import validate_repo_id
validate_repo_id(model_repo)
if template not in HF_SPACE_TEMPLATES:
raise ValueError(
f"Unknown template: {template!r}. "
f"Available: {', '.join(HF_SPACE_TEMPLATES.keys())}"
)
spec = HF_SPACE_TEMPLATES[template]
return {
"app.py": spec["app.py"].replace("{MODEL_REPO}", model_repo),
"requirements.txt": spec["requirements.txt"],
"README.md": spec["README.md"].replace("{MODEL_REPO}", model_repo),
}
@app.command()
def ollama(
model: Optional[str] = typer.Option(
None,
"--model",
"-m",
help="Path to GGUF model file",
),
name: Optional[str] = typer.Option(
None,
"--name",
"-n",
help="Ollama model name (e.g. soup-my-model)",
),
system: Optional[str] = typer.Option(
None,
"--system",
"-s",
help="System prompt for the model",
),
template: str = typer.Option(
"auto",
"--template",
"-t",
help="Chat template: auto, chatml, llama, mistral, vicuna, zephyr",
),
parameter: Optional[List[str]] = typer.Option(
None,
"--parameter",
"-p",
help="Ollama parameter (repeatable): temperature=0.7, top_p=0.9, etc.",
),
list_models: bool = typer.Option(
False,
"--list",
"-l",
help="List Soup-deployed models in Ollama",
),
remove: Optional[str] = typer.Option(
None,
"--remove",
"-r",
help="Remove a model from Ollama by name",
),
yes: bool = typer.Option(
False,
"--yes",
"-y",
help="Skip confirmation prompt",
),
):
"""Deploy a GGUF model to local Ollama instance."""
from soup_cli.utils.ollama import (
OLLAMA_TEMPLATES,
create_modelfile,
deploy_to_ollama,
detect_ollama,
list_soup_models,
remove_model,
validate_gguf_path,
validate_model_name,
)
# --- List mode ---
if list_models:
version = detect_ollama()
if not version:
console.print("[red]Ollama not found.[/] Install from https://ollama.com")
raise typer.Exit(1)
models = list_soup_models()
if not models:
console.print("[yellow]No Soup-deployed models found in Ollama.[/]")
console.print("[dim]Deploy a model with: soup deploy ollama --model <gguf>[/]")
raise typer.Exit(0)
table = Table(title="Soup Models in Ollama")
table.add_column("Name", style="bold cyan")
table.add_column("Size", style="green")
for entry in models:
table.add_row(entry["name"], entry["size"])
console.print(table)
raise typer.Exit(0)
# --- Remove mode ---
if remove:
valid_name, name_err = validate_model_name(remove)
if not valid_name:
console.print(f"[red]Invalid model name:[/] {name_err}")
raise typer.Exit(1)
version = detect_ollama()
if not version:
console.print("[red]Ollama not found.[/] Install from https://ollama.com")
raise typer.Exit(1)
if not yes:
confirm = typer.confirm(f"Remove model '{remove}' from Ollama?")
if not confirm:
raise typer.Exit(0)
success, message = remove_model(remove)
if success:
console.print(f"[green]{message}[/]")
else:
console.print(f"[red]{message}[/]")
raise typer.Exit(1)
raise typer.Exit(0)
# --- Deploy mode: require --model and --name ---
if not model:
console.print("[red]--model is required for deploy.[/]")
console.print("[dim]Usage: soup deploy ollama --model <gguf> --name <name>[/]")
raise typer.Exit(1)
if not name:
console.print("[red]--name is required for deploy.[/]")
console.print("[dim]Usage: soup deploy ollama --model <gguf> --name <name>[/]")
raise typer.Exit(1)
# Validate model name
valid_name, name_err = validate_model_name(name)
if not valid_name:
console.print(f"[red]Invalid model name:[/] {name_err}")
raise typer.Exit(1)
# Validate GGUF path
gguf_path = Path(model)
valid_path, path_err = validate_gguf_path(gguf_path)
if not valid_path:
console.print(f"[red]{path_err}[/]")
raise typer.Exit(1)
# Check Ollama is installed
version = detect_ollama()
if not version:
console.print(
"[red]Ollama not found.[/]\n"
"Install from: [bold]https://ollama.com[/]"
)
raise typer.Exit(1)
# Resolve template
resolved_template = None
if template == "auto":
# Try to infer from soup.yaml in cwd
resolved_template = _auto_detect_template()
if not resolved_template:
resolved_template = "chatml" # Default fallback
elif template in OLLAMA_TEMPLATES:
resolved_template = template
else:
console.print(
f"[red]Unknown template: {template}[/]\n"
f"Available: auto, {', '.join(OLLAMA_TEMPLATES.keys())}"
)
raise typer.Exit(1)
# Parse parameters
params = {}
if parameter:
for param_str in parameter:
if "=" not in param_str:
console.print(f"[red]Invalid parameter format: {param_str}[/]")
console.print("[dim]Expected format: key=value (e.g. temperature=0.7)[/]")
raise typer.Exit(1)
key, value = param_str.split("=", 1)
params[key.strip()] = value.strip()
# Show deploy plan
console.print(
Panel(
f"Model: [bold]{name}[/]\n"
f"GGUF: [bold]{gguf_path}[/]\n"
f"Template: [bold]{resolved_template}[/]"
+ (f"\nSystem: [bold]{system}[/]" if system else "")
+ (f"\nParams: [bold]{params}[/]" if params else ""),
title="Deploy to Ollama",
)
)
# Confirmation — warn that this overwrites an existing model
if not yes:
console.print(
"[yellow]Warning:[/] This will overwrite any existing Ollama model "
f"named '{name}'."
)
confirm = typer.confirm("Proceed?")
if not confirm:
raise typer.Exit(0)
# Generate Modelfile
console.print(f"[green]\u2713[/] Ollama v{version} detected")
try:
modelfile = create_modelfile(
gguf_path=gguf_path,
template=resolved_template,
system_prompt=system,
parameters=params,
)
except ValueError as exc:
console.print(f"[red]Invalid parameter:[/] {exc}")
raise typer.Exit(1)
console.print("[green]\u2713[/] Modelfile generated")
# Deploy
console.print("[dim]Creating model in Ollama...[/]")
success, message = deploy_to_ollama(name, modelfile)
if not success:
console.print(f"[red]Deploy failed:[/] {message}")
raise typer.Exit(1)
console.print(f"[green]\u2713[/] Model created: [bold]{name}[/]")
console.print(
Panel(
f"Run: [bold]ollama run {name}[/]",
title="[bold green]Deploy Complete![/]",
)
)
@app.command(name="hf-space")
def hf_space(
model: str = typer.Option(
...,
"--model",
"-m",
help="HuggingFace model repo id to wrap in the Space (e.g. user/my-model)",
),
space: str = typer.Option(
...,
"--space",
"-s",
help="HuggingFace Space repo id to create (e.g. user/my-space)",
),
template: str = typer.Option(
"gradio-chat",
"--template",
"-t",
help=f"Space template: {', '.join(HF_SPACE_TEMPLATES.keys())}",
),
template_dir: Optional[str] = typer.Option(
None,
"--template-dir",
help=(
"Custom template directory (overrides --template). "
"Must contain app.py + README.md, optionally requirements.txt. "
"Use {MODEL_REPO} placeholder for substitution."
),
),
private: bool = typer.Option(
False, "--private", help="Create the Space as private",
),
yes: bool = typer.Option(
False, "--yes", "-y", help="Skip confirmation",
),
):
"""Create a HuggingFace Space wrapping a fine-tuned model.
Uploads an app.py, requirements.txt, and README.md rendered from the
chosen template. Supports gradio-chat and streamlit-chat.
"""
from soup_cli.utils.hf import (
get_hf_api,
resolve_endpoint,
resolve_token,
validate_repo_id,
)
# --- Validate space repo id up-front; model is validated by
# render_space_template which is the authoritative entry point. ---
try:
validate_repo_id(space)
except ValueError as exc:
console.print(f"[red]Invalid --space repo id:[/] {exc}")
raise typer.Exit(1) from exc
if template_dir is None and template not in HF_SPACE_TEMPLATES:
console.print(
f"[red]Unknown template: {template}[/]\n"
f"Available: {', '.join(HF_SPACE_TEMPLATES.keys())}"
)
raise typer.Exit(1)
# --- Resolve credentials ---
token = resolve_token()
if token is None:
console.print(
"[red]No HuggingFace token found.[/]\n"
"Set HF_TOKEN env var or run: [bold]huggingface-cli login[/]"
)
raise typer.Exit(1)
try:
endpoint = resolve_endpoint()
except ValueError as exc:
console.print(f"[red]HF_ENDPOINT invalid:[/] {exc}")
raise typer.Exit(1) from exc
# --- Render template files ---
try:
if template_dir is not None:
from soup_cli.utils.hf_space import (
detect_space_sdk,
render_custom_template_dir,
)
files = render_custom_template_dir(template_dir, model_repo=model)
# v0.53.8 #69 — auto-pick space_sdk from requirements.txt
# (closes the v0.40.2 known limitation that custom templates
# always created the Space with space_sdk="gradio").
sdk = detect_space_sdk(files.get("requirements.txt"))
else:
files = render_space_template(template, model_repo=model)
sdk = HF_SPACE_TEMPLATES[template]["sdk"]
except ValueError as exc:
console.print(f"[red]Template render failed:[/] {exc}")
raise typer.Exit(1) from exc
except FileNotFoundError as exc:
console.print(f"[red]Template directory error:[/] {exc}")
raise typer.Exit(1) from exc
template_label = template_dir if template_dir is not None else template
console.print(
Panel(
f"Space: [bold]{space}[/]\n"
f"Model: [bold]{model}[/]\n"
f"Template: [bold]{template_label}[/]\n"
f"Private: [bold]{private}[/]",
title="Deploy HuggingFace Space",
)
)
if not yes:
confirm = typer.confirm("Create Space and upload files?", default=True)
if not confirm:
raise typer.Exit(0)
# --- Create repo + upload ---
try:
api = get_hf_api(token=token, endpoint=endpoint)
except ImportError as exc:
console.print(f"[red]{exc}[/]")
raise typer.Exit(1) from exc
try:
api.create_repo(
repo_id=space, repo_type="space",
space_sdk=sdk,
private=private, exist_ok=True,
)
for in_repo_name, content in files.items():
api.upload_file(
path_or_fileobj=content.encode("utf-8"),
path_in_repo=in_repo_name,
repo_id=space,
repo_type="space",
commit_message=f"Soup CLI: add {in_repo_name}",
)
except Exception as exc:
console.print(f"[red]Space deploy failed:[/] {exc}")
raise typer.Exit(1) from exc
console.print(
Panel(
f"Space: [bold blue]https://huggingface.co/spaces/{space}[/]\n"
f"Model: [bold]{model}[/]",
title="[bold green]Space Deployed![/]",
)
)
@app.command(name="autopilot")
def autopilot(
target: Optional[str] = typer.Option(
None,
"--target",
"-t",
help="Profile name (e.g. mac-m3, rtx-4090-24gb, ollama-local).",
),
base: str = typer.Option(
"meta-llama/Llama-3.2-1B",
"--base",
"-b",
help="Base model HF repo id or local path to embed in the recipe.",
),
recipe_out: str = typer.Option(
"deploy_autopilot.yaml",
"--recipe-out",
help="Where to write the rendered soup.yaml recipe (under cwd).",
),
script_out: str = typer.Option(
"deploy_autopilot.sh",
"--script-out",
help="Where to write the planned deploy shell script (under cwd).",
),
output_dir: str = typer.Option(
"./output",
"--output-dir",
help="Recipe's training output directory.",
),
list_targets: bool = typer.Option(
False,
"--list",
"-l",
help="List all known deploy profiles.",
),
measure: bool = typer.Option(
False,
"--measure",
help=(
"Run Quant-Lobotomy measurement on candidate quants. "
"Requires --tasks. Results cached at "
"~/.soup/deploy_autopilot_cache.json (v0.53.1 #109)."
),
),
tasks_file: Optional[str] = typer.Option(
None,
"--tasks",
help="JSONL eval tasks for --measure (one prompt + expected per line).",
),
measure_candidates: Optional[str] = typer.Option(
None,
"--measure-candidates",
help=(
"Comma-separated quant candidates to measure (default: profile's "
"primary quant). Example: 4bit,gptq,awq"
),
),
):
"""Pick PEFT + quant + spec-decoding combo for a hardware target.
Writes a ready-to-train ``soup.yaml`` recipe and a planned deploy
shell script. Pass ``--measure --tasks <jsonl>`` to also run the
Quant-Lobotomy measurement loop across candidate quants (v0.53.1 #109).
"""
from soup_cli.utils.deploy_autopilot import (
get_profile,
list_profiles,
write_deploy_script,
write_recipe,
)
if list_targets:
table = Table(title="Deploy Autopilot Profiles")
table.add_column("Name", style="bold cyan")
table.add_column("Runtime", style="magenta")
table.add_column("Quant", style="green")
table.add_column("PEFT", style="yellow")
table.add_column("Description")
for profile in list_profiles().values():
table.add_row(
profile.name,
profile.runtime,
profile.quant,
profile.peft,
profile.description,
)
console.print(table)
raise typer.Exit(0)
if not target:
console.print("[red]--target is required (or use --list).[/]")
raise typer.Exit(2)
try:
profile = get_profile(target)
except (KeyError, ValueError, TypeError) as exc:
from rich.markup import escape
console.print(f"[red]Unknown profile:[/] {escape(str(exc))}")
console.print(
"[dim]Run [bold]soup deploy autopilot --list[/] to see options.[/]"
)
raise typer.Exit(2) from exc
try:
recipe_path = write_recipe(
profile, base=base, output_dir=output_dir, recipe_path=recipe_out
)
script_path = write_deploy_script(
profile, model_path=output_dir, script_path=script_out
)
except (ValueError, TypeError) as exc:
from rich.markup import escape
console.print(f"[red]Autopilot failed:[/] {escape(str(exc))}")
raise typer.Exit(1) from exc
from rich.markup import escape as _escape
console.print(
Panel(
f"Target: [bold]{_escape(profile.name)}[/]\n"
f"Runtime: [bold]{_escape(profile.runtime)}[/]\n"
f"Quant: [bold]{_escape(profile.quant)}[/]\n"
f"PEFT: [bold]{_escape(profile.peft)}[/]\n"
f"Spec dec: [bold]{profile.spec_decoding}[/]\n"
f"Recipe: [bold]{_escape(recipe_path)}[/]\n"
f"Script: [bold]{_escape(script_path)}[/]",
title="[bold green]Deploy Autopilot[/]",
)
)
if profile.notes:
console.print(f"[dim]Notes: {_escape(profile.notes)}[/]")
# v0.53.1 #109 — optional live measurement
if measure:
if not tasks_file:
console.print(
"[red]--measure requires --tasks <jsonl>.[/]"
)
raise typer.Exit(2)
_run_deploy_autopilot_measure(
profile=profile,
base=base,
tasks_file=tasks_file,
measure_candidates=measure_candidates,
)
def _auto_detect_template() -> Optional[str]:
"""Try to infer chat template from soup.yaml in cwd."""
from soup_cli.utils.ollama import infer_chat_template
config_path = Path("soup.yaml")
if not config_path.exists():
return None
try:
import yaml
with open(config_path, encoding="utf-8") as fh:
config = yaml.safe_load(fh)
if not isinstance(config, dict):
return None
data_section = config.get("data", {})
if isinstance(data_section, dict):
fmt = data_section.get("format")
return infer_chat_template(fmt)
except (yaml.YAMLError, OSError, KeyError, ImportError):
return None
return None
# --- v0.53.1 #109 — deploy autopilot live measurement -----------------------
def _run_deploy_autopilot_measure(
*,
profile: "DeployProfile",
base: str,
tasks_file: str,
measure_candidates: Optional[str],
) -> None:
"""Lazy-import + invoke the measurement helper from utils.deploy_measure."""
import hashlib
from rich.markup import escape
from soup_cli.utils.deploy_measure import (
pick_best,
render_measure_table,
run_measure,
)
from soup_cli.utils.paths import is_under_cwd
if not is_under_cwd(tasks_file):
console.print(
f"[red]--tasks {escape(tasks_file)!r} must stay under cwd[/]"
)
raise typer.Exit(2)
if not Path(tasks_file).is_file():
console.print(f"[red]Tasks file not found: {escape(tasks_file)}[/]")
raise typer.Exit(2)
# Determine candidate list
if measure_candidates:
candidates = [
c.strip() for c in measure_candidates.split(",") if c.strip()
]
if not candidates:
console.print("[red]--measure-candidates parsed to empty list.[/]")
raise typer.Exit(2)
else:
# Default: just the profile's primary quant
candidates = [profile.quant]
# Build a base sha — local path → realpath; HF repo → name
base_sha_seed = base if not os.path.isdir(base) else os.path.realpath(base)
base_sha = hashlib.sha256(base_sha_seed.encode("utf-8")).hexdigest()[:16]
console.print(
f"[dim]Measuring {len(candidates)} candidate(s) "
f"against {Path(tasks_file).name}...[/]"
)
# Injected generators (test seam / advanced operator workflows) still win;
# otherwise v0.71.22 #143 first-party transformers factories load the real
# base (before) + per-candidate Quant-Menu-quantized model (after), lazily
# on first prompt so a cache hit never loads a model.
from soup_cli.utils import deploy_measure as _dm
injected_before = getattr(_dm, "_DEPLOY_MEASURE_BEFORE_GEN", None)
injected_after = getattr(_dm, "_DEPLOY_MEASURE_AFTER_FACTORY", None)
if injected_before is None and injected_after is None:
console.print(
"[dim]Using live transformers generators (greedy decode; models "
"load lazily per candidate)...[/]"
)
before_gen = injected_before or _dm.build_before_generator(base)
after_factory = injected_after or _dm.build_after_generator_factory(base)
try:
results, cache_hit = run_measure(
profile_name=profile.name,
base_sha=base_sha,
candidates=candidates,
tasks_file=tasks_file,
before_gen=before_gen,
after_gen_factory=after_factory,
)
except (TypeError, ValueError, FileNotFoundError) as exc:
console.print(f"[red]Measure failed:[/] {escape(str(exc))}")
raise typer.Exit(1) from exc
except (RuntimeError, ImportError, OSError) as exc:
# Model-load failures from the live factories (missing quant kernel,
# OOM, network) — friendly exit, no traceback dump.
console.print(f"[red]Live measure failed:[/] {escape(str(exc))}")
raise typer.Exit(1) from exc
console.print(render_measure_table(results))
if cache_hit:
console.print(
"[dim](cache hit — delete ~/.soup/deploy_autopilot_cache.json or "
"change --tasks to refresh)[/]"
)
best = pick_best(results)
if best is not None:
console.print(
f"[bold green]Recommended:[/] {escape(best.candidate)} "
f"(verdict={best.verdict}, delta={best.delta:+.3f})"
)