File size: 4,817 Bytes
a9cd940
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
HF Space Test - Small dataset processor
Tests if HF Spaces can handle Bhaari AI processing within 50GB disk limit
Uses small pending dataset: ADP CodeActInstruct (56.8MB)
"""

import json, time, os, sys
from pathlib import Path
from huggingface_hub import HfApi, hf_hub_download, upload_file

TOKEN = os.environ.get("HF_TOKEN", "")
HF_REPO = "mohit-vashishta/bhaari-training-data-100b"
TARGET_REPO = "mohit-vashishta/bhaari-training-data-100b"

WORK_DIR = Path("/workspace")
DATA_DIR = WORK_DIR / "data"
TOKENIZER_PATH = WORK_DIR / "tokenizer.json"

for d in [DATA_DIR]:
    d.mkdir(parents=True, exist_ok=True)

def log(msg):
    ts = time.strftime("%H:%M:%S")
    print(f"[{ts}] {msg}")
    sys.stdout.flush()

def check_disk():
    stat = os.statvfs("/workspace")
    free_gb = (stat.f_bavail * stat.f_frsize) / 1e9
    used_gb = ((stat.f_blocks - stat.f_bfree) * stat.f_frsize) / 1e9
    total_gb = (stat.f_blocks * stat.f_frsize) / 1e9
    return total_gb, used_gb, free_gb

def main():
    log("=" * 60)
    log("HF Space Test - Small Dataset (ADP CodeActInstruct)")
    log("=" * 60)
    
    total, used, free = check_disk()
    log(f"Disk: Total={total:.1f}GB, Used={used:.1f}GB, Free={free:.1f}GB")
    
    if not TOKEN:
        log("ERROR: HF_TOKEN not set in Space secrets")
        return
    
    # Download tokenizer
    log("Downloading tokenizer...")
    try:
        hf_hub_download(HF_REPO, "tokenizer/tokenizer.json", 
                       local_dir=str(WORK_DIR), token=TOKEN)
        log("Tokenizer downloaded")
    except Exception as e:
        log(f"Tokenizer download failed: {e}")
        return
    
    # Test dataset: ADP CodeActInstruct (56.8MB JSONL)
    test_file = "phase_6_agent/adp_codeactinstruct.jsonl"
    log(f"Downloading {test_file}...")
    
    try:
        local_jsonl = hf_hub_download(HF_REPO, test_file, 
                                     repo_type="dataset", token=TOKEN)
        log(f"Downloaded: {os.path.getsize(local_jsonl)/1e6:.1f}MB")
    except Exception as e:
        log(f"Download failed: {e}")
        log("Trying alternative small dataset...")
        
        # Fallback: Score Reasoning (236.9MB)
        test_file = "phase_3_reasoning/score_reasoning.jsonl"
        try:
            local_jsonl = hf_hub_download(HF_REPO, test_file, 
                                         repo_type="dataset", token=TOKEN)
            log(f"Downloaded: {os.path.getsize(local_jsonl)/1e6:.1f}MB")
        except Exception as e2:
            log(f"Fallback also failed: {e2}")
            return
    
    total, used, free = check_disk()
    log(f"Disk after download: Used={used:.1f}GB, Free={free:.1f}GB")
    
    # Tokenize with Python (no Rust needed for test)
    log("Loading tokenizer...")
    from tokenizers import Tokenizer
    tok = Tokenizer.from_file(str(TOKENIZER_PATH))
    
    log("Tokenizing...")
    t0 = time.time()
    all_tokens = []
    row_count = 0
    
    with open(local_jsonl) as f:
        for line in f:
            try:
                obj = json.loads(line)
                text = obj.get("text", "").strip()
                if len(text) >= 50:
                    encoded = tok.encode(text)
                    all_tokens.extend(encoded.ids)
                    row_count += 1
            except:
                continue
    
    tok_time = time.time() - t0
    log(f"Tokenized {row_count:,} rows, {len(all_tokens):,} tokens in {tok_time:.1f}s")
    log(f"Speed: {len(all_tokens)/tok_time/1000:.0f}K tokens/sec")
    
    # Save as BHRI format
    output_path = DATA_DIR / "adp_codeactinstruct.npy"
    log(f"Saving BHRI format to {output_path}...")
    
    import struct
    with open(output_path, "wb") as f:
        f.write(b"BHRI")
        f.write(struct.pack("<I", 1))  # version
        f.write(struct.pack("<I", len(all_tokens)))
        f.write(struct.pack("<I", 500000))  # control token
        for token in all_tokens:
            f.write(struct.pack("<I", token))
    
    output_size = output_path.stat().st_size
    log(f"Output: {output_size/1e6:.1f}MB")
    
    total, used, free = check_disk()
    log(f"Disk after tokenize: Used={used:.1f}GB, Free={free:.1f}GB")
    
    # Upload back to HF
    log("Uploading to HF...")
    try:
        api = HfApi(token=TOKEN)
        api.upload_file(
            path_or_fileobj=str(output_path),
            path_in_repo="phase_6_agent/adp_codeactinstruct.npy",
            repo_id=TARGET_REPO,
            repo_type="dataset"
        )
        log("Upload successful!")
    except Exception as e:
        log(f"Upload failed: {e}")
    
    total, used, free = check_disk()
    log(f"Final disk: Used={used:.1f}GB, Free={free:.1f}GB")
    
    log("=" * 60)
    log("TEST COMPLETE")
    log("=" * 60)

if __name__ == "__main__":
    main()