Instructions to use ClementRomac/gia2-small with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ClementRomac/gia2-small with Transformers:
# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("ClementRomac/gia2-small", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download processor.py from ClementRomac/gia2-small: direct link, hf CLI and curl.
- Browser
- Download file 1.8 kB
-
https://huggingface.co/ClementRomac/gia2-small/resolve/main/processor.py
- Command line
-
hf download hf://ClementRomac/gia2-small/processor.py
-
curl -L -o processor.py https://huggingface.co/ClementRomac/gia2-small/resolve/main/processor.py
1.8 kB
| from itertools import chain | |
| from transformers import GitProcessor | |
| class GIAProcessor(GitProcessor): | |
| def __init__(self, image_processor, tokenizer): | |
| super().__init__(image_processor, tokenizer) | |
| def _cut_text(self, examples, max_input_size): | |
| results = { | |
| "input_ids": [], | |
| "attention_mask": [] | |
| } | |
| for i in range(len(examples["input_ids"])): | |
| _input_size = len(examples["input_ids"][i]) | |
| for j in range(max(1, _input_size // max_input_size)): # skip last if smaller than max_input_size | |
| results["input_ids"].append(examples["input_ids"][i][j*max_input_size:(j + 1) * max_input_size]) | |
| results["attention_mask"].append(examples["attention_mask"][i][j * max_input_size:(j + 1) * max_input_size]) | |
| return results | |
| def __call__(self, examples, max_input_size, return_tensors=None, **kwargs): | |
| if "text" in examples and not "images" in examples: | |
| encoded_text = self.tokenizer(examples["text"], return_tensors=return_tensors, max_length=max_input_size, | |
| truncation=False, padding="max_length") | |
| encoding = self._cut_text(encoded_text, max_input_size) | |
| elif "text" in examples and "images" in examples: | |
| encoding = super().__call__(examples["text"], examples["images"], return_tensors, **kwargs) | |
| return encoding | |
| def batch_decode(self, *args, **kwargs): | |
| return self.tokenizer.batch_decode(*args, **kwargs) | |
| def decode(self, *args, **kwargs): | |
| return self.tokenizer.decode(*args, **kwargs) | |
| def model_input_names(self): | |
| return ["input_ids", "attention_mask", "pixel_values"] | |
| GIAProcessor.register_for_auto_class("AutoProcessor") |