liamcripwell
commited on
Commit
•
bd7932e
1
Parent(s):
ef2cfd6
Update README.md
Browse files
README.md
CHANGED
@@ -1,199 +1,168 @@
|
|
1 |
---
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
---
|
5 |
|
6 |
-
#
|
7 |
|
8 |
-
|
9 |
|
|
|
10 |
|
|
|
11 |
|
12 |
-
|
13 |
|
14 |
-
|
15 |
|
16 |
-
|
17 |
|
18 |
-
|
19 |
|
20 |
-
-
|
21 |
-
- **Funded by [optional]:** [More Information Needed]
|
22 |
-
- **Shared by [optional]:** [More Information Needed]
|
23 |
-
- **Model type:** [More Information Needed]
|
24 |
-
- **Language(s) (NLP):** [More Information Needed]
|
25 |
-
- **License:** [More Information Needed]
|
26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
27 |
|
28 |
-
|
|
|
|
|
29 |
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
|
36 |
-
##
|
37 |
|
38 |
-
|
39 |
|
40 |
-
|
|
|
|
|
41 |
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
|
|
45 |
|
46 |
-
|
47 |
|
48 |
-
|
|
|
|
|
|
|
49 |
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
|
|
|
55 |
|
56 |
-
|
57 |
|
58 |
-
|
59 |
|
60 |
-
|
|
|
61 |
|
62 |
-
|
|
|
63 |
|
64 |
-
|
|
|
|
|
|
|
65 |
|
66 |
-
|
|
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
[
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
<!-- These are the evaluation metrics being used, ideally with a description of why. -->
|
124 |
-
|
125 |
-
[More Information Needed]
|
126 |
-
|
127 |
-
### Results
|
128 |
-
|
129 |
-
[More Information Needed]
|
130 |
-
|
131 |
-
#### Summary
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
## Model Examination [optional]
|
136 |
-
|
137 |
-
<!-- Relevant interpretability work for the model goes here -->
|
138 |
-
|
139 |
-
[More Information Needed]
|
140 |
-
|
141 |
-
## Environmental Impact
|
142 |
-
|
143 |
-
<!-- Total emissions (in grams of CO2eq) and additional considerations, such as electricity usage, go here. Edit the suggested text below accordingly -->
|
144 |
-
|
145 |
-
Carbon emissions can be estimated using the [Machine Learning Impact calculator](https://mlco2.github.io/impact#compute) presented in [Lacoste et al. (2019)](https://arxiv.org/abs/1910.09700).
|
146 |
-
|
147 |
-
- **Hardware Type:** [More Information Needed]
|
148 |
-
- **Hours used:** [More Information Needed]
|
149 |
-
- **Cloud Provider:** [More Information Needed]
|
150 |
-
- **Compute Region:** [More Information Needed]
|
151 |
-
- **Carbon Emitted:** [More Information Needed]
|
152 |
-
|
153 |
-
## Technical Specifications [optional]
|
154 |
-
|
155 |
-
### Model Architecture and Objective
|
156 |
-
|
157 |
-
[More Information Needed]
|
158 |
-
|
159 |
-
### Compute Infrastructure
|
160 |
-
|
161 |
-
[More Information Needed]
|
162 |
-
|
163 |
-
#### Hardware
|
164 |
-
|
165 |
-
[More Information Needed]
|
166 |
-
|
167 |
-
#### Software
|
168 |
-
|
169 |
-
[More Information Needed]
|
170 |
-
|
171 |
-
## Citation [optional]
|
172 |
-
|
173 |
-
<!-- If there is a paper or blog post introducing the model, the APA and Bibtex information for that should go in this section. -->
|
174 |
-
|
175 |
-
**BibTeX:**
|
176 |
-
|
177 |
-
[More Information Needed]
|
178 |
-
|
179 |
-
**APA:**
|
180 |
-
|
181 |
-
[More Information Needed]
|
182 |
-
|
183 |
-
## Glossary [optional]
|
184 |
-
|
185 |
-
<!-- If relevant, include terms and calculations in this section that can help readers understand the model or model card. -->
|
186 |
-
|
187 |
-
[More Information Needed]
|
188 |
-
|
189 |
-
## More Information [optional]
|
190 |
-
|
191 |
-
[More Information Needed]
|
192 |
-
|
193 |
-
## Model Card Authors [optional]
|
194 |
-
|
195 |
-
[More Information Needed]
|
196 |
-
|
197 |
-
## Model Card Contact
|
198 |
-
|
199 |
-
[More Information Needed]
|
|
|
1 |
---
|
2 |
+
license: mit
|
3 |
+
language:
|
4 |
+
- multilingual
|
5 |
+
tags:
|
6 |
+
- nlp
|
7 |
+
base_model: HuggingFaceTB/SmolLM2-1.7B
|
8 |
+
pipeline_tag: text-generation
|
9 |
+
inference: true
|
10 |
---
|
11 |
|
12 |
+
# NuExtract-v1.5 by NuMind 🔥
|
13 |
|
14 |
+
NuExtract-1.5-smol is a fine-tuning of Hugging Face's [SmolLM2-1.7B](https://huggingface.co/HuggingFaceTB/SmolLM2-1.7B), intended for structured information extraction. It uses the same training data as [NuExtract-1.5](https://huggingface.co/numind/NuExtract-1.5) and supports multiple languages, while being less than half the size (1.7B vs 3.8B).
|
15 |
|
16 |
+
To use the model, provide an input text and a JSON template describing the information you need to extract.
|
17 |
|
18 |
+
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.
|
19 |
|
20 |
+
Check out the [blog post](https://numind.ai/blog/nuextract-1-5---multilingual-infinite-context-still-small-and-better-than-gpt-4o).
|
21 |
|
22 |
+
Try the 3.8B model here: [Playground](https://huggingface.co/spaces/numind/NuExtract-v1.5)
|
23 |
|
24 |
+
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)
|
25 |
|
26 |
+
## Benchmark
|
27 |
|
28 |
+
Zero-shot performance (English):
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
+
<p align="left">
|
31 |
+
<img src="english_bench.png" style="height: auto;">
|
32 |
+
</p>
|
33 |
|
34 |
+
Zero-shot performance (Multilingual):
|
35 |
|
36 |
+
<p align="left">
|
37 |
+
<img src="multilingual_bench.png" style="height: auto;">
|
38 |
+
</p>
|
39 |
|
40 |
+
## Usage
|
41 |
|
42 |
+
To use the model:
|
43 |
|
44 |
+
```python
|
45 |
+
import json
|
46 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
47 |
|
48 |
+
def predict_NuExtract(model, tokenizer, texts, template, batch_size=1, max_length=10_000, max_new_tokens=4_000):
|
49 |
+
template = json.dumps(json.loads(template), indent=4)
|
50 |
+
prompts = [f"""<|input|>\n### Template:\n{template}\n### Text:\n{text}\n\n<|output|>""" for text in texts]
|
51 |
+
|
52 |
+
outputs = []
|
53 |
+
with torch.no_grad():
|
54 |
+
for i in range(0, len(prompts), batch_size):
|
55 |
+
batch_prompts = prompts[i:i+batch_size]
|
56 |
+
batch_encodings = tokenizer(batch_prompts, return_tensors="pt", truncation=True, padding=True, max_length=max_length).to(model.device)
|
57 |
|
58 |
+
pred_ids = model.generate(**batch_encodings, max_new_tokens=max_new_tokens)
|
59 |
+
outputs += tokenizer.batch_decode(pred_ids, skip_special_tokens=True)
|
60 |
|
61 |
+
return [output.split("<|output|>")[1] for output in outputs]
|
62 |
|
63 |
+
model_name = "numind/NuExtract-v1.5"
|
64 |
+
device = "cuda"
|
65 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, trust_remote_code=True).to(device).eval()
|
66 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
67 |
|
68 |
+
text = """We introduce Mistral 7B, a 7–billion-parameter language model engineered for
|
69 |
+
superior performance and efficiency. Mistral 7B outperforms the best open 13B
|
70 |
+
model (Llama 2) across all evaluated benchmarks, and the best released 34B
|
71 |
+
model (Llama 1) in reasoning, mathematics, and code generation. Our model
|
72 |
+
leverages grouped-query attention (GQA) for faster inference, coupled with sliding
|
73 |
+
window attention (SWA) to effectively handle sequences of arbitrary length with a
|
74 |
+
reduced inference cost. We also provide a model fine-tuned to follow instructions,
|
75 |
+
Mistral 7B – Instruct, that surpasses Llama 2 13B – chat model both on human and
|
76 |
+
automated benchmarks. Our models are released under the Apache 2.0 license.
|
77 |
+
Code: <https://github.com/mistralai/mistral-src>
|
78 |
+
Webpage: <https://mistral.ai/news/announcing-mistral-7b/>"""
|
79 |
|
80 |
+
template = """{
|
81 |
+
"Model": {
|
82 |
+
"Name": "",
|
83 |
+
"Number of parameters": "",
|
84 |
+
"Number of max token": "",
|
85 |
+
"Architecture": []
|
86 |
+
},
|
87 |
+
"Usage": {
|
88 |
+
"Use case": [],
|
89 |
+
"Licence": ""
|
90 |
+
}
|
91 |
+
}"""
|
92 |
|
93 |
+
prediction = predict_NuExtract(model, tokenizer, [text], template)[0]
|
94 |
+
print(prediction)
|
95 |
|
96 |
+
```
|
97 |
|
98 |
+
Sliding window prompting:
|
99 |
|
100 |
+
```python
|
101 |
+
import json
|
102 |
|
103 |
+
MAX_INPUT_SIZE = 20_000
|
104 |
+
MAX_NEW_TOKENS = 6000
|
105 |
|
106 |
+
def clean_json_text(text):
|
107 |
+
text = text.strip()
|
108 |
+
text = text.replace("\#", "#").replace("\&", "&")
|
109 |
+
return text
|
110 |
|
111 |
+
def predict_chunk(text, template, current, model, tokenizer):
|
112 |
+
current = clean_json_text(current)
|
113 |
|
114 |
+
input_llm = f"<|input|>\n### Template:\n{template}\n### Current:\n{current}\n### Text:\n{text}\n\n<|output|>" + "{"
|
115 |
+
input_ids = tokenizer(input_llm, return_tensors="pt", truncation=True, max_length=MAX_INPUT_SIZE).to("cuda")
|
116 |
+
output = tokenizer.decode(model.generate(**input_ids, max_new_tokens=MAX_NEW_TOKENS)[0], skip_special_tokens=True)
|
117 |
+
|
118 |
+
return clean_json_text(output.split("<|output|>")[1])
|
119 |
+
|
120 |
+
def split_document(document, window_size, overlap):
|
121 |
+
tokens = tokenizer.tokenize(document)
|
122 |
+
print(f"\tLength of document: {len(tokens)} tokens")
|
123 |
+
|
124 |
+
chunks = []
|
125 |
+
if len(tokens) > window_size:
|
126 |
+
for i in range(0, len(tokens), window_size-overlap):
|
127 |
+
print(f"\t{i} to {i + len(tokens[i:i + window_size])}")
|
128 |
+
chunk = tokenizer.convert_tokens_to_string(tokens[i:i + window_size])
|
129 |
+
chunks.append(chunk)
|
130 |
+
|
131 |
+
if i + len(tokens[i:i + window_size]) >= len(tokens):
|
132 |
+
break
|
133 |
+
else:
|
134 |
+
chunks.append(document)
|
135 |
+
print(f"\tSplit into {len(chunks)} chunks")
|
136 |
+
|
137 |
+
return chunks
|
138 |
+
|
139 |
+
def handle_broken_output(pred, prev):
|
140 |
+
try:
|
141 |
+
if all([(v in ["", []]) for v in json.loads(pred).values()]):
|
142 |
+
# if empty json, return previous
|
143 |
+
pred = prev
|
144 |
+
except:
|
145 |
+
# if broken json, return previous
|
146 |
+
pred = prev
|
147 |
+
|
148 |
+
return pred
|
149 |
+
|
150 |
+
def sliding_window_prediction(text, template, model, tokenizer, window_size=4000, overlap=128):
|
151 |
+
# split text into chunks of n tokens
|
152 |
+
tokens = tokenizer.tokenize(text)
|
153 |
+
chunks = split_document(text, window_size, overlap)
|
154 |
+
|
155 |
+
# iterate over text chunks
|
156 |
+
prev = template
|
157 |
+
for i, chunk in enumerate(chunks):
|
158 |
+
print(f"Processing chunk {i}...")
|
159 |
+
pred = predict_chunk(chunk, template, prev, model, tokenizer)
|
160 |
+
|
161 |
+
# handle broken output
|
162 |
+
pred = handle_broken_output(pred, prev)
|
163 |
+
|
164 |
+
# iterate
|
165 |
+
prev = pred
|
166 |
+
|
167 |
+
return pred
|
168 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|