Steve Goodman commited on
Commit
60d0acc
·
verified ·
1 Parent(s): f9170d7

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +60 -1
README.md CHANGED
@@ -3,4 +3,63 @@ license: mit
3
  base_model:
4
  - MiaoshouAI/Florence-2-base-PromptGen-v1.5
5
  - microsoft/Florence-2-base
6
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  base_model:
4
  - MiaoshouAI/Florence-2-base-PromptGen-v1.5
5
  - microsoft/Florence-2-base
6
+ ---
7
+
8
+ # Florence-2-base-PromptGen v1.5 (with config and code updates)
9
+
10
+ This is [MiaoshouAI/Florence-2-base-PromptGen-v1.5](https://huggingface.co/MiaoshouAI/Florence-2-base-PromptGen-v1.5) which retains its existing features, but with changes to supporting configuration and code to ensure drop-in replacement for [Microsoft Florence-2 Model Base](https://huggingface.co/microsoft/Florence-2-base) the when using Transformers library in Python.
11
+
12
+ 1) The config.json has been updated with an auto_map property and key-values added matching the Florence-2-base [resolving this issue](https://huggingface.co/MiaoshouAI/Florence-2-base-PromptGen-v1.5/discussions/4).
13
+ 2) Python code that is located in the root of the repo in Florence-2-base but in florence2_base_ft in [MiaoshouAI/Florence-2-base-PromptGen-v1.5](https://huggingface.co/MiaoshouAI/Florence-2-base-PromptGen-v1.5) has been moved to the root of the repo as this prevented trust_remove_code=True in the AutoProcessor.from_pretrained from loading the code.
14
+ 3) Changes to Florence2-base's modeling_florence2.py to ensure that the class Florence2LanguageForConditionalGeneration inherits from GenerationMixin, secondary to PreTrainedModel to ensure compatibility with transformers from v4.50 onwards .
15
+
16
+ ## About PromptGen
17
+ Florence-2-base-PromptGen is a model trained by [MiaoshouAI](https://huggingface.co/MiaoshouAI) that specializes in generating highly descriptive prompts and tags that assist with training image generation models like [FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev) and creating descriptive prompts for image generation.
18
+
19
+ Supported prompts include standard prompts from Florence2-base such as <0D> for identifying object locations and enhanced prompts by MiaoshouAI including <CAPTION>, <DETAILED_CAPTION>, <MORE_DETAILED_CAPTION> and additional prompts included <GENERATE_TAGS> and <MIXED_CAPTION>. See the original repo for more details.
20
+
21
+ ## How to use:
22
+
23
+ To use this model, you can load it directly from the Hugging Face Model Hub.
24
+
25
+ First, install dependancies (in a virtual environent if you prefer), for example:
26
+ ```
27
+ pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cu124 -U
28
+ pip3 install transformers pillow einops timm
29
+ ```
30
+
31
+ The following code is based on the microsoft/Florence2-base example but with updated prompt and model, and correct imports.
32
+
33
+ ```python
34
+ import requests
35
+ import torch
36
+ from PIL import Image
37
+ from transformers import AutoProcessor, AutoModelForCausalLM
38
+
39
+
40
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
41
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
42
+
43
+ model = AutoModelForCausalLM.from_pretrained("/Florence-2-base-PromptGen-v1.5", torch_dtype=torch_dtype, trust_remote_code=True).to(device)
44
+ processor = AutoProcessor.from_pretrained("/Florence-2-base-PromptGen-v1.5", trust_remote_code=True)
45
+
46
+ # Examples include CAPTION>, <DETAILED_CAPTION>, <MORE_DETAILED_CAPTION>,<GENERATE_TAGS>, <MIXED_CAPTION>, <0D>
47
+ prompt = "<CAPTION>"
48
+
49
+ url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true"
50
+ image = Image.open(requests.get(url, stream=True).raw)
51
+
52
+ inputs = processor(text=prompt, images=image, return_tensors="pt").to(device, torch_dtype)
53
+
54
+ generated_ids = model.generate(
55
+ input_ids=inputs["input_ids"],
56
+ pixel_values=inputs["pixel_values"],
57
+ max_new_tokens=1024,
58
+ num_beams=3,
59
+ do_sample=False
60
+ )
61
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
62
+ parsed_answer = processor.post_process_generation(generated_text, task=prompt, image_size=(image.width, image.height))
63
+
64
+ print(parsed_answer[prompt])
65
+ ```