Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ import spaces
|
|
| 8 |
MID = "apple/FastVLM-0.5B"
|
| 9 |
IMAGE_TOKEN_INDEX = -200
|
| 10 |
|
| 11 |
-
# Load model and tokenizer
|
| 12 |
tok = None
|
| 13 |
model = None
|
| 14 |
|
|
@@ -17,157 +17,61 @@ def load_model():
|
|
| 17 |
if tok is None or model is None:
|
| 18 |
print("Loading model...")
|
| 19 |
tok = AutoTokenizer.from_pretrained(MID, trust_remote_code=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
MID,
|
| 22 |
-
torch_dtype=
|
| 23 |
-
device_map="cuda"
|
| 24 |
trust_remote_code=True,
|
| 25 |
)
|
| 26 |
-
print("Model loaded successfully!")
|
| 27 |
return tok, model
|
| 28 |
|
| 29 |
@spaces.GPU(duration=60)
|
| 30 |
def caption_image(image, custom_prompt=None):
|
| 31 |
-
"""
|
| 32 |
-
Generate a caption for the input image.
|
| 33 |
-
|
| 34 |
-
Args:
|
| 35 |
-
image: PIL Image from Gradio
|
| 36 |
-
custom_prompt: Optional custom prompt to use instead of default
|
| 37 |
-
|
| 38 |
-
Returns:
|
| 39 |
-
Generated caption text
|
| 40 |
-
"""
|
| 41 |
if image is None:
|
| 42 |
return "Please upload an image first."
|
| 43 |
|
| 44 |
try:
|
| 45 |
-
# Load model if not already loaded
|
| 46 |
tok, model = load_model()
|
| 47 |
-
# Convert image to RGB if needed
|
| 48 |
if image.mode != "RGB":
|
| 49 |
image = image.convert("RGB")
|
| 50 |
-
|
| 51 |
-
# Use custom prompt or default
|
| 52 |
prompt = custom_prompt if custom_prompt else "Describe this image in detail."
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
{"role": "user", "content": f"<image>\n{prompt}"}
|
| 57 |
-
]
|
| 58 |
-
|
| 59 |
-
# Render to string to place <image> token correctly
|
| 60 |
-
rendered = tok.apply_chat_template(
|
| 61 |
-
messages, add_generation_prompt=True, tokenize=False
|
| 62 |
-
)
|
| 63 |
-
|
| 64 |
-
# Split at image token
|
| 65 |
pre, post = rendered.split("<image>", 1)
|
| 66 |
-
|
| 67 |
-
# Tokenize text around the image token
|
| 68 |
pre_ids = tok(pre, return_tensors="pt", add_special_tokens=False).input_ids
|
| 69 |
post_ids = tok(post, return_tensors="pt", add_special_tokens=False).input_ids
|
| 70 |
-
|
| 71 |
-
# Insert IMAGE token id at placeholder position
|
| 72 |
img_tok = torch.tensor([[IMAGE_TOKEN_INDEX]], dtype=pre_ids.dtype)
|
| 73 |
input_ids = torch.cat([pre_ids, img_tok, post_ids], dim=1).to(model.device)
|
| 74 |
attention_mask = torch.ones_like(input_ids, device=model.device)
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
px = model.get_vision_tower().image_processor(
|
| 78 |
-
images=image, return_tensors="pt"
|
| 79 |
-
)["pixel_values"]
|
| 80 |
px = px.to(model.device, dtype=model.dtype)
|
| 81 |
-
|
| 82 |
-
# Generate caption
|
| 83 |
with torch.no_grad():
|
| 84 |
out = model.generate(
|
| 85 |
inputs=input_ids,
|
| 86 |
attention_mask=attention_mask,
|
| 87 |
images=px,
|
| 88 |
max_new_tokens=128,
|
| 89 |
-
do_sample=False,
|
| 90 |
temperature=1.0,
|
| 91 |
)
|
| 92 |
-
|
| 93 |
-
# Decode and return the generated text
|
| 94 |
generated_text = tok.decode(out[0], skip_special_tokens=True)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
if "assistant" in generated_text:
|
| 98 |
-
response = generated_text.split("assistant")[-1].strip()
|
| 99 |
-
else:
|
| 100 |
-
response = generated_text
|
| 101 |
-
|
| 102 |
-
return response
|
| 103 |
-
|
| 104 |
except Exception as e:
|
| 105 |
return f"Error generating caption: {str(e)}"
|
| 106 |
-
|
| 107 |
-
# Create Gradio interface
|
| 108 |
-
with gr.Blocks(title="FastVLM Image Captioning") as demo:
|
| 109 |
-
gr.Markdown(
|
| 110 |
-
"""
|
| 111 |
-
# 🖼️ FastVLM Image Captioning
|
| 112 |
-
|
| 113 |
-
Upload an image to generate a detailed caption using Apple's FastVLM-0.5B model.
|
| 114 |
-
You can use the default prompt or provide your own custom prompt.
|
| 115 |
-
"""
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
with gr.Row():
|
| 119 |
-
with gr.Column():
|
| 120 |
-
image_input = gr.Image(
|
| 121 |
-
type="pil",
|
| 122 |
-
label="Upload Image",
|
| 123 |
-
elem_id="image-upload"
|
| 124 |
-
)
|
| 125 |
-
|
| 126 |
-
custom_prompt = gr.Textbox(
|
| 127 |
-
label="Custom Prompt (Optional)",
|
| 128 |
-
placeholder="Leave empty for default: 'Describe this image in detail.'",
|
| 129 |
-
lines=2
|
| 130 |
-
)
|
| 131 |
-
|
| 132 |
-
with gr.Row():
|
| 133 |
-
clear_btn = gr.ClearButton([image_input, custom_prompt])
|
| 134 |
-
generate_btn = gr.Button("Generate Caption", variant="primary")
|
| 135 |
-
|
| 136 |
-
with gr.Column():
|
| 137 |
-
output = gr.Textbox(
|
| 138 |
-
label="Generated Caption",
|
| 139 |
-
lines=8,
|
| 140 |
-
max_lines=15,
|
| 141 |
-
show_copy_button=True
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
# Event handlers
|
| 145 |
-
generate_btn.click(
|
| 146 |
-
fn=caption_image,
|
| 147 |
-
inputs=[image_input, custom_prompt],
|
| 148 |
-
outputs=output
|
| 149 |
-
)
|
| 150 |
-
|
| 151 |
-
# Also generate on image upload if no custom prompt
|
| 152 |
-
image_input.change(
|
| 153 |
-
fn=lambda img, prompt: caption_image(img, prompt) if img is not None and not prompt else None,
|
| 154 |
-
inputs=[image_input, custom_prompt],
|
| 155 |
-
outputs=output
|
| 156 |
-
)
|
| 157 |
-
|
| 158 |
-
gr.Markdown(
|
| 159 |
-
"""
|
| 160 |
-
---
|
| 161 |
-
**Model:** [apple/FastVLM-0.5B](https://huggingface.co/apple/FastVLM-0.5B)
|
| 162 |
-
|
| 163 |
-
**Note:** This Space uses ZeroGPU for dynamic GPU allocation.
|
| 164 |
-
"""
|
| 165 |
-
)
|
| 166 |
-
|
| 167 |
-
if __name__ == "__main__":
|
| 168 |
-
demo.launch(
|
| 169 |
-
share=False,
|
| 170 |
-
show_error=True,
|
| 171 |
-
server_name="0.0.0.0",
|
| 172 |
-
server_port=7860
|
| 173 |
-
)
|
|
|
|
| 8 |
MID = "apple/FastVLM-0.5B"
|
| 9 |
IMAGE_TOKEN_INDEX = -200
|
| 10 |
|
| 11 |
+
# Load model and tokenizer
|
| 12 |
tok = None
|
| 13 |
model = None
|
| 14 |
|
|
|
|
| 17 |
if tok is None or model is None:
|
| 18 |
print("Loading model...")
|
| 19 |
tok = AutoTokenizer.from_pretrained(MID, trust_remote_code=True)
|
| 20 |
+
|
| 21 |
+
# Fallback: GPU if available, else CPU
|
| 22 |
+
if torch.cuda.is_available():
|
| 23 |
+
device = "cuda"
|
| 24 |
+
dtype = torch.float16
|
| 25 |
+
else:
|
| 26 |
+
device = "cpu"
|
| 27 |
+
dtype = torch.float32 # safer on CPU
|
| 28 |
+
|
| 29 |
model = AutoModelForCausalLM.from_pretrained(
|
| 30 |
MID,
|
| 31 |
+
torch_dtype=dtype,
|
| 32 |
+
device_map=device, # can be "cuda" or "cpu"
|
| 33 |
trust_remote_code=True,
|
| 34 |
)
|
| 35 |
+
print(f"Model loaded on {device.upper()} successfully!")
|
| 36 |
return tok, model
|
| 37 |
|
| 38 |
@spaces.GPU(duration=60)
|
| 39 |
def caption_image(image, custom_prompt=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if image is None:
|
| 41 |
return "Please upload an image first."
|
| 42 |
|
| 43 |
try:
|
|
|
|
| 44 |
tok, model = load_model()
|
|
|
|
| 45 |
if image.mode != "RGB":
|
| 46 |
image = image.convert("RGB")
|
| 47 |
+
|
|
|
|
| 48 |
prompt = custom_prompt if custom_prompt else "Describe this image in detail."
|
| 49 |
+
messages = [{"role": "user", "content": f"<image>\n{prompt}"}]
|
| 50 |
+
rendered = tok.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
|
| 51 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
pre, post = rendered.split("<image>", 1)
|
|
|
|
|
|
|
| 53 |
pre_ids = tok(pre, return_tensors="pt", add_special_tokens=False).input_ids
|
| 54 |
post_ids = tok(post, return_tensors="pt", add_special_tokens=False).input_ids
|
| 55 |
+
|
|
|
|
| 56 |
img_tok = torch.tensor([[IMAGE_TOKEN_INDEX]], dtype=pre_ids.dtype)
|
| 57 |
input_ids = torch.cat([pre_ids, img_tok, post_ids], dim=1).to(model.device)
|
| 58 |
attention_mask = torch.ones_like(input_ids, device=model.device)
|
| 59 |
+
|
| 60 |
+
px = model.get_vision_tower().image_processor(images=image, return_tensors="pt")["pixel_values"]
|
|
|
|
|
|
|
|
|
|
| 61 |
px = px.to(model.device, dtype=model.dtype)
|
| 62 |
+
|
|
|
|
| 63 |
with torch.no_grad():
|
| 64 |
out = model.generate(
|
| 65 |
inputs=input_ids,
|
| 66 |
attention_mask=attention_mask,
|
| 67 |
images=px,
|
| 68 |
max_new_tokens=128,
|
| 69 |
+
do_sample=False,
|
| 70 |
temperature=1.0,
|
| 71 |
)
|
| 72 |
+
|
|
|
|
| 73 |
generated_text = tok.decode(out[0], skip_special_tokens=True)
|
| 74 |
+
return generated_text.split("assistant")[-1].strip() if "assistant" in generated_text else generated_text
|
| 75 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
except Exception as e:
|
| 77 |
return f"Error generating caption: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|