File size: 15,511 Bytes
ebe598e
 
 
 
 
 
 
 
93ed7a1
ebe598e
2da5c04
c417358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebe598e
 
 
 
 
 
 
 
 
 
 
 
c417358
 
 
2da5c04
 
c417358
 
2da5c04
 
 
ebe598e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2da5c04
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebe598e
 
 
93ed7a1
 
 
 
 
 
 
 
 
 
 
 
2da5c04
 
ebe598e
 
 
fe5f524
ebe598e
 
fe5f524
 
 
2da5c04
fe5f524
 
 
 
 
 
 
2da5c04
fe5f524
 
 
ebe598e
 
93ed7a1
 
2da5c04
 
ebe598e
 
 
 
 
 
 
 
 
2da5c04
 
 
 
ebe598e
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/env python3
"""
Setup script for Hugging Face Dataset repository for Trackio experiments
"""

import os
import json
from datetime import datetime
from pathlib import Path
from datasets import Dataset
from huggingface_hub import HfApi, create_repo
import subprocess

def get_username_from_token(token: str) -> str:
    """Get username from HF token with fallback to CLI"""
    try:
        # Try API first
        api = HfApi(token=token)
        user_info = api.whoami()
        
        # Handle different possible response formats
        if isinstance(user_info, dict):
            # Try different possible keys for username
            username = (
                user_info.get('name') or 
                user_info.get('username') or 
                user_info.get('user') or 
                None
            )
        elif isinstance(user_info, str):
            # If whoami returns just the username as string
            username = user_info
        else:
            username = None
            
        if username:
            print(f"βœ… Got username from API: {username}")
            return username
        else:
            print("⚠️  Could not get username from API, trying CLI...")
            return get_username_from_cli(token)
            
    except Exception as e:
        print(f"⚠️  API whoami failed: {e}")
        print("⚠️  Trying CLI fallback...")
        return get_username_from_cli(token)

def get_username_from_cli(token: str) -> str:
    """Fallback method to get username using CLI"""
    try:
        # Set HF token for CLI
        os.environ['HF_TOKEN'] = token
        
        # Get username using CLI
        result = subprocess.run(
            ["huggingface-cli", "whoami"],
            capture_output=True,
            text=True,
            timeout=30
        )
        
        if result.returncode == 0:
            username = result.stdout.strip()
            if username:
                print(f"βœ… Got username from CLI: {username}")
                return username
            else:
                print("⚠️  CLI returned empty username")
                return None
        else:
            print(f"⚠️  CLI whoami failed: {result.stderr}")
            return None
            
    except Exception as e:
        print(f"⚠️  CLI fallback failed: {e}")
        return None

