Views
No views yet
minhvn4/florence2-icon is a fine-tuned version of Microsoft's Florence-2-base specifically tailored for Icon Captioning. This model understands and generates descriptive captions for UI icons, symbols, and pictograms.trust_remote_code=True when loading the model.AutoModelForCausalLM)transformers, torch, Pillow, einops, timm).1import torch
2from PIL import Image
3from transformers import AutoProcessor, AutoModelForCausalLM
4
5# Set model ID
6model_id = "minhvn4/florence2-icon"
7
8# Load the processor and model
9processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
10model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).eval()
11
12# Move model to target device
13device = "cuda" if torch.cuda.is_available() else "cpu"
14model.to(device)
15
16def generate_caption(image_path, prompt="<CAPTION>"):
17 image = Image.open(image_path).convert("RGB")
18
19 # Process inputs
20 inputs = processor(text=prompt, images=image, return_tensors="pt").to(device)
21
22 # Generate text
23 with torch.inference_mode():
24 generated_ids = model.generate(
25 input_ids=inputs["input_ids"],
26 pixel_values=inputs["pixel_values"],
27 max_new_tokens=20,
28 num_beams=1,
29 do_sample=False
30 )
31
32 # Decode output
33 caption = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
34 return caption.strip()
35
36# Run inference on an icon
37image_path = "path/to/your/icon.png"
38caption = generate_caption(image_path)
39print(f"Generated caption: {caption}")ValueError: The model class you are passing is not supported, ensure you are passing trust_remote_code=True to both the AutoProcessor and the AutoModelForCausalLM. You may also need to install einops and timm which are required by the Florence-2 architecture.