Image-Text-to-Text
Transformers
Safetensors
English
Thai
qwen3_vl
OCR
vision-language
document-understanding
multilingual
conversational
Instructions to use wealthcoders/typhoon-ocr with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use wealthcoders/typhoon-ocr with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="wealthcoders/typhoon-ocr") 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)# Load model directly from transformers import AutoProcessor, AutoModelForImageTextToText processor = AutoProcessor.from_pretrained("wealthcoders/typhoon-ocr") model = AutoModelForImageTextToText.from_pretrained("wealthcoders/typhoon-ocr") 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 = processor.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=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use wealthcoders/typhoon-ocr with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "wealthcoders/typhoon-ocr" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "wealthcoders/typhoon-ocr", "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/wealthcoders/typhoon-ocr
- SGLang
How to use wealthcoders/typhoon-ocr 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 "wealthcoders/typhoon-ocr" \ --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": "wealthcoders/typhoon-ocr", "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 "wealthcoders/typhoon-ocr" \ --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": "wealthcoders/typhoon-ocr", "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 wealthcoders/typhoon-ocr with Docker Model Runner:
docker model run hf.co/wealthcoders/typhoon-ocr
Create handler.py
Browse files- handler.py +86 -0
handler.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModel, AutoTokenizer
|
| 2 |
+
from typing import Dict, List, Any
|
| 3 |
+
import torch
|
| 4 |
+
import base64
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import os
|
| 8 |
+
import tempfile
|
| 9 |
+
|
| 10 |
+
class EndpointHandler:
|
| 11 |
+
def __init__(self, model_dir = 'scb10x/typhoon-ocr1.5-2b'):
|
| 12 |
+
model_path = model_dir
|
| 13 |
+
|
| 14 |
+
self.model = AutoModelForImageTextToText.from_pretrained(model_path, dtype="auto", device_map="auto")
|
| 15 |
+
selfprocessor = AutoProcessor.from_pretrained(model_path)
|
| 16 |
+
|
| 17 |
+
def __call__(self, data: Dict[str, Any]) -> str:
|
| 18 |
+
try:
|
| 19 |
+
base64_string = None
|
| 20 |
+
if "inputs" in data and isinstance(data["inputs"], str):
|
| 21 |
+
base64_string = data["inputs"]
|
| 22 |
+
|
| 23 |
+
# Case 2: Base64 in nested inputs dictionary
|
| 24 |
+
elif "inputs" in data and isinstance(data["inputs"], dict):
|
| 25 |
+
base64_string = data["inputs"].get("base64")
|
| 26 |
+
|
| 27 |
+
# Case 3: Direct base64 at root level
|
| 28 |
+
elif "base64" in data:
|
| 29 |
+
base64_string = data["base64"]
|
| 30 |
+
|
| 31 |
+
# Case 4: Try raw data as base64
|
| 32 |
+
elif isinstance(data, str):
|
| 33 |
+
base64_string = data
|
| 34 |
+
|
| 35 |
+
if not base64_string:
|
| 36 |
+
return {"error": "No base64 string found in input data. Available keys: " + str(data.keys())}
|
| 37 |
+
|
| 38 |
+
print("Found base64 string, length:", len(base64_string))
|
| 39 |
+
|
| 40 |
+
# Remove data URL prefix if present
|
| 41 |
+
if ',' in base64_string:
|
| 42 |
+
base64_string = base64_string.split(',')[1]
|
| 43 |
+
|
| 44 |
+
# Decode base64 to image
|
| 45 |
+
image_data = base64.b64decode(base64_string)
|
| 46 |
+
|
| 47 |
+
messages = [
|
| 48 |
+
{
|
| 49 |
+
"role": "user",
|
| 50 |
+
"content": [
|
| 51 |
+
{
|
| 52 |
+
"type": "image",
|
| 53 |
+
"image": image_data,
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"type": "text",
|
| 57 |
+
"text": prompt
|
| 58 |
+
}
|
| 59 |
+
],
|
| 60 |
+
}
|
| 61 |
+
]
|
| 62 |
+
# Preparation for inference
|
| 63 |
+
inputs = self.processor.apply_chat_template(
|
| 64 |
+
messages,
|
| 65 |
+
tokenize=True,
|
| 66 |
+
add_generation_prompt=True,
|
| 67 |
+
return_dict=True,
|
| 68 |
+
return_tensors="pt"
|
| 69 |
+
)
|
| 70 |
+
inputs = inputs.to(self.model.device)
|
| 71 |
+
|
| 72 |
+
# Inference: Generation of the output
|
| 73 |
+
generated_ids = self.model.generate(**inputs, max_new_tokens=10000)
|
| 74 |
+
generated_ids_trimmed = [
|
| 75 |
+
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
| 76 |
+
]
|
| 77 |
+
output_text = self.processor.batch_decode(
|
| 78 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 79 |
+
)
|
| 80 |
+
print(output_text[0])
|
| 81 |
+
|
| 82 |
+
return output_text[0]
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"Error processing image: {e}")
|
| 86 |
+
return str(e)
|