yass4 commited on
Commit
9ddbe6c
·
verified ·
1 Parent(s): 3cf590d

Fetch published config.json from Hub on load (enables download tracking)

Browse files
Files changed (1) hide show
  1. halt_cot/transformers_backend.py +53 -2
halt_cot/transformers_backend.py CHANGED
@@ -2,8 +2,9 @@
2
 
3
  from __future__ import annotations
4
 
5
- from dataclasses import replace
6
- import math
 
7
  from typing import Sequence
8
 
9
  from .core import (
@@ -19,6 +20,53 @@ from .core import (
19
  )
20
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  class TextStopCriteria:
23
  """Transformers stopping criterion that halts when generated text hits a marker."""
24
 
@@ -74,6 +122,9 @@ class HaltCoTForCausalLM:
74
 
75
  from transformers import AutoModelForCausalLM, AutoTokenizer
76
 
 
 
 
77
  tokenizer = AutoTokenizer.from_pretrained(
78
  model_id,
79
  trust_remote_code=trust_remote_code,
 
2
 
3
  from __future__ import annotations
4
 
5
+ from dataclasses import fields, replace
6
+ import json
7
+ import os
8
  from typing import Sequence
9
 
10
  from .core import (
 
20
  )
21
 
22
 
23
+ HALT_COT_CONFIG_REPO = os.getenv("HALT_COT_CONFIG_REPO", "yass4/halt-cot")
24
+
25
+
26
+ def load_config_from_hub(
27
+ repo_id: str = HALT_COT_CONFIG_REPO,
28
+ *,
29
+ filename: str = "config.json",
30
+ ) -> HaltCoTConfig | None:
31
+ """Fetch the published HALT-CoT defaults from the Hub.
32
+
33
+ Downloading ``config.json`` through the Hub also lets Hugging Face count
34
+ real usage of the method. Any failure (offline, missing repo, malformed
35
+ file) returns ``None`` so callers fall back to the built-in defaults and a
36
+ run is never blocked.
37
+ """
38
+
39
+ try:
40
+ from huggingface_hub import hf_hub_download
41
+
42
+ path = hf_hub_download(repo_id=repo_id, filename=filename)
43
+ payload = json.loads(open(path, encoding="utf-8").read())
44
+ except Exception:
45
+ return None
46
+
47
+ values = payload.get("halt_cot", payload)
48
+ if not isinstance(values, dict):
49
+ return None
50
+
51
+ defaults = HaltCoTConfig()
52
+ tuple_fields = {
53
+ f.name for f in fields(HaltCoTConfig)
54
+ if isinstance(getattr(defaults, f.name), tuple)
55
+ }
56
+ kwargs = {}
57
+ for field in fields(HaltCoTConfig):
58
+ if field.name not in values:
59
+ continue
60
+ value = values[field.name]
61
+ if field.name in tuple_fields and isinstance(value, list):
62
+ value = tuple(value)
63
+ kwargs[field.name] = value
64
+ try:
65
+ return HaltCoTConfig(**kwargs)
66
+ except (TypeError, ValueError):
67
+ return None
68
+
69
+
70
  class TextStopCriteria:
71
  """Transformers stopping criterion that halts when generated text hits a marker."""
72
 
 
122
 
123
  from transformers import AutoModelForCausalLM, AutoTokenizer
124
 
125
+ if config is None:
126
+ config = load_config_from_hub()
127
+
128
  tokenizer = AutoTokenizer.from_pretrained(
129
  model_id,
130
  trust_remote_code=trust_remote_code,