GreatCaptainNemo commited on
Commit
5d9b5f6
1 Parent(s): 5da7aa0

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +110 -0
README.md ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ ---
4
+ # ProLLaMA: A Protein Large Language Model for Multi-Task Protein Language Processing
5
+
6
+ [Paper on arxiv](https://arxiv.org/abs/2402.16445) for more information
7
+
8
+ [Github](https://github.com/Lyu6PosHao/ProLLaMA) for more information
9
+
10
+ ProLLaMA_Stage_1 is based on Llama-2-7b, so please follow the license of Llama2.
11
+ # Quick usage:
12
+ ```bash
13
+ # you can replace the model_path with your local path
14
+ CUDA_VISIBLE_DEVICES=0 python main.py --model "GreatCaptainNemo/ProLLaMA_Stage_1" --interactive
15
+ # main.py is as follows 👇:
16
+ ```
17
+
18
+ ```python
19
+ import argparse
20
+ import json, os
21
+ import torch
22
+ from transformers import LlamaForCausalLM, LlamaTokenizer
23
+ from transformers import GenerationConfig
24
+ from tqdm import tqdm
25
+
26
+ generation_config = GenerationConfig(
27
+ temperature=0.2,
28
+ top_k=40,
29
+ top_p=0.9,
30
+ do_sample=True,
31
+ num_beams=1,
32
+ repetition_penalty=1.2,
33
+ max_new_tokens=400
34
+ )
35
+
36
+ parser = argparse.ArgumentParser()
37
+ parser.add_argument('--model', default=None, type=str,help="The local path of the model. If None, the model will be downloaded from HuggingFace")
38
+ parser.add_argument('--interactive', action='store_true',help="If True, you can input instructions interactively. If False, the input instructions should be in the input_file.")
39
+ parser.add_argument('--input_file', default=None, help="You can put all your input instructions in this file (one instruction per line).")
40
+ parser.add_argument('--output_file', default=None, help="All the outputs will be saved in this file.")
41
+ args = parser.parse_args()
42
+
43
+ if __name__ == '__main__':
44
+ if args.interactive and args.input_file:
45
+ raise ValueError("interactive is True, but input_file is not None.")
46
+ if (not args.interactive) and (args.input_file is None):
47
+ raise ValueError("interactive is False, but input_file is None.")
48
+ if args.input_file and (args.output_file is None):
49
+ raise ValueError("input_file is not None, but output_file is None.")
50
+
51
+ load_type = torch.bfloat16
52
+ if torch.cuda.is_available():
53
+ device = torch.device(0)
54
+ else:
55
+ raise ValueError("No GPU available.")
56
+
57
+
58
+ model = LlamaForCausalLM.from_pretrained(
59
+ args.model,
60
+ torch_dtype=load_type,
61
+ low_cpu_mem_usage=True,
62
+ device_map='auto',
63
+ quantization_config=None
64
+ )
65
+ tokenizer = LlamaTokenizer.from_pretrained(args.model)
66
+
67
+ model.eval()
68
+ with torch.no_grad():
69
+ if args.interactive:
70
+ while True:
71
+ raw_input_text = input("Input:")
72
+ if len(raw_input_text.strip())==0:
73
+ break
74
+ input_text = raw_input_text
75
+ input_text = tokenizer(input_text,return_tensors="pt")
76
+
77
+ generation_output = model.generate(
78
+ input_ids = input_text["input_ids"].to(device),
79
+ attention_mask = input_text['attention_mask'].to(device),
80
+ eos_token_id=tokenizer.eos_token_id,
81
+ pad_token_id=tokenizer.pad_token_id,
82
+ generation_config = generation_config,
83
+ output_attentions=False
84
+ )
85
+ s = generation_output[0]
86
+ output = tokenizer.decode(s,skip_special_tokens=True)
87
+ print("Output:",output)
88
+ print("\n")
89
+ else:
90
+ outputs=[]
91
+ with open(args.input_file, 'r') as f:
92
+ examples =f.read().splitlines()
93
+ print("Start generating...")
94
+ for index, example in tqdm(enumerate(examples),total=len(examples)):
95
+ input_text = tokenizer(example,return_tensors="pt") #add_special_tokens=False ?
96
+
97
+ generation_output = model.generate(
98
+ input_ids = input_text["input_ids"].to(device),
99
+ attention_mask = input_text['attention_mask'].to(device),
100
+ eos_token_id=tokenizer.eos_token_id,
101
+ pad_token_id=tokenizer.pad_token_id,
102
+ generation_config = generation_config
103
+ )
104
+ s = generation_output[0]
105
+ output = tokenizer.decode(s,skip_special_tokens=True)
106
+ outputs.append(output)
107
+ with open(args.output_file,'w') as f:
108
+ f.write("\n".join(outputs))
109
+ print("All the outputs have been saved in",args.output_file)
110
+ ```