File size: 1,033 Bytes
7c0199b
 
 
 
afe352a
7c0199b
04e9e1a
b24eb89
7c0199b
b24eb89
7c0199b
 
 
1659ccb
7c0199b
 
 
 
 
7c17b3b
7c0199b
 
afe352a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import string


def generate_text(text, context, model_name, model, tokenizer, minimum=15, maximum=300):
    text = f'{context} {text}'
    if 'GODEL' in model_name:
        text = f'Instruction: you need to response discreetly. [CONTEXT] {text}'
        text.replace('\t', ' EOS ')
    else:
        text = text.replace('\t', '\n')
    input_ids = tokenizer(text, return_tensors="pt").input_ids
    outputs = model.generate(input_ids, max_new_tokens=maximum, min_new_tokens=minimum, top_p=0.9, do_sample=True)
    output = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return capitalization(output)


def capitalization(line):
    line, end = line[:-1], line[-1]
    for mark in '.?!':
        line = f'{mark} '.join([part.strip()[0].upper() + part.strip()[1:] for part in line.split(mark) if len(part) > 1])
    line = ' '.join([word.capitalize() if word.translate(str.maketrans('', '', string.punctuation)) == 'i'
                    else word for word in line.split()])
    return line.replace(' i\'',  ' I\'') + end