xiaohei66 commited on
Commit
f623282
·
verified ·
1 Parent(s): f5ed513

update transformers usage

Browse files
Files changed (1) hide show
  1. README.md +42 -71
README.md CHANGED
@@ -169,49 +169,39 @@ Currently, the PaddleOCR-VL-1.5-0.9B model facilitates seamless inference via th
169
  ```python
170
  from PIL import Image
171
  import torch
172
- from transformers import AutoModelForCausalLM, AutoProcessor
 
 
173
 
174
  # ---- Settings ----
175
- model_path = "PaddlePaddle/PaddleOCR-VL"
176
  image_path = "test.png"
177
- task = "ocr" # Options: 'ocr' | 'table' | 'chart' | 'formula'
178
  # ------------------
179
 
180
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
181
-
182
- PROMPTS = {
183
- "ocr": "OCR:",
184
- "table": "Table Recognition:",
185
- "formula": "Formula Recognition:",
186
- "chart": "Chart Recognition:",
187
- }
188
-
189
  image = Image.open(image_path).convert("RGB")
190
-
191
- model = AutoModelForCausalLM.from_pretrained(
192
- model_path, trust_remote_code=True, torch_dtype=torch.bfloat16
193
- ).to(DEVICE).eval()
194
- processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
195
-
196
  messages = [
197
- {"role": "user",
198
- "content": [
 
199
  {"type": "image", "image": image},
200
  {"type": "text", "text": PROMPTS[task]},
201
  ]
202
  }
203
  ]
204
  inputs = processor.apply_chat_template(
205
- messages,
206
- tokenize=True,
207
- add_generation_prompt=True,
208
- return_dict=True,
209
- return_tensors="pt"
210
- ).to(DEVICE)
211
-
212
- outputs = model.generate(**inputs, max_new_tokens=1024)
213
- outputs = processor.batch_decode(outputs, skip_special_tokens=True)[0]
214
- print(outputs)
215
  ```
216
 
217
  <details>
@@ -223,60 +213,41 @@ pip install flash-attn --no-build-isolation
223
  ```
224
 
225
  ```python
226
- import torch
227
- from transformers import AutoModelForCausalLM, AutoProcessor
228
  from PIL import Image
 
 
 
 
229
 
230
  # ---- Settings ----
231
- model_path = "PaddlePaddle/PaddleOCR-VL"
232
  image_path = "test.png"
233
- task = "ocr" # ← change to "table" | "chart" | "formula"
234
  # ------------------
235
 
236
- DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
237
-
238
- model = AutoModelForCausalLM.from_pretrained(
239
- model_path,
240
- trust_remote_code=True,
241
- torch_dtype=torch.bfloat16,
242
- attn_implementation="flash_attention_2",
243
- ).to(dtype=torch.bfloat16, device=DEVICE).eval()
244
- processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
245
-
246
- PROMPTS = {
247
- "ocr": "OCR:",
248
- "table": "Table Recognition:",
249
- "chart": "Chart Recognition:",
250
- "formula": "Formula Recognition:",
251
- }
252
  messages = [
253
  {
254
- "role": "user",
255
  "content": [
256
- {"type": "image", "image": Image.open(image_path).convert("RGB")},
257
- {"type": "text", "text": PROMPTS[task]}
258
  ]
259
  }
260
  ]
261
-
262
  inputs = processor.apply_chat_template(
263
- messages,
264
- tokenize=True,
265
- add_generation_prompt=True,
266
- return_dict=True,
267
- return_tensors="pt"
268
- ).to(DEVICE)
269
-
270
- with torch.inference_mode():
271
- out = model.generate(
272
- **inputs,
273
- max_new_tokens=1024,
274
- do_sample=False,
275
- use_cache=True
276
- )
277
-
278
- outputs = processor.batch_decode(out, skip_special_tokens=True)[0]
279
- print(outputs)
280
  ```
281
 
282
  </details>
 
169
  ```python
170
  from PIL import Image
171
  import torch
172
+ from transformers import AutoProcessor, AutoModelForImageTextToText
173
+
174
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
175
 
176
  # ---- Settings ----
177
+ model_path = "PaddlePaddle/PaddleOCR-VL-1.5"
178
  image_path = "test.png"
179
+ task = "ocr" # Options: 'ocr' | 'table' | 'chart' | 'formula' | 'spotting'
180
  # ------------------
181
 
182
+ model = AutoModelForImageTextToText.from_pretrained(model_path, dtype="bfloat16").to(DEVICE).eval()
183
+ processor = AutoProcessor.from_pretrained(model_path)
 
 
 
 
 
 
 
184
  image = Image.open(image_path).convert("RGB")
 
 
 
 
 
 
185
  messages = [
186
+ {
187
+ "role": "user",
188
+ "content": [
189
  {"type": "image", "image": image},
190
  {"type": "text", "text": PROMPTS[task]},
191
  ]
192
  }
193
  ]
194
  inputs = processor.apply_chat_template(
195
+ messages,
196
+ add_generation_prompt=True,
197
+ tokenize=True,
198
+ return_dict=True,
199
+ return_tensors="pt",
200
+ ).to(model.device)
201
+
202
+ outputs = model.generate(**inputs, max_new_tokens=100)
203
+ result = processor.decode(outputs[0][inputs["input_ids"].shape[-1]:-1])
204
+ print(result)
205
  ```
206
 
207
  <details>
 
213
  ```
214
 
215
  ```python
 
 
216
  from PIL import Image
217
+ import torch
218
+ from transformers import AutoProcessor, AutoModelForImageTextToText
219
+
220
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
221
 
222
  # ---- Settings ----
223
+ model_path = "PaddlePaddle/PaddleOCR-VL-1.5"
224
  image_path = "test.png"
225
+ task = "ocr" # Options: 'ocr' | 'table' | 'chart' | 'formula' | 'spotting'
226
  # ------------------
227
 
228
+ model = AutoModelForImageTextToText.from_pretrained(model_path, dtype="bfloat16", attn_implementation="flash_attention_2").to(DEVICE).eval()
229
+ processor = AutoProcessor.from_pretrained(model_path)
230
+ image = Image.open(image_path).convert("RGB")
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  messages = [
232
  {
233
+ "role": "user",
234
  "content": [
235
+ {"type": "image", "image": image},
236
+ {"type": "text", "text": PROMPTS[task]},
237
  ]
238
  }
239
  ]
 
240
  inputs = processor.apply_chat_template(
241
+ messages,
242
+ add_generation_prompt=True,
243
+ tokenize=True,
244
+ return_dict=True,
245
+ return_tensors="pt",
246
+ ).to(model.device)
247
+
248
+ outputs = model.generate(**inputs, max_new_tokens=100)
249
+ result = processor.decode(outputs[0][inputs["input_ids"].shape[-1]:-1])
250
+ print(result)
 
 
 
 
 
 
 
251
  ```
252
 
253
  </details>