Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,70 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
-
from PIL import Image
|
| 4 |
import torch
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Device setup
|
| 7 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
|
|
|
| 16 |
def generate_alt_text(files):
|
| 17 |
-
"""
|
| 18 |
-
Generate alt-text captions for each uploaded image file.
|
| 19 |
-
"""
|
| 20 |
captions = []
|
| 21 |
for f in files:
|
| 22 |
-
path = f.name if hasattr(f,
|
| 23 |
try:
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
text = f"<Error processing {path}: {e}>"
|
| 29 |
captions.append(text.strip())
|
|
|
|
| 30 |
return "\n".join(f"{i+1}. {cap}" for i, cap in enumerate(captions))
|
| 31 |
|
| 32 |
-
# Build
|
| 33 |
-
demo = gr.Blocks(
|
| 34 |
with demo:
|
| 35 |
-
gr.Markdown("#
|
| 36 |
-
gr.Markdown("Upload multiple images, preview them, and get
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
label="Upload Images",
|
| 41 |
-
file_count="multiple",
|
| 42 |
-
file_types=["image"]
|
| 43 |
-
)
|
| 44 |
|
| 45 |
-
# 2) Preview gallery (4 columns) — NO .style() here
|
| 46 |
-
preview = gr.Gallery(label="Image Preview", columns=4)
|
| 47 |
-
|
| 48 |
-
# 3) Controls
|
| 49 |
with gr.Row():
|
| 50 |
generate_btn = gr.Button("Generate Alt Text")
|
| 51 |
clear_btn = gr.Button("Clear All")
|
| 52 |
copy_btn = gr.Button("Copy to Clipboard")
|
| 53 |
|
| 54 |
-
|
| 55 |
-
output = gr.Textbox(
|
| 56 |
-
label="Generated Alt Texts",
|
| 57 |
-
lines=10,
|
| 58 |
-
elem_id="output_box"
|
| 59 |
-
)
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
file_input.upload(lambda files: files, inputs=file_input, outputs=
|
| 63 |
-
generate_btn.click(
|
| 64 |
-
clear_btn.click(lambda: ([], [], ""), inputs=None, outputs=[file_input,
|
| 65 |
-
copy_btn.click(
|
| 66 |
-
fn=None,
|
| 67 |
-
inputs=None,
|
| 68 |
-
outputs=None,
|
| 69 |
-
js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }"
|
| 70 |
-
)
|
| 71 |
|
| 72 |
-
#
|
| 73 |
demo.queue(max_size=10)
|
| 74 |
-
|
| 75 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
|
|
|
| 3 |
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import easyocr
|
| 6 |
|
| 7 |
+
# Device setup
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
|
| 10 |
+
# OCR reader (English)
|
| 11 |
+
reader = easyocr.Reader(['en'], gpu=(device=='cuda'))
|
| 12 |
+
|
| 13 |
+
# Load BLIP (large) model for high-quality image captioning
|
| 14 |
+
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 15 |
+
model = BlipForConditionalGeneration.from_pretrained(
|
| 16 |
+
"Salesforce/blip-image-captioning-large",
|
| 17 |
+
torch_dtype=torch.float16 if device=='cuda' else torch.float32
|
| 18 |
+
).to(device)
|
| 19 |
|
| 20 |
+
# Generate alt-text including OCR-detected text
|
| 21 |
def generate_alt_text(files):
|
|
|
|
|
|
|
|
|
|
| 22 |
captions = []
|
| 23 |
for f in files:
|
| 24 |
+
path = f.name if hasattr(f, 'name') else f
|
| 25 |
try:
|
| 26 |
+
# Load image
|
| 27 |
+
image = Image.open(path).convert('RGB')
|
| 28 |
+
# Extract visible text via OCR
|
| 29 |
+
ocr_results = reader.readtext(path, detail=0)
|
| 30 |
+
visible_text = ' '.join(ocr_results).strip() or '<no text detected>'
|
| 31 |
+
# Prepare a prompt instructing model to include visible text
|
| 32 |
+
prompt = f"Describe this image in one sentence, and mention this text: '{visible_text}'."
|
| 33 |
+
# Tokenize inputs
|
| 34 |
+
inputs = processor(images=image, text=prompt, return_tensors="pt").to(device)
|
| 35 |
+
# Generate caption
|
| 36 |
+
with torch.no_grad():
|
| 37 |
+
out = model.generate(**inputs, max_new_tokens=50)
|
| 38 |
+
text = processor.decode(out[0], skip_special_tokens=True)
|
| 39 |
except Exception as e:
|
| 40 |
text = f"<Error processing {path}: {e}>"
|
| 41 |
captions.append(text.strip())
|
| 42 |
+
# Numbered list
|
| 43 |
return "\n".join(f"{i+1}. {cap}" for i, cap in enumerate(captions))
|
| 44 |
|
| 45 |
+
# Build Gradio UI
|
| 46 |
+
demo = gr.Blocks()
|
| 47 |
with demo:
|
| 48 |
+
gr.Markdown("# Accurate Alt Text Generator with OCR + BLIP")
|
| 49 |
+
gr.Markdown("Upload multiple images, preview them, and get concise descriptions including embedded text.")
|
| 50 |
|
| 51 |
+
file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
|
| 52 |
+
gallery = gr.Gallery(label="Image Preview", columns=4)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
with gr.Row():
|
| 55 |
generate_btn = gr.Button("Generate Alt Text")
|
| 56 |
clear_btn = gr.Button("Clear All")
|
| 57 |
copy_btn = gr.Button("Copy to Clipboard")
|
| 58 |
|
| 59 |
+
output = gr.Textbox(label="Generated Alt Texts", lines=10, elem_id="output_box")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# Bind events
|
| 62 |
+
file_input.upload(lambda files: files, inputs=file_input, outputs=gallery)
|
| 63 |
+
generate_btn.click(generate_alt_text, inputs=file_input, outputs=output)
|
| 64 |
+
clear_btn.click(lambda: ([], [], ""), inputs=None, outputs=[file_input, gallery, output])
|
| 65 |
+
copy_btn.click(None, None, None, js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# Optional: queueing
|
| 68 |
demo.queue(max_size=10)
|
| 69 |
+
# Launch
|
| 70 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|