Views
No views yet
pip install transformers torch accelerate Pillow qwen-vl-utils1from transformers import AutoModelForImageTextToText, AutoProcessor
2from PIL import Image
3import torch
4
5model_id = "Glazkov/qwen2.5-vl-table-extraction-ru-v0.1-bnb-4bit"
6model = AutoModelForImageTextToText.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16)
7processor = AutoProcessor.from_pretrained(model_id)
8
9# Load image
10image = Image.open("table_image.png")
11
12# Create conversation
13conversation = [
14 {
15 "role": "user",
16 "content": [
17 {"type": "image", "image": image},
18 {"type": "text", "text": "Extract the data from this table image and return it as a JSON array where each object has the keys: 'parameter', 'date', 'value', and 'measurement'."},
19 ],
20 }
21]
22
23# Process and generate
24text = processor.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
25image_inputs, _ = process_vision_info(conversation)
26inputs = processor(text=[text], images=image_inputs, return_tensors="pt", padding=True)
27
28# Generate
29with torch.no_grad():
30 outputs = model.generate(**inputs, max_new_tokens=4096)
31
32response = processor.batch_decode(outputs[:, inputs["input_ids"].shape[1]:], skip_special_tokens=True)[0]
33print(response)1[
2 {
3 "parameter": "指标名称",
4 "date": "年份",
5 "value": "数值",
6 "measurement": "单位"
7 }
8]