Update app.py
Browse files
app.py
CHANGED
|
@@ -1,53 +1,61 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
-
Returns numbered list of detected text strings.
|
| 12 |
"""
|
| 13 |
-
|
| 14 |
for f in files:
|
| 15 |
path = f.name if hasattr(f, 'name') else f
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
text =
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
results.append(text)
|
| 24 |
-
# Format as numbered list
|
| 25 |
-
return "\n".join([f"{i+1}. {t}" for i, t in enumerate(results)])
|
| 26 |
|
| 27 |
-
# Build Gradio
|
| 28 |
-
with gr.Blocks(title="
|
| 29 |
-
gr.Markdown("#
|
| 30 |
-
gr.Markdown(
|
|
|
|
|
|
|
| 31 |
file_input = gr.File(
|
| 32 |
label="Upload Images",
|
| 33 |
file_count="multiple",
|
| 34 |
file_types=["image"]
|
| 35 |
)
|
| 36 |
output_box = gr.Textbox(
|
| 37 |
-
label="
|
| 38 |
-
placeholder="Detected text will appear here...",
|
| 39 |
lines=10,
|
|
|
|
| 40 |
elem_id="output_box"
|
| 41 |
)
|
| 42 |
with gr.Row():
|
| 43 |
-
|
| 44 |
copy_btn = gr.Button("Copy to Clipboard")
|
| 45 |
-
|
| 46 |
-
fn=
|
| 47 |
inputs=file_input,
|
| 48 |
outputs=output_box
|
| 49 |
)
|
| 50 |
-
# Copy to clipboard
|
| 51 |
copy_btn.click(
|
| 52 |
fn=None,
|
| 53 |
inputs=None,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
| 3 |
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Use GPU if available for faster inference
|
| 7 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
|
| 9 |
+
# Load BLIP-2 base model for balanced speed and accuracy
|
| 10 |
+
processor = Blip2Processor.from_pretrained("Salesforce/blip2-flan-t5-base")
|
| 11 |
+
model = (
|
| 12 |
+
Blip2ForConditionalGeneration.from_pretrained(
|
| 13 |
+
"Salesforce/blip2-flan-t5-base",
|
| 14 |
+
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 15 |
+
)
|
| 16 |
+
.to(device)
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
def generate_alt_text(files):
|
| 20 |
"""
|
| 21 |
+
Generates descriptive alt-text captions for uploaded images using BLIP-2.
|
|
|
|
| 22 |
"""
|
| 23 |
+
captions = []
|
| 24 |
for f in files:
|
| 25 |
path = f.name if hasattr(f, 'name') else f
|
| 26 |
+
image = Image.open(path).convert("RGB")
|
| 27 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
| 28 |
+
with torch.no_grad():
|
| 29 |
+
outputs = model.generate(**inputs, max_new_tokens=50)
|
| 30 |
+
text = processor.decode(outputs[0], skip_special_tokens=True)
|
| 31 |
+
captions.append(text.strip())
|
| 32 |
+
return "\n".join([f"{i+1}. {cap}" for i, cap in enumerate(captions)])
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Build Gradio interface
|
| 35 |
+
with gr.Blocks(title="Efficient & Accurate Alt Text Generator") as demo:
|
| 36 |
+
gr.Markdown("# Alt‑Text Generator (BLIP‑2 Base)")
|
| 37 |
+
gr.Markdown(
|
| 38 |
+
"Upload multiple images for fast, accurate descriptions powered by Salesforce BLIP‑2."
|
| 39 |
+
)
|
| 40 |
file_input = gr.File(
|
| 41 |
label="Upload Images",
|
| 42 |
file_count="multiple",
|
| 43 |
file_types=["image"]
|
| 44 |
)
|
| 45 |
output_box = gr.Textbox(
|
| 46 |
+
label="Generated Alt Texts",
|
|
|
|
| 47 |
lines=10,
|
| 48 |
+
placeholder="Your generated descriptions will appear here...",
|
| 49 |
elem_id="output_box"
|
| 50 |
)
|
| 51 |
with gr.Row():
|
| 52 |
+
generate_btn = gr.Button("Generate Alt Text")
|
| 53 |
copy_btn = gr.Button("Copy to Clipboard")
|
| 54 |
+
generate_btn.click(
|
| 55 |
+
fn=generate_alt_text,
|
| 56 |
inputs=file_input,
|
| 57 |
outputs=output_box
|
| 58 |
)
|
|
|
|
| 59 |
copy_btn.click(
|
| 60 |
fn=None,
|
| 61 |
inputs=None,
|