MedGRPO Team commited on
Commit
8ef4c38
Β·
1 Parent(s): 9bb9edd
Files changed (3) hide show
  1. app.py +41 -16
  2. test_hf_token.py +157 -0
  3. upload_initial_data.py +128 -0
app.py CHANGED
@@ -297,25 +297,50 @@ def save_leaderboard(df: pd.DataFrame):
297
  # Upload to private HuggingFace repo
298
  try:
299
  token = os.environ.get('HF_TOKEN')
300
- if token:
301
- print("⏳ Uploading leaderboard to private repository...")
 
 
 
302
 
303
- api = HfApi()
304
- api.upload_file(
305
- path_or_fileobj=str(LEADERBOARD_FILE),
306
- path_in_repo="leaderboard.json",
307
- repo_id="UIIAmerica/MedVidBench-GroundTruth",
308
- repo_type="dataset",
309
- token=token,
310
- commit_message=f"Update leaderboard: {len(df)} entries ({datetime.now().strftime('%Y-%m-%d %H:%M:%S')})"
311
- )
 
 
 
 
 
 
 
 
 
312
 
313
- print(f"βœ“ Uploaded leaderboard to private repo")
314
- else:
315
- print("⚠️ HF_TOKEN not found, skipping upload to private repo")
316
  except Exception as e:
317
- print(f"⚠️ Failed to upload leaderboard to private repo: {e}")
318
- print(" Leaderboard is still saved locally")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
319
 
320
 
321
  def backup_results_to_repo(model_name: str, results_dir: Path):
 
297
  # Upload to private HuggingFace repo
298
  try:
299
  token = os.environ.get('HF_TOKEN')
300
+ if not token:
301
+ print("⚠️ HF_TOKEN not found in environment")
302
+ print(" Set HF_TOKEN secret in Space settings to enable repo sync")
303
+ print(" Leaderboard saved locally only (will not persist across restarts)")
304
+ return
305
 
306
+ print("⏳ Uploading leaderboard to private repository...")
307
+ print(f" Target: UIIAmerica/MedVidBench-GroundTruth/leaderboard.json")
308
+ print(f" Entries: {len(df)}")
309
+
310
+ api = HfApi()
311
+
312
+ # Upload with detailed error handling
313
+ result = api.upload_file(
314
+ path_or_fileobj=str(LEADERBOARD_FILE),
315
+ path_in_repo="leaderboard.json",
316
+ repo_id="UIIAmerica/MedVidBench-GroundTruth",
317
+ repo_type="dataset",
318
+ token=token,
319
+ commit_message=f"Update leaderboard: {len(df)} entries ({datetime.now().strftime('%Y-%m-%d %H:%M:%S')})"
320
+ )
321
+
322
+ print(f"βœ“ Successfully uploaded leaderboard to private repo")
323
+ print(f" Commit URL: {result}")
324
 
 
 
 
325
  except Exception as e:
326
+ error_msg = str(e)
327
+ print(f"❌ Failed to upload leaderboard to private repo")
328
+ print(f" Error: {error_msg}")
329
+
330
+ # Provide specific guidance based on error type
331
+ if "401" in error_msg or "Unauthorized" in error_msg:
332
+ print(" β†’ Issue: Invalid or expired token")
333
+ print(" β†’ Fix: Regenerate HF_TOKEN with write permission")
334
+ elif "404" in error_msg or "Not Found" in error_msg:
335
+ print(" β†’ Issue: Repository not found")
336
+ print(" β†’ Fix: Create UIIAmerica/MedVidBench-GroundTruth repo")
337
+ elif "403" in error_msg or "Forbidden" in error_msg:
338
+ print(" β†’ Issue: Token lacks write permission")
339
+ print(" β†’ Fix: Use token with write access to dataset")
340
+ else:
341
+ print(f" β†’ Check HuggingFace status and repo permissions")
342
+
343
+ print(" ⚠️ Leaderboard saved locally only (will not persist)")
344
 
345
 
