File size: 6,810 Bytes
a7ecc6f 1abd2de a7ecc6f 1abd2de a7ecc6f | 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | import nltk
import re
import random
import numpy as np
from .config import SUMMARY_PROMPTS, GENERATION_PROMPTS, random_params
try:
nltk.data.find('tokenizers/punkt_tab')
except LookupError:
nltk.download('punkt_tab')
def get_sentences(text):
spans = list(nltk.tokenize.punkt.PunktSentenceTokenizer().span_tokenize(text))
sentences = []
for i, (start, end) in enumerate(spans):
if i < len(spans) - 1:
next_start = spans[i + 1][0]
else:
next_start = len(text)
expanded_end = end
while expanded_end < next_start and expanded_end < len(text):
if text[expanded_end].isspace():
expanded_end += 1
else:
break
sentences.append(text[start:expanded_end])
return sentences
def clean_text(text):
text = re.sub(r'<\|.*?\|>', '', text)
return text.replace('\n\n', '\n').strip()
def subsample_tokens(text, labels, max_len=350):
words = text.split()
if len(words) <= max_len:
return text, labels
trans = [i for i in range(1, len(labels)) if labels[i] != labels[i - 1]]
if len(trans) >= 2:
cut = trans[0] + 1
return subsample_tokens(' '.join(words[cut:]), labels[cut:], max_len)
elif len(trans) == 1:
boundary = trans[0]
half = max_len // 2
start = max(0, boundary - random.randint(0, half))
end = min(len(words), start + max_len)
return ' '.join(words[start:end]), labels[start:end]
else:
start = random.randint(0, max(0, len(words) - max_len))
return ' '.join(words[start:start + max_len]), labels[start:start + max_len]
def simple_augment(text, labels):
"""Basic online augmentation: random middle truncation, never label-destroying."""
words = text.split()
if len(words) < 50 or random.random() > 0.3:
return text, labels, []
augs = []
if random.random() < 0.5:
keep_from = random.randint(0, max(0, len(words) - 250))
words = words[keep_from:keep_from + 250]
labels = labels[keep_from:keep_from + 250]
augs.append('subsample')
return ' '.join(words), labels, augs
def regenerated_in_the_middle(hub, model_name, text, params, is_text_mode=False):
sentences = get_sentences(text)
if len(sentences) < 3:
return None, None, []
first_part = len(sentences) // 3
second_part = 2 * len(sentences) // 3
for _ in range(10):
lens = [len(x) for x in sentences]
first_size = sum(lens[:first_part])
second_size = sum(lens[first_part:second_part])
third_size = sum(lens[second_part:])
changed = False
if first_size - lens[first_part - 1] > second_size + lens[first_part - 1]:
first_part -= 1; changed = True
elif second_size - lens[second_part - 1] > third_size + lens[second_part - 1]:
second_part -= 1; changed = True
elif first_part < len(sentences) - 1 and first_size + lens[first_part] < second_size - lens[first_part]:
first_part += 1; changed = True
elif second_part < len(sentences) - 1 and second_size + lens[second_part] < third_size - lens[second_part]:
second_part += 1; changed = True
if not changed:
break
begin = ''.join(sentences[:first_part])
middle = ''.join(sentences[first_part:second_part])
end = ''.join(sentences[second_part:])
middle_stripped = middle.rstrip()
diff = len(middle) - len(middle_stripped)
end = middle[-diff:] + end
middle = middle_stripped
summary_idx = random.randint(0, len(SUMMARY_PROMPTS) - 1)
gen_idx = random.randint(0, len(GENERATION_PROMPTS) - 1)
middle_size = len(middle.split())
if is_text_mode:
summary = hub.text_completion(model_name, f'{SUMMARY_PROMPTS[summary_idx]}\n\n{middle}', params)
else:
summary = hub.chat_completion(model_name, [
{'role': 'system', 'content': SUMMARY_PROMPTS[summary_idx]},
{'role': 'user', 'content': middle},
], params)
gen_prompt = GENERATION_PROMPTS[gen_idx] + f' The middle should be about {middle_size} words long'
user_content = f'begin: {begin}\nend: {end}\nsummary: {summary}'
if is_text_mode:
gen_middle = hub.text_completion(model_name, f'{gen_prompt}\n\n{user_content}', params)
else:
gen_middle = hub.chat_completion(model_name, [
{'role': 'system', 'content': gen_prompt},
{'role': 'user', 'content': user_content},
], params)
gen_middle = clean_text(gen_middle)
full_text = begin + gen_middle + end
labels = [0] * len(begin.split()) + [1] * len(gen_middle.strip().split()) + [0] * len(end.split())
return full_text, labels, list(hub._call_history)
def generate_ai_completion(hub, model_name, prompt, params, is_text_mode=False):
if is_text_mode:
completion = hub.text_completion(model_name, prompt, params)
else:
completion = hub.chat_completion(model_name, [
{'role': 'system', 'content': 'You\'re a text completion model, just complete text that user sended you'},
{'role': 'user', 'content': prompt},
], params)
return clean_text(completion)
def generate_one_sample(hub, model_name, gen_type, is_text_mode, src_text, prompt_text):
"""Generate one sample. Returns dict or None on failure."""
hub._call_history = []
params = random_params()
if gen_type == 'ai_in_middle':
text, labels, call_info = regenerated_in_the_middle(hub, model_name, src_text, params, is_text_mode)
if text is None:
return None
else:
completion = generate_ai_completion(hub, model_name, prompt_text, params, is_text_mode)
call_info = list(hub._call_history)
if not completion:
return None
if random.random() < 0.615:
cnt_first = len(prompt_text.split())
text = prompt_text + ' ' + completion
labels = [0] * cnt_first + [1] * len(completion.split())
else:
text = completion
labels = [1] * len(completion.split())
text, labels, augs = simple_augment(text, labels)
text, labels = subsample_tokens(text, labels)
if len(labels) < 10:
return None
# llm_type = the actual model that produced the (final) output, not the slot name.
# call_info is hub._call_history: list of {'provider','model','slot'}; the last
# successful entry generated the text we keep.
llm_type = call_info[-1]['model'] if call_info else model_name
return {
'text': text, 'labels': labels, 'model': model_name,
'llm_type': llm_type,
'model_info': call_info,
'params': params, 'type': gen_type, 'augmentations': augs,
}
|