Views
No views yet
pip install transformers torch accelerate Pillow qwen-vl-utils llmcompressor1from llmcompressor.transformers import SparseAutoModelForCausalLM
2from transformers import AutoProcessor
3from PIL import Image
4import torch
5
6model_id = "Glazkov/qwen2.5-vl-table-extraction-FP8-Dynamic"
7model = SparseAutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16)
8processor = AutoProcessor.from_pretrained(model_id)
9
10# Load image
11image = Image.open("table_image.png")
12
13# Create conversation
14conversation = [
15 {
16 "role": "user",
17 "content": [
18 {"type": "image", "image": image},
19 {"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'."},
20 ],
21 }
22]
23
24# Process and generate
25text = processor.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
26image_inputs, _ = process_vision_info(conversation)
27inputs = processor(text=[text], images=image_inputs, return_tensors="pt", padding=True)
28
29# Generate
30with torch.no_grad():
31 outputs = model.generate(**inputs, max_new_tokens=4096)
32
33response = processor.batch_decode(outputs[:, inputs["input_ids"].shape[1]:], skip_special_tokens=True)[0]
34print(response)1[
2 {
3 "parameter": "指标名称",
4 "date": "年份",
5 "value": "数值",
6 "measurement": "单位"
7 }
8]