momergul commited on
Commit
7280ad1
·
verified ·
1 Parent(s): ff0580d

Upload processor_git.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. processor_git.py +62 -0
processor_git.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import ProcessorMixin, AutoProcessor
2
+ from transformers.models.auto.processing_auto import AutoProcessor
3
+ from transformers.processing_utils import ProcessorMixin
4
+ from transformers.tokenization_utils_base import BatchEncoding
5
+ import json
6
+ import os
7
+
8
+ class GITProcessor(ProcessorMixin):
9
+ """
10
+ Custom processor that combines a tokenizer and feature extractor.
11
+ """
12
+ attributes = ["image_processor", "tokenizer"]
13
+ image_processor_class = "AutoImageProcessor"
14
+ tokenizer_class = "AutoTokenizer"
15
+
16
+ def __init__(self, image_processor, tokenizer):
17
+ super().__init__(image_processor, tokenizer)
18
+
19
+ def __call__(self, text=None, images=None, **kwargs):
20
+ """
21
+ Main processing method that handles both text and images.
22
+
23
+ Args:
24
+ text: Text input(s) to tokenize
25
+ images: Image input(s) to process
26
+ **kwargs: Additional arguments passed to tokenizer/image_processor
27
+
28
+ Returns:
29
+ Dictionary with processed inputs
30
+ """
31
+ if text is None and images is None:
32
+ raise ValueError("You need to specify either text or images")
33
+
34
+ encoding = {}
35
+
36
+ # Process text if provided
37
+ if text is not None:
38
+ text_encoding = self.tokenizer(text, **kwargs)
39
+ encoding.update(text_encoding)
40
+
41
+ # Process images if provided
42
+ if images is not None:
43
+ image_encoding = self.image_processor(images, **kwargs)
44
+ # Add prefix to avoid key conflicts
45
+ for key, value in image_encoding.items():
46
+ encoding[f"pixel_values" if key == "pixel_values" else f"image_{key}"] = value
47
+
48
+ return BatchEncoding(encoding)
49
+
50
+ def batch_decode(self, *args, **kwargs):
51
+ """
52
+ Delegate batch decoding to the tokenizer.
53
+ """
54
+ return self.tokenizer.batch_decode(*args, **kwargs)
55
+
56
+ def decode(self, *args, **kwargs):
57
+ """
58
+ Delegate decoding to the tokenizer.
59
+ """
60
+ return self.tokenizer.decode(*args, **kwargs)
61
+
62
+