Visual Question Answering
Transformers
Safetensors
English
Chinese
minicpmv
feature-extraction
custom_code
Instructions to use SwordElucidator/MiniCPM-Llama3-V-2_5 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use SwordElucidator/MiniCPM-Llama3-V-2_5 with Transformers:
# Use a pipeline as a high-level helper # Warning: Pipeline type "visual-question-answering" is no longer supported in transformers v5. # You must load the model directly (see below) or downgrade to v4.x with: # pip install "transformers<5.0.0" from transformers import pipeline pipe = pipeline("visual-question-answering", model="SwordElucidator/MiniCPM-Llama3-V-2_5", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("SwordElucidator/MiniCPM-Llama3-V-2_5", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Download handler.py from SwordElucidator/MiniCPM-Llama3-V-2_5: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/SwordElucidator/MiniCPM-Llama3-V-2_5/resolve/main/handler.py
- Command line
-
hf download hf://SwordElucidator/MiniCPM-Llama3-V-2_5/handler.py
-
curl -L -o handler.py https://huggingface.co/SwordElucidator/MiniCPM-Llama3-V-2_5/resolve/main/handler.py
1.37 kB
| import base64 | |
| from io import BytesIO | |
| from typing import Any, List, Dict | |
| from PIL import Image | |
| from transformers import AutoTokenizer, AutoModel | |
| class EndpointHandler(): | |
| def __init__(self, path=""): | |
| # Use a pipeline as a high-level helper | |
| model_name = "SwordElucidator/MiniCPM-Llama3-V-2_5" | |
| model = AutoModel.from_pretrained(model_name, trust_remote_code=True) | |
| tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True) | |
| model.eval() | |
| self.model = model | |
| self.tokenizer = tokenizer | |
| def __call__(self, data: Any) -> List[List[Dict[str, float]]]: | |
| inputs = data.pop("inputs", data) | |
| image = inputs.pop("image", None) # base64 image as bytes | |
| question = inputs.pop("question", None) | |
| msgs = inputs.pop("msgs", None) | |
| parameters = data.pop("parameters", {}) | |
| image = Image.open(BytesIO(base64.b64decode(image))) | |
| if not msgs: | |
| msgs = [{'role': 'user', 'content': question}] | |
| res = self.model.chat( | |
| image=image, | |
| msgs=msgs, | |
| tokenizer=self.tokenizer, | |
| sampling=True, # if sampling=False, beam_search will be used by default | |
| temperature=parameters.get('temperature', 0.7), | |
| # system_prompt='' # pass system_prompt if needed | |
| ) | |
| return res |