Update ml.py
Browse files
ml.py
CHANGED
|
@@ -1,34 +1,34 @@
|
|
| 1 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, StoppingCriteria, StoppingCriteriaList
|
| 2 |
-
import torch
|
| 3 |
-
import json
|
| 4 |
-
import json
|
| 5 |
-
import re
|
| 6 |
-
import numpy as np
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def create_prompt(text, template, examples):
|
| 10 |
-
template = json.dumps(json.loads(template),indent = 4)
|
| 11 |
-
|
| 12 |
-
prompt = "<|input|>\n### Template:\n"+template+"\n"
|
| 13 |
-
|
| 14 |
-
if examples[0]:
|
| 15 |
-
example1 = json.dumps(json.loads(examples[0]),indent = 4)
|
| 16 |
-
prompt+= "### Example:\n"+example1+"\n"
|
| 17 |
-
if examples[1]:
|
| 18 |
-
example2 = json.dumps(json.loads(examples[1]),indent = 4)
|
| 19 |
-
prompt+= "### Example:\n"+example1+"\n"
|
| 20 |
-
if examples[2]:
|
| 21 |
-
example3 = json.dumps(json.loads(examples[1]),indent = 4)
|
| 22 |
-
prompt+= "### Example:\n"+example3+"\n"
|
| 23 |
-
|
| 24 |
-
prompt += "### Text:\n"+text+'''\n<|output|>'''
|
| 25 |
-
|
| 26 |
-
return prompt
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
def generate_answer_short(prompt,model, tokenizer):
|
| 30 |
-
model_input = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=3000).to("cuda")
|
| 31 |
-
with torch.no_grad():
|
| 32 |
-
gen = tokenizer.decode(model.generate(**model_input, max_new_tokens=1500)[0], skip_special_tokens=True)
|
| 33 |
-
print(gen.split("<|output|>")[1]
|
| 34 |
-
return gen.split("<|output|>")[1].split("<|end-output|>")[0]
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, StoppingCriteria, StoppingCriteriaList
|
| 2 |
+
import torch
|
| 3 |
+
import json
|
| 4 |
+
import json
|
| 5 |
+
import re
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def create_prompt(text, template, examples):
|
| 10 |
+
template = json.dumps(json.loads(template),indent = 4)
|
| 11 |
+
|
| 12 |
+
prompt = "<|input|>\n### Template:\n"+template+"\n"
|
| 13 |
+
|
| 14 |
+
if examples[0]:
|
| 15 |
+
example1 = json.dumps(json.loads(examples[0]),indent = 4)
|
| 16 |
+
prompt+= "### Example:\n"+example1+"\n"
|
| 17 |
+
if examples[1]:
|
| 18 |
+
example2 = json.dumps(json.loads(examples[1]),indent = 4)
|
| 19 |
+
prompt+= "### Example:\n"+example1+"\n"
|
| 20 |
+
if examples[2]:
|
| 21 |
+
example3 = json.dumps(json.loads(examples[1]),indent = 4)
|
| 22 |
+
prompt+= "### Example:\n"+example3+"\n"
|
| 23 |
+
|
| 24 |
+
prompt += "### Text:\n"+text+'''\n<|output|>'''
|
| 25 |
+
|
| 26 |
+
return prompt
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def generate_answer_short(prompt,model, tokenizer):
|
| 30 |
+
model_input = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=3000).to("cuda")
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
gen = tokenizer.decode(model.generate(**model_input, max_new_tokens=1500)[0], skip_special_tokens=True)
|
| 33 |
+
print(gen.split("<|output|>")[1])
|
| 34 |
+
return gen.split("<|output|>")[1].split("<|end-output|>")[0]
|