Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,36 @@
|
|
1 |
-
---
|
2 |
-
license: apache-2.0
|
3 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: apache-2.0
|
3 |
+
---
|
4 |
+
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
```
|
9 |
+
import torch
|
10 |
+
import os
|
11 |
+
from transformers import AutoTokenizer, AutoModel
|
12 |
+
from icecream import ic
|
13 |
+
import time
|
14 |
+
|
15 |
+
class DocOwlInfer():
|
16 |
+
def __init__(self, ckpt_path):
|
17 |
+
self.tokenizer = AutoTokenizer.from_pretrained(ckpt_path, use_fast=False)
|
18 |
+
self.model = AutoModel.from_pretrained(ckpt_path, trust_remote_code=True, low_cpu_mem_usage=True, torch_dtype=torch.float16, device_map='auto')
|
19 |
+
self.model.init_processor(tokenizer=self.tokenizer, basic_image_size=504, crop_anchors='grid_12')
|
20 |
+
|
21 |
+
def inference(self, images, query):
|
22 |
+
messages = [{'role': 'USER', 'content': '<|image|>'*len(images)+query}]
|
23 |
+
answer = self.model.chat(messages=messages, images=images, tokenizer=self.tokenizer)
|
24 |
+
return answer
|
25 |
+
|
26 |
+
|
27 |
+
docowl = DocOwlInfer(ckpt_path='mPLUG/DocOwl2')
|
28 |
+
|
29 |
+
images = ['/nas-alinlp/anwenhu/tmp/paper.png']
|
30 |
+
|
31 |
+
query = "What is this paper about, provide detailed information."
|
32 |
+
|
33 |
+
answer = docowl.inference(images, query)
|
34 |
+
|
35 |
+
|
36 |
+
‘’‘
|