RichardErkhov commited on
Commit
cd48913
·
verified ·
1 Parent(s): 7e43330

uploaded readme

Browse files
Files changed (1) hide show
  1. README.md +202 -0
README.md ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Quantization made by Richard Erkhov.
2
+
3
+ [Github](https://github.com/RichardErkhov)
4
+
5
+ [Discord](https://discord.gg/pvy7H8DZMG)
6
+
7
+ [Request more models](https://github.com/RichardErkhov/quant_request)
8
+
9
+
10
+ NuExtract-v1.5 - bnb 4bits
11
+ - Model creator: https://huggingface.co/numind/
12
+ - Original model: https://huggingface.co/numind/NuExtract-v1.5/
13
+
14
+
15
+
16
+
17
+ Original model description:
18
+ ---
19
+ license: mit
20
+ language:
21
+ - multilingual
22
+ tags:
23
+ - nlp
24
+ base_model: microsoft/Phi-3.5-mini-instruct
25
+ pipeline_tag: text-generation
26
+ ---
27
+
28
+ # NuExtract-v1.5 by NuMind 🔥
29
+
30
+ NuExtract-v1.5 is a fine-tuning of [Phi-3.5-mini-instruct](https://huggingface.co/microsoft/Phi-3.5-mini-instruct), trained on a private high-quality dataset for structured information extraction. It supports long documents and several languages (English, French, Spanish, German, Portuguese, and Italian).
31
+ To use the model, provide an input text and a JSON template describing the information you need to extract.
32
+
33
+ Note: This model is trained to prioritize pure extraction, so in most cases all text generated by the model is present as is in the original text.
34
+
35
+ Check out the [blog post](https://numind.ai/blog/nuextract-1-5---multilingual-infinite-context-still-small-and-better-than-gpt-4o).
36
+
37
+ Try it here: [Playground](https://huggingface.co/spaces/numind/NuExtract-v1.5)
38
+
39
+ We also provide a tiny (0.5B) version which is based on Qwen2.5-0.5B: [NuExtract-tiny-v1.5](https://huggingface.co/numind/NuExtract-tiny-v1.5)
40
+
41
+ ## Benchmark
42
+
43
+ Zero-shot performance (English):
44
+
45
+ <p align="left">
46
+ <img src="english_bench.png" style="height: auto;">
47
+ </p>
48
+
49
+ Zero-shot performance (Multilingual):
50
+
51
+ <p align="left">
52
+ <img src="multilingual_bench.png" style="height: auto;">
53
+ </p>
54
+
55
+ Long documents (8-10k tokens):
56
+
57
+ <p align="left">
58
+ <img src="8-10_long_context.png" style="height: auto;">
59
+ </p>
60
+
61
+ Very long documents (10-20k tokens):
62
+
63
+ <p align="left">
64
+ <img src="10-20_long_context.png" style="height: auto;">
65
+ </p>
66
+
67
+ Few-shot fine-tuning:
68
+
69
+ <p align="left">
70
+ <img src="fewshot_bench.png" style="height: auto;">
71
+ </p>
72
+
73
+ ## Usage
74
+
75
+ To use the model:
76
+
77
+ ```python
78
+ import json
79
+ from transformers import AutoModelForCausalLM, AutoTokenizer
80
+
81
+ def predict_NuExtract(model, tokenizer, texts, template, batch_size=1, max_length=10_000, max_new_tokens=4_000):
82
+ template = json.dumps(json.loads(template), indent=4)
83
+ prompts = [f"""<|input|>\n### Template:\n{template}\n### Text:\n{text}\n\n<|output|>""" for text in texts]
84
+
85
+ outputs = []
86
+ with torch.no_grad():
87
+ for i in range(0, len(prompts), batch_size):
88
+ batch_prompts = prompts[i:i+batch_size]
89
+ batch_encodings = tokenizer(batch_prompts, return_tensors="pt", truncation=True, padding=True, max_length=max_length).to(model.device)
90
+
91
+ pred_ids = model.generate(**batch_encodings, max_new_tokens=max_new_tokens)
92
+ outputs += tokenizer.batch_decode(pred_ids, skip_special_tokens=True)
93
+
94
+ return [output.split("<|output|>")[1] for output in outputs]
95
+
96
+ model_name = "numind/NuExtract-v1.5"
97
+ device = "cuda"
98
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, trust_remote_code=True).to(device).eval()
99
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
100
+
101
+ text = """We introduce Mistral 7B, a 7–billion-parameter language model engineered for
102
+ superior performance and efficiency. Mistral 7B outperforms the best open 13B
103
+ model (Llama 2) across all evaluated benchmarks, and the best released 34B
104
+ model (Llama 1) in reasoning, mathematics, and code generation. Our model
105
+ leverages grouped-query attention (GQA) for faster inference, coupled with sliding
106
+ window attention (SWA) to effectively handle sequences of arbitrary length with a
107
+ reduced inference cost. We also provide a model fine-tuned to follow instructions,
108
+ Mistral 7B – Instruct, that surpasses Llama 2 13B – chat model both on human and
109
+ automated benchmarks. Our models are released under the Apache 2.0 license.
110
+ Code: <https://github.com/mistralai/mistral-src>
111
+ Webpage: <https://mistral.ai/news/announcing-mistral-7b/>"""
112
+
113
+ template = """{
114
+ "Model": {
115
+ "Name": "",
116
+ "Number of parameters": "",
117
+ "Number of max token": "",
118
+ "Architecture": []
119
+ },
120
+ "Usage": {
121
+ "Use case": [],
122
+ "Licence": ""
123
+ }
124
+ }"""
125
+
126
+ prediction = predict_NuExtract(model, tokenizer, [text], template)[0]
127
+ print(prediction)
128
+
129
+ ```
130
+
131
+ Sliding window prompting:
132
+
133
+ ```python
134
+ import json
135
+
136
+ MAX_INPUT_SIZE = 20_000
137
+ MAX_NEW_TOKENS = 6000
138
+
139
+ def clean_json_text(text):
140
+ text = text.strip()
141
+ text = text.replace("\#", "#").replace("\&", "&")
142
+ return text
143
+
144
+ def predict_chunk(text, template, current, model, tokenizer):
145
+ current = clean_json_text(current)
146
+
147
+ input_llm = f"<|input|>\n### Template:\n{template}\n### Current:\n{current}\n### Text:\n{text}\n\n<|output|>" + "{"
148
+ input_ids = tokenizer(input_llm, return_tensors="pt", truncation=True, max_length=MAX_INPUT_SIZE).to("cuda")
149
+ output = tokenizer.decode(model.generate(**input_ids, max_new_tokens=MAX_NEW_TOKENS)[0], skip_special_tokens=True)
150
+
151
+ return clean_json_text(output.split("<|output|>")[1])
152
+
153
+ def split_document(document, window_size, overlap):
154
+ tokens = tokenizer.tokenize(document)
155
+ print(f"\tLength of document: {len(tokens)} tokens")
156
+
157
+ chunks = []
158
+ if len(tokens) > window_size:
159
+ for i in range(0, len(tokens), window_size-overlap):
160
+ print(f"\t{i} to {i + len(tokens[i:i + window_size])}")
161
+ chunk = tokenizer.convert_tokens_to_string(tokens[i:i + window_size])
162
+ chunks.append(chunk)
163
+
164
+ if i + len(tokens[i:i + window_size]) >= len(tokens):
165
+ break
166
+ else:
167
+ chunks.append(document)
168
+ print(f"\tSplit into {len(chunks)} chunks")
169
+
170
+ return chunks
171
+
172
+ def handle_broken_output(pred, prev):
173
+ try:
174
+ if all([(v in ["", []]) for v in json.loads(pred).values()]):
175
+ # if empty json, return previous
176
+ pred = prev
177
+ except:
178
+ # if broken json, return previous
179
+ pred = prev
180
+
181
+ return pred
182
+
183
+ def sliding_window_prediction(text, template, model, tokenizer, window_size=4000, overlap=128):
184
+ # split text into chunks of n tokens
185
+ tokens = tokenizer.tokenize(text)
186
+ chunks = split_document(text, window_size, overlap)
187
+
188
+ # iterate over text chunks
189
+ prev = template
190
+ for i, chunk in enumerate(chunks):
191
+ print(f"Processing chunk {i}...")
192
+ pred = predict_chunk(chunk, template, prev, model, tokenizer)
193
+
194
+ # handle broken output
195
+ pred = handle_broken_output(pred, prev)
196
+
197
+ # iterate
198
+ prev = pred
199
+
200
+ return pred
201
+ ```
202
+