File size: 2,589 Bytes
0a18477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os


exp_name = './exp0_2023_09_27_03_10_22/'

gpt_log_path = f'{exp_name}gpt_log_audio.txt'
caption_path = f'{exp_name}log.txt'

modality = 'audio'
postfix = '.wav'
save_path = f'{exp_name}{modality}_t2x.json'

with open(caption_path, 'r', encoding='utf-8') as f:
    captions = []
    for line in f.readlines():
        captions.append(line.split('\t')[-1].strip())


with open(gpt_log_path, 'r', encoding='utf-8') as f:
    dialogues = []
    _temp = []
    _instances = []
    # need to split the data by *****
    for line in f.readlines():
        if line.startswith('***********'):
            _instances.append(_temp)
            _temp = []
        else:
            if line != '\n':
                _temp.append(line.strip())
    assert len(captions) == len(_instances)
    for idx, (instance, cap) in enumerate(zip(_instances, captions)):
        if len(instance) > 11:
            pass
        else:
            _human = ''
            _gpt = ''
            idy = 0
            for ins in instance:
                if ins.startswith('Human:'):
                    if _human == '':
                        _human = ins.split('Human: ')[-1]
                elif ins.startswith('GPT:'):
                    if _gpt == '':
                        _gpt = ins.split('GPT: ')[-1]
                elif ins.startswith('----'):
                    if _human != '' and _gpt != '':
                        _res = [
                            {'from': 'human', 'value': _human, 'input_modality': 'text'},
                            {'from': 'gpt', 'value': _gpt, 'caption': cap, 'output_modality': modality}
                        ]
                        res = {'image_name': f't2x00{idx}{idy}'+postfix, 'output_modality': modality, 'conversation': _res}
                        dialogues.append(res)
                        _human = ''
                        _gpt = ''
                        idy += 1
                else:
                    continue
            if _human != '' and _gpt != '':
                _res = [
                    {'from': 'human', 'value': _human, 'input_modality': 'text'},
                    {'from': 'gpt', 'value': _gpt, 'caption': cap, 'output_modality': modality}
                ]
                res = {'image_name': f't2x00{idx}{idy}' + postfix, 'output_modality': modality, 'conversation': _res}
                dialogues.append(res)
    print(len(dialogues))

with open(save_path, 'w', encoding='utf-8') as f:
    json.dump(dialogues, f, indent=4)