Image-Text-to-Text
Transformers
Safetensors
multilingual
minicpmv
feature-extraction
minicpm-v
vision
ocr
multi-image
video
custom_code
conversational
Instructions to use fredaddy/MiniCPM-V-2_6-Deployable with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use fredaddy/MiniCPM-V-2_6-Deployable with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="fredaddy/MiniCPM-V-2_6-Deployable", trust_remote_code=True) 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 AutoModel model = AutoModel.from_pretrained("fredaddy/MiniCPM-V-2_6-Deployable", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use fredaddy/MiniCPM-V-2_6-Deployable with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "fredaddy/MiniCPM-V-2_6-Deployable" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "fredaddy/MiniCPM-V-2_6-Deployable", "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/fredaddy/MiniCPM-V-2_6-Deployable
- SGLang
How to use fredaddy/MiniCPM-V-2_6-Deployable 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 "fredaddy/MiniCPM-V-2_6-Deployable" \ --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": "fredaddy/MiniCPM-V-2_6-Deployable", "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 "fredaddy/MiniCPM-V-2_6-Deployable" \ --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": "fredaddy/MiniCPM-V-2_6-Deployable", "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 fredaddy/MiniCPM-V-2_6-Deployable with Docker Model Runner:
docker model run hf.co/fredaddy/MiniCPM-V-2_6-Deployable
Create handler.py
Browse files- handler.py +59 -0
handler.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import base64
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
from transformers import AutoModel, AutoTokenizer
|
| 6 |
+
|
| 7 |
+
class EndpointHandler:
|
| 8 |
+
def __init__(self, path="/repository"):
|
| 9 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 10 |
+
|
| 11 |
+
# Load the model
|
| 12 |
+
self.model = AutoModel.from_pretrained(
|
| 13 |
+
path,
|
| 14 |
+
trust_remote_code=True,
|
| 15 |
+
attn_implementation='sdpa',
|
| 16 |
+
torch_dtype=torch.bfloat16 if self.device.type == "cuda" else torch.float32,
|
| 17 |
+
).to(self.device)
|
| 18 |
+
self.model.eval()
|
| 19 |
+
|
| 20 |
+
# Load the tokenizer
|
| 21 |
+
self.tokenizer = AutoTokenizer.from_pretrained(
|
| 22 |
+
path,
|
| 23 |
+
trust_remote_code=True,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
def __call__(self, data):
|
| 27 |
+
# Extract image and text from the input data
|
| 28 |
+
image_data = data.get("inputs", {}).get("image", "")
|
| 29 |
+
text_prompt = data.get("inputs", {}).get("text", "")
|
| 30 |
+
|
| 31 |
+
if not image_data or not text_prompt:
|
| 32 |
+
return {"error": "Both 'image' and 'text' must be provided in the input data."}
|
| 33 |
+
|
| 34 |
+
# Process the image data
|
| 35 |
+
try:
|
| 36 |
+
image_bytes = base64.b64decode(image_data)
|
| 37 |
+
image = Image.open(BytesIO(image_bytes)).convert("RGB")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
return {"error": f"Failed to process image data: {e}"}
|
| 40 |
+
|
| 41 |
+
# Prepare the messages for the model
|
| 42 |
+
msgs = [{'role': 'user', 'content': [image, text_prompt]}]
|
| 43 |
+
|
| 44 |
+
# Generate output
|
| 45 |
+
with torch.no_grad():
|
| 46 |
+
res = self.model.chat(
|
| 47 |
+
image=None,
|
| 48 |
+
msgs=msgs,
|
| 49 |
+
tokenizer=self.tokenizer,
|
| 50 |
+
sampling=True,
|
| 51 |
+
temperature=0.7,
|
| 52 |
+
top_p=0.95,
|
| 53 |
+
max_length=2000,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# The result is the generated text
|
| 57 |
+
output_text = res
|
| 58 |
+
|
| 59 |
+
return {"generated_text": output_text}
|