Views
No views yet
"A101") appearing repeatedly. This may have caused the model to hallucinate common store names even when they're not present in the image.| Metric | Score |
|---|---|
| val_edit_distance | 0.112 |
1from transformers import Pix2StructForConditionalGeneration, Pix2StructProcessor
2from PIL import Image
3import torch
4
5model_id = "turgutguvercin/pix2struct-turkish-receipts"
6
7# Load model and processor
8model = Pix2StructForConditionalGeneration.from_pretrained(model_id)
9processor = Pix2StructProcessor.from_pretrained(model_id)
10
11# Put model in eval mode and move to device
12device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13model.to(device)
14model.eval()
15
16
17# Load your image (make sure it's in RGB)
18image_path = "test_1.jpg"
19image = Image.open(image_path).convert("RGB")
20
21# Preprocess image
22inputs = processor(images=image, return_tensors="pt").to(device)
23
24# Generate prediction
25with torch.no_grad():
26 outputs = model.generate(**inputs,
27 max_length=768,
28 early_stopping=True,
29 num_beams=1, # Reduce beam search for faster processing
30 do_sample=False,
31 use_cache=True,
32 pad_token_id=processor.tokenizer.pad_token_id,
33 eos_token_id=processor.tokenizer.eos_token_id,)
34
35# Decode tokens
36predicted_text = processor.decode(outputs[0], skip_special_tokens=True)
37print(predicted_text)
38# returns <s_store_name> SOK MARKETLER T.A.S.</s_store_name><s_tax_id> 81301318199</s_tax_id><s_date> 21/02/2025</s_date><s_menu><s_nm> MIS UHT SUT LAKTOZSU</s_nm><s_cnt> 1 x</s_cnt><s_price> 39,75</s_price><s_tax_rate> %01</s_tax_rate> <sep/><s_nm> LEZZCAFE SALEP 17 GR</s_nm><s_cnt> 4 x</s_cnt><s_price> 18,00</s_price><s_tax_rate> %01</s_tax_rate></s_menu><s_sub_total><s_tax_price> 0,57</s_tax_price></s_sub_total><s_total><s_total_price> 57,75</s_total_price></s_total>
39