import torch
from transformers import AutoTokenizer, MistralForSequenceClassification, BitsAndBytesConfig
from peft import PeftModel
repo_id = "https://huggingface.co/ChrisLalk/mistral-7b-therapy-speaker-id"
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.float16,
)
tokenizer = AutoTokenizer.from_pretrained(repo_id, model_max_length=2048)
tokenizer.pad_token = tokenizer.eos_token
base_model = MistralForSequenceClassification.from_pretrained(
repo_id,
quantization_config=bnb_config,
device_map="auto",
torch_dtype=torch.float16,
)
base_model.config.pad_token_id = tokenizer.pad_token_id
model = PeftModel.from_pretrained(base_model, repo_id)
model.eval()
prompt = """Du bist eine sehr intelligente Psychiaterin.
Untersuche den Verlauf einer Therapiesitzung. Bitte ordne die folgende AUSSAGE im
Psychotherapietranskript dem Sprecher zu. Spricht der Therapeut (T) oder der Patient (P)?
Du erhaeltst den KONTEXT der AUSSAGE unten. Die AUSSAGE wird innerhalb des KONTEXTES
markiert ("--").
## AUSSAGE: Und wie fuehlen Sie sich dabei, wenn Sie daran zurueckdenken?
## KONTEXT:
...
-- Und wie fuehlen Sie sich dabei, wenn Sie daran zurueckdenken?
...
"""
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048).to(model.device)
with torch.no_grad():
logits = model(**inputs).logits
probs = torch.softmax(logits, dim=-1)[0]
pred = id2label[int(probs.argmax())]
print(pred, probs.tolist())