Instructions to use mamakos/CMClassifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use mamakos/CMClassifier with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("teknium/OpenHermes-2.5-Mistral-7B") model = PeftModel.from_pretrained(base_model, "mamakos/CMClassifier") - Notebooks
- Google Colab
- Kaggle
Upload closedmindeness.py
Browse files- closedmindeness.py +73 -0
closedmindeness.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 2 |
+
from peft import PeftModel
|
| 3 |
+
import torch
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
base_model_id = 'teknium/OpenHermes-2.5-Mistral-7B'
|
| 7 |
+
ft_model_id = 'mamakos/CMClassifier'
|
| 8 |
+
|
| 9 |
+
class classifier:
|
| 10 |
+
def __init__(self):
|
| 11 |
+
bnb_config = BitsAndBytesConfig(load_in_4bit=True,
|
| 12 |
+
bnb_4bit_quant_type='nf4',
|
| 13 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 14 |
+
bnb_4bit_use_double_quant=False)
|
| 15 |
+
|
| 16 |
+
base_model = AutoModelForCausalLM.from_pretrained(base_model_id,
|
| 17 |
+
quantization_config=bnb_config,
|
| 18 |
+
device_map={'': 'cuda'},
|
| 19 |
+
trust_remote_code=True)
|
| 20 |
+
|
| 21 |
+
self.tokenizer = AutoTokenizer.from_pretrained(base_model_id,
|
| 22 |
+
add_bos_token=True,
|
| 23 |
+
trust_remote_code=True)
|
| 24 |
+
|
| 25 |
+
self.ft_model = PeftModel.from_pretrained(base_model, ft_model_id)
|
| 26 |
+
self.ft_model.eval()
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def predict(self, texts):
|
| 30 |
+
if isinstance(texts, str):
|
| 31 |
+
texts = [texts]
|
| 32 |
+
|
| 33 |
+
prompts = [self.get_prompt(text) for text in texts]
|
| 34 |
+
|
| 35 |
+
probs = np.zeros(len(texts))
|
| 36 |
+
|
| 37 |
+
with torch.no_grad():
|
| 38 |
+
for i in range(len(texts)):
|
| 39 |
+
tokenized_prompt = self.tokenizer(prompts[i], return_tensors='pt').to('cuda')
|
| 40 |
+
|
| 41 |
+
output = self.ft_model.generate(**tokenized_prompt,
|
| 42 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
| 43 |
+
max_new_tokens=1,
|
| 44 |
+
do_sample=False,
|
| 45 |
+
return_dict_in_generate=True,
|
| 46 |
+
output_scores=True)
|
| 47 |
+
|
| 48 |
+
transition_score = self.ft_model.compute_transition_scores(output.sequences,
|
| 49 |
+
output.scores,
|
| 50 |
+
normalize_logits=True)
|
| 51 |
+
|
| 52 |
+
prob = np.exp(transition_score[0][0].cpu().numpy())
|
| 53 |
+
response = self.tokenizer.decode(output.sequences[:, -1][0])
|
| 54 |
+
if response == 'No':
|
| 55 |
+
prob = 1 - prob
|
| 56 |
+
probs[i] = prob
|
| 57 |
+
|
| 58 |
+
return probs
|
| 59 |
+
|
| 60 |
+
def get_prompt(self, text):
|
| 61 |
+
prompt = f'''<|im_start|>system
|
| 62 |
+
You are a helpful assistant.
|
| 63 |
+
<|im_end|>
|
| 64 |
+
<|im_start|>user
|
| 65 |
+
Classify this text as to whether it displays closed-mindedness: "{text}"
|
| 66 |
+
If this text displays closed-mindedness, your response must be "Yes".
|
| 67 |
+
If this text doesn\'t display closed-mindedness, your response must be "No".
|
| 68 |
+
<|im_end|>
|
| 69 |
+
<|im_start|>assistant
|
| 70 |
+
'''
|
| 71 |
+
|
| 72 |
+
return prompt
|
| 73 |
+
|