346
  def backup_results_to_repo(model_name: str, results_dir: Path):
test_hf_token.py ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Test HF_TOKEN configuration for repository access.
2
+
3
+ Run this script to verify that HF_TOKEN is properly configured
4
+ and has the necessary permissions for the private repo.
5
+ """
6
+
7
+ import os
8
+ import sys
9
+ from huggingface_hub import HfApi
10
+ from pathlib import Path
11
+
12
+ REPO_ID = "UIIAmerica/MedVidBench-GroundTruth"
13
+ REPO_TYPE = "dataset"
14
+
15
+ def test_hf_token():
16
+ """Test HF_TOKEN configuration."""
17
+ print("=" * 80)
18
+ print("TESTING HF_TOKEN CONFIGURATION")
19
+ print("=" * 80)
20
+
21
+ # Check if token exists
22
+ print("\n[1/4] Checking HF_TOKEN environment variable...")
23
+ token = os.environ.get('HF_TOKEN')
24
+
25
+ if not token:
26
+ print("❌ FAILED: HF_TOKEN not found in environment")
27
+ print("\nHow to fix:")
28
+ print("1. Generate token at: https://huggingface.co/settings/tokens")
29
+ print("2. Grant 'write' permission to repositories")
30
+ print("3. Set as environment variable:")
31
+ print(" export HF_TOKEN='your_token_here'")
32
+ print("\n4. For HuggingFace Spaces:")
33
+ print(" Settings β†’ Repository secrets β†’ Add HF_TOKEN")
34
+ sys.exit(1)
35
+
36
+ print(f"βœ“ HF_TOKEN found (length: {len(token)} chars)")
37
+ masked_token = token[:7] + "..." + token[-4:] if len(token) > 11 else "***"
38
+ print(f" Token: {masked_token}")
39
+
40
+ # Initialize API
41
+ print("\n[2/4] Initializing HuggingFace API...")
42
+ try:
43
+ api = HfApi()
44
+ print("βœ“ HfApi initialized")
45
+ except Exception as e:
46
+ print(f"❌ FAILED: {e}")
47
+ sys.exit(1)
48
+
49
+ # Test repository access (read)
50
+ print(f"\n[3/4] Testing READ access to {REPO_ID}...")
51
+ try:
52
+ repo_info = api.repo_info(
53
+ repo_id=REPO_ID,
54
+ repo_type=REPO_TYPE,
55
+ token=token
56
+ )
57
+ print(f"βœ“ Successfully accessed repository")
58
+ print(f" Repo: {repo_info.id}")
59
+ print(f" Private: {repo_info.private}")
60
+ print(f" Last modified: {repo_info.lastModified}")
61
+
62
+ # List files
63
+ files = api.list_repo_files(
64
+ repo_id=REPO_ID,
65
+ repo_type=REPO_TYPE,
66
+ token=token
67
+ )
68
+ print(f" Files in repo: {len(files)}")
69
+ for file in files:
70
+ print(f" - {file}")
71
+
72
+ except Exception as e:
73
+ error_msg = str(e)
74
+ print(f"❌ FAILED: {error_msg}")
75
+
76
+ if "401" in error_msg or "Unauthorized" in error_msg:
77
+ print("\n→ Issue: Invalid or expired token")
78
+ print("β†’ Fix: Regenerate token at https://huggingface.co/settings/tokens")
79
+ elif "404" in error_msg or "Not Found" in error_msg:
80
+ print(f"\n→ Issue: Repository '{REPO_ID}' not found")
81
+ print("β†’ Fix: Create the repository:")
82
+ print(f" 1. Go to: https://huggingface.co/new-dataset")
83
+ print(f" 2. Owner: UIIAmerica")
84
+ print(f" 3. Name: MedVidBench-GroundTruth")
85
+ print(f" 4. Visibility: Private")
86
+ elif "403" in error_msg or "Forbidden" in error_msg:
87
+ print("\n→ Issue: No access to private repository")
88
+ print("β†’ Fix: Ensure you're a member of UIIAmerica organization")
89
+
90
+ sys.exit(1)
91
+
92
+ # Test write access
93
+ print(f"\n[4/4] Testing WRITE access to {REPO_ID}...")
94
+ try:
95
+ # Create a test file
96
+ test_file = Path("test_upload.txt")
97
+ with open(test_file, 'w') as f:
98
+ f.write("Test upload to verify write permissions\n")
99
+
100
+ print(" Creating test file...")
101
+ result = api.upload_file(
102
+ path_or_fileobj=str(test_file),
103
+ path_in_repo="test_upload.txt",
104
+ repo_id=REPO_ID,
105
+ repo_type=REPO_TYPE,
106
+ token=token,
107
+ commit_message="Test write access"
108
+ )
109
+
110
+ print(f"βœ“ Successfully uploaded test file")
111
+ print(f" Commit: {result}")
112
+
113
+ # Clean up test file
114
+ print(" Cleaning up test file...")
115
+ api.delete_file(
116
+ path_in_repo="test_upload.txt",
117
+ repo_id=REPO_ID,
118
+ repo_type=REPO_TYPE,
119
+ token=token,
120
+ commit_message="Remove test file"
121
+ )
122
+ test_file.unlink()
123
+
124
+ print(f"βœ“ Successfully deleted test file")
125
+
126
+ except Exception as e:
127
+ error_msg = str(e)
128
+ print(f"❌ FAILED: {error_msg}")
129
+
130
+ if "403" in error_msg or "Forbidden" in error_msg:
131
+ print("\n→ Issue: Token does not have write permission")
132
+ print("β†’ Fix:")
133
+ print(" 1. Go to: https://huggingface.co/settings/tokens")
134
+ print(" 2. Create new token with WRITE permission")
135
+ print(" 3. Update HF_TOKEN environment variable")
136
+ elif "401" in error_msg:
137
+ print("\n→ Issue: Token invalid for write operations")
138
+ print("β†’ Fix: Ensure token has 'write' scope")
139
+
140
+ sys.exit(1)
141
+
142
+ # Success
143
+ print("\n" + "=" * 80)
144
+ print("βœ… ALL TESTS PASSED")
145
+ print("=" * 80)
146
+ print("\nYour HF_TOKEN is correctly configured with:")
147
+ print(" βœ“ Valid authentication")
148
+ print(" βœ“ Read access to private repository")
149
+ print(" βœ“ Write access to private repository")
150
+ print("\nYou can now:")
151
+ print(" 1. Deploy app.py to HuggingFace Spaces")
152
+ print(" 2. Add HF_TOKEN as a Space secret")
153
+ print(" 3. Leaderboard will automatically sync to private repo")
154
+
155
+
156
+ if __name__ == "__main__":
157
+ test_hf_token()
upload_initial_data.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Upload initial data to private HuggingFace repository.
2
+
3
+ This script uploads:
4
+ 1. ground_truth.json - Private test set
5
+ 2. leaderboard.json - Initial leaderboard with MedGRPO model
6
+
7
+ Run this once during initial setup.
8
+ """
9
+
10
+ import os
11
+ import sys
12
+ import json
13
+ from pathlib import Path
14
+ from huggingface_hub import HfApi
15
+
16
+ # Configuration
17
+ REPO_ID = "UIIAmerica/MedVidBench-GroundTruth"
18
+ REPO_TYPE = "dataset"
19
+
20
+ def create_initial_leaderboard():
21
+ """Create initial leaderboard.json with MedGRPO model data."""
22
+ leaderboard_data = [
23
+ {
24
+ "rank": 1,
25
+ "model_name": "Qwen2.5-VL-7B-MedGRPO",
26
+ "organization": "UII",
27
+ "cvs_acc": 0.914,
28
+ "nap_acc": 0.427,
29
+ "sa_acc": 0.244,
30
+ "stg_miou": 0.202,
31
+ "tag_miou_03": 0.216,
32
+ "tag_miou_05": 0.156,
33
+ "dvc_llm": 3.797,
34
+ "dvc_f1": 0.210,
35
+ "vs_llm": 4.184,
36
+ "rc_llm": 3.442,
37
+ "date": "2025-01-14",
38
+ "contact": "gaozhongpai@gmail.com"
39
+ }
40
+ ]
41
+
42
+ leaderboard_file = Path("leaderboard.json")
43
+ with open(leaderboard_file, 'w') as f:
44
+ json.dump(leaderboard_data, f, indent=2)
45
+
46
+ print(f"βœ“ Created leaderboard.json with 1 entry (Qwen2.5-VL-7B-MedGRPO)")
47
+ return leaderboard_file
48
+
49
+
50
+ def main():
51
+ """Upload initial files to private repo."""
52
+ # Check token
53
+ token = os.environ.get('HF_TOKEN')
54
+ if not token:
55
+ print("❌ HF_TOKEN environment variable not set")
56
+ print(" Please run: export HF_TOKEN='your_token_here'")
57
+ sys.exit(1)
58
+
59
+ print("=" * 80)
60
+ print(f"UPLOADING INITIAL DATA TO {REPO_ID}")
61
+ print("=" * 80)
62
+
63
+ api = HfApi()
64
+
65
+ # 1. Upload ground truth
66
+ print("\n[1/2] Uploading ground_truth.json...")
67
+ ground_truth_file = Path("data/ground_truth.json")
68
+
69
+ if not ground_truth_file.exists():
70
+ print(f" ❌ File not found: {ground_truth_file}")
71
+ print(f" Skipping ground_truth.json upload...")
72
+ else:
73
+ file_size = ground_truth_file.stat().st_size / (1024 * 1024) # MB
74
+ print(f" File size: {file_size:.2f} MB")
75
+
76
+ try:
77
+ api.upload_file(
78
+ path_or_fileobj=str(ground_truth_file),
79
+ path_in_repo="ground_truth.json",
80
+ repo_id=REPO_ID,
81
+ repo_type=REPO_TYPE,
82
+ token=token,
83
+ commit_message="Upload ground truth data"
84
+ )
85
+ print(f" βœ“ Uploaded ground_truth.json")
86
+ except Exception as e:
87
+ print(f" ❌ Failed: {e}")
88
+ sys.exit(1)
89
+
90
+ # 2. Upload leaderboard
91
+ print("\n[2/2] Uploading leaderboard.json...")
92
+
93
+ # Create leaderboard with MedGRPO data
94
+ leaderboard_file = create_initial_leaderboard()
95
+
96
+ file_size = leaderboard_file.stat().st_size
97
+ print(f" File size: {file_size} bytes")
98
+
99
+ try:
100
+ api.upload_file(
101
+ path_or_fileobj=str(leaderboard_file),
102
+ path_in_repo="leaderboard.json",
103
+ repo_id=REPO_ID,
104
+ repo_type=REPO_TYPE,
105
+ token=token,
106
+ commit_message="Initialize leaderboard with Qwen2.5-VL-7B-MedGRPO"
107
+ )
108
+ print(f" βœ“ Uploaded leaderboard.json")
109
+ except Exception as e:
110
+ print(f" ❌ Failed: {e}")
111
+ sys.exit(1)
112
+
113
+ print("\n" + "=" * 80)
114
+ print("βœ… UPLOAD COMPLETE")
115
+ print("=" * 80)
116
+ print(f"\nRepository: https://huggingface.co/datasets/{REPO_ID}")
117
+ print("\nUploaded:")
118
+ print(" βœ“ ground_truth.json (if available)")
119
+ print(" βœ“ leaderboard.json with Qwen2.5-VL-7B-MedGRPO")
120
+ print("\nNext steps:")
121
+ print("1. Verify files in repository")
122
+ print("2. Add HF_TOKEN secret to HuggingFace Space")
123
+ print("3. Deploy app.py to Space")
124
+ print("4. Check app logs for successful loading")
125
+
126
+
127
+ if __name__ == "__main__":
128
+ main()