Instructions to use Mit1208/Florence-2-DocLayNet with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Mit1208/Florence-2-DocLayNet with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="Mit1208/Florence-2-DocLayNet", trust_remote_code=True)# Load model directly from transformers import AutoProcessor, AutoModelForImageTextToText processor = AutoProcessor.from_pretrained("Mit1208/Florence-2-DocLayNet", trust_remote_code=True) model = AutoModelForImageTextToText.from_pretrained("Mit1208/Florence-2-DocLayNet", trust_remote_code=True) - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use Mit1208/Florence-2-DocLayNet with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Mit1208/Florence-2-DocLayNet" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Mit1208/Florence-2-DocLayNet", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/Mit1208/Florence-2-DocLayNet
- SGLang
How to use Mit1208/Florence-2-DocLayNet 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 "Mit1208/Florence-2-DocLayNet" \ --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": "Mit1208/Florence-2-DocLayNet", "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 "Mit1208/Florence-2-DocLayNet" \ --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": "Mit1208/Florence-2-DocLayNet", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use Mit1208/Florence-2-DocLayNet with Docker Model Runner:
docker model run hf.co/Mit1208/Florence-2-DocLayNet
Update README.md
Browse files
README.md
CHANGED
|
@@ -46,6 +46,59 @@ Use the code below to get started with the model.
|
|
| 46 |
|
| 47 |
[More Information Needed]
|
| 48 |
|
| 49 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|
|
|
|
| 46 |
|
| 47 |
[More Information Needed]
|
| 48 |
|
| 49 |
+
### Inference Procedure
|
| 50 |
+
|
| 51 |
+
```python
|
| 52 |
+
|
| 53 |
+
!pip install -qU transformers
|
| 54 |
+
!pip install -qU accelerate bitsandbytes einops flash_attn timm
|
| 55 |
+
!pip install -q datasets
|
| 56 |
+
|
| 57 |
+
from PIL import Image
|
| 58 |
+
import requests
|
| 59 |
+
import torch
|
| 60 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq, BitsAndBytesConfig, TrainingArguments, AutoModelForCausalLM
|
| 61 |
+
import requests
|
| 62 |
+
import re
|
| 63 |
+
from transformers import AutoConfig, AutoProcessor, AutoModelForCausalLM
|
| 64 |
+
|
| 65 |
+
base_model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True,)
|
| 66 |
+
processor = AutoProcessor.from_pretrained("microsoft/Florence-2-base-ft", trust_remote_code=True,)
|
| 67 |
+
model = AutoModelForCausalLM.from_pretrained("Mit1208/Florence-2-DocLayNet", trust_remote_code=True, config = base_model.config)
|
| 68 |
+
|
| 69 |
+
def run_example(task_prompt, image, text_input=None):
|
| 70 |
+
if text_input is None:
|
| 71 |
+
prompt = task_prompt
|
| 72 |
+
else:
|
| 73 |
+
prompt = task_prompt + text_input
|
| 74 |
+
print(prompt)
|
| 75 |
+
inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
|
| 76 |
+
generated_ids = model.generate(
|
| 77 |
+
input_ids=inputs["input_ids"],
|
| 78 |
+
pixel_values=inputs["pixel_values"],
|
| 79 |
+
max_new_tokens=1024,
|
| 80 |
+
early_stopping=False,
|
| 81 |
+
do_sample=False,
|
| 82 |
+
num_beams=3,
|
| 83 |
+
)
|
| 84 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 85 |
+
print(generated_text)
|
| 86 |
+
parsed_answer = processor.post_process_generation(
|
| 87 |
+
generated_text,
|
| 88 |
+
task=task_prompt,
|
| 89 |
+
image_size=(image.width, image.height)
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
return parsed_answer
|
| 93 |
+
|
| 94 |
+
from PIL import Image
|
| 95 |
+
import requests
|
| 96 |
+
|
| 97 |
+
image = Image.open('form-1.png').convert('RGB')
|
| 98 |
+
task_prompt = '<OD>'
|
| 99 |
+
results = run_example(task_prompt, example['image'].resize(size=(1000, 1000)))
|
| 100 |
+
print(results)
|
| 101 |
+
|
| 102 |
+
```
|
| 103 |
|
| 104 |
<!-- This relates heavily to the Technical Specifications. Content here should link to that section when it is relevant to the training procedure. -->
|