Instructions to use vikhyatk/moondream2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use vikhyatk/moondream2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="vikhyatk/moondream2", trust_remote_code=True)# Load model directly from transformers import AutoModelForCausalLM model = AutoModelForCausalLM.from_pretrained("vikhyatk/moondream2", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use vikhyatk/moondream2 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "vikhyatk/moondream2" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "vikhyatk/moondream2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/vikhyatk/moondream2
- SGLang
How to use vikhyatk/moondream2 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 "vikhyatk/moondream2" \ --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": "vikhyatk/moondream2", "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 "vikhyatk/moondream2" \ --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": "vikhyatk/moondream2", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use vikhyatk/moondream2 with Docker Model Runner:
docker model run hf.co/vikhyatk/moondream2
No module named 'transformers_modules.moondream2.fourier_features'
Hi
I get a ModuleNotFoundError: No module named 'transformers_modules.moondream2.fourier_features' when I try to use the latest (2024-08-26) model if it is loaded from a local path. The files (fourier_features.py etc) are there. Any clues?
Unfortunately not... would recommend opening an issue with HF transformers
Hi,
Based on the logs, it seems the fourier_features module is being imported from transformers_modules.moondream2 rather than directly from the directory containing the local model files. This happens because Hugging Face uses a cached directory for dynamic imports when trust_remote_code=True.
To resolve this issue, you can bypass AutoModelForCausalLM and directly import the custom model class from your local directory. Here's how you can do it:
import sys
sys.path.append("local_model_directory")
from hf_moondream import HfMoondream
model = HfMoondream.from_pretrained("local_model_directory")
This approach skips the AutoModelForCausalLM mechanism, which attempts to use dynamic imports, and directly loads the model class from your local files.
Let me know if this works for you!
@karszyma Thank you
I get the following error:
File "C:\ComfyUI_windows_portable\ComfyUI\custom_nodes\ComfyUI-Hangover-Moondream\ho_moondream.py", line 148, in interrogate
from hf_moondream import HfMoondream
File "C:\ComfyUI_windows_portable\ComfyUI\models\moondream2\hf_moondream.py", line 3, in <module>
from .config import MoondreamConfig
ImportError: attempted relative import with no known parent package
Sorry, my bad. Please change the code to:
import sys
sys.path.append("C:\\ComfyUI_windows_portable\\ComfyUI\\models")
from moondream2.hf_moondream import HfMoondream
model = HfMoondream.from_pretrained("C:\\ComfyUI_windows_portable\\ComfyUI\\models\\moondream2")
Let me know if this works!
There's another solution to use local downloaded model. To use the latest version, change
self.tokenizer = Tokenizer.from_pretrained(
"vikhyatk/moondream2", revision="2024-08-26"
)
in moondream.py around line 69 to:
self.tokenizer = Tokenizer.from_file("YOUR LOCAL PATH OF tokenizer.json")