munish0838 commited on
Commit
a91a5db
·
verified ·
1 Parent(s): cc6abed

Upload README.md with huggingface_hub

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