munish0838
commited on
Commit
•
8ed91c1
1
Parent(s):
0bd8e69
Upload README.md with huggingface_hub
Browse files
README.md
ADDED
@@ -0,0 +1,181 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
---
|
3 |
+
|
4 |
+
license: mit
|
5 |
+
language:
|
6 |
+
- multilingual
|
7 |
+
tags:
|
8 |
+
- nlp
|
9 |
+
base_model: HuggingFaceTB/SmolLM2-1.7B
|
10 |
+
pipeline_tag: text-generation
|
11 |
+
inference: true
|
12 |
+
|
13 |
+
---
|
14 |
+
|
15 |
+
[![QuantFactory Banner](https://lh7-rt.googleusercontent.com/docsz/AD_4nXeiuCm7c8lEwEJuRey9kiVZsRn2W-b4pWlu3-X534V3YmVuVc2ZL-NXg2RkzSOOS2JXGHutDuyyNAUtdJI65jGTo8jT9Y99tMi4H4MqL44Uc5QKG77B0d6-JfIkZHFaUA71-RtjyYZWVIhqsNZcx8-OMaA?key=xt3VSDoCbmTY7o-cwwOFwQ)](https://hf.co/QuantFactory)
|
16 |
+
|
17 |
+
|
18 |
+
# QuantFactory/NuExtract-1.5-smol-GGUF
|
19 |
+
This is quantized version of [numind/NuExtract-1.5-smol](https://huggingface.co/numind/NuExtract-1.5-smol) created using llama.cpp
|
20 |
+
|
21 |
+
# Original Model Card
|
22 |
+
|
23 |
+
|
24 |
+
# NuExtract-1.5-smol by NuMind 🔥
|
25 |
+
|
26 |
+
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).
|
27 |
+
|
28 |
+
To use the model, provide an input text and a JSON template describing the information you need to extract.
|
29 |
+
|
30 |
+
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.
|
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 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)
|
37 |
+
|
38 |
+
## Benchmark
|
39 |
+
|
40 |
+
Zero-shot performance (English):
|
41 |
+
|
42 |
+
<p align="left">
|
43 |
+
<img src="english_bench.png" style="height: auto;">
|
44 |
+
</p>
|
45 |
+
|
46 |
+
Zero-shot performance (Multilingual):
|
47 |
+
|
48 |
+
<p align="left">
|
49 |
+
<img src="multilingual_bench.png" style="height: auto;">
|
50 |
+
</p>
|
51 |
+
|
52 |
+
## Usage
|
53 |
+
|
54 |
+
To use the model:
|
55 |
+
|
56 |
+
```python
|
57 |
+
import json
|
58 |
+
import torch
|
59 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
60 |
+
|
61 |
+
def predict_NuExtract(model, tokenizer, texts, template, batch_size=1, max_length=10_000, max_new_tokens=4_000):
|
62 |
+
template = json.dumps(json.loads(template), indent=4)
|
63 |
+
prompts = [f"""<|input|>\n### Template:\n{template}\n### Text:\n{text}\n\n<|output|>""" for text in texts]
|
64 |
+
|
65 |
+
outputs = []
|
66 |
+
with torch.no_grad():
|
67 |
+
for i in range(0, len(prompts), batch_size):
|
68 |
+
batch_prompts = prompts[i:i+batch_size]
|
69 |
+
batch_encodings = tokenizer(batch_prompts, return_tensors="pt", truncation=True, padding=True, max_length=max_length).to(model.device)
|
70 |
+
|
71 |
+
pred_ids = model.generate(**batch_encodings, max_new_tokens=max_new_tokens)
|
72 |
+
outputs += tokenizer.batch_decode(pred_ids, skip_special_tokens=True)
|
73 |
+
|
74 |
+
return [output.split("<|output|>")[1] for output in outputs]
|
75 |
+
|
76 |
+
model_name = "numind/NuExtract-1.5-smol"
|
77 |
+
device = "cuda"
|
78 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, trust_remote_code=True).to(device).eval()
|
79 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
80 |
+
|
81 |
+
text = """We introduce Mistral 7B, a 7–billion-parameter language model engineered for
|
82 |
+
superior performance and efficiency. Mistral 7B outperforms the best open 13B
|
83 |
+
model (Llama 2) across all evaluated benchmarks, and the best released 34B
|
84 |
+
model (Llama 1) in reasoning, mathematics, and code generation. Our model
|
85 |
+
leverages grouped-query attention (GQA) for faster inference, coupled with sliding
|
86 |
+
window attention (SWA) to effectively handle sequences of arbitrary length with a
|
87 |
+
reduced inference cost. We also provide a model fine-tuned to follow instructions,
|
88 |
+
Mistral 7B – Instruct, that surpasses Llama 2 13B – chat model both on human and
|
89 |
+
automated benchmarks. Our models are released under the Apache 2.0 license.
|
90 |
+
Code: <https://github.com/mistralai/mistral-src>
|
91 |
+
Webpage: <https://mistral.ai/news/announcing-mistral-7b/>"""
|
92 |
+
|
93 |
+
template = """{
|
94 |
+
"Model": {
|
95 |
+
"Name": "",
|
96 |
+
"Number of parameters": "",
|
97 |
+
"Number of max token": "",
|
98 |
+
"Architecture": []
|
99 |
+
},
|
100 |
+
"Usage": {
|
101 |
+
"Use case": [],
|
102 |
+
"Licence": ""
|
103 |
+
}
|
104 |
+
}"""
|
105 |
+
|
106 |
+
prediction = predict_NuExtract(model, tokenizer, [text], template)[0]
|
107 |
+
print(prediction)
|
108 |
+
|
109 |
+
```
|
110 |
+
|
111 |
+
Sliding window prompting:
|
112 |
+
|
113 |
+
```python
|
114 |
+
import json
|
115 |
+
|
116 |
+
MAX_INPUT_SIZE = 20_000
|
117 |
+
MAX_NEW_TOKENS = 6000
|
118 |
+
|
119 |
+
def clean_json_text(text):
|
120 |
+
text = text.strip()
|
121 |
+
text = text.replace("\#", "#").replace("\&", "&")
|
122 |
+
return text
|
123 |
+
|
124 |
+
def predict_chunk(text, template, current, model, tokenizer):
|
125 |
+
current = clean_json_text(current)
|
126 |
+
|
127 |
+
input_llm = f"<|input|>\n### Template:\n{template}\n### Current:\n{current}\n### Text:\n{text}\n\n<|output|>" + "{"
|
128 |
+
input_ids = tokenizer(input_llm, return_tensors="pt", truncation=True, max_length=MAX_INPUT_SIZE).to("cuda")
|
129 |
+
output = tokenizer.decode(model.generate(**input_ids, max_new_tokens=MAX_NEW_TOKENS)[0], skip_special_tokens=True)
|
130 |
+
|
131 |
+
return clean_json_text(output.split("<|output|>")[1])
|
132 |
+
|
133 |
+
def split_document(document, window_size, overlap):
|
134 |
+
tokens = tokenizer.tokenize(document)
|
135 |
+
print(f"\tLength of document: {len(tokens)} tokens")
|
136 |
+
|
137 |
+
chunks = []
|
138 |
+
if len(tokens) > window_size:
|
139 |
+
for i in range(0, len(tokens), window_size-overlap):
|
140 |
+
print(f"\t{i} to {i + len(tokens[i:i + window_size])}")
|
141 |
+
chunk = tokenizer.convert_tokens_to_string(tokens[i:i + window_size])
|
142 |
+
chunks.append(chunk)
|
143 |
+
|
144 |
+
if i + len(tokens[i:i + window_size]) >= len(tokens):
|
145 |
+
break
|
146 |
+
else:
|
147 |
+
chunks.append(document)
|
148 |
+
print(f"\tSplit into {len(chunks)} chunks")
|
149 |
+
|
150 |
+
return chunks
|
151 |
+
|
152 |
+
def handle_broken_output(pred, prev):
|
153 |
+
try:
|
154 |
+
if all([(v in ["", []]) for v in json.loads(pred).values()]):
|
155 |
+
# if empty json, return previous
|
156 |
+
pred = prev
|
157 |
+
except:
|
158 |
+
# if broken json, return previous
|
159 |
+
pred = prev
|
160 |
+
|
161 |
+
return pred
|
162 |
+
|
163 |
+
def sliding_window_prediction(text, template, model, tokenizer, window_size=4000, overlap=128):
|
164 |
+
# split text into chunks of n tokens
|
165 |
+
tokens = tokenizer.tokenize(text)
|
166 |
+
chunks = split_document(text, window_size, overlap)
|
167 |
+
|
168 |
+
# iterate over text chunks
|
169 |
+
prev = template
|
170 |
+
for i, chunk in enumerate(chunks):
|
171 |
+
print(f"Processing chunk {i}...")
|
172 |
+
pred = predict_chunk(chunk, template, prev, model, tokenizer)
|
173 |
+
|
174 |
+
# handle broken output
|
175 |
+
pred = handle_broken_output(pred, prev)
|
176 |
+
|
177 |
+
# iterate
|
178 |
+
prev = pred
|
179 |
+
|
180 |
+
return pred
|
181 |
+
```
|