Download utils/functions/regular.py from ZoneTwelve/bilibot: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/datasets/ZoneTwelve/bilibot/resolve/main/utils/functions/regular.py
- Command line
-
hf download hf://datasets/ZoneTwelve/bilibot/utils/functions/regular.py
-
curl -L -o regular.py https://huggingface.co/datasets/ZoneTwelve/bilibot/resolve/main/utils/functions/regular.py
1.48 kB
| #!/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) |