Instructions to use to-be/ID_cards_v1 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use to-be/ID_cards_v1 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="to-be/ID_cards_v1")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForMultimodalLM tokenizer = AutoTokenizer.from_pretrained("to-be/ID_cards_v1") model = AutoModelForMultimodalLM.from_pretrained("to-be/ID_cards_v1", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use to-be/ID_cards_v1 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "to-be/ID_cards_v1" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "to-be/ID_cards_v1", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/to-be/ID_cards_v1
- SGLang
How to use to-be/ID_cards_v1 with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "to-be/ID_cards_v1" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "to-be/ID_cards_v1", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "to-be/ID_cards_v1" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "to-be/ID_cards_v1", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use to-be/ID_cards_v1 with Docker Model Runner:
docker model run hf.co/to-be/ID_cards_v1
Download handler.py from to-be/ID_cards_v1: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/to-be/ID_cards_v1/resolve/main/handler.py
- Command line
-
hf download hf://to-be/ID_cards_v1/handler.py
-
curl -L -o handler.py https://huggingface.co/to-be/ID_cards_v1/resolve/main/handler.py
1.61 kB
| from typing import Dict, List, Any | |
| from transformers import DonutProcessor, VisionEncoderDecoderModel | |
| import torch | |
| # check for GPU | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| class EndpointHandler: | |
| def __init__(self, path=""): | |
| # load the model | |
| self.processor = DonutProcessor.from_pretrained(path) | |
| self.model = VisionEncoderDecoderModel.from_pretrained(path) | |
| # move model to device | |
| self.model.to(device) | |
| self.decoder_input_ids = self.processor.tokenizer( | |
| "<s_cord-v2>", add_special_tokens=False, return_tensors="pt" | |
| ).input_ids | |
| def __call__(self, data: Any) -> List[List[Dict[str, float]]]: | |
| inputs = data.pop("inputs", data) | |
| # preprocess the input | |
| pixel_values = self.processor(inputs, return_tensors="pt").pixel_values | |
| # forward pass | |
| outputs = self.model.generate( | |
| pixel_values.to(device), | |
| decoder_input_ids=self.decoder_input_ids.to(device), | |
| max_length=self.model.decoder.config.max_position_embeddings, | |
| early_stopping=True, | |
| pad_token_id=self.processor.tokenizer.pad_token_id, | |
| eos_token_id=self.processor.tokenizer.eos_token_id, | |
| use_cache=True, | |
| num_beams=1, | |
| bad_words_ids=[[self.processor.tokenizer.unk_token_id]], | |
| return_dict_in_generate=True, | |
| ) | |
| # process output | |
| prediction = self.processor.batch_decode(outputs.sequences)[0] | |
| prediction = self.processor.token2json(prediction) | |
| return prediction |