Update app.py
Browse files
app.py
CHANGED
|
@@ -3,68 +3,63 @@ from transformers import pipeline
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
# ---
|
| 7 |
-
|
| 8 |
-
MODEL_NAME = "Salesforce/blip2-flan-t5-base"
|
| 9 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
|
| 11 |
-
# Initialize pipeline with trust_remote_code (required for BLIP-2)
|
| 12 |
captioner = pipeline(
|
| 13 |
"image-to-text",
|
| 14 |
-
model=
|
| 15 |
-
torch_dtype=
|
| 16 |
device_map="auto",
|
| 17 |
-
trust_remote_code=True
|
| 18 |
)
|
| 19 |
|
| 20 |
# --- Caption Generation Function ---
|
| 21 |
-
def generate_alt_text(files,
|
| 22 |
captions = []
|
| 23 |
for f in files or []:
|
| 24 |
img = Image.open(f.name).convert("RGB")
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
img,
|
| 28 |
generate_kwargs={
|
| 29 |
-
"max_new_tokens": int(
|
| 30 |
-
"num_beams":
|
| 31 |
-
"no_repeat_ngram_size": 3
|
| 32 |
}
|
| 33 |
)
|
| 34 |
-
text =
|
| 35 |
captions.append(text)
|
| 36 |
return "\n\n".join(captions)
|
| 37 |
|
| 38 |
-
# --- Gradio
|
| 39 |
-
with gr.Blocks(title="BLIP
|
| 40 |
-
gr.Markdown("# BLIP
|
| 41 |
-
gr.Markdown(
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
clear_btn
|
| 48 |
|
| 49 |
gallery = gr.Gallery(label="Image Preview", columns=4)
|
| 50 |
-
output
|
| 51 |
|
| 52 |
file_input.change(
|
| 53 |
fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
|
| 54 |
inputs=file_input,
|
| 55 |
-
outputs=gallery
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
fn=generate_alt_text,
|
| 60 |
-
inputs=[file_input,
|
| 61 |
outputs=output,
|
| 62 |
queue=True
|
| 63 |
)
|
| 64 |
|
| 65 |
clear_btn.click(
|
| 66 |
fn=lambda: ([], 64, [], ""),
|
| 67 |
-
outputs=[file_input,
|
| 68 |
)
|
| 69 |
|
| 70 |
demo.launch()
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# --- Device & Model Setup ---
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
captioner = pipeline(
|
| 9 |
"image-to-text",
|
| 10 |
+
model="Salesforce/blip2-flan-t5-xl",
|
| 11 |
+
torch_dtype=torch.float16 if device=="cuda" else torch.float32,
|
| 12 |
device_map="auto",
|
| 13 |
+
trust_remote_code=True, # required for BLIP-2
|
| 14 |
)
|
| 15 |
|
| 16 |
# --- Caption Generation Function ---
|
| 17 |
+
def generate_alt_text(files, max_new_tokens):
|
| 18 |
captions = []
|
| 19 |
for f in files or []:
|
| 20 |
img = Image.open(f.name).convert("RGB")
|
| 21 |
+
# Funnel all generation args through generate_kwargs
|
| 22 |
+
outputs = captioner(
|
| 23 |
img,
|
| 24 |
generate_kwargs={
|
| 25 |
+
"max_new_tokens": int(max_new_tokens),
|
| 26 |
+
"num_beams": 4
|
|
|
|
| 27 |
}
|
| 28 |
)
|
| 29 |
+
text = outputs[0]["generated_text"].strip().capitalize()
|
| 30 |
captions.append(text)
|
| 31 |
return "\n\n".join(captions)
|
| 32 |
|
| 33 |
+
# --- Gradio App ---
|
| 34 |
+
with gr.Blocks(title="BLIP‑2 XL Alt‑Text Generator") as demo:
|
| 35 |
+
gr.Markdown("# BLIP‑2 XL Alt‑Text Generator")
|
| 36 |
+
gr.Markdown("Model: **Salesforce/blip2‑flan‑t5‑xl**\n\nUses `generate_kwargs` to set `max_new_tokens` & `num_beams`.")
|
| 37 |
|
| 38 |
with gr.Row():
|
| 39 |
file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
|
| 40 |
+
slider = gr.Slider(16, 256, value=64, step=16, label="Max New Tokens")
|
| 41 |
+
run_btn = gr.Button("Generate Alt‑Text")
|
| 42 |
+
clear_btn = gr.Button("Clear All")
|
| 43 |
|
| 44 |
gallery = gr.Gallery(label="Image Preview", columns=4)
|
| 45 |
+
output = gr.Textbox(label="Generated Alt‑Text", lines=10, show_copy_button=True)
|
| 46 |
|
| 47 |
file_input.change(
|
| 48 |
fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
|
| 49 |
inputs=file_input,
|
| 50 |
+
outputs=gallery,
|
| 51 |
)
|
| 52 |
|
| 53 |
+
run_btn.click(
|
| 54 |
fn=generate_alt_text,
|
| 55 |
+
inputs=[file_input, slider],
|
| 56 |
outputs=output,
|
| 57 |
queue=True
|
| 58 |
)
|
| 59 |
|
| 60 |
clear_btn.click(
|
| 61 |
fn=lambda: ([], 64, [], ""),
|
| 62 |
+
outputs=[file_input, slider, gallery, output]
|
| 63 |
)
|
| 64 |
|
| 65 |
demo.launch()
|