1from transformers import AutoModel, AutoTokenizer, AutoModelForCausalLM, LlamaForCausalLM, LlamaTokenizerFast
2from peft import PeftModel # 0.5.034# Load Models5base_model ="internlm/internlm-20b"6peft_model ="FinGPT/fingpt-sentiment_internlm-20b_lora"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()1213# Make prompts14prompt =[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]2526# Generate results27tokens = 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]3132# show results33for sentiment in out_text:34print(sentiment)3536# Output: 37# positive38# neutral39# negative