Improve language tag
Browse filesHi! As the model is multilingual, this is a PR to add other languages than English to the language tag to improve the referencing. Note that 29 languages are announced in the README, but only 13 are explicitly listed. I was therefore only able to add these 13 languages.
README.md
CHANGED
|
@@ -1,170 +1,182 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: mit
|
| 3 |
-
language:
|
| 4 |
-
-
|
| 5 |
-
|
| 6 |
-
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 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 |
-
model
|
| 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 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
```
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
language:
|
| 4 |
+
- zho
|
| 5 |
+
- eng
|
| 6 |
+
- fra
|
| 7 |
+
- spa
|
| 8 |
+
- por
|
| 9 |
+
- deu
|
| 10 |
+
- ita
|
| 11 |
+
- rus
|
| 12 |
+
- jpn
|
| 13 |
+
- kor
|
| 14 |
+
- vie
|
| 15 |
+
- tha
|
| 16 |
+
- ara
|
| 17 |
+
tags:
|
| 18 |
+
- nlp
|
| 19 |
+
base_model: Qwen/Qwen2.5-0.5B
|
| 20 |
+
pipeline_tag: text-generation
|
| 21 |
+
---
|
| 22 |
+
|
| 23 |
+
# NuExtract-tiny-v1.5 by NuMind 🔥
|
| 24 |
+
|
| 25 |
+
NuExtract-tiny-v1.5 is a fine-tuning of [Qwen/Qwen2.5-0.5B](https://huggingface.co/Qwen/Qwen2.5-0.5B), 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).
|
| 26 |
+
To use the model, provide an input text and a JSON template describing the information you need to extract.
|
| 27 |
+
|
| 28 |
+
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.
|
| 29 |
+
|
| 30 |
+
We also provide a 3.8B version which is based on Phi-3.5-mini-instruct: [NuExtract-v1.5](https://huggingface.co/numind/NuExtract-v1.5)
|
| 31 |
+
|
| 32 |
+
Check out the [blog post](https://numind.ai/blog/nuextract-1-5---multilingual-infinite-context-still-small-and-better-than-gpt-4o).
|
| 33 |
+
|
| 34 |
+
Try the 3.8B model here: [Playground](https://huggingface.co/spaces/numind/NuExtract-v1.5)
|
| 35 |
+
|
| 36 |
+
⚠️ We recommend using NuExtract with a temperature at or very close to 0. Some inference frameworks, such as Ollama, use a default of 0.7 which is not well suited to pure extraction tasks.
|
| 37 |
+
|
| 38 |
+
## Benchmark
|
| 39 |
+
|
| 40 |
+
Zero-shot performance (English):
|
| 41 |
+
|
| 42 |
+
<p align="left">
|
| 43 |
+
<img src="english_bench.png" style="width: 600; height: auto;">
|
| 44 |
+
</p>
|
| 45 |
+
|
| 46 |
+
Few-shot fine-tuning:
|
| 47 |
+
|
| 48 |
+
<p align="left">
|
| 49 |
+
<img src="fewshot_bench.png" style="width: 750; height: auto;">
|
| 50 |
+
</p>
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
## Usage
|
| 54 |
+
|
| 55 |
+
To use the model:
|
| 56 |
+
|
| 57 |
+
```python
|
| 58 |
+
import json
|
| 59 |
+
import torch
|
| 60 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 61 |
+
|
| 62 |
+
def predict_NuExtract(model, tokenizer, texts, template, batch_size=1, max_length=10_000, max_new_tokens=4_000):
|
| 63 |
+
template = json.dumps(json.loads(template), indent=4)
|
| 64 |
+
prompts = [f"""<|input|>\n### Template:\n{template}\n### Text:\n{text}\n\n<|output|>""" for text in texts]
|
| 65 |
+
|
| 66 |
+
outputs = []
|
| 67 |
+
with torch.no_grad():
|
| 68 |
+
for i in range(0, len(prompts), batch_size):
|
| 69 |
+
batch_prompts = prompts[i:i+batch_size]
|
| 70 |
+
batch_encodings = tokenizer(batch_prompts, return_tensors="pt", truncation=True, padding=True, max_length=max_length).to(model.device)
|
| 71 |
+
|
| 72 |
+
pred_ids = model.generate(**batch_encodings, max_new_tokens=max_new_tokens)
|
| 73 |
+
outputs += tokenizer.batch_decode(pred_ids, skip_special_tokens=True)
|
| 74 |
+
|
| 75 |
+
return [output.split("<|output|>")[1] for output in outputs]
|
| 76 |
+
|
| 77 |
+
model_name = "numind/NuExtract-tiny-v1.5"
|
| 78 |
+
device = "cuda"
|
| 79 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, trust_remote_code=True).to(device).eval()
|
| 80 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 81 |
+
|
| 82 |
+
text = """We introduce Mistral 7B, a 7–billion-parameter language model engineered for
|
| 83 |
+
superior performance and efficiency. Mistral 7B outperforms the best open 13B
|
| 84 |
+
model (Llama 2) across all evaluated benchmarks, and the best released 34B
|
| 85 |
+
model (Llama 1) in reasoning, mathematics, and code generation. Our model
|
| 86 |
+
leverages grouped-query attention (GQA) for faster inference, coupled with sliding
|
| 87 |
+
window attention (SWA) to effectively handle sequences of arbitrary length with a
|
| 88 |
+
reduced inference cost. We also provide a model fine-tuned to follow instructions,
|
| 89 |
+
Mistral 7B – Instruct, that surpasses Llama 2 13B – chat model both on human and
|
| 90 |
+
automated benchmarks. Our models are released under the Apache 2.0 license.
|
| 91 |
+
Code: <https://github.com/mistralai/mistral-src>
|
| 92 |
+
Webpage: <https://mistral.ai/news/announcing-mistral-7b/>"""
|
| 93 |
+
|
| 94 |
+
template = """{
|
| 95 |
+
"Model": {
|
| 96 |
+
"Name": "",
|
| 97 |
+
"Number of parameters": "",
|
| 98 |
+
"Number of max token": "",
|
| 99 |
+
"Architecture": []
|
| 100 |
+
},
|
| 101 |
+
"Usage": {
|
| 102 |
+
"Use case": [],
|
| 103 |
+
"Licence": ""
|
| 104 |
+
}
|
| 105 |
+
}"""
|
| 106 |
+
|
| 107 |
+
prediction = predict_NuExtract(model, tokenizer, [text], template)[0]
|
| 108 |
+
print(prediction)
|
| 109 |
+
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
Sliding window prompting:
|
| 113 |
+
|
| 114 |
+
```python
|
| 115 |
+
import json
|
| 116 |
+
|
| 117 |
+
MAX_INPUT_SIZE = 20_000
|
| 118 |
+
MAX_NEW_TOKENS = 6000
|
| 119 |
+
|
| 120 |
+
def clean_json_text(text):
|
| 121 |
+
text = text.strip()
|
| 122 |
+
text = text.replace("\#", "#").replace("\&", "&")
|
| 123 |
+
return text
|
| 124 |
+
|
| 125 |
+
def predict_chunk(text, template, current, model, tokenizer):
|
| 126 |
+
current = clean_json_text(current)
|
| 127 |
+
|
| 128 |
+
input_llm = f"<|input|>\n### Template:\n{template}\n### Current:\n{current}\n### Text:\n{text}\n\n<|output|>" + "{"
|
| 129 |
+
input_ids = tokenizer(input_llm, return_tensors="pt", truncation=True, max_length=MAX_INPUT_SIZE).to("cuda")
|
| 130 |
+
output = tokenizer.decode(model.generate(**input_ids, max_new_tokens=MAX_NEW_TOKENS)[0], skip_special_tokens=True)
|
| 131 |
+
|
| 132 |
+
return clean_json_text(output.split("<|output|>")[1])
|
| 133 |
+
|
| 134 |
+
def split_document(document, window_size, overlap):
|
| 135 |
+
tokens = tokenizer.tokenize(document)
|
| 136 |
+
print(f"\tLength of document: {len(tokens)} tokens")
|
| 137 |
+
|
| 138 |
+
chunks = []
|
| 139 |
+
if len(tokens) > window_size:
|
| 140 |
+
for i in range(0, len(tokens), window_size-overlap):
|
| 141 |
+
print(f"\t{i} to {i + len(tokens[i:i + window_size])}")
|
| 142 |
+
chunk = tokenizer.convert_tokens_to_string(tokens[i:i + window_size])
|
| 143 |
+
chunks.append(chunk)
|
| 144 |
+
|
| 145 |
+
if i + len(tokens[i:i + window_size]) >= len(tokens):
|
| 146 |
+
break
|
| 147 |
+
else:
|
| 148 |
+
chunks.append(document)
|
| 149 |
+
print(f"\tSplit into {len(chunks)} chunks")
|
| 150 |
+
|
| 151 |
+
return chunks
|
| 152 |
+
|
| 153 |
+
def handle_broken_output(pred, prev):
|
| 154 |
+
try:
|
| 155 |
+
if all([(v in ["", []]) for v in json.loads(pred).values()]):
|
| 156 |
+
# if empty json, return previous
|
| 157 |
+
pred = prev
|
| 158 |
+
except:
|
| 159 |
+
# if broken json, return previous
|
| 160 |
+
pred = prev
|
| 161 |
+
|
| 162 |
+
return pred
|
| 163 |
+
|
| 164 |
+
def sliding_window_prediction(text, template, model, tokenizer, window_size=4000, overlap=128):
|
| 165 |
+
# split text into chunks of n tokens
|
| 166 |
+
tokens = tokenizer.tokenize(text)
|
| 167 |
+
chunks = split_document(text, window_size, overlap)
|
| 168 |
+
|
| 169 |
+
# iterate over text chunks
|
| 170 |
+
prev = template
|
| 171 |
+
for i, chunk in enumerate(chunks):
|
| 172 |
+
print(f"Processing chunk {i}...")
|
| 173 |
+
pred = predict_chunk(chunk, template, prev, model, tokenizer)
|
| 174 |
+
|
| 175 |
+
# handle broken output
|
| 176 |
+
pred = handle_broken_output(pred, prev)
|
| 177 |
+
|
| 178 |
+
# iterate
|
| 179 |
+
prev = pred
|
| 180 |
+
|
| 181 |
+
return pred
|
| 182 |
```
|