Update README.md
Browse files
README.md
CHANGED
@@ -106,17 +106,82 @@ special_tokens:
|
|
106 |
[<img src="https://raw.githubusercontent.com/wandb/assets/main/wandb-github-badge-28.svg" alt="Visualize in Weights & Biases" width="200" height="32"/>](https://wandb.ai/sncds/ocr-ft/runs/rotjhntf)
|
107 |
# llama-3.1-8b-ocr-correction
|
108 |
|
109 |
-
This model is a fine-tuned
|
110 |
It achieves the following results on the evaluation set:
|
111 |
- Loss: 0.1901
|
112 |
|
113 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
-
More information needed
|
116 |
|
117 |
## Intended uses & limitations
|
118 |
|
119 |
-
|
|
|
|
|
|
|
|
|
120 |
|
121 |
## Training and evaluation data
|
122 |
|
|
|
106 |
[<img src="https://raw.githubusercontent.com/wandb/assets/main/wandb-github-badge-28.svg" alt="Visualize in Weights & Biases" width="200" height="32"/>](https://wandb.ai/sncds/ocr-ft/runs/rotjhntf)
|
107 |
# llama-3.1-8b-ocr-correction
|
108 |
|
109 |
+
This model is a qlora fine-tuned adapter for [meta-llama/Meta-Llama-3.1-8B](https://huggingface.co/meta-llama/Meta-Llama-3.1-8B) on the [pbevan11/synthetic-ocr-correction-gpt4o](https://huggingface.co/pbevan11/synthetic-ocr-correction-gpt4o) dataset.
|
110 |
It achieves the following results on the evaluation set:
|
111 |
- Loss: 0.1901
|
112 |
|
113 |
+
## Usage
|
114 |
+
|
115 |
+
First, download the model
|
116 |
+
|
117 |
+
```python
|
118 |
+
from peft import AutoPeftModelForCausalLM
|
119 |
+
from transformers import AutoTokenizer
|
120 |
+
model_id='pbevan11/llama-3.1-8b-ocr-correction'
|
121 |
+
model = AutoPeftModelForCausalLM.from_pretrained(model_id).cuda()
|
122 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
123 |
+
tokenizer.pad_token = tokenizer.eos_token
|
124 |
+
```
|
125 |
+
|
126 |
+
Then, construct the prompt template like so:
|
127 |
+
|
128 |
+
```python
|
129 |
+
def prompt(instruction, inp):
|
130 |
+
return f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
|
131 |
+
|
132 |
+
### Instruction:
|
133 |
+
{instruction}
|
134 |
+
|
135 |
+
### Input:
|
136 |
+
{inp}
|
137 |
+
|
138 |
+
### Response:
|
139 |
+
"""
|
140 |
+
|
141 |
+
def prompt_tok(instruction, inp, return_ids=False):
|
142 |
+
_p = prompt(instruction, inp)
|
143 |
+
input_ids = tokenizer(_p, return_tensors="pt", truncation=True).input_ids.cuda()
|
144 |
+
out_ids = model.generate(input_ids=input_ids, max_new_tokens=5000,
|
145 |
+
do_sample=False)
|
146 |
+
ids = out_ids.detach().cpu().numpy()
|
147 |
+
if return_ids: return out_ids
|
148 |
+
|
149 |
+
full_output = tokenizer.batch_decode(ids, skip_special_tokens=True)[0]
|
150 |
+
response_start = full_output.find("### Response:")
|
151 |
+
if response_start != -1:
|
152 |
+
return full_output[response_start + len("### Response:"):]
|
153 |
+
else:
|
154 |
+
return full_output[len(_p):]
|
155 |
+
```
|
156 |
+
|
157 |
+
Finally, you can get predictions like this:
|
158 |
+
|
159 |
+
```python
|
160 |
+
# model inputs
|
161 |
+
instruction = "You are an assistant that takes a piece of text that has been corrupted during OCR digitisation, and produce a corrected version of the same text."
|
162 |
+
inp = "Do Not Kule Oi't hy.er-l'rieed AjijqIi: imac - Analyst (fteuiers) Hcuiers - A | ) | ilf, <;/) in |) nter |iic . conic! deeiilf. l.o sell n lower-|)rieofl wersinn oi its Macintosh cornutor to nttinct ronsnnu-rs already euami'red ot its iPod music jiayo-r untl annoyoil. by sccnrit.y problems ivitJi Willtlows PCs , Piper.iaffray analyst. (Jcne Muster <aid on Tlinrtiday."
|
163 |
+
|
164 |
+
# print prediction
|
165 |
+
out = prompt_tok(instruction, inp)
|
166 |
+
print(out.replace('\\', ' '))
|
167 |
+
```
|
168 |
+
|
169 |
+
This will give you a prediction that looks like this:
|
170 |
+
|
171 |
+
```md
|
172 |
+
"Do Not Rule Out Lower-Priced Mac - Analyst (Reuters) Reuters - Apple Inc. may be considering a lower-priced version of its Macintosh computer to attract consumers already enamored of its iPod music player and annoyed by security problems with Windows PCs, PiperJaffray analyst Gene Munster said on Thursday."
|
173 |
+
```
|
174 |
+
|
175 |
+
Alternatively, you can play with this model on Replicate: [tbc](tbc)
|
176 |
|
|
|
177 |
|
178 |
## Intended uses & limitations
|
179 |
|
180 |
+
Reconstructions should not be taken as the truth, the model is likely to make some things up to fill in the gaps, and so some things may not be perfectly histoically acurate.
|
181 |
+
|
182 |
+
This model was intended to be used to restore historical documents that have been imperfectly digitalised using OCR.
|
183 |
+
|
184 |
+
This model could be used to transform poorly transcribed text into semi-synthetic training data, potentially unlocking millions of tokens of training data for future LLMs. The llama 3.1 license allows training on outputs, so this semi-synthetic data is perfectly legal to use.
|
185 |
|
186 |
## Training and evaluation data
|
187 |
|