starride-teklia commited on
Commit
efde046
·
verified ·
1 Parent(s): 48f1314

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +112 -0
README.md ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ base_model: QWEN/Qwen3.5-9B
3
+ library_name: peft
4
+ tags:
5
+ - QWEN
6
+ - ner
7
+ - historical
8
+ - handwritten
9
+ language:
10
+ - fr
11
+ pipeline_tag: image-to-text
12
+ ---
13
+
14
+ # QWEN3.5 - DAI NER Nested Model
15
+
16
+ ## Introduction
17
+
18
+ This version of Qwen3.5-9B is specialized for NER on French parish records from the 16th-18th centuries.
19
+ Ref: https://redmine.teklia.com/issues/13357
20
+
21
+ ## Training
22
+
23
+ The model is a [Qwen3.5-9B](https://huggingface.co/Qwen/Qwen3.5-9B) fine-tuned for NER on French parish records using LoRA.
24
+
25
+ Parameters:
26
+ - Image width: 1500 pixels
27
+ - LoRa rank: 8
28
+ - LoRa alpha: 32
29
+ - Epochs: 10 (about 4k steps)
30
+
31
+ ## Usage
32
+
33
+ Here we show a code snippet to show you how to use the model with `transformers` and `qwen_vl_utils`:
34
+
35
+ * Prediction script
36
+
37
+ ```python
38
+ from transformers import AutoProcessor, AutoModelForMultimodalLM
39
+ from qwen_vl_utils import process_vision_info
40
+ from pathlib import Path
41
+ import torch
42
+
43
+ # Load QWEN
44
+ model = AutoModelForMultimodalLM.from_pretrained(
45
+ "starride-teklia/DAI-NER-nested",
46
+ torch_dtype=torch.bfloat16,
47
+ attn_implementation="flash_attention_2",
48
+ device_map="auto",
49
+ )
50
+ processor = AutoProcessor.from_pretrained("starride-teklia/DAI-NER-nested")
51
+
52
+ # Prompt
53
+ SYSTEM = Path("system.txt").read_text() # Available in the model directory
54
+ IMAGE = "record.jpg" # https://europe.iiif.teklia.com/iiif/2/geneanet%2FArdennes_BMS%2F379692%2F00100.jpg/1425,793,1252,284/full/0/default.jpg
55
+ HTR_TRANSCRIPTION = "Mort de Pierre Soyer gardien de la redoutte du prez d'an...gne et ligne de cette frontiere\n\nL'an mil sept cens et neuf le vingt neuvième jour du mois de jeanvier a esté decedée en cette paroisse Pierre Soyer gardien au pre d'A[...]gne de la paroisse d'Aumont agee de quarante deux ans ou environs lequel a esté inhumé le trantième dudit mois dans le cimetier de cette paroisse ou nous lavons conduit avec les ceremonies accoustumée en présence avec les témoins qui ont signé avec nous\n\nJames Gileux T Stenva prb Mathieu Lallement."
56
+
57
+ messages = [
58
+ {
59
+ "role": "system",
60
+ "content": [
61
+ {
62
+ "type": "text",
63
+ "text": SYSTEM
64
+ }
65
+ ]
66
+ },
67
+ {
68
+ "role": "user",
69
+ "content": [
70
+ {
71
+ "type": "image",
72
+ "image": "record.jpg"
73
+ },
74
+ {
75
+ "type": "text",
76
+ "text": f"Convert this record in JSON. To help you, here is the transcription produced by an OCR model: {HTR_TRANSCRIPTION}"
77
+ },
78
+ ],
79
+ }
80
+ ]
81
+
82
+ # Preparation for inference
83
+ text = processor.apply_chat_template(
84
+ messages, tokenize=False, add_generation_prompt=True, enable_thinking=False
85
+ )
86
+ image_inputs, video_inputs = process_vision_info(messages)
87
+ inputs = processor(
88
+ text=[text],
89
+ images=image_inputs,
90
+ videos=video_inputs,
91
+ padding=True,
92
+ return_tensors="pt",
93
+ )
94
+ inputs = inputs.to("cuda")
95
+
96
+ # Inference: Generation of the output
97
+ generated_ids = model.generate(**inputs, max_new_tokens=1024)
98
+ generated_ids_trimmed = [
99
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
100
+ ]
101
+ output_text = processor.batch_decode(
102
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
103
+ )[0]
104
+ print(output_text)
105
+
106
+ ```
107
+
108
+ * Output
109
+
110
+ ```
111
+ {"événements": {"décès": {"année": "mil sept cens et neuf", "jour": "vingt neuvième", "mois": "janvier", "lieu": "en cette paroisse"}, "acte": {"jour": "trantième", "mois": "dudit mois", "lieu": "cette paroisse"}}, "individus": [{"rôle": "défunt", "attributes": {"prénom": "Pierre", "nom": "Soyer", "profession": "gardien au pre d'A[...]gne", "âge": "quarante deux ans ou environs"}}, {"rôle": "temoin_1", "attributes": {"prénom": "James", "nom": "Gileux"}}, {"rôle": "temoin_2", "attributes": {"prénom": "T", "nom": "Stenva"}}, {"rôle": "temoin_3", "attributes": {"prénom": "Mathieu", "nom": "Lallement"}}]}
112
+ ```