Instructions to use microsoft/Florence-2-base-ft with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use microsoft/Florence-2-base-ft with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="microsoft/Florence-2-base-ft", trust_remote_code=True)# Load model directly from transformers import AutoProcessor, AutoModelForImageTextToText processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True) model = AutoModelForImageTextToText.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use microsoft/Florence-2-base-ft with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "microsoft/Florence-2-base-ft" # 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-base-ft", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/microsoft/Florence-2-base-ft
- SGLang
How to use microsoft/Florence-2-base-ft 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-base-ft" \ --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-base-ft", "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-base-ft" \ --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-base-ft", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use microsoft/Florence-2-base-ft with Docker Model Runner:
docker model run hf.co/microsoft/Florence-2-base-ft
update model init with float16
Browse files- README.md +9 -5
- config.json +1 -1
README.md
CHANGED
|
@@ -27,7 +27,7 @@ Resources and Technical Documentation:
|
|
| 27 |
|
| 28 |
## How to Get Started with the Model
|
| 29 |
|
| 30 |
-
Use the code below to get started with the model.
|
| 31 |
|
| 32 |
```python
|
| 33 |
import requests
|
|
@@ -35,8 +35,10 @@ import requests
|
|
| 35 |
from PIL import Image
|
| 36 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 37 |
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
| 40 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
| 41 |
|
| 42 |
prompt = "<OD>"
|
|
@@ -44,7 +46,7 @@ prompt = "<OD>"
|
|
| 44 |
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
|
| 45 |
image = Image.open(requests.get(url, stream=True).raw)
|
| 46 |
|
| 47 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 48 |
|
| 49 |
generated_ids = model.generate(
|
| 50 |
input_ids=inputs["input_ids"],
|
|
@@ -77,8 +79,10 @@ import requests
|
|
| 77 |
from PIL import Image
|
| 78 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 79 |
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
| 82 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
| 83 |
|
| 84 |
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
|
|
@@ -89,7 +93,7 @@ def run_example(task_prompt, text_input=None):
|
|
| 89 |
prompt = task_prompt
|
| 90 |
else:
|
| 91 |
prompt = task_prompt + text_input
|
| 92 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt")
|
| 93 |
generated_ids = model.generate(
|
| 94 |
input_ids=inputs["input_ids"],
|
| 95 |
pixel_values=inputs["pixel_values"],
|
|
|
|
| 27 |
|
| 28 |
## How to Get Started with the Model
|
| 29 |
|
| 30 |
+
Use the code below to get started with the model. All models are trained with float16.
|
| 31 |
|
| 32 |
```python
|
| 33 |
import requests
|
|
|
|
| 35 |
from PIL import Image
|
| 36 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 37 |
|
| 38 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 39 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 40 |
|
| 41 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base-ft", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
|
| 42 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
| 43 |
|
| 44 |
prompt = "<OD>"
|
|
|
|
| 46 |
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
|
| 47 |
image = Image.open(requests.get(url, stream=True).raw)
|
| 48 |
|
| 49 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
|
| 50 |
|
| 51 |
generated_ids = model.generate(
|
| 52 |
input_ids=inputs["input_ids"],
|
|
|
|
| 79 |
from PIL import Image
|
| 80 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 81 |
|
| 82 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 83 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 84 |
|
| 85 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base-ft", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
|
| 86 |
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True)
|
| 87 |
|
| 88 |
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
|
|
|
|
| 93 |
prompt = task_prompt
|
| 94 |
else:
|
| 95 |
prompt = task_prompt + text_input
|
| 96 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
|
| 97 |
generated_ids = model.generate(
|
| 98 |
input_ids=inputs["input_ids"],
|
| 99 |
pixel_values=inputs["pixel_values"],
|
config.json
CHANGED
|
@@ -79,7 +79,7 @@
|
|
| 79 |
"image_feature_source": ["spatial_avg_pool", "temporal_avg_pool"]
|
| 80 |
},
|
| 81 |
"vocab_size": 51289,
|
| 82 |
-
"torch_dtype": "
|
| 83 |
"transformers_version": "4.41.0.dev0",
|
| 84 |
"is_encoder_decoder": true
|
| 85 |
}
|
|
|
|
| 79 |
"image_feature_source": ["spatial_avg_pool", "temporal_avg_pool"]
|
| 80 |
},
|
| 81 |
"vocab_size": 51289,
|
| 82 |
+
"torch_dtype": "float16",
|
| 83 |
"transformers_version": "4.41.0.dev0",
|
| 84 |
"is_encoder_decoder": true
|
| 85 |
}
|