read json
(data_path, max_seq_len=512)
| 61 | |
| 62 | |
| 63 | def reader(data_path, max_seq_len=512): |
| 64 | """ |
| 65 | read json |
| 66 | """ |
| 67 | with open(data_path, "r", encoding="utf-8") as f: |
| 68 | for line in f: |
| 69 | json_line = json.loads(line) |
| 70 | content = json_line["content"].strip() |
| 71 | prompt = json_line["prompt"] |
| 72 | # Model Input is aslike: [CLS] Prompt [SEP] Content [SEP] |
| 73 | # It include three summary tokens. |
| 74 | if max_seq_len <= len(prompt) + 3: |
| 75 | raise ValueError("The value of max_seq_len is too small, please set a larger value") |
| 76 | max_content_len = max_seq_len - len(prompt) - 3 |
| 77 | if len(content) <= max_content_len: |
| 78 | yield json_line |
| 79 | else: |
| 80 | result_list = json_line["result_list"] |
| 81 | json_lines = [] |
| 82 | accumulate = 0 |
| 83 | while True: |
| 84 | cur_result_list = [] |
| 85 | for result in result_list: |
| 86 | if result["end"] - result["start"] > max_content_len: |
| 87 | logger.warning( |
| 88 | "result['end'] - result ['start'] exceeds max_content_len, which will result in no valid instance being returned" |
| 89 | ) |
| 90 | if ( |
| 91 | result["start"] + 1 <= max_content_len < result["end"] |
| 92 | and result["end"] - result["start"] <= max_content_len |
| 93 | ): |
| 94 | max_content_len = result["start"] |
| 95 | break |
| 96 | |
| 97 | cur_content = content[:max_content_len] |
| 98 | res_content = content[max_content_len:] |
| 99 | |
| 100 | while True: |
| 101 | if len(result_list) == 0: |
| 102 | break |
| 103 | elif result_list[0]["end"] <= max_content_len: |
| 104 | if result_list[0]["end"] > 0: |
| 105 | cur_result = result_list.pop(0) |
| 106 | cur_result_list.append(cur_result) |
| 107 | else: |
| 108 | cur_result_list = [result for result in result_list] |
| 109 | break |
| 110 | else: |
| 111 | break |
| 112 | |
| 113 | json_line = {"content": cur_content, "result_list": cur_result_list, "prompt": prompt} |
| 114 | json_lines.append(json_line) |
| 115 | |
| 116 | for result in result_list: |
| 117 | if result["end"] <= 0: |
| 118 | break |
| 119 | result["start"] -= max_content_len |
| 120 | result["end"] -= max_content_len |
nothing calls this directly
no outgoing calls
no test coverage detected