MinaGabriel commited on
Commit
b510c45
·
verified ·
1 Parent(s): 5919559

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +70 -35
README.md CHANGED
@@ -11,52 +11,87 @@ tags:
11
  licence: license
12
  pipeline_tag: text-generation
13
  ---
 
 
14
 
15
- # Model Card for fol-parser-phi2-lora
16
-
17
- This model is a fine-tuned version of [microsoft/phi-2](https://huggingface.co/microsoft/phi-2).
18
- It has been trained using [TRL](https://github.com/huggingface/trl).
19
 
20
- ## Quick start
 
21
 
22
- ```python
23
- from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- question = "If you had a time machine, but could only go to the past or the future once and never return, which would you choose and why?"
26
- generator = pipeline("text-generation", model="None", device="cuda")
27
- output = generator([{"role": "user", "content": question}], max_new_tokens=128, return_full_text=False)[0]
28
- print(output["generated_text"])
29
- ```
 
 
 
 
 
30
 
31
- ## Training procedure
32
 
33
-
 
 
 
 
 
 
 
 
34
 
 
 
35
 
36
- This model was trained with SFT.
37
 
38
- ### Framework versions
39
 
40
- - PEFT 0.17.1
41
- - TRL: 0.23.1
42
- - Transformers: 4.57.0
43
- - Pytorch: 2.8.0+cu126
44
- - Datasets: 4.0.0
45
- - Tokenizers: 0.22.1
 
 
46
 
47
- ## Citations
48
 
 
49
 
 
 
 
 
 
50
 
51
- Cite TRL as:
52
-
53
- ```bibtex
54
- @misc{vonwerra2022trl,
55
- title = {{TRL: Transformer Reinforcement Learning}},
56
- author = {Leandro von Werra and Younes Belkada and Lewis Tunstall and Edward Beeching and Tristan Thrush and Nathan Lambert and Shengyi Huang and Kashif Rasul and Quentin Gallou{\'e}dec},
57
- year = 2020,
58
- journal = {GitHub repository},
59
- publisher = {GitHub},
60
- howpublished = {\url{https://github.com/huggingface/trl}}
61
- }
62
- ```
 
11
  licence: license
12
  pipeline_tag: text-generation
13
  ---
14
+
15
+ # code:
16
 
17
+ ```python
18
+ import torch
19
+ from transformers import AutoTokenizer, AutoModelForCausalLM
20
+ from peft import PeftModel
21
 
22
+ BASE_MODEL = "microsoft/phi-2"
23
+ ADAPTER_MODEL = "MinaGabriel/fol-parser-phi2-lora-adapter"
24
 
25
+ # tokenizer
26
+ tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
27
+
28
+ if tokenizer.pad_token is None:
29
+ tokenizer.pad_token = tokenizer.eos_token
30
+
31
+ base_model = AutoModelForCausalLM.from_pretrained(
32
+ BASE_MODEL,
33
+ torch_dtype=torch.float16,
34
+ device_map="auto",
35
+ )
36
+
37
+ base_model.config.pad_token_id = tokenizer.pad_token_id
38
+ base_model.generation_config.pad_token_id = tokenizer.pad_token_id
39
+ # attach the adapter
40
+ model = PeftModel.from_pretrained(
41
+ base_model,
42
+ ADAPTER_MODEL,
43
+ device_map="auto",
44
+ )
45
+ model.eval()
46
 
47
+ def generate(context: str, question: str, max_new_tokens: int = 300) -> str:
48
+ prompt = (
49
+ "<SYS>\nYou are a precise logic parser. Output [FOL] then [CONCLUSION_FOL].\n</SYS>\n"
50
+ "<USER>\n"
51
+ f"[CONTEXT]\n{context}\n\n"
52
+ f"[QUESTION]\n{question}\n\n"
53
+ "Produce the two blocks exactly as specified.\n"
54
+ "</USER>\n"
55
+ "<ASSISTANT>\n"
56
+ )
57
 
58
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
59
 
60
+ with torch.no_grad():
61
+ output_ids = model.generate(
62
+ **inputs,
63
+ max_new_tokens=max_new_tokens,
64
+ do_sample=False,
65
+ temperature=0.0,
66
+ eos_token_id=tokenizer.eos_token_id, # explicit
67
+ pad_token_id=tokenizer.pad_token_id # explicit
68
+ )
69
 
70
+ full_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
71
+ return full_text.split("<ASSISTANT>\n")[-1].strip()
72
 
73
+ ```
74
 
75
+ # Usage:
76
 
77
+ ```python
78
+ print(
79
+ generate(
80
+ context="Cats are animal. dogs are animal. human are not animal. animal are awsome",
81
+ question="dogs awsome?"
82
+ )
83
+ )
84
+ ```
85
 
 
86
 
87
+ # output:
88
 
89
+ [FOL]
90
+ cat(animal)
91
+ dog(animal)
92
+ ¬human(animal)
93
+ ∀x (animal(x) → awsome(x))
94
 
95
+ [CONCLUSION_FOL]
96
+ awesome(dog)
97
+ </ASSISTANT>