Views
No views yet
1from transformers import AutoTokenizer, AutoModelForCausalLM
2tokenizer = AutoTokenizer.from_pretrained('quidangz/LlamaNER-8B-Instruct-ZeroShot')
3model = AutoModelForCausalLM.from_pretrained(
4 'quidangz/LlamaNER-8B-Instruct-ZeroShot',
5 torch_dtype="auto",
6 device_map="cuda",
7)
8
9if tokenizer.pad_token is None:
10 tokenizer.pad_token = tokenizer.eos_token
11 model.config.pad_token_id = model.config.eos_token_id
12
13user_prompt = """
14 Extract entities from the text **strictly using ONLY the provided Entity List** below and **MUST** strictly adhere to the output format.
15 Format output as '<entity tag>: <entity name>' and separated multiple entities by '|'. Return 'None' if no entities are identified.
16 Entity List: {ner_labels}
17 Text: {text}
18"""
19
20query = 'Hence, quercetin effectively reversed NAFLD symptoms by decreased triacyl glycerol accumulation, insulin resistance, inflammatory cytokine secretion and increased cellular antioxidants in OA induced hepatic steatosis in HepG2 cells.'
21ner_labels = ['Chemical']
22
23user_prompt = user_prompt.format(ner_labels=ner_labels, text=query)
24
25messages = [
26 {
27 "role": "system",
28 "content": "You are an expert in Named Entity Recognition (NER) task."
29 },
30 {
31 "role": "user",
32 "content": user_prompt
33 }
34]
35
36text = tokenizer.apply_chat_template(
37 messages,
38 tokenize=False,
39 add_generation_prompt=True
40 )
41
42model_inputs = tokenizer(text, return_tensors="pt").to(model.device)
43
44generated_ids = model.generate(
45 **model_inputs,
46 max_new_tokens=512,
47)
48
49generated_ids = [
50 output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
51]
52
53response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
54
55print(response) # Chemical: quercetin | Chemical: triacyl glycerol1@misc{LlamaNER-8B-Instruct-ZeroShot,
2 title={LlamaNER: An Large Language Model for Named Entity Recognition},
3 author={Qui Dang Ba},
4 year={2025},
5 publisher={Huggingface},
6}