Update app.py
Browse files
app.py
CHANGED
|
@@ -1,106 +1,51 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
from PIL import Image
|
| 4 |
-
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 5 |
-
import torch.nn as _nn
|
| 6 |
|
| 7 |
-
# ---
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
return False
|
| 13 |
-
return _orig_getattr(self, name)
|
| 14 |
-
_nn.Module.__getattr__ = _patched_getattr
|
| 15 |
-
|
| 16 |
-
# --- Device & Model Setup ---
|
| 17 |
-
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
| 18 |
-
torch_dtype = torch.float16 if device.type == 'cuda' else torch.float32
|
| 19 |
-
|
| 20 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
-
'microsoft/Florence-2-large',
|
| 22 |
-
torch_dtype=torch_dtype,
|
| 23 |
-
trust_remote_code=True
|
| 24 |
-
).to(device)
|
| 25 |
-
processor = AutoProcessor.from_pretrained(
|
| 26 |
-
'microsoft/Florence-2-large',
|
| 27 |
-
trust_remote_code=True
|
| 28 |
)
|
| 29 |
-
PROMPT = '<OD>'
|
| 30 |
|
| 31 |
-
# ---
|
| 32 |
-
def generate_alt_text(files,
|
| 33 |
captions = []
|
| 34 |
for f in files or []:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
).to(device, torch_dtype)
|
| 43 |
-
# 3) Generate with caching disabled
|
| 44 |
-
outputs = model.generate(
|
| 45 |
-
input_ids=inputs['input_ids'],
|
| 46 |
-
pixel_values=inputs['pixel_values'],
|
| 47 |
-
max_new_tokens=int(max_new_tokens),
|
| 48 |
-
num_beams=3,
|
| 49 |
-
do_sample=False,
|
| 50 |
-
use_cache=False
|
| 51 |
)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
raw_text,
|
| 56 |
-
task=PROMPT,
|
| 57 |
-
image_size=(img.width, img.height)
|
| 58 |
-
)
|
| 59 |
-
# 5) Extract caption text
|
| 60 |
-
data = parsed.get(PROMPT, '')
|
| 61 |
-
if isinstance(data, dict):
|
| 62 |
-
text = data.get('text', '')
|
| 63 |
-
else:
|
| 64 |
-
text = data
|
| 65 |
-
caption = text.strip().capitalize()
|
| 66 |
-
captions.append(caption)
|
| 67 |
-
return '\n\n'.join(captions)
|
| 68 |
|
| 69 |
-
# --- Gradio
|
| 70 |
-
with gr.Blocks(title=
|
| 71 |
-
gr.Markdown(
|
| 72 |
-
gr.Markdown(
|
| 73 |
|
| 74 |
with gr.Row():
|
| 75 |
-
file_input = gr.File(
|
| 76 |
-
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
minimum=64, maximum=4096, step=64, value=512,
|
| 80 |
-
label='Max New Tokens'
|
| 81 |
-
)
|
| 82 |
-
gen_btn = gr.Button('Generate Alt-Text')
|
| 83 |
-
clear_btn = gr.Button('Clear All')
|
| 84 |
|
| 85 |
-
gallery = gr.Gallery(label=
|
| 86 |
-
output
|
| 87 |
-
label='Generated Alt-Text', lines=5, show_copy_button=True
|
| 88 |
-
)
|
| 89 |
|
| 90 |
file_input.change(
|
| 91 |
-
fn=lambda files: [Image.open(f.name).convert(
|
| 92 |
inputs=file_input,
|
| 93 |
outputs=gallery
|
| 94 |
)
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
inputs=[file_input, slider],
|
| 98 |
-
outputs=output,
|
| 99 |
-
queue=True
|
| 100 |
-
)
|
| 101 |
-
clear_btn.click(
|
| 102 |
-
fn=lambda: ([], 512, [], ''),
|
| 103 |
-
outputs=[file_input, slider, gallery, output]
|
| 104 |
-
)
|
| 105 |
|
| 106 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
from PIL import Image
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# --- Lightweight pipeline ---
|
| 7 |
+
captioner = pipeline(
|
| 8 |
+
"image-to-text",
|
| 9 |
+
model="nlpconnect/vit-gpt2-image-captioning",
|
| 10 |
+
device=0 # set to -1 for CPU only
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
)
|
|
|
|
| 12 |
|
| 13 |
+
# --- Generation fn ---
|
| 14 |
+
def generate_alt_text(files, max_length):
|
| 15 |
captions = []
|
| 16 |
for f in files or []:
|
| 17 |
+
img = Image.open(f.name).convert("RGB")
|
| 18 |
+
out = captioner(
|
| 19 |
+
img,
|
| 20 |
+
generate_kwargs={
|
| 21 |
+
"max_length": int(max_length),
|
| 22 |
+
"num_beams": 3
|
| 23 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
)
|
| 25 |
+
text = out[0].get("generated_text", "").strip().capitalize()
|
| 26 |
+
captions.append(text)
|
| 27 |
+
return "\n\n".join(captions)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# --- Gradio UI ---
|
| 30 |
+
with gr.Blocks(title="Fast Alt‑Text Generator") as demo:
|
| 31 |
+
gr.Markdown("## Fast & Lightweight Alt‑Text Generator")
|
| 32 |
+
gr.Markdown("Model: `nlpconnect/vit-gpt2-image-captioning`")
|
| 33 |
|
| 34 |
with gr.Row():
|
| 35 |
+
file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
|
| 36 |
+
slider = gr.Slider(16, 64, value=32, step=8, label="Max Tokens")
|
| 37 |
+
run_btn = gr.Button("Generate")
|
| 38 |
+
clear_btn = gr.Button("Clear")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
gallery = gr.Gallery(label="Preview", columns=4)
|
| 41 |
+
output = gr.Textbox(label="Captions", lines=6, show_copy_button=True)
|
|
|
|
|
|
|
| 42 |
|
| 43 |
file_input.change(
|
| 44 |
+
fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
|
| 45 |
inputs=file_input,
|
| 46 |
outputs=gallery
|
| 47 |
)
|
| 48 |
+
run_btn.click(generate_alt_text, [file_input, slider], output, queue=True)
|
| 49 |
+
clear_btn.click(lambda: ([], 32, [], ""), outputs=[file_input, slider, gallery, output])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
demo.launch()
|