Views
No views yet
pip install sentencepiece
pip install transformers
pip install accelerate -U1import torch
2from transformers import T5Tokenizer, T5ForConditionalGeneration, TrainingArguments, Trainer
3model = T5ForConditionalGeneration.from_pretrained("Jwizzed/TaskToSubtask").to('cpu')
4tokenizer = T5Tokenizer.from_pretrained("t5-small")
5
6def infer(input_text, model, tokenizer):
7 model.eval()
8 input_tensor = tokenizer.encode(input_text, return_tensors="pt").to('cpu')
9 with torch.no_grad():
10 output = model.generate(input_tensor, max_length=1024)
11 decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
12 decoded_output = decoded_output.replace(" and ", ", ").strip()
13 return decoded_output
14
15
16sample_text = """
17Dear Students
18Please watch all class materials for our topic before you answer the following questions.
191. Give your own definition of mental health.
202. What does a person with good mental health look like?
213. How do you deal with your stress?
224. Out of many suggestions to help improve your mental health, which strategies you think most helpful to you?
23
24Please type your answer. Don’t forget to put your name and ID on the right top cornor. Written in full sentences is preferable than mind map
25
26Plagiarism/ copying your friends’ answers will be penalized.
27"""
28
29print(infer(sample_text, model.to('cpu'), tokenizer))
30