def setup_trackio_dataset():
    """Set up the Trackio experiments dataset on Hugging Face Hub"""
    
    # Configuration - get from environment variables with fallbacks
    hf_token = os.environ.get('HF_TOKEN')
    
    if not hf_token:
        print("❌ HF_TOKEN not found. Please set the HF_TOKEN environment variable.")
        print("You can get your token from: https://huggingface.co/settings/tokens")
        return False
    
    username = get_username_from_token(hf_token)
    if not username:
        print("❌ Could not determine username from token. Please check your token.")
        return False
    
    print(f"βœ… Authenticated as: {username}")
    
    # Use username in dataset repository if not specified
    dataset_repo = os.environ.get('TRACKIO_DATASET_REPO', f'{username}/trackio-experiments')
    
    print(f"πŸš€ Setting up Trackio dataset: {dataset_repo}")
    print(f"πŸ”§ Using dataset repository: {dataset_repo}")
    
    # Initial experiment data
    initial_experiments = [
        {
            'experiment_id': 'exp_20250720_130853',
            'name': 'petite-elle-l-aime-3',
            'description': 'SmolLM3 fine-tuning experiment',
            'created_at': '2025-07-20T11:20:01.780908',
            'status': 'running',
            'metrics': json.dumps([
                {
                    'timestamp': '2025-07-20T11:20:01.780908',
                    'step': 25,
                    'metrics': {
                        'loss': 1.1659,
                        'grad_norm': 10.3125,
                        'learning_rate': 7e-08,
                        'num_tokens': 1642080.0,
                        'mean_token_accuracy': 0.75923578992486,
                        'epoch': 0.004851130919895701
                    }
                },
                {
                    'timestamp': '2025-07-20T11:26:39.042155',
                    'step': 50,
                    'metrics': {
                        'loss': 1.165,
                        'grad_norm': 10.75,
                        'learning_rate': 1.4291666666666667e-07,
                        'num_tokens': 3324682.0,
                        'mean_token_accuracy': 0.7577659255266189,
                        'epoch': 0.009702261839791402
                    }
                },
                {
                    'timestamp': '2025-07-20T11:33:16.203045',
                    'step': 75,
                    'metrics': {
                        'loss': 1.1639,
                        'grad_norm': 10.6875,
                        'learning_rate': 2.1583333333333334e-07,
                        'num_tokens': 4987941.0,
                        'mean_token_accuracy': 0.7581205774843692,
                        'epoch': 0.014553392759687101
                    }
                },
                {
                    'timestamp': '2025-07-20T11:39:53.453917',
                    'step': 100,
                    'metrics': {
                        'loss': 1.1528,
                        'grad_norm': 10.75,
                        'learning_rate': 2.8875e-07,
                        'num_tokens': 6630190.0,
                        'mean_token_accuracy': 0.7614579878747463,
                        'epoch': 0.019404523679582803
                    }
                }
            ]),
            'parameters': json.dumps({
                'model_name': 'HuggingFaceTB/SmolLM3-3B',
                'max_seq_length': 12288,
                'use_flash_attention': True,
                'use_gradient_checkpointing': False,
                'batch_size': 8,
                'gradient_accumulation_steps': 16,
                'learning_rate': 3.5e-06,
                'weight_decay': 0.01,
                'warmup_steps': 1200,
                'max_iters': 18000,
                'eval_interval': 1000,
                'log_interval': 25,
                'save_interval': 2000,
                'optimizer': 'adamw_torch',
                'beta1': 0.9,
                'beta2': 0.999,
                'eps': 1e-08,
                'scheduler': 'cosine',
                'min_lr': 3.5e-07,
                'fp16': False,
                'bf16': True,
                'ddp_backend': 'nccl',
                'ddp_find_unused_parameters': False,
                'save_steps': 2000,
                'eval_steps': 1000,
                'logging_steps': 25,
                'save_total_limit': 5,
                'eval_strategy': 'steps',
                'metric_for_best_model': 'eval_loss',
                'greater_is_better': False,
                'load_best_model_at_end': True,
                'data_dir': None,
                'train_file': None,
                'validation_file': None,
                'test_file': None,
                'use_chat_template': True,
                'chat_template_kwargs': {'add_generation_prompt': True, 'no_think_system_message': True},
                'enable_tracking': True,
                'trackio_url': 'https://tonic-test-trackio-test.hf.space',
                'trackio_token': None,
                'log_artifacts': True,
                'log_metrics': True,
                'log_config': True,
                'experiment_name': 'petite-elle-l-aime-3',
                'dataset_name': 'legmlai/openhermes-fr',
                'dataset_split': 'train',
                'input_field': 'prompt',
                'target_field': 'accepted_completion',
                'filter_bad_entries': True,
                'bad_entry_field': 'bad_entry',
                'packing': False,
                'max_prompt_length': 12288,
                'max_completion_length': 8192,
                'truncation': True,
                'dataloader_num_workers': 10,
                'dataloader_pin_memory': True,
                'dataloader_prefetch_factor': 3,
                'max_grad_norm': 1.0,
                'group_by_length': True
            }),
            'artifacts': json.dumps([]),
            'logs': json.dumps([]),
            'last_updated': datetime.now().isoformat()
        },
        {
            'experiment_id': 'exp_20250720_134319',
            'name': 'petite-elle-l-aime-3-1',
            'description': 'SmolLM3 fine-tuning experiment',
            'created_at': '2025-07-20T11:54:31.993219',
            'status': 'running',
            'metrics': json.dumps([
                {
                    'timestamp': '2025-07-20T11:54:31.993219',
                    'step': 25,
                    'metrics': {
                        'loss': 1.166,
                        'grad_norm': 10.375,
                        'learning_rate': 7e-08,
                        'num_tokens': 1642080.0,
                        'mean_token_accuracy': 0.7590958896279335,
                        'epoch': 0.004851130919895701
                    }
                },
                {
                    'timestamp': '2025-07-20T11:54:33.589487',
                    'step': 25,
                    'metrics': {
                        'gpu_0_memory_allocated': 17.202261447906494,
                        'gpu_0_memory_reserved': 75.474609375,
                        'gpu_0_utilization': 0,
                        'cpu_percent': 2.7,
                        'memory_percent': 10.1
                    }
                }
            ]),
            'parameters': json.dumps({
                'model_name': 'HuggingFaceTB/SmolLM3-3B',
                'max_seq_length': 12288,
                'use_flash_attention': True,
                'use_gradient_checkpointing': False,
                'batch_size': 8,
                'gradient_accumulation_steps': 16,
                'learning_rate': 3.5e-06,
                'weight_decay': 0.01,
                'warmup_steps': 1200,
                'max_iters': 18000,
                'eval_interval': 1000,
                'log_interval': 25,
                'save_interval': 2000,
                'optimizer': 'adamw_torch',
                'beta1': 0.9,
                'beta2': 0.999,
                'eps': 1e-08,
                'scheduler': 'cosine',
                'min_lr': 3.5e-07,
                'fp16': False,
                'bf16': True,
                'ddp_backend': 'nccl',
                'ddp_find_unused_parameters': False,
                'save_steps': 2000,
                'eval_steps': 1000,
                'logging_steps': 25,
                'save_total_limit': 5,
                'eval_strategy': 'steps',
                'metric_for_best_model': 'eval_loss',
                'greater_is_better': False,
                'load_best_model_at_end': True,
                'data_dir': None,
                'train_file': None,
                'validation_file': None,
                'test_file': None,
                'use_chat_template': True,
                'chat_template_kwargs': {'add_generation_prompt': True, 'no_think_system_message': True},
                'enable_tracking': True,
                'trackio_url': 'https://tonic-test-trackio-test.hf.space',
                'trackio_token': None,
                'log_artifacts': True,
                'log_metrics': True,
                'log_config': True,
                'experiment_name': 'petite-elle-l-aime-3-1',
                'dataset_name': 'legmlai/openhermes-fr',
                'dataset_split': 'train',
                'input_field': 'prompt',
                'target_field': 'accepted_completion',
                'filter_bad_entries': True,
                'bad_entry_field': 'bad_entry',
                'packing': False,
                'max_prompt_length': 12288,
                'max_completion_length': 8192,
                'truncation': True,
                'dataloader_num_workers': 10,
                'dataloader_pin_memory': True,
                'dataloader_prefetch_factor': 3,
                'max_grad_norm': 1.0,
                'group_by_length': True
            }),
            'artifacts': json.dumps([]),
            'logs': json.dumps([]),
            'last_updated': datetime.now().isoformat()
        }
    ]
    
    try:
        # Initialize HF API
        api = HfApi(token=hf_token)
        
        # First, try to create the dataset repository
        print(f"Creating dataset repository: {dataset_repo}")
        try:
            create_repo(
                repo_id=dataset_repo,
                token=hf_token,
                repo_type="dataset",
                exist_ok=True,
                private=True  # Make it private for security
            )
            print(f"βœ… Dataset repository created: {dataset_repo}")
        except Exception as e:
            print(f"⚠️  Repository creation failed (may already exist): {e}")
        
        # Create dataset
        dataset = Dataset.from_list(initial_experiments)
        
        # Get the project root directory (2 levels up from this script)
        project_root = Path(__file__).parent.parent.parent
        templates_dir = project_root / "templates" / "datasets"
        readme_path = templates_dir / "readme.md"
        
        # Read README content if it exists
        readme_content = None
        if readme_path.exists():
            with open(readme_path, 'r', encoding='utf-8') as f:
                readme_content = f.read()
            print(f"βœ… Found README template: {readme_path}")
        
        # Push to HF Hub
        print("Pushing dataset to HF Hub...")
        dataset.push_to_hub(
            dataset_repo,
            token=hf_token,
            private=False  # Make it private for security
        )
        
        # Create README separately if available
        if readme_content:
            try:
                print("Uploading README.md...")
                api.upload_file(
                    path_or_fileobj=readme_content.encode('utf-8'),
                    path_in_repo="README.md",
                    repo_id=dataset_repo,
                    repo_type="dataset",
                    token=hf_token
                )
                print("πŸ“ Uploaded README.md successfully")
            except Exception as e:
                print(f"⚠️  Could not upload README: {e}")
        
        print(f"βœ… Successfully created dataset: {dataset_repo}")
        print(f"πŸ“Š Added {len(initial_experiments)} experiments")
        if readme_content:
            print("πŸ“ Included README from templates")
        print("πŸ”“ Dataset is public (accessible to everyone)")
        print(f"πŸ‘€ Created by: {username}")
        print("\n🎯 Next steps:")
        print("1. Set HF_TOKEN in your Hugging Face Space environment")
        print("2. Deploy the updated app.py to your Space")
        print("3. The app will now load experiments from the dataset")
        
        return True
        
    except Exception as e:
        print(f"❌ Failed to create dataset: {e}")
        print("\nTroubleshooting:")
        print("1. Check that your HF token has write permissions")
        print("2. Verify the dataset repository name is available")
        print("3. Try creating the dataset manually on HF first")
        return False

if __name__ == "__main__":
    setup_trackio_dataset()