Views
No views yet
1import torch
2from transformers import AutoProcessor, Pix2StructForConditionalGeneration
3from PIL import Image
4import requests
5from io import BytesIO
6
7# Load model and processor
8device = "cuda" if torch.cuda.is_available() else "cpu"
9processor = AutoProcessor.from_pretrained("KennethTM/pix2struct-base-table2html")
10model = Pix2StructForConditionalGeneration.from_pretrained("KennethTM/pix2struct-base-table2html")
11model.to(device)
12model.eval()
13
14# Load example image from URL
15url = "https://huggingface.co/KennethTM/pix2struct-base-table2html/resolve/main/example_recog_1.jpg"
16response = requests.get(url)
17image = Image.open(BytesIO(response.content))
18
19# Run model inference
20encoding = processor(image, return_tensors="pt", max_patches=1024)
21with torch.inference_mode():
22 flattened_patches = encoding.pop("flattened_patches").to(device)
23 attention_mask = encoding.pop("attention_mask").to(device)
24 predictions = model.generate(flattened_patches=flattened_patches, attention_mask=attention_mask, max_new_tokens=1024)
25
26predictions_decoded = processor.tokenizer.batch_decode(predictions, skip_special_tokens=True)
27
28# Show predictions as text
29print(predictions_decoded[0])
1<table border="1" cellspacing="0">
2 <tr>
3 <th>
4 Rank
5 </th>
6 <th>
7 Lane
8 </th>
9 <th>
10 Name
11 </th>
12 <th>
13 Nationality
14 </th>
15 <th>
16 Time
17 </th>
18 <th>
19 Notes
20 </th>
21 </tr>
22 <tr>
23 <td>
24 </td>
25 <td>
26 4
27 </td>
28 <td>
29 Michael Phelps
30 </td>
31 <td>
32 United States
33 </td>
34 <td>
35 51.25
36 </td>
37 <td>
38 OR
39 </td>
40 </tr>
41 <tr>
42 <td>
43 </td>
44 <td>
45 3
46 </td>
47 <td>
48 Ian Crocker
49 </td>
50 <td>
51 United States
52 </td>
53 <td>
54 51.29
55 </td>
56 <td>
57 </td>
58 </tr>
59 <tr>
60 <td>
61 </td>
62 <td>
63 5
64 </td>
65 <td>
66 Andriy Serdinov
67 </td>
68 <td>
69 Ukraine
70 </td>
71 <td>
72 51.36
73 </td>
74 <td>
75 EU
76 </td>
77 </tr>
78 <tr>
79 <td>
80 4
81 </td>
82 <td>
83 1
84 </td>
85 <td>
86 Thomas Rupprath
87 </td>
88 <td>
89 Germany
90 </td>
91 <td>
92 52.27
93 </td>
94 <td>
95 </td>
96 </tr>
97 <tr>
98 <td>
99 5
100 </td>
101 <td>
102 6
103 </td>
104 <td>
105 Igor Marchenko
106 </td>
107 <td>
108 Russia
109 </td>
110 <td>
111 52.32
112 </td>
113 <td>
114 </td>
115 </tr>
116 <tr>
117 <td>
118 6
119 </td>
120 <td>
121 2
122 </td>
123 <td>
124 Gabriel Mangabeira
125 </td>
126 <td>
127 Brazil
128 </td>
129 <td>
130 52.34
131 </td>
132 <td>
133 </td>
134 </tr>
135 <tr>
136 <td>
137 7
138 </td>
139 <td>
140 8
141 </td>
142 <td>
143 Duje Draganja
144 </td>
145 <td>
146 Croatia
147 </td>
148 <td>
149 52.46
150 </td>
151 <td>
152 </td>
153 </tr>
154 <tr>
155 <td>
156 8
157 </td>
158 <td>
159 7
160 </td>
161 <td>
162 Geoff Huegill
163 </td>
164 <td>
165 Australia
166 </td>
167 <td>
168 52.56
169 </td>
170 <td>
171 </td>
172 </tr>
173</table>| Rank | Lane | Name | Nationality | Time | Notes |
|---|---|---|---|---|---|
| 4 | Michael Phelps | United States | 51.25 | OR | |
| 3 | Ian Crocker | United States | 51.29 | ||
| 5 | Andriy Serdinov | Ukraine | 51.36 | EU | |
| 4 | 1 | Thomas Rupprath | Germany | 52.27 | |
| 5 | 6 | Igor Marchenko | Russia | 52.32 | |
| 6 | 2 | Gabriel Mangabeira | Brazil | 52.34 | |
| 7 | 8 | Duje Draganja | Croatia | 52.46 | |
| 8 | 7 | Geoff Huegill | Australia | 52.56 |