初始化项目,由ModelHub XC社区提供模型
Model: numind/NuExtract-1.5-smol Source: Original Platform
This commit is contained in:
36
.gitattributes
vendored
Normal file
36
.gitattributes
vendored
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
*.7z filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.arrow filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.bin filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ftz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.gz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.h5 filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.joblib filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.model filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.npy filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.npz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.onnx filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.ot filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.parquet filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pb filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pickle filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pkl filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pt filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.pth filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.rar filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
||||||
|
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tar filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tflite filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.tgz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.wasm filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.xz filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.zip filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.zst filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
||||||
|
model.safetensors filter=lfs diff=lfs merge=lfs -text
|
||||||
172
README.md
Normal file
172
README.md
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
---
|
||||||
|
license: mit
|
||||||
|
language:
|
||||||
|
- multilingual
|
||||||
|
tags:
|
||||||
|
- nlp
|
||||||
|
base_model: HuggingFaceTB/SmolLM2-1.7B
|
||||||
|
new_version: numind/NuExtract3
|
||||||
|
pipeline_tag: text-generation
|
||||||
|
inference: true
|
||||||
|
---
|
||||||
|
|
||||||
|
# NuExtract-1.5-smol by NuMind 🔥
|
||||||
|
|
||||||
|
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).
|
||||||
|
|
||||||
|
To use the model, provide an input text and a JSON template describing the information you need to extract.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Check out the [blog post](https://numind.ai/blog/nuextract-1-5---multilingual-infinite-context-still-small-and-better-than-gpt-4o).
|
||||||
|
|
||||||
|
Try the 3.8B model here: [Playground](https://huggingface.co/spaces/numind/NuExtract-v1.5)
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
⚠️ 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.
|
||||||
|
|
||||||
|
## Benchmark
|
||||||
|
|
||||||
|
Zero-shot performance (English):
|
||||||
|
|
||||||
|
<p align="left">
|
||||||
|
<img src="english_bench.png" style="height: auto;">
|
||||||
|
</p>
|
||||||
|
|
||||||
|
Zero-shot performance (Multilingual):
|
||||||
|
|
||||||
|
<p align="left">
|
||||||
|
<img src="multilingual_bench.png" style="height: auto;">
|
||||||
|
</p>
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
To use the model:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import json
|
||||||
|
import torch
|
||||||
|
from transformers import AutoModelForCausalLM, AutoTokenizer
|
||||||
|
|
||||||
|
def predict_NuExtract(model, tokenizer, texts, template, batch_size=1, max_length=10_000, max_new_tokens=4_000):
|
||||||
|
template = json.dumps(json.loads(template), indent=4)
|
||||||
|
prompts = [f"""<|input|>\n### Template:\n{template}\n### Text:\n{text}\n\n<|output|>""" for text in texts]
|
||||||
|
|
||||||
|
outputs = []
|
||||||
|
with torch.no_grad():
|
||||||
|
for i in range(0, len(prompts), batch_size):
|
||||||
|
batch_prompts = prompts[i:i+batch_size]
|
||||||
|
batch_encodings = tokenizer(batch_prompts, return_tensors="pt", truncation=True, padding=True, max_length=max_length).to(model.device)
|
||||||
|
|
||||||
|
pred_ids = model.generate(**batch_encodings, max_new_tokens=max_new_tokens)
|
||||||
|
outputs += tokenizer.batch_decode(pred_ids, skip_special_tokens=True)
|
||||||
|
|
||||||
|
return [output.split("<|output|>")[1] for output in outputs]
|
||||||
|
|
||||||
|
model_name = "numind/NuExtract-1.5-smol"
|
||||||
|
device = "cuda"
|
||||||
|
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, trust_remote_code=True).to(device).eval()
|
||||||
|
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
||||||
|
|
||||||
|
text = """We introduce Mistral 7B, a 7–billion-parameter language model engineered for
|
||||||
|
superior performance and efficiency. Mistral 7B outperforms the best open 13B
|
||||||
|
model (Llama 2) across all evaluated benchmarks, and the best released 34B
|
||||||
|
model (Llama 1) in reasoning, mathematics, and code generation. Our model
|
||||||
|
leverages grouped-query attention (GQA) for faster inference, coupled with sliding
|
||||||
|
window attention (SWA) to effectively handle sequences of arbitrary length with a
|
||||||
|
reduced inference cost. We also provide a model fine-tuned to follow instructions,
|
||||||
|
Mistral 7B – Instruct, that surpasses Llama 2 13B – chat model both on human and
|
||||||
|
automated benchmarks. Our models are released under the Apache 2.0 license.
|
||||||
|
Code: <https://github.com/mistralai/mistral-src>
|
||||||
|
Webpage: <https://mistral.ai/news/announcing-mistral-7b/>"""
|
||||||
|
|
||||||
|
template = """{
|
||||||
|
"Model": {
|
||||||
|
"Name": "",
|
||||||
|
"Number of parameters": "",
|
||||||
|
"Number of max token": "",
|
||||||
|
"Architecture": []
|
||||||
|
},
|
||||||
|
"Usage": {
|
||||||
|
"Use case": [],
|
||||||
|
"Licence": ""
|
||||||
|
}
|
||||||
|
}"""
|
||||||
|
|
||||||
|
prediction = predict_NuExtract(model, tokenizer, [text], template)[0]
|
||||||
|
print(prediction)
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
Sliding window prompting:
|
||||||
|
|
||||||
|
```python
|
||||||
|
import json
|
||||||
|
|
||||||
|
MAX_INPUT_SIZE = 20_000
|
||||||
|
MAX_NEW_TOKENS = 6000
|
||||||
|
|
||||||
|
def clean_json_text(text):
|
||||||
|
text = text.strip()
|
||||||
|
text = text.replace("\#", "#").replace("\&", "&")
|
||||||
|
return text
|
||||||
|
|
||||||
|
def predict_chunk(text, template, current, model, tokenizer):
|
||||||
|
current = clean_json_text(current)
|
||||||
|
|
||||||
|
input_llm = f"<|input|>\n### Template:\n{template}\n### Current:\n{current}\n### Text:\n{text}\n\n<|output|>" + "{"
|
||||||
|
input_ids = tokenizer(input_llm, return_tensors="pt", truncation=True, max_length=MAX_INPUT_SIZE).to("cuda")
|
||||||
|
output = tokenizer.decode(model.generate(**input_ids, max_new_tokens=MAX_NEW_TOKENS)[0], skip_special_tokens=True)
|
||||||
|
|
||||||
|
return clean_json_text(output.split("<|output|>")[1])
|
||||||
|
|
||||||
|
def split_document(document, window_size, overlap):
|
||||||
|
tokens = tokenizer.tokenize(document)
|
||||||
|
print(f"\tLength of document: {len(tokens)} tokens")
|
||||||
|
|
||||||
|
chunks = []
|
||||||
|
if len(tokens) > window_size:
|
||||||
|
for i in range(0, len(tokens), window_size-overlap):
|
||||||
|
print(f"\t{i} to {i + len(tokens[i:i + window_size])}")
|
||||||
|
chunk = tokenizer.convert_tokens_to_string(tokens[i:i + window_size])
|
||||||
|
chunks.append(chunk)
|
||||||
|
|
||||||
|
if i + len(tokens[i:i + window_size]) >= len(tokens):
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
chunks.append(document)
|
||||||
|
print(f"\tSplit into {len(chunks)} chunks")
|
||||||
|
|
||||||
|
return chunks
|
||||||
|
|
||||||
|
def handle_broken_output(pred, prev):
|
||||||
|
try:
|
||||||
|
if all([(v in ["", []]) for v in json.loads(pred).values()]):
|
||||||
|
# if empty json, return previous
|
||||||
|
pred = prev
|
||||||
|
except:
|
||||||
|
# if broken json, return previous
|
||||||
|
pred = prev
|
||||||
|
|
||||||
|
return pred
|
||||||
|
|
||||||
|
def sliding_window_prediction(text, template, model, tokenizer, window_size=4000, overlap=128):
|
||||||
|
# split text into chunks of n tokens
|
||||||
|
tokens = tokenizer.tokenize(text)
|
||||||
|
chunks = split_document(text, window_size, overlap)
|
||||||
|
|
||||||
|
# iterate over text chunks
|
||||||
|
prev = template
|
||||||
|
for i, chunk in enumerate(chunks):
|
||||||
|
print(f"Processing chunk {i}...")
|
||||||
|
pred = predict_chunk(chunk, template, prev, model, tokenizer)
|
||||||
|
|
||||||
|
# handle broken output
|
||||||
|
pred = handle_broken_output(pred, prev)
|
||||||
|
|
||||||
|
# iterate
|
||||||
|
prev = pred
|
||||||
|
|
||||||
|
return pred
|
||||||
|
```
|
||||||
30
config.json
Normal file
30
config.json
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
{
|
||||||
|
"_name_or_path": "numind/NuExtract-1.5-smol",
|
||||||
|
"architectures": [
|
||||||
|
"LlamaForCausalLM"
|
||||||
|
],
|
||||||
|
"attention_bias": false,
|
||||||
|
"attention_dropout": 0.0,
|
||||||
|
"bos_token_id": 0,
|
||||||
|
"eos_token_id": 0,
|
||||||
|
"head_dim": 64,
|
||||||
|
"hidden_act": "silu",
|
||||||
|
"hidden_size": 2048,
|
||||||
|
"initializer_range": 0.02,
|
||||||
|
"intermediate_size": 8192,
|
||||||
|
"max_position_embeddings": 8192,
|
||||||
|
"mlp_bias": false,
|
||||||
|
"model_type": "llama",
|
||||||
|
"num_attention_heads": 32,
|
||||||
|
"num_hidden_layers": 24,
|
||||||
|
"num_key_value_heads": 32,
|
||||||
|
"pretraining_tp": 1,
|
||||||
|
"rms_norm_eps": 1e-05,
|
||||||
|
"rope_scaling": null,
|
||||||
|
"rope_theta": 130000,
|
||||||
|
"tie_word_embeddings": true,
|
||||||
|
"torch_dtype": "bfloat16",
|
||||||
|
"transformers_version": "4.45.2",
|
||||||
|
"use_cache": true,
|
||||||
|
"vocab_size": 49152
|
||||||
|
}
|
||||||
1
configuration.json
Normal file
1
configuration.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"framework": "pytorch", "task": "text-generation", "allow_remote": true}
|
||||||
BIN
english_bench.png
Normal file
BIN
english_bench.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 143 KiB |
7
generation_config.json
Normal file
7
generation_config.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"_from_model_config": true,
|
||||||
|
"bos_token_id": 0,
|
||||||
|
"eos_token_id": 0,
|
||||||
|
"pad_token_id": 0,
|
||||||
|
"transformers_version": "4.45.2"
|
||||||
|
}
|
||||||
48901
merges.txt
Normal file
48901
merges.txt
Normal file
File diff suppressed because it is too large
Load Diff
3
model.safetensors
Normal file
3
model.safetensors
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
version https://git-lfs.github.com/spec/v1
|
||||||
|
oid sha256:19a928d7e27479c06c6fb6f2e9c4276a78874b94eeb16fa59b51b25a34decb6a
|
||||||
|
size 3422777952
|
||||||
BIN
multilingual_bench.png
Normal file
BIN
multilingual_bench.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 179 KiB |
43
special_tokens_map.json
Normal file
43
special_tokens_map.json
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
{
|
||||||
|
"additional_special_tokens": [
|
||||||
|
"<|endoftext|>",
|
||||||
|
"<|im_start|>",
|
||||||
|
"<|im_end|>",
|
||||||
|
"<repo_name>",
|
||||||
|
"<reponame>",
|
||||||
|
"<file_sep>",
|
||||||
|
"<filename>",
|
||||||
|
"<gh_stars>",
|
||||||
|
"<issue_start>",
|
||||||
|
"<issue_comment>",
|
||||||
|
"<issue_closed>",
|
||||||
|
"<jupyter_start>",
|
||||||
|
"<jupyter_text>",
|
||||||
|
"<jupyter_code>",
|
||||||
|
"<jupyter_output>",
|
||||||
|
"<jupyter_script>",
|
||||||
|
"<empty_output>"
|
||||||
|
],
|
||||||
|
"bos_token": {
|
||||||
|
"content": "<|endoftext|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false
|
||||||
|
},
|
||||||
|
"eos_token": {
|
||||||
|
"content": "<|endoftext|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false
|
||||||
|
},
|
||||||
|
"pad_token": "<|endoftext|>",
|
||||||
|
"unk_token": {
|
||||||
|
"content": "<|endoftext|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false
|
||||||
|
}
|
||||||
|
}
|
||||||
244949
tokenizer.json
Normal file
244949
tokenizer.json
Normal file
File diff suppressed because it is too large
Load Diff
169
tokenizer_config.json
Normal file
169
tokenizer_config.json
Normal file
@@ -0,0 +1,169 @@
|
|||||||
|
{
|
||||||
|
"add_prefix_space": false,
|
||||||
|
"added_tokens_decoder": {
|
||||||
|
"0": {
|
||||||
|
"content": "<|endoftext|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"1": {
|
||||||
|
"content": "<|im_start|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"2": {
|
||||||
|
"content": "<|im_end|>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"3": {
|
||||||
|
"content": "<repo_name>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"4": {
|
||||||
|
"content": "<reponame>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"5": {
|
||||||
|
"content": "<file_sep>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"6": {
|
||||||
|
"content": "<filename>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"7": {
|
||||||
|
"content": "<gh_stars>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"8": {
|
||||||
|
"content": "<issue_start>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"9": {
|
||||||
|
"content": "<issue_comment>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"10": {
|
||||||
|
"content": "<issue_closed>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"11": {
|
||||||
|
"content": "<jupyter_start>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"12": {
|
||||||
|
"content": "<jupyter_text>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"13": {
|
||||||
|
"content": "<jupyter_code>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"14": {
|
||||||
|
"content": "<jupyter_output>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"15": {
|
||||||
|
"content": "<jupyter_script>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
},
|
||||||
|
"16": {
|
||||||
|
"content": "<empty_output>",
|
||||||
|
"lstrip": false,
|
||||||
|
"normalized": false,
|
||||||
|
"rstrip": false,
|
||||||
|
"single_word": false,
|
||||||
|
"special": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"additional_special_tokens": [
|
||||||
|
"<|endoftext|>",
|
||||||
|
"<|im_start|>",
|
||||||
|
"<|im_end|>",
|
||||||
|
"<repo_name>",
|
||||||
|
"<reponame>",
|
||||||
|
"<file_sep>",
|
||||||
|
"<filename>",
|
||||||
|
"<gh_stars>",
|
||||||
|
"<issue_start>",
|
||||||
|
"<issue_comment>",
|
||||||
|
"<issue_closed>",
|
||||||
|
"<jupyter_start>",
|
||||||
|
"<jupyter_text>",
|
||||||
|
"<jupyter_code>",
|
||||||
|
"<jupyter_output>",
|
||||||
|
"<jupyter_script>",
|
||||||
|
"<empty_output>"
|
||||||
|
],
|
||||||
|
"bos_token": "<|endoftext|>",
|
||||||
|
"clean_up_tokenization_spaces": false,
|
||||||
|
"eos_token": "<|endoftext|>",
|
||||||
|
"model_max_length": 8192,
|
||||||
|
"pad_token": "<|endoftext|>",
|
||||||
|
"padding_side": "left",
|
||||||
|
"tokenizer_class": "GPT2Tokenizer",
|
||||||
|
"unk_token": "<|endoftext|>",
|
||||||
|
"vocab_size": 49152
|
||||||
|
}
|
||||||
1
vocab.json
Normal file
1
vocab.json
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user