한국어 테이블 이미지에서 **테이블 구조(TableSchema JSON)**를 자동 추출하는 QLoRA 파인튜닝 어댑터입니다.
1from peft import PeftModel
2from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
3from qwen_vl_utils import process_vision_info
4
5# 이 리포는 LoRA 어댑터(weight)만 포함하고 있습니다.
6# base_model(Qwen/Qwen3-VL-8B-Instruct)은 아래 코드에서 자동으로 다운로드됩니다.
7
8# 베이스 모델 로드 (4-bit 양자화)
9from transformers import BitsAndBytesConfig
10bnb_config = BitsAndBytesConfig(
11 load_in_4bit=True,
12 bnb_4bit_quant_type="nf4",
13 bnb_4bit_compute_dtype="bfloat16",
14)
15
16model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
17 "Qwen/Qwen3-VL-8B-Instruct",
18 quantization_config=bnb_config,
19 device_map="auto",
20)
21
22# LoRA 어댑터 로드
23model = PeftModel.from_pretrained(model, "cywellai/tablescope-structure-extractor-8b")
24processor = AutoProcessor.from_pretrained("cywellai/tablescope-structure-extractor-8b")
25
26# 추론
27messages = [
28 {"role": "system", "content": "당신은 테이블 이미지에서 구조를 추출하는 전문가입니다. 주어진 테이블 이미지를 분석하여 TableSchema JSON을 생성하세요."},
29 {"role": "user", "content": [
30 {"type": "image", "image": "path/to/table.png"},
31 {"type": "text", "text": "이 테이블 이미지의 구조를 JSON으로 추출하세요."},
32 ]},
33]
34
35text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
36image_inputs, video_inputs = process_vision_info(messages)
37inputs = processor(text=[text], images=image_inputs, videos=video_inputs, return_tensors="pt").to(model.device)
38
39output_ids = model.generate(**inputs, max_new_tokens=4096)
40output = processor.batch_decode(output_ids[:, inputs.input_ids.shape[1]:], skip_special_tokens=True)[0]
41print(output) # TableSchema JSON
1{
2 "col_headers": [
3 {"labels": ["이름", "나이", "직급"], "spans": {}}
4 ],
5 "row_headers": [],
6 "data": [
7 [{"value": "김철수"}, {"value": "35"}, {"value": "대리"}],
8 [{"value": "이영희"}, {"value": "42"}, {"value": "과장"}]
9 ],
10 "merged_regions": []
11}