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, device_map="auto")# 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 Tasks examples
Browse files- Change prompt text to a string
- Change hardcoded `task='<OD>'` to `task=prompt`
These changes are necessary for most of the examples to work, and aligns the README with the Jupyter notebook examples.
These changes are also necessary in all of the other Florence-2 repos, but I'll only open the PR here since I don't want to spam PRs.
README.md
CHANGED
|
@@ -99,7 +99,7 @@ def run_example(prompt, text_input=None):
|
|
| 99 |
)
|
| 100 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 101 |
|
| 102 |
-
parsed_answer = processor.post_process_generation(generated_text, task=
|
| 103 |
|
| 104 |
print(parsed_answer)
|
| 105 |
```
|
|
@@ -113,7 +113,7 @@ Here are the tasks `Florence-2` could perform:
|
|
| 113 |
### OCR
|
| 114 |
|
| 115 |
```python
|
| 116 |
-
prompt = <OCR>
|
| 117 |
run_example(prompt)
|
| 118 |
```
|
| 119 |
|
|
@@ -121,25 +121,25 @@ run_example(prompt)
|
|
| 121 |
OCR with region output format:
|
| 122 |
{'\<OCR_WITH_REGION>': {'quad_boxes': [[x1, y1, x2, y2, x3, y3, x4, y4], ...], 'labels': ['text1', ...]}}
|
| 123 |
```python
|
| 124 |
-
prompt = <OCR_WITH_REGION>
|
| 125 |
run_example(prompt)
|
| 126 |
```
|
| 127 |
|
| 128 |
### Caption
|
| 129 |
```python
|
| 130 |
-
prompt = <CAPTION>
|
| 131 |
run_example(prompt)
|
| 132 |
```
|
| 133 |
|
| 134 |
### Detailed Caption
|
| 135 |
```python
|
| 136 |
-
prompt = <DETAILED_CAPTION>
|
| 137 |
run_example(prompt)
|
| 138 |
```
|
| 139 |
|
| 140 |
### More Detailed Caption
|
| 141 |
```python
|
| 142 |
-
prompt = <MORE_DETAILED_CAPTION>
|
| 143 |
run_example(prompt)
|
| 144 |
```
|
| 145 |
|
|
@@ -150,7 +150,7 @@ OD results format:
|
|
| 150 |
'labels': ['label1', 'label2', ...]} }
|
| 151 |
|
| 152 |
```python
|
| 153 |
-
prompt = <OD>
|
| 154 |
run_example(prompt)
|
| 155 |
```
|
| 156 |
|
|
@@ -159,7 +159,7 @@ Dense region caption results format:
|
|
| 159 |
{'\<DENSE_REGION_CAPTION>' : {'bboxes': [[x1, y1, x2, y2], ...],
|
| 160 |
'labels': ['label1', 'label2', ...]} }
|
| 161 |
```python
|
| 162 |
-
prompt = <DENSE_REGION_CAPTION>
|
| 163 |
run_example(prompt)
|
| 164 |
```
|
| 165 |
|
|
@@ -168,7 +168,7 @@ Dense region caption results format:
|
|
| 168 |
{'\<REGION_PROPOSAL>': {'bboxes': [[x1, y1, x2, y2], ...],
|
| 169 |
'labels': ['', '', ...]}}
|
| 170 |
```python
|
| 171 |
-
prompt = <REGION_PROPOSAL>
|
| 172 |
run_example(prompt)
|
| 173 |
```
|
| 174 |
|
|
|
|
| 99 |
)
|
| 100 |
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 101 |
|
| 102 |
+
parsed_answer = processor.post_process_generation(generated_text, task=prompt, image_size=(image.width, image.height))
|
| 103 |
|
| 104 |
print(parsed_answer)
|
| 105 |
```
|
|
|
|
| 113 |
### OCR
|
| 114 |
|
| 115 |
```python
|
| 116 |
+
prompt = '<OCR>'
|
| 117 |
run_example(prompt)
|
| 118 |
```
|
| 119 |
|
|
|
|
| 121 |
OCR with region output format:
|
| 122 |
{'\<OCR_WITH_REGION>': {'quad_boxes': [[x1, y1, x2, y2, x3, y3, x4, y4], ...], 'labels': ['text1', ...]}}
|
| 123 |
```python
|
| 124 |
+
prompt = '<OCR_WITH_REGION>'
|
| 125 |
run_example(prompt)
|
| 126 |
```
|
| 127 |
|
| 128 |
### Caption
|
| 129 |
```python
|
| 130 |
+
prompt = '<CAPTION>'
|
| 131 |
run_example(prompt)
|
| 132 |
```
|
| 133 |
|
| 134 |
### Detailed Caption
|
| 135 |
```python
|
| 136 |
+
prompt = '<DETAILED_CAPTION>'
|
| 137 |
run_example(prompt)
|
| 138 |
```
|
| 139 |
|
| 140 |
### More Detailed Caption
|
| 141 |
```python
|
| 142 |
+
prompt = '<MORE_DETAILED_CAPTION>'
|
| 143 |
run_example(prompt)
|
| 144 |
```
|
| 145 |
|
|
|
|
| 150 |
'labels': ['label1', 'label2', ...]} }
|
| 151 |
|
| 152 |
```python
|
| 153 |
+
prompt = '<OD>'
|
| 154 |
run_example(prompt)
|
| 155 |
```
|
| 156 |
|
|
|
|
| 159 |
{'\<DENSE_REGION_CAPTION>' : {'bboxes': [[x1, y1, x2, y2], ...],
|
| 160 |
'labels': ['label1', 'label2', ...]} }
|
| 161 |
```python
|
| 162 |
+
prompt = '<DENSE_REGION_CAPTION>'
|
| 163 |
run_example(prompt)
|
| 164 |
```
|
| 165 |
|
|
|
|
| 168 |
{'\<REGION_PROPOSAL>': {'bboxes': [[x1, y1, x2, y2], ...],
|
| 169 |
'labels': ['', '', ...]}}
|
| 170 |
```python
|
| 171 |
+
prompt = '<REGION_PROPOSAL>'
|
| 172 |
run_example(prompt)
|
| 173 |
```
|
| 174 |
|