samaresh55 commited on
Commit
4666d78
·
1 Parent(s): b7a4cff

Upload colab_script.py

Browse files
Files changed (1) hide show
  1. colab_script.py +89 -0
colab_script.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Code adapted from https://github.com/tloen/alpaca-lora
2
+ !pip install git+https://github.com/huggingface/transformers.git
3
+ !pip install sentencepiece
4
+ !pip install peft
5
+ !pip install accelerate
6
+ !pip install bitsandbytes
7
+ import torch
8
+ from peft import PeftModel
9
+ import transformers
10
+
11
+ from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig
12
+
13
+ tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
14
+ device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
15
+ print(device)
16
+ model = LlamaForCausalLM.from_pretrained(
17
+ "decapoda-research/llama-7b-hf",
18
+ load_in_8bit=True,
19
+ torch_dtype=torch.float16,
20
+ device_map="auto",
21
+ offload_folder = "."
22
+ )
23
+
24
+ model = PeftModel.from_pretrained(
25
+ model,
26
+ "tloen/alpaca-lora-7b",
27
+ torch_dtype=torch.float16,
28
+ device_map="auto",
29
+ load_in_8bit = True,
30
+ offload_folder="."
31
+ )
32
+
33
+ def generate_prompt(instruction, input=None):
34
+ if input:
35
+ return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
36
+
37
+ ### Instruction:
38
+ {instruction}
39
+
40
+ ### Input:
41
+ {input}
42
+
43
+ ### Response:"""
44
+ else:
45
+ return f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
46
+
47
+ ### Instruction:
48
+ {instruction}
49
+
50
+ ### Response:"""
51
+
52
+
53
+ model.eval()
54
+
55
+
56
+ def evaluate(
57
+ instruction,
58
+ input=None,
59
+ temperature=0.1,
60
+ top_p=0.75,
61
+ top_k=40,
62
+ num_beams=4,
63
+ **kwargs,
64
+ ):
65
+ prompt = generate_prompt(instruction, input)
66
+ inputs = tokenizer(prompt, return_tensors="pt")
67
+ input_ids = inputs["input_ids"].to(device)
68
+ generation_config = GenerationConfig(
69
+ temperature=temperature,
70
+ top_p=top_p,
71
+ top_k=top_k,
72
+ num_beams=num_beams,
73
+ **kwargs,
74
+ )
75
+ with torch.no_grad():
76
+ generation_output = model.generate(
77
+ input_ids=input_ids,
78
+ generation_config=generation_config,
79
+ return_dict_in_generate=True,
80
+ output_scores=True,
81
+ max_new_tokens=2048,
82
+ )
83
+ s = generation_output.sequences[0]
84
+ output = tokenizer.decode(s)
85
+ return output.split("### Response:")[1].strip()
86
+ input = """RESEARCH REPORT NO. 1037027Final Clinical Study Report -NV25118: A Randomized, Multicentre, Single Blinded, Parallel Study of the Safety of 100 mg and 200 mg Oseltamivir Administered Intravenously for the Treatment of Influenza in Patients Aged > 13 Years. Report No. 1037027. June 3, 2013 Study Sponsor(s):Dr F Hoffman La-Roche LtdStudy Dates:First patient screened: Jan 15, 2010 Last patient visit: Sept 14, 2012Trial Phase:II/IIIIndication:InfluenzaName of Principal Investigator Thomas MathewSponsor's Signatory: Personnel Responsible for Clinical and Statistical Analyses: Clinical analysis:John ThomasSafety analysis:William RichardStatistical analysis:Charles DanielPharmacokinetic analysis:Robert DavidGCP Compliance: This study was conducted in accordance with the principles of GCP"""
87
+ instruction = "Extract Names, Diseases, Dates from below text:"
88
+ # input = "RESEARCH REPORT NO. 1037027Final Clinical Study Report -NV25118: A Randomized, Multicentre, Single Blinded, Parallel Study of the Safety of 100 mg and 200 mg Oseltamivir Administered Intravenously for the Treatment of Influenza in Patients Aged > 13 Years. Report No. 1037027. June 3, 2013 Study Sponsor(s):Dr F Hoffman La-Roche LtdStudy Dates:First patient screened: Jan 15, 2010 Last patient visit: Sept 14, 2012"
89
+ print("Response:", evaluate(instruction,input))