-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_to_axolotl_vl.py
More file actions
128 lines (102 loc) · 3.78 KB
/
convert_to_axolotl_vl.py
File metadata and controls
128 lines (102 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import os
import json
import shutil
from pathlib import Path
# ---------------- CONFIG ----------------
INPUT_DIR = "out" # your raw data folder
OUTPUT_DIR = "dataset" # your target data folder
IMAGES_DIR = os.path.join(OUTPUT_DIR, "images")
OUTPUT_JSONL = os.path.join(OUTPUT_DIR, "train.jsonl")
INSTRUCTION_TEXT = (
"Extract all form fields from this document and return the result as structured JSON. "
"Return ONLY valid JSON. Do not include explanations."
)
# Optional key normalization
KEY_MAP = {
"geb": "birth_date",
"strasse": "street",
"telefon": "phone",
"beruf": "profession",
"verart": "insurance",
"warten": "reason_for_visit",
"alergia": "allergies",
"erkrankung": "conditions"
}
# ---------------- HELPERS ----------------
def normalize_key(key: str) -> str:
return KEY_MAP.get(key, key)
def extract_fields(annotation_json: dict) -> dict:
result = {}
for field in annotation_json.get("fields", []):
name = field.get("name")
value = field.get("value")
if value is None:
continue
key = normalize_key(name)
# Checkbox groups → arrays
if isinstance(value, list):
result[key] = value
else:
result[key] = value
return result
# ---------------- MAIN ----------------
def main():
input_path = Path(INPUT_DIR)
output_path = Path(OUTPUT_DIR)
images_path = Path(IMAGES_DIR)
if not input_path.exists():
raise FileNotFoundError(f"Input folder not found: {INPUT_DIR}")
# Create output folders
output_path.mkdir(exist_ok=True)
images_path.mkdir(exist_ok=True)
samples_written = 0
with open(OUTPUT_JSONL, "w", encoding="utf-8") as jsonl_file:
for json_file in input_path.glob("*.json"):
base_name = json_file.stem
image_file = input_path / f"{base_name}.png"
if not image_file.exists():
print(f"[WARN] Image missing for {json_file.name}, skipping.")
continue
# Load annotation
with open(json_file, "r", encoding="utf-8") as f:
annotation = json.load(f)
extracted_data = extract_fields(annotation)
# Copy image
target_image_path = images_path / image_file.name
shutil.copy(image_file, target_image_path)
# Build Axolotl / Qwen2.5-VL sample
sample = {
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"image": f"images/{image_file.name}"
},
{
"type": "text",
"text": INSTRUCTION_TEXT
}
]
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": json.dumps(
extracted_data,
ensure_ascii=False,
indent=2
)
}
]
}
]
}
jsonl_file.write(json.dumps(sample, ensure_ascii=False) + "\n")
samples_written += 1
print(f"✅ Done. {samples_written} samples written to {OUTPUT_JSONL}")
if __name__ == "__main__":
main()