jonathanjordan21
commited on
Commit
•
b9ce6e0
1
Parent(s):
02912f7
Create README.md
Browse files
README.md
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
tags:
|
3 |
+
- summarization
|
4 |
+
widget:
|
5 |
+
- text: >-
|
6 |
+
def add ( severity , progname , & block ) return true if io . nil? ||
|
7 |
+
severity < level message = format_message ( severity , progname , yield )
|
8 |
+
MUTEX . synchronize { io . write ( message ) } true end
|
9 |
+
license: mit
|
10 |
+
language:
|
11 |
+
- id
|
12 |
+
- en
|
13 |
+
pipeline_tag: document-question-answering
|
14 |
+
---
|
15 |
+
|
16 |
+
## Model description
|
17 |
+
|
18 |
+
This model is based on the `naver-clova-ix/donut-base` model. The training dataset is created by manually scrapping images across the internet
|
19 |
+
|
20 |
+
## Usage & limitations
|
21 |
+
|
22 |
+
The model could be used to detect the nutritional facts or compositions from images of food or drug packages. It is capable to create a json format of the components described in the image. However, due to lack of data, the texts in the image must be concisely upright.
|
23 |
+
|
24 |
+
### Output Example
|
25 |
+
```python
|
26 |
+
'<s_kmpsi><s_komposisi><s_obat>Phyllantus niruri herba extract (Meniran)</s_obat><s_takaran>150mg</s_takaran><sep/><s_obat>Blumea balsamifera folium extract (Sembung)</s_obat><s_takaran>150mg</s_takaran><sep/><s_obat>Zingiber officinale var. Rubra rhizoma extract (Jahe Merah)</s_obat><s_takaran>150mg</s_takaran><sep/><s_obat>Andrographis paniculata herba extract (Sambiloto)</s_obat><s_takaran>100mg</s_takaran></s_komposisi><s_desc>Tiap kapsul mengandung :</s_desc></s_kmpsi>'
|
27 |
+
```
|
28 |
+
|
29 |
+
### How to use
|
30 |
+
|
31 |
+
Load Donut Processor and Model
|
32 |
+
```python
|
33 |
+
import re
|
34 |
+
from transformers import DonutProcessor, VisionEncoderDecoderModel
|
35 |
+
|
36 |
+
# Load processor
|
37 |
+
processor = DonutProcessor.from_pretrained("jonathanjordan21/donut_fine_tuning_food_composition_id")
|
38 |
+
|
39 |
+
# Load model
|
40 |
+
model = VisionEncoderDecoderModel.from_pretrained("jonathanjordan21/donut_fine_tuning_food_composition_id")
|
41 |
+
```
|
42 |
+
|
43 |
+
Create JSON parser
|
44 |
+
```python
|
45 |
+
from PIL import Image
|
46 |
+
from io import BytesIO
|
47 |
+
import torch
|
48 |
+
|
49 |
+
def get_komposisi(image_path, image=None):
|
50 |
+
image = Image.open(image_path).convert('RGB') if image== None else image.convert('RGB')
|
51 |
+
|
52 |
+
task_prompt = "<s_kmpsi>"
|
53 |
+
decoder_input_ids = processor.tokenizer(task_prompt, add_special_tokens=False, return_tensors="pt").input_ids
|
54 |
+
|
55 |
+
pixel_values = processor(image, return_tensors="pt").pixel_values
|
56 |
+
|
57 |
+
outputs = model.generate(
|
58 |
+
pixel_values.to(device),
|
59 |
+
decoder_input_ids=decoder_input_ids.to(device),
|
60 |
+
max_length=model.decoder.config.max_position_embeddings,
|
61 |
+
early_stopping=True,
|
62 |
+
pad_token_id=processor.tokenizer.pad_token_id,
|
63 |
+
eos_token_id=processor.tokenizer.eos_token_id,
|
64 |
+
use_cache=True,
|
65 |
+
bad_words_ids=[[processor.tokenizer.unk_token_id]],
|
66 |
+
return_dict_in_generate=True,
|
67 |
+
)
|
68 |
+
|
69 |
+
|
70 |
+
sequence1 = processor.batch_decode(outputs.sequences)[0]
|
71 |
+
sequence2 = sequence1.replace(processor.tokenizer.eos_token, "").replace(processor.tokenizer.pad_token, "")
|
72 |
+
sequence3 = re.sub(r"<.*?>", "", sequence2, count=1).strip() # remove first task start token
|
73 |
+
|
74 |
+
return processor.token2json(sequence3)
|
75 |
+
```
|
76 |
+
|
77 |
+
Get json output from an image
|
78 |
+
```python
|
79 |
+
import requests
|
80 |
+
import io
|
81 |
+
|
82 |
+
image = requests.get('https://pintarjualan.id/wp-content/uploads/sites/2/2022/04/label-nustrisi-fact-1.png').content
|
83 |
+
print(get_komposisi("", Image.open(io.BytesIO(image))))
|
84 |
+
```
|