Views
No views yet
1from diffusers.modular_pipelines.mellon_node_utils import MellonNodeConfig, MellonParam
2
3SUPPORTED_ANNOTATION_TASKS = [
4 "<OD>",
5 "<REFERRING_EXPRESSION_SEGMENTATION>",
6 "<CAPTION>",
7 "<DETAILED_CAPTION>",
8 "<MORE_DETAILED_CAPTION>",
9 "<DENSE_REGION_CAPTION>",
10 "<CAPTION_TO_PHRASE_GROUNDING>",
11 "<OPEN_VOCABULARY_DETECTION>",
12]
13
14SUPPORTED_ANNOTATION_OUTPUT_TYPES = [
15 "mask_image",
16 "bounding_box",
17 "mask_overlay",
18]
19
20node_config = MellonNodeConfig(
21 inputs= [
22 # just a string since it is a "known" input, mellon knows how to config, e.g, getting info from https://github.com/huggingface/diffusers/blob/main/src/diffusers/modular_pipelines/mellon_node_utils.py#L29
23 "image",
24 # for custom inputs, we to specify how we would like it to be displayed on UI, but we can generate a default one based on the fields in corresponding `InputParam`,
25 # https://huggingface.co/YiYiXu/florence-2-block/blob/main/block.py#L43, e.g. `type` can be derived from our `type_hint`, `value` can be derived from our `default`...
26 MellonParam(name="annotation_task", label="Annotation Task", type="string", options=SUPPORTED_ANNOTATION_TASKS, value="<CAPTION_TO_PHRASE_GROUNDING>"),
27 MellonParam(name="annotation_prompt", label="Annotation Prompt", type="string", default="", display="textarea"),
28 MellonParam(
29 name="annotation_output_type",
30 label="Annotation Output Type",
31 type="string",
32 options=SUPPORTED_ANNOTATION_OUTPUT_TYPES,
33 value="bounding_box",
34 onChange={
35 "mask_image": ["mask_image"],
36 "bounding_box": [],
37 "mask_overlay": [],
38 }),
39 ],
40 model_inputs= [],
41 outputs= [
42 MellonParam(name="images", label="Images", type="image", display="output"),
43 MellonParam(name="annotations", label="Annotations", type="string", display="output"),
44 MellonParam(name="mask_image", label="Mask Image", type="image", display="output"),
45 ],
46 blocks_names= ["Florence2ImageAnnotatorBlock"],
47 node_type="custom",
48)
49
50node_config.save_mellon_config("YiYiXu/florence-2-block", push_to_hub=True)1import torch
2from diffusers.modular_pipelines import ModularPipeline
3from diffusers.utils import load_image
4
5repo_id = "YiYiXu/florence-2-block"
6# fetch the Florence2 image annotator block that will create our mask
7pipe = ModularPipeline.from_pretrained("./florence-2-custom-block", trust_remote_code=True)
8pipe.load_components(torch_dtype=torch.float16)
9pipe.to("cuda")
10
11
12
13image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/car.jpg?download=true")
14image = image.resize((1024, 1024))
15
16annotation_task = '<CAPTION_TO_PHRASE_GROUNDING>'
17annotation_prompt = "car"
18
19output = pipe(
20 image=image,
21 annotation_task=annotation_task,
22 annotation_prompt=annotation_prompt,
23 annotation_output_type="bounding_box",
24).image[0].save("output.png")