jonathanjordan21 commited on
Commit
572aca7
1 Parent(s): b4d6381

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +96 -0
README.md ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ widget:
3
+ - text: >-
4
+ def add ( severity , progname , & block ) return true if io . nil? ||
5
+ severity < level message = format_message ( severity , progname , yield )
6
+ MUTEX . synchronize { io . write ( message ) } true end
7
+ license: mit
8
+ language:
9
+ - id
10
+ - en
11
+ datasets:
12
+ - jonathanjordan21/drugs-composition-indonesian-donut
13
+ library_name: transformers
14
+ tags:
15
+ - medical
16
+ - chemistry
17
+ ---
18
+
19
+ ## Model description
20
+
21
+ This model is based on the `jonathanjordan21/donut_fine_tuning_food_composition_id` model. The training dataset is created by manually scrapping images across the internet, available in `jonathanjordan21/drugs-composition-indonesian-donut`
22
+
23
+ ## Usage & limitations
24
+
25
+ The model could be used to detect the text of drug compositions from images of 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.
26
+
27
+ ### Output Example
28
+
29
+ Model Output :
30
+ ```python
31
+ '<s_kmpsi><s_komposisi><s_obat>Vitamin E</s_obat><s_takaran>30 I.U.</s_takaran><sep/><s_obat>Tiamin HCl (B1)</s_obat><s_takaran>100 mg</s_takaran><sep/><s_obat>Piridoksin HCl (B6)</s_obat><s_takaran>50 mg</s_takaran><sep/><s_obat>Sianokobalamin (B12)</s_obat><s_takaran>100 mcg</s_takaran><sep/><s_obat>K-l-aspartat</s_obat><s_takaran>100 mg</s_takaran><sep/><s_obat>Mg-l-aspartat</s_obat><s_takaran>100 mg</s_takaran></s_komposisi><s_desc></s_desc></s_kmpsi>'
32
+ ```
33
+
34
+ Json Parsed Output :
35
+ ```python
36
+ {'komposisi': [{'obat': 'Vitamin E', 'takaran': '30 I.U.'}, {'obat': 'Tiamin HCl (B1)', 'takaran': '100 mg'}, {'obat': 'Piridoksin HCl (B6)', 'takaran': '50 mg'}, {'obat': 'Sianokobalamin (B12)', 'takaran': '100 mcg'}, {'obat': 'K-l-aspartat', 'takaran': '100 mg'}, {'obat': 'Mg-l-aspartat', 'takaran': '100 mg'}], 'desc': ''}
37
+ ```
38
+
39
+ ### How to use
40
+
41
+ Load Donut Processor and Model
42
+ ```python
43
+ from transformers import DonutProcessor, VisionEncoderDecoderModel
44
+
45
+ # Load processor
46
+ processor = DonutProcessor.from_pretrained("jonathanjordan21/donut-finetuned-drugs-composition-indonesian")
47
+
48
+ # Load model
49
+ model = VisionEncoderDecoderModel.from_pretrained("jonathanjordan21/donut-finetuned-drugs-composition-indonesian")
50
+ ```
51
+
52
+ Create JSON parser
53
+ ```python
54
+ from PIL import Image
55
+ from io import BytesIO
56
+ import re
57
+
58
+ import torch
59
+
60
+ def get_komposisi(image_path, image=None):
61
+
62
+ device = "cuda" if torch.cuda.is_available() else "cpu"
63
+
64
+ image = Image.open(image_path).convert('RGB') if image== None else image.convert('RGB')
65
+
66
+ task_prompt = "<s_kmpsi>"
67
+ decoder_input_ids = processor.tokenizer(task_prompt, add_special_tokens=False, return_tensors="pt").input_ids
68
+
69
+ pixel_values = processor(image, return_tensors="pt").pixel_values
70
+
71
+ outputs = model.generate(
72
+ pixel_values.to(device),
73
+ decoder_input_ids=decoder_input_ids.to(device),
74
+ max_length=model.decoder.config.max_position_embeddings,
75
+ early_stopping=True,
76
+ pad_token_id=processor.tokenizer.pad_token_id,
77
+ eos_token_id=processor.tokenizer.eos_token_id,
78
+ use_cache=True,
79
+ bad_words_ids=[[processor.tokenizer.unk_token_id]],
80
+ return_dict_in_generate=True,
81
+ )
82
+
83
+ sequence1 = processor.batch_decode(outputs.sequences)[0]
84
+ sequence2 = sequence1.replace(processor.tokenizer.eos_token, "").replace(processor.tokenizer.pad_token, "")
85
+ sequence3 = re.sub(r"<.*?>", "", sequence2, count=1).strip() # remove first task start token
86
+
87
+ return processor.token2json(sequence3)
88
+ ```
89
+
90
+ Get JSON output from an image
91
+ ```python
92
+ import requests
93
+
94
+ image = requests.get('https://down-id.img.susercontent.com/file/b6812557ba97d24354970cebeac04d48').content
95
+ print(get_komposisi("", Image.open(BytesIO(image))))
96
+ ```