Ideon commited on
Commit
a0c6e4e
·
verified ·
1 Parent(s): 7bbaa17

Upload 3 files

Browse files
Files changed (2) hide show
  1. model_adapters.py +37 -0
  2. requirements.txt +0 -1
model_adapters.py CHANGED
@@ -1,6 +1,9 @@
1
  import json
2
  import os
3
  import glob
 
 
 
4
  from typing import Any, Dict, List
5
 
6
 
@@ -117,6 +120,7 @@ class NanochatAdapter:
117
 
118
  import torch
119
  from huggingface_hub import snapshot_download
 
120
  from nanochat.gpt import GPT, GPTConfig
121
  from nanochat.tokenizer import RustBPETokenizer
122
 
@@ -227,6 +231,39 @@ def file_in_snapshot(root, filename):
227
  return path
228
 
229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
  def parse_json_env(name: str, default):
231
  raw = os.getenv(name)
232
  if not raw:
 
1
  import json
2
  import os
3
  import glob
4
+ import shutil
5
+ import subprocess
6
+ import sys
7
  from typing import Any, Dict, List
8
 
9
 
 
120
 
121
  import torch
122
  from huggingface_hub import snapshot_download
123
+ ensure_nanochat_runtime()
124
  from nanochat.gpt import GPT, GPTConfig
125
  from nanochat.tokenizer import RustBPETokenizer
126
 
 
231
  return path
232
 
233
 
234
+ def ensure_nanochat_runtime():
235
+ try:
236
+ import nanochat.gpt # noqa: F401
237
+ import nanochat.tokenizer # noqa: F401
238
+ return
239
+ except Exception:
240
+ pass
241
+
242
+ repo_url = os.getenv("RFAB_NANOCHAT_REPO", "https://github.com/karpathy/nanochat.git")
243
+ ref = os.getenv("RFAB_NANOCHAT_REF", "dc54a1a3077cab11d68fac4c5d1cd5c51f5d8c7a")
244
+ cache_dir = os.getenv("RFAB_NANOCHAT_CACHE_DIR", "/tmp/rfab_nanochat_runtime")
245
+
246
+ if not os.path.exists(os.path.join(cache_dir, "nanochat")):
247
+ if os.path.exists(cache_dir):
248
+ shutil.rmtree(cache_dir)
249
+ subprocess.check_call([
250
+ "git",
251
+ "clone",
252
+ "--depth",
253
+ "1",
254
+ repo_url,
255
+ cache_dir,
256
+ ])
257
+ subprocess.check_call(["git", "fetch", "--depth", "1", "origin", ref], cwd=cache_dir)
258
+ subprocess.check_call(["git", "checkout", ref], cwd=cache_dir)
259
+
260
+ if cache_dir not in sys.path:
261
+ sys.path.insert(0, cache_dir)
262
+
263
+ import nanochat.gpt # noqa: F401
264
+ import nanochat.tokenizer # noqa: F401
265
+
266
+
267
  def parse_json_env(name: str, default):
268
  raw = os.getenv(name)
269
  if not raw:
requirements.txt CHANGED
@@ -7,4 +7,3 @@ huggingface_hub
7
  spaces
8
  tiktoken
9
  rustbpe
10
- nanochat @ git+https://github.com/karpathy/nanochat.git
 
7
  spaces
8
  tiktoken
9
  rustbpe