Instructions to use microsoft/Florence-2-large with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/Florence-2-large with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="microsoft/Florence-2-large", trust_remote_code=True)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True) model = AutoModelForMultimodalLM.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use microsoft/Florence-2-large with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/Florence-2-large" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "microsoft/Florence-2-large", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/microsoft/Florence-2-large
- SGLang
How to use microsoft/Florence-2-large 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 "microsoft/Florence-2-large" \ --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": "microsoft/Florence-2-large", "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 "microsoft/Florence-2-large" \ --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": "microsoft/Florence-2-large", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use microsoft/Florence-2-large with Docker Model Runner:
docker model run hf.co/microsoft/Florence-2-large
Fix two transformers-v5 AttributeErrors in Florence-2 remote code
Browse filestransformers v5 changed two things this repo's remote code still assumes the old behavior for (see MIGRATION_GUIDE_V5.md):
1. `forced_bos_token_id` is no longer auto-set as a config instance attribute ('Modeling > Generate'). `Florence2LanguageConfig` read `self.forced_bos_token_id` assuming the old behavior.
2. `additional_special_tokens` was renamed to `extra_special_tokens`. `Florence2Processor.__init__` read the old attribute name, raising AttributeError.
Both fixes are minimal and backward compatible with transformers v4. Related: kijai/ComfyUI-Florence2#187, #199, Comfy-Org/ComfyUI#12277, Lightricks/ComfyUI-LTXVideo#394 all hit the symptom of bug 1. #119 also contains fixes for both but bundles them with a full-repo rewrite that corrupts config.json (swaps in Florence-2-base's architecture dimensions), so it's not safe to merge as-is -- this PR is scoped to just these two AttributeErrors. Supersedes #121, which fixed bug 1 alone and later had bug 2 added as a second commit; this PR bundles both from the start instead.
- configuration_florence2.py +1 -0
- processing_florence2.py +3 -1
|
@@ -261,6 +261,7 @@ class Florence2LanguageConfig(PretrainedConfig):
|
|
| 261 |
**kwargs,
|
| 262 |
)
|
| 263 |
|
|
|
|
| 264 |
# ensure backward compatibility for BART CNN models
|
| 265 |
if self.forced_bos_token_id is None and kwargs.get("force_bos_token_to_be_generated", False):
|
| 266 |
self.forced_bos_token_id = self.bos_token_id
|
|
|
|
| 261 |
**kwargs,
|
| 262 |
)
|
| 263 |
|
| 264 |
+
self.forced_bos_token_id = kwargs.get("forced_bos_token_id", None)
|
| 265 |
# ensure backward compatibility for BART CNN models
|
| 266 |
if self.forced_bos_token_id is None and kwargs.get("force_bos_token_to_be_generated", False):
|
| 267 |
self.forced_bos_token_id = self.bos_token_id
|
|
@@ -86,7 +86,9 @@ class Florence2Processor(ProcessorMixin):
|
|
| 86 |
|
| 87 |
tokens_to_add = {
|
| 88 |
'additional_special_tokens': \
|
| 89 |
-
tokenizer
|
|
|
|
|
|
|
| 90 |
['<od>', '</od>', '<ocr>', '</ocr>'] + \
|
| 91 |
[f'<loc_{x}>' for x in range(1000)] + \
|
| 92 |
['<cap>', '</cap>', '<ncap>', '</ncap>','<dcap>', '</dcap>', '<grounding>', '</grounding>', '<seg>', '</seg>', '<sep>', '<region_cap>', '</region_cap>', '<region_to_desciption>', '</region_to_desciption>', '<proposal>', '</proposal>', '<poly>', '</poly>', '<and>']
|
|
|
|
| 86 |
|
| 87 |
tokens_to_add = {
|
| 88 |
'additional_special_tokens': \
|
| 89 |
+
(getattr(tokenizer, "extra_special_tokens", None)
|
| 90 |
+
if getattr(tokenizer, "extra_special_tokens", None) is not None
|
| 91 |
+
else getattr(tokenizer, "additional_special_tokens", [])) + \
|
| 92 |
['<od>', '</od>', '<ocr>', '</ocr>'] + \
|
| 93 |
[f'<loc_{x}>' for x in range(1000)] + \
|
| 94 |
['<cap>', '</cap>', '<ncap>', '</ncap>','<dcap>', '</dcap>', '<grounding>', '</grounding>', '<seg>', '</seg>', '<sep>', '<region_cap>', '</region_cap>', '<region_to_desciption>', '</region_to_desciption>', '<proposal>', '</proposal>', '<poly>', '</poly>', '<and>']
|