dvilasuero commited on
Commit
9fae527
·
verified ·
1 Parent(s): 316475b

Upload eval_runner_v2.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. eval_runner_v2.py +107 -0
eval_runner_v2.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # ]
12
+ # ///
13
+ """
14
+ Simple runner that downloads an eval script and runs it using inspect CLI.
15
+ """
16
+ import os
17
+ import sys
18
+ import subprocess
19
+ import tempfile
20
+ import urllib.request
21
+ from pathlib import Path
22
+ from huggingface_hub import HfApi
23
+ from inspect_ai.log import bundle_log_dir
24
+
25
+
26
+ def bundle_and_upload_to_space(log_dir: str, hf_space_id: str, hf_token: str):
27
+ """Bundle logs and upload to HF Space."""
28
+ log_path = Path(log_dir)
29
+ if not log_path.exists():
30
+ print(f"Warning: Log directory '{log_dir}' does not exist, skipping upload")
31
+ return
32
+
33
+ with tempfile.TemporaryDirectory() as temp_bundle_dir:
34
+ bundle_output_dir = os.path.join(temp_bundle_dir, "bundle")
35
+
36
+ print(f"Bundling logs from {log_dir}...")
37
+ bundle_log_dir(log_dir=log_dir, output_dir=bundle_output_dir, overwrite=True)
38
+
39
+ api = HfApi(token=hf_token)
40
+
41
+ # Upload all files
42
+ print(f"Uploading bundle to Space {hf_space_id}...")
43
+ uploaded_count = 0
44
+ for root, dirs, files in os.walk(bundle_output_dir):
45
+ for file in files:
46
+ local_path = os.path.join(root, file)
47
+ rel_path = os.path.relpath(local_path, bundle_output_dir)
48
+ path_in_repo = rel_path.replace(os.sep, "/")
49
+
50
+ api.upload_file(
51
+ path_or_fileobj=local_path,
52
+ path_in_repo=path_in_repo,
53
+ repo_id=hf_space_id,
54
+ repo_type="space",
55
+ )
56
+ uploaded_count += 1
57
+
58
+ print(f"Successfully uploaded {uploaded_count} files")
59
+ print(f"View at: https://huggingface.co/spaces/{hf_space_id}")
60
+
61
+
62
+ if __name__ == "__main__":
63
+ # Usage: eval_runner_v2.py <eval_script_url> <model> <hf_space_id> [log_dir] [extra_args...]
64
+ if len(sys.argv) < 4:
65
+ print("Usage: eval_runner_v2.py <eval_script_url> <model> <hf_space_id> [log_dir] [extra_args...]")
66
+ sys.exit(1)
67
+
68
+ eval_script_url = sys.argv[1]
69
+ model = sys.argv[2]
70
+ hf_space_id = sys.argv[3]
71
+ log_dir = sys.argv[4] if len(sys.argv) > 4 else "./logs"
72
+ extra_args = sys.argv[5:] if len(sys.argv) > 5 else []
73
+
74
+ # Download eval script
75
+ print(f"Downloading eval script from {eval_script_url}...")
76
+ with urllib.request.urlopen(eval_script_url) as response:
77
+ eval_code = response.read().decode('utf-8')
78
+
79
+ # Write eval code to a temporary file
80
+ with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
81
+ f.write(eval_code)
82
+ temp_eval_file = f.name
83
+
84
+ try:
85
+ # Run inspect eval with the downloaded script
86
+ print(f"Running inspect eval with model {model}...")
87
+ cmd = [
88
+ "inspect", "eval", temp_eval_file,
89
+ "--model", model,
90
+ "--log-dir", log_dir,
91
+ ]
92
+ cmd.extend(extra_args)
93
+
94
+ print(f"Command: {' '.join(cmd)}")
95
+ result = subprocess.run(cmd, check=True)
96
+
97
+ # Upload logs to space
98
+ print(f"\nUploading logs to {hf_space_id}...")
99
+ hf_token = os.getenv("HF_TOKEN")
100
+ if not hf_token:
101
+ print("Warning: HF_TOKEN not set, skipping log upload")
102
+ else:
103
+ bundle_and_upload_to_space(log_dir, hf_space_id, hf_token)
104
+
105
+ finally:
106
+ # Clean up temp file
107
+ os.unlink(temp_eval_file)