Image-Text-to-Text
Transformers
Safetensors
glm_ocr
endpoints-template
glm-ocr
ocr
document-understanding
conversational
Instructions to use caiofabio1/GLM-OCR-endpoint with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use caiofabio1/GLM-OCR-endpoint with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="caiofabio1/GLM-OCR-endpoint") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForMultimodalLM tokenizer = AutoTokenizer.from_pretrained("caiofabio1/GLM-OCR-endpoint") model = AutoModelForMultimodalLM.from_pretrained("caiofabio1/GLM-OCR-endpoint", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use caiofabio1/GLM-OCR-endpoint with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "caiofabio1/GLM-OCR-endpoint" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "caiofabio1/GLM-OCR-endpoint", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/caiofabio1/GLM-OCR-endpoint
- SGLang
How to use caiofabio1/GLM-OCR-endpoint 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 "caiofabio1/GLM-OCR-endpoint" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "caiofabio1/GLM-OCR-endpoint", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'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 "caiofabio1/GLM-OCR-endpoint" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "caiofabio1/GLM-OCR-endpoint", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use caiofabio1/GLM-OCR-endpoint with Docker Model Runner:
docker model run hf.co/caiofabio1/GLM-OCR-endpoint
Download handler.py from caiofabio1/GLM-OCR-endpoint: direct link, hf CLI and curl.
- Browser
- Download file 2.78 kB
-
https://huggingface.co/caiofabio1/GLM-OCR-endpoint/resolve/main/handler.py
- Command line
-
hf download hf://caiofabio1/GLM-OCR-endpoint/handler.py
-
curl -L -o handler.py https://huggingface.co/caiofabio1/GLM-OCR-endpoint/resolve/main/handler.py
2.78 kB
| import subprocess | |
| import sys | |
| import os | |
| # Install transformers from source BEFORE any transformers import | |
| if not os.path.exists("/tmp/.transformers_installed"): | |
| subprocess.check_call([ | |
| sys.executable, "-m", "pip", "install", "-q", "--upgrade", | |
| "git+https://github.com/huggingface/transformers.git", | |
| "accelerate" | |
| ]) | |
| open("/tmp/.transformers_installed", "w").close() | |
| import torch | |
| import base64 | |
| import io | |
| from typing import Dict, List, Any | |
| from PIL import Image | |
| from transformers import AutoProcessor, AutoModelForImageTextToText | |
| class EndpointHandler: | |
| def __init__(self, path=""): | |
| self.processor = AutoProcessor.from_pretrained(path, trust_remote_code=True) | |
| self.model = AutoModelForImageTextToText.from_pretrained( | |
| path, | |
| torch_dtype=torch.float16, | |
| device_map="auto", | |
| trust_remote_code=True, | |
| ).eval() | |
| self.device = next(self.model.parameters()).device | |
| print(f"GLM-OCR loaded on {self.device}") | |
| def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]: | |
| inputs_data = data.get("inputs", data) | |
| if isinstance(inputs_data, dict): | |
| image_b64 = inputs_data.get("image", "") | |
| prompt = inputs_data.get("prompt", "Text Recognition:") | |
| elif isinstance(inputs_data, str): | |
| image_b64 = inputs_data | |
| prompt = "Text Recognition:" | |
| else: | |
| return [{"error": "Send {inputs: {image: base64, prompt: str}}"}] | |
| try: | |
| image_bytes = base64.b64decode(image_b64) | |
| image = Image.open(io.BytesIO(image_bytes)).convert("RGB") | |
| except Exception as e: | |
| return [{"error": f"Image decode failed: {str(e)}"}] | |
| messages = [{ | |
| "role": "user", | |
| "content": [ | |
| {"type": "image", "image": image}, | |
| {"type": "text", "text": prompt}, | |
| ], | |
| }] | |
| text = self.processor.apply_chat_template( | |
| messages, tokenize=False, add_generation_prompt=True | |
| ) | |
| proc_inputs = self.processor( | |
| text=[text], images=[image], padding=True, return_tensors="pt" | |
| ) | |
| proc_inputs = {k: v.to(self.device) for k, v in proc_inputs.items()} | |
| with torch.no_grad(): | |
| output = self.model.generate( | |
| **proc_inputs, | |
| temperature=0.1, | |
| max_new_tokens=8192, | |
| do_sample=True, | |
| ) | |
| prompt_len = proc_inputs["input_ids"].shape[1] | |
| new_tokens = output[:, prompt_len:] | |
| text_output = self.processor.tokenizer.batch_decode( | |
| new_tokens, skip_special_tokens=True | |
| )[0] | |
| return [{"generated_text": text_output}] | |