|
|
|
|
|
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) |
|
|
|
matches = [(k, v) for k, v in matches if k != 'system'] |
|
return dict(matches) |
|
|
|
result = df['text'].apply(process_text) |
|
|
|
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) |