Views
No views yet
english_balanced_10k.jsonl subset)transformers and datasets libraries installed. You can install them using pip:pip install transformers datasets1from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, BitsAndBytesConfig
2import torch
3
4# Quantization configuration
5bnb_config = BitsAndBytesConfig(
6 load_in_4bit=True,
7 bnb_4bit_quant_type="nf4",
8 bnb_4bit_compute_dtype=torch.float16,
9 bnb_4bit_use_double_quant=True,
10)
11
12# System instructions for PII redaction
13system_instructions = """Replace the following types of personal information in the text below with '[REDACTED]': [FIRST_NAME_x], [CITY_x], [STATE_x]. Ensure that each type of information is replaced in a way that maintains the grammatical structure of the sentence. You should only return the new text with the relevant replacements made, without the original text or any additional annotations.
14Input:"""
15
16example_prompt = "My name is Clara and I live in Berkeley, California."
17
18# Load model function
19def load_model(repo, step):
20 model = AutoModelForCausalLM.from_pretrained(repo,
21 device_map="cuda:0",
22 trust_remote_code=True,
23 quantization_config=bnb_config,
24 adapter_kwargs={"subfolder": f"checkpoint-{step}"},
25 attn_implementation="flash_attention_2")
26 return model
27
28# Initialize tokenizer and model
29device = "cuda"
30tokenizer = AutoTokenizer.from_pretrained("google/gemma-1.1-2b-it", use_fast=True)
31
32# Apply chat template for input
33chat = [
34 {"role": "system", "content": system_instructions},
35 {"role": "user", "content": example_prompt},
36]
37
38inputs = tokenizer.apply_chat_template(chat, tokenize=False, return_tensors="pt", padding=True, truncation=False)
39model = load_model("UlrikKoren/PIIMask-EN", step=1159)
40outputs = model.generate(input_ids=inputs['input_ids'].to(device), max_new_tokens=2048)
41decoded_outputs = [tokenizer.decode(output, skip_special_tokens=False) for output in outputs]
42print(decoded_outputs[0])UlrikKoren/PIIMask-EN/tree/main/checkpoint-579UlrikKoren/PIIMask-EN/checkpoint-1159UlrikKoren/PIIMask-EN/checkpoint-1739UlrikKoren/PIIMask-EN/checkpoint-2316