File size: 1,476 Bytes
5da6fa2
 
 
 
 
 
 
 
 
 
 
6c099c7
 
5da6fa2
 
 
6c099c7
 
5da6fa2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python

import pandas as pd
import re

def process(df: pd.DataFrame) -> pd.DataFrame:

    pattern = r'<\|im_start\|>(\w+)\n(.*?)<\|im_end\|>'
    
    def process_text(text):
        matches = re.findall(pattern, text, re.DOTALL)
        # remove the 'system' in matches
        matches = [(k, v) for k, v in matches if k != 'system']
        return dict(matches)
    
    result = df['text'].apply(process_text)
    # remove the columns that named 'text' and 'system'
    result = result.drop(columns=['text', 'system'])
    return pd.DataFrame(result.tolist())


def main(file: str = None) -> None:
    if file != None:
        df = pd.read_json(file, lines=True)
        result = process(df)
        print(result)
    else:
        data = {
            "text": [
                '<|im_start|>system You are a helpful assistant<|im_end|> <|im_start|>user "阿猛,你觉得做视频最重要的是什么,除了内容好,还有什么能让视频更吸引人呢?"<|im_end|> <|im_start|>assistant 视频做的很好,但美中不足的是。。。时长太短,不!够!看!<|im_end|>',
                '<|im_start|>system You are a helpful assistant<|im_end|> <|im_start|>user "要如何學會加法"<|im_end|> <|im_start|>assistant 這個問題,我也不太會<|im_end|>',
            ]
        }
        df = pd.DataFrame(data)
        result = process(df)
        print(result)

if __name__ == "__main__":
    import fire
    fire.Fire(main)