File size: 2,829 Bytes
d8ee309
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from random_data import generate_random_byte_array
import json
import os
import random
import base64

def base64_encode_byte_array(byte_array):
    return base64.b64encode(byte_array).decode('utf-8')

def generate_dataset_item(seed):
    length = random.Random(seed + 1000).randint(0, 127)
    byte_array = generate_random_byte_array(length=length, seed=seed + 1001)

    output_formats = [
        'hex', 
        'json'
    ]
    output_format = random.Random(seed + 1002).choice(output_formats)

    names_hex = [
        'Hexadecimal',
        'hexadecimal',
        'hex',
        'Hex',
        'HEX',
    ]
    names_json = [
        'Json',
        'json',
        'JSON',
    ]

    name_output = None
    if output_format == 'hex':
        name_output = random.Random(seed + 1003).choice(names_hex)
    else:
        if output_format == 'json':
            name_output = random.Random(seed + 1004).choice(names_json)

    name_inputs = [
        'base64',
        'Base64',
        'BASE64',
    ]
    name_input = random.Random(seed + 1005).choice(name_inputs)

    instructions = [
        f'Decode {name_input} to {name_output}',
        f'decode {name_input} to {name_output}',
        f'convert {name_input} to {name_output}',
        f'Convert {name_input} to {name_output}',
        f'Transform {name_input} to {name_output}',
        f'transform {name_input} to {name_output}',
        f'Change {name_input} to {name_output}',
        f'change {name_input} to {name_output}',
        f'{name_input} to {name_output}',
        f'{name_output} from {name_input}',
    ]

    instruction = random.Random(seed + 1006).choice(instructions)

    input = base64_encode_byte_array(byte_array)

    output = None
    if output_format == 'hex':
        output = byte_array.hex()
    else:
        if output_format == 'json':
            output = json.dumps(list(byte_array), separators=(',', ':'))

    dict = {
        'instruction': instruction,
        'input': input,
        'output': output
    }
    return dict

def generate_dataset(max_num_samples=1000, max_byte_size=1024*1024, seed_start=0):
    dataset = []
    dataset_byte_size = 0
    for i in range(max_num_samples):
        item = generate_dataset_item(seed_start + i)
        bytes = len(json.dumps(item))
        if dataset_byte_size + bytes > max_byte_size:
            break
        dataset_byte_size += bytes
        dataset.append(item)
    return dataset

dataset = generate_dataset(
    max_num_samples=50000,
    max_byte_size=1024*1024*20,
)

# Save dataset to file
filename = 'base64-decode-v1.jsonl'
with open(filename, 'w') as f:
    for item in dataset:
        f.write(json.dumps(item) + '\n')

# Summary
file_size = os.path.getsize(filename)
print(f"Generated {len(dataset)} samples, saved to {filename}, file size: {file_size} bytes.")