Views
No views yet
1import json
2import re
3from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
4
5# Helper to expand excerpts in the answer
6def expand(document, text):
7 excerpt_pattern = r"<excerpt>(.*?)<\.\.\.>(.*?)</excerpt>"
8 matches = re.findall(excerpt_pattern, text, flags=re.DOTALL)
9 replacements = {}
10 for prefix, suffix in matches:
11 match = re.search(
12 re.escape(prefix) + r" (.*?) " + re.escape(suffix),
13 document,
14 flags=re.DOTALL,
15 )
16 try:
17 if match:
18 replacements[f"<excerpt>{prefix}<...>{suffix}</excerpt>"] = match.group(
19 0
20 )
21 else:
22 return None
23 except Exception:
24 return None
25 for old, new in replacements.items():
26 text = text.replace(old, new)
27 return text
28
29# Load tokenizer and model
30tokenizer = AutoTokenizer.from_pretrained('fineinstructions/template_instantiator', revision=None)
31tokenizer.padding_side = 'left'
32model = AutoModelForCausalLM.from_pretrained('fineinstructions/template_instantiator', revision=None)
33pipe = pipeline('text-generation', model=model, tokenizer=tokenizer, pad_token_id=tokenizer.pad_token_id, return_full_text=False)
34
35# Run inference to instantiate the instruction template and generate an answer
36inputs = [json.dumps({
37 "instruction_template": "...",
38 "document": "..."
39}, indent=2)]
40prompts = [tokenizer.apply_chat_template([{'role': 'user', 'content': i}], tokenize=False, add_generation_prompt=True) for i in inputs]
41generations = pipe(prompts, max_length=131072, truncation=True, temperature=None, top_p=None, do_sample=False)
42output = generations[0][0]['generated_text']
43output_json = json.loads()
44
45# Expand the answer
46output_json["answer"] = expand(document=inputs[0]["document"], text=output_json["answer"])
47
48# Print the output JSON
49print(output_json)
50
51##### Output JSON:
52# {
53# ..
54# }
55# @article{patel2026fineinstructions,
title={FineInstructions: Scaling Synthetic Instructions to Pre-Training Scale},
author={Patel, Ajay and Raffel, Colin and Callison-Burch, Chris},
journal={arXiv preprint arXiv:2601.22146},
year={2026},
archivePrefix={arXiv},
primaryClass={cs.CL},
doi={10.48550/arXiv.2601.22146}
}