1from transformers import AutoModel, AutoTokenizer, AutoModelForCausalLM, LlamaForCausalLM, LlamaTokenizerFast
2from peft import PeftModel # 0.5.0
3
4# Load Models
5base_model = "NousResearch/Llama-2-13b-hf"
6peft_model = "oliverwang15/FinGPT_v33_Llama2_13B_Sentiment_Instruction_LoRA_FT_8bit"
7tokenizer = LlamaTokenizerFast.from_pretrained(base_model, trust_remote_code=True)
8tokenizer.pad_token = tokenizer.eos_token
9model = LlamaForCausalLM.from_pretrained(base_model, trust_remote_code=True, device_map = "cuda:0", load_in_8bit = True,)
10model = PeftModel.from_pretrained(model, peft_model)
11model = model.eval()
12
13# Make prompts
14prompt = [
15'''Instruction: What is the sentiment of this news? Please choose an answer from {negative/neutral/positive}
16Input: FINANCING OF ASPOCOMP 'S GROWTH Aspocomp is aggressively pursuing its growth strategy by increasingly focusing on technologically more demanding HDI printed circuit boards PCBs .
17Answer: ''',
18'''Instruction: What is the sentiment of this news? Please choose an answer from {negative/neutral/positive}
19Input: According to Gran , the company has no plans to move all production to Russia , although that is where the company is growing .
20Answer: ''',
21'''Instruction: What is the sentiment of this news? Please choose an answer from {negative/neutral/positive}
22Input: A tinyurl link takes users to a scamming site promising that users can earn thousands of dollars by becoming a Google ( NASDAQ : GOOG ) Cash advertiser .
23Answer: ''',
24]
25
26# Generate results
27tokens = tokenizer(prompt, return_tensors='pt', padding=True, max_length=512)
28res = model.generate(**tokens, max_length=512)
29res_sentences = [tokenizer.decode(i) for i in res]
30out_text = [o.split("Answer: ")[1] for o in res_sentences]
31
32# show results
33for sentiment in out_text:
34 print(sentiment)
35
36# Output:
37# positive
38# neutral
39# negative