Views
No views yet
1from peft import AutoPeftModelForCausalLM
2from transformers import AutoTokenizer, pipeline
3
4# Load the model
5model = AutoPeftModelForCausalLM.from_pretrained("Moritz-Pfeifer/financial-times-classification-llama-2-7b-v1.3")
6tokenizer = AutoTokenizer.from_pretrained("Moritz-Pfeifer/financial-times-classification-llama-2-7b-v1.3")
7
8# Create a prompt for the model
9def predict_text(test, model, tokenizer):
10 prompt = f"""
11 You are given an opinion about the Bank of England (BoE).
12 Analyze the sentiment of the opinon about the reputation of the Bank of England (BoE) enclosed in square brackets,
13 determine if it is positive, negative or other, and return the answer as the corresponding sentiment label
14 "positive" or "negative". If the opinion is not related, return "other".
15
16 [{test}] ="""
17 pipe = pipeline(task="text-generation",
18 model=model,
19 tokenizer=tokenizer,
20 max_new_tokens = 1,
21 temperature = 0.1,
22 )
23 result = pipe(prompt)
24 answer = result[0]['generated_text'].split("=")[-1]
25 # print(answer)
26 if "positive" in answer.lower():
27 return "positive"
28 elif "negative" in answer.lower():
29 return "negative"
30 else:
31 return "other"
32
33# Use the model
34input_text = 'The report, by Lord Justice Bingham, said the Bank failed to take appropriate action after receiving a series of warnings over many years that fraud was taking place at BCCI.'
35predict_text(input_text, model, tokenizer)