dvilasuero commited on
Commit
3befd84
·
verified ·
1 Parent(s): 61f9680

Upload runner.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. runner.py +113 -0
runner.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # /// script
3
+ # requires-python = ">=3.10"
4
+ # dependencies = [
5
+ # "inspect-ai",
6
+ # "datasets",
7
+ # "openai",
8
+ # "transformers",
9
+ # "accelerate",
10
+ # "huggingface_hub",
11
+ # "inspect_evals",
12
+ # ]
13
+ # ///
14
+ """Runner that downloads an eval script and executes it using inspect CLI."""
15
+ import os
16
+ import sys
17
+ import subprocess
18
+ import tempfile
19
+ import urllib.request
20
+ from pathlib import Path
21
+ from huggingface_hub import HfApi
22
+ from inspect_ai.log import bundle_log_dir
23
+
24
+
25
+ def bundle_and_upload_to_space(log_dir: str, hf_space_id: str, hf_token: str):
26
+ """Bundle logs and upload to HF Space."""
27
+ log_path = Path(log_dir)
28
+ if not log_path.exists():
29
+ print(f"Warning: Log directory '{log_dir}' does not exist")
30
+ return
31
+
32
+ with tempfile.TemporaryDirectory() as temp_bundle_dir:
33
+ bundle_output_dir = os.path.join(temp_bundle_dir, "bundle")
34
+
35
+ print(f"Bundling logs from {log_dir}...")
36
+ bundle_log_dir(log_dir=log_dir, output_dir=bundle_output_dir, overwrite=True)
37
+
38
+ api = HfApi(token=hf_token)
39
+
40
+ print(f"Uploading to Space {hf_space_id}...")
41
+ uploaded_count = 0
42
+ for root, _, files in os.walk(bundle_output_dir):
43
+ for file in files:
44
+ local_path = os.path.join(root, file)
45
+ rel_path = os.path.relpath(local_path, bundle_output_dir)
46
+ path_in_repo = rel_path.replace(os.sep, "/")
47
+
48
+ api.upload_file(
49
+ path_or_fileobj=local_path,
50
+ path_in_repo=path_in_repo,
51
+ repo_id=hf_space_id,
52
+ repo_type="space",
53
+ )
54
+ uploaded_count += 1
55
+
56
+ print(f"Uploaded {uploaded_count} files")
57
+ print(f"View at: https://huggingface.co/spaces/{hf_space_id}")
58
+
59
+
60
+ if __name__ == "__main__":
61
+ if len(sys.argv) < 4:
62
+ print("Usage: eval_runner.py <eval_ref> <model> <space_id> [log_dir] [--inspect-evals] [extra_args...]")
63
+ sys.exit(1)
64
+
65
+ eval_ref = sys.argv[1]
66
+ model = sys.argv[2]
67
+ hf_space_id = sys.argv[3]
68
+ log_dir = sys.argv[4] if len(sys.argv) > 4 and not sys.argv[4].startswith("--") else "./logs"
69
+
70
+ # Check if this is an inspect_evals path
71
+ is_inspect_evals = "--inspect-evals" in sys.argv
72
+ extra_args = [arg for arg in sys.argv[5:] if arg != "--inspect-evals"]
73
+
74
+ if is_inspect_evals:
75
+ # Use inspect_evals path directly
76
+ print(f"Using inspect_evals: {eval_ref}")
77
+ eval_target = eval_ref
78
+ cleanup_file = None
79
+ else:
80
+ # Download custom eval script
81
+ print(f"Downloading eval from {eval_ref}...")
82
+ with urllib.request.urlopen(eval_ref) as response:
83
+ eval_code = response.read().decode('utf-8')
84
+
85
+ eval_filename = "downloaded_eval.py"
86
+ with open(eval_filename, 'w') as f:
87
+ f.write(eval_code)
88
+
89
+ eval_target = eval_filename
90
+ cleanup_file = eval_filename
91
+
92
+ try:
93
+ print(f"Running inspect eval with model {model}...")
94
+ cmd = [
95
+ "inspect", "eval", eval_target,
96
+ "--model", model,
97
+ "--log-dir", log_dir,
98
+ ]
99
+ cmd.extend(extra_args)
100
+
101
+ print(f"Command: {' '.join(cmd)}")
102
+ subprocess.run(cmd, check=True)
103
+
104
+ print(f"\nUploading logs to {hf_space_id}...")
105
+ hf_token = os.getenv("HF_TOKEN")
106
+ if not hf_token:
107
+ print("Warning: HF_TOKEN not set, skipping upload")
108
+ else:
109
+ bundle_and_upload_to_space(log_dir, hf_space_id, hf_token)
110
+
111
+ finally:
112
+ if cleanup_file and os.path.exists(cleanup_file):
113
+ os.unlink(cleanup_file)