Views
No views yet
familorujov/llama-3.2-3B-financial-sentiment-finetuned is a Llama 3.2 3B Instruct model fine tuned for financial sentiment classification using Unsloth QLoRA.PositiveNegativeNeutraltransformers without separately loading LoRA adapters.sbhatti/financial-sentiment-analysis.1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3
4model_id = "familorujov/llama-3.2-3B-financial-sentiment-finetuned"
5
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7model = AutoModelForCausalLM.from_pretrained(
8 model_id,
9 device_map="auto",
10 torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
11)
12
13SYSTEM_PROMPT = (
14 "You are a financial sentiment analyst. "
15 "Given the financial text, reply with exactly one word: Positive, Negative, or Neutral."
16)
17
18def classify_fin_sentiment(text: str, max_new_tokens: int = 4):
19 messages = [
20 {"role": "system", "content": SYSTEM_PROMPT},
21 {"role": "user", "content": text},
22 ]
23 inputs = tokenizer.apply_chat_template(
24 messages,
25 add_generation_prompt=True,
26 return_tensors="pt",
27 ).to(model.device)
28
29 with torch.no_grad():
30 out = model.generate(
31 inputs,
32 max_new_tokens=max_new_tokens,
33 do_sample=False,
34 temperature=0.0,
35 pad_token_id=tokenizer.eos_token_id,
36 )
37
38 gen = tokenizer.decode(out[0][inputs.shape[1]:], skip_special_tokens=True).strip()
39 first = gen.split()[0].replace(".", "").strip().capitalize()
40
41 if first not in {"Positive", "Negative", "Neutral"}:
42 first = "Neutral"
43
44 return first, gen
45
46label, raw = classify_fin_sentiment(
47 "The company beat earnings expectations and raised full year guidance."
48)
49print(label)
50print(raw)