Views
No views yet
John went to the store. He bought some milk. His wife Sarah was happy when he returned.
John went to the store. John bought some milk. John's wife Sarah was happy when John returned.
1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from peft import PeftModel
4
5# Load base model
6base_model = AutoModelForCausalLM.from_pretrained(
7 "openai/gpt-oss-20b",
8 torch_dtype=torch.bfloat16,
9 device_map="auto",
10)
11
12# Load LoRA adapter
13model = PeftModel.from_pretrained(base_model, "wjbmattingly/gpt-oss-20b-coref-resolution-lora")
14
15# Load tokenizer
16tokenizer = AutoTokenizer.from_pretrained("openai/gpt-oss-20b")
17
18system_prompt = """You are an expert at coreference resolution. Given a text containing pronouns and other referring expressions, rewrite the text replacing all pronouns with the full name of the entity they refer to.
19
20Keep the text otherwise identical - only replace pronouns with the names they refer to."""
21
22messages = [
23 {"role": "system", "content": system_prompt},
24 {"role": "user", "content": "John went to the store. He bought milk."},
25]
26
27prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
28inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
29
30with torch.no_grad():
31 outputs = model.generate(**inputs, max_new_tokens=512, do_sample=False)
32response = tokenizer.decode(outputs[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
33print(response)1import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3from peft import PeftModel
4
5# Load base model
6base_model = AutoModelForCausalLM.from_pretrained(
7 "openai/gpt-oss-20b",
8 torch_dtype=torch.bfloat16,
9 device_map="auto",
10)
11
12# Load and merge LoRA adapter
13model = PeftModel.from_pretrained(base_model, "wjbmattingly/gpt-oss-20b-coref-resolution-lora")
14model = model.merge_and_unload()
15
16# Now model is a regular transformers model with merged weights1# Step 1: Merge and save (run this once)
2import torch
3from transformers import AutoModelForCausalLM, AutoTokenizer
4from peft import PeftModel
5
6base_model = AutoModelForCausalLM.from_pretrained(
7 "openai/gpt-oss-20b",
8 torch_dtype=torch.float16,
9 device_map="cpu", # Use CPU to avoid GPU memory issues
10)
11model = PeftModel.from_pretrained(base_model, "wjbmattingly/gpt-oss-20b-coref-resolution-lora")
12model = model.merge_and_unload()
13
14# Save merged model
15model.save_pretrained("./gpt-oss-20b-coref-resolution-lora-merged", safe_serialization=True)
16tokenizer = AutoTokenizer.from_pretrained("openai/gpt-oss-20b")
17tokenizer.save_pretrained("./gpt-oss-20b-coref-resolution-lora-merged")1pip install mlx-lm
2
3# Convert to MLX (with quantization for smaller size)
4mlx_lm.convert --hf-path ./gpt-oss-20b-coref-resolution-lora-merged --mlx-path ./gpt-oss-20b-coref-resolution-lora-mlx -q
5
6# Generate text
7mlx_lm.generate --model ./gpt-oss-20b-coref-resolution-lora-mlx --prompt "Your text here"1from mlx_lm import load, generate
2
3model, tokenizer = load("./gpt-oss-20b-coref-resolution-lora-mlx")
4
5system_prompt = """You are an expert at coreference resolution. Given a text containing pronouns, rewrite the text replacing all pronouns with the full name of the entity they refer to."""
6
7# Format prompt according to the model's chat template
8prompt = f"<|system|>\n{system_prompt}<|end|>\n<|user|>\nJohn went to the store. He bought milk.<|end|>\n<|assistant|>\n"
9
10response = generate(model, tokenizer, prompt=prompt, max_tokens=512)
11print(response)