Update app.py
Browse files
app.py
CHANGED
|
@@ -6,9 +6,10 @@ import easyocr
|
|
| 6 |
|
| 7 |
# --- Configuration ---
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
# Ensure sentencepiece is installed
|
| 12 |
processor = Blip2Processor.from_pretrained(model_name, use_fast=False)
|
| 13 |
model = (
|
| 14 |
Blip2ForConditionalGeneration.from_pretrained(
|
|
@@ -18,7 +19,7 @@ model = (
|
|
| 18 |
.to(device)
|
| 19 |
)
|
| 20 |
|
| 21 |
-
# Initialize EasyOCR (CPU-friendly
|
| 22 |
ocr_reader = easyocr.Reader(['en'], gpu=(device == "cuda"))
|
| 23 |
# Object detection with DETR for object context
|
| 24 |
detector = pipeline(
|
|
@@ -29,10 +30,6 @@ detector = pipeline(
|
|
| 29 |
|
| 30 |
# --- Core Generation Function ---
|
| 31 |
def generate_alt_text(files, max_tokens):
|
| 32 |
-
"""
|
| 33 |
-
Generate detailed alt text for each image using BLIP-2, OCR, and object detection.
|
| 34 |
-
Returns a newline-separated string of captions.
|
| 35 |
-
"""
|
| 36 |
captions = []
|
| 37 |
for f in files or []:
|
| 38 |
path = f.name if hasattr(f, 'name') else f
|
|
@@ -42,7 +39,7 @@ def generate_alt_text(files, max_tokens):
|
|
| 42 |
except Exception as e:
|
| 43 |
captions.append(f"Error opening {path}: {e}")
|
| 44 |
continue
|
| 45 |
-
#
|
| 46 |
try:
|
| 47 |
ocr_texts = ocr_reader.readtext(path, detail=0)
|
| 48 |
visible_text = ' '.join(ocr_texts).strip()
|
|
@@ -51,7 +48,6 @@ def generate_alt_text(files, max_tokens):
|
|
| 51 |
# Detect objects
|
| 52 |
try:
|
| 53 |
objs = detector(image)
|
| 54 |
-
# get unique labels, top-5
|
| 55 |
labels = list({o['label'] for o in objs[:5]})
|
| 56 |
objects_text = ', '.join(labels)
|
| 57 |
except Exception:
|
|
@@ -72,13 +68,13 @@ def generate_alt_text(files, max_tokens):
|
|
| 72 |
)
|
| 73 |
caption = processor.decode(outputs[0], skip_special_tokens=True).strip().capitalize()
|
| 74 |
captions.append(caption)
|
| 75 |
-
return "\n".join(captions)
|
| 76 |
|
| 77 |
# --- Gradio Interface ---
|
| 78 |
with gr.Blocks(title="Refined High-End Alt Text Generator") as demo:
|
| 79 |
gr.Markdown("# Refined High‑End Alt Text Generator")
|
| 80 |
gr.Markdown(
|
| 81 |
-
"This app uses BLIP‑2
|
| 82 |
)
|
| 83 |
|
| 84 |
with gr.Row():
|
|
@@ -104,12 +100,12 @@ with gr.Blocks(title="Refined High-End Alt Text Generator") as demo:
|
|
| 104 |
show_copy_button=True
|
| 105 |
)
|
| 106 |
|
| 107 |
-
# Bind events
|
| 108 |
-
file_input.upload(lambda files: files, file_input, gallery)
|
| 109 |
generate_btn.click(
|
| 110 |
fn=generate_alt_text,
|
| 111 |
inputs=[file_input, max_tokens],
|
| 112 |
-
outputs=output,
|
| 113 |
queue=True
|
| 114 |
)
|
| 115 |
clear_btn.click(
|
|
|
|
| 6 |
|
| 7 |
# --- Configuration ---
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
# Use a valid BLIP-2 model identifier
|
| 10 |
+
model_name = "Salesforce/blip2-flan-t5-xl"
|
| 11 |
|
| 12 |
+
# Ensure sentencepiece is installed: pip install sentencepiece
|
| 13 |
processor = Blip2Processor.from_pretrained(model_name, use_fast=False)
|
| 14 |
model = (
|
| 15 |
Blip2ForConditionalGeneration.from_pretrained(
|
|
|
|
| 19 |
.to(device)
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Initialize EasyOCR (CPU-friendly)
|
| 23 |
ocr_reader = easyocr.Reader(['en'], gpu=(device == "cuda"))
|
| 24 |
# Object detection with DETR for object context
|
| 25 |
detector = pipeline(
|
|
|
|
| 30 |
|
| 31 |
# --- Core Generation Function ---
|
| 32 |
def generate_alt_text(files, max_tokens):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
captions = []
|
| 34 |
for f in files or []:
|
| 35 |
path = f.name if hasattr(f, 'name') else f
|
|
|
|
| 39 |
except Exception as e:
|
| 40 |
captions.append(f"Error opening {path}: {e}")
|
| 41 |
continue
|
| 42 |
+
# OCR extraction
|
| 43 |
try:
|
| 44 |
ocr_texts = ocr_reader.readtext(path, detail=0)
|
| 45 |
visible_text = ' '.join(ocr_texts).strip()
|
|
|
|
| 48 |
# Detect objects
|
| 49 |
try:
|
| 50 |
objs = detector(image)
|
|
|
|
| 51 |
labels = list({o['label'] for o in objs[:5]})
|
| 52 |
objects_text = ', '.join(labels)
|
| 53 |
except Exception:
|
|
|
|
| 68 |
)
|
| 69 |
caption = processor.decode(outputs[0], skip_special_tokens=True).strip().capitalize()
|
| 70 |
captions.append(caption)
|
| 71 |
+
return "\n".join(captions)
|
| 72 |
|
| 73 |
# --- Gradio Interface ---
|
| 74 |
with gr.Blocks(title="Refined High-End Alt Text Generator") as demo:
|
| 75 |
gr.Markdown("# Refined High‑End Alt Text Generator")
|
| 76 |
gr.Markdown(
|
| 77 |
+
"This app uses BLIP‑2 XL for rich captions, EasyOCR for text extraction, and DETR for object context."
|
| 78 |
)
|
| 79 |
|
| 80 |
with gr.Row():
|
|
|
|
| 100 |
show_copy_button=True
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# Bind events correctly using inputs/outputs lists
|
| 104 |
+
file_input.upload(lambda files: files, inputs=[file_input], outputs=[gallery])
|
| 105 |
generate_btn.click(
|
| 106 |
fn=generate_alt_text,
|
| 107 |
inputs=[file_input, max_tokens],
|
| 108 |
+
outputs=[output],
|
| 109 |
queue=True
|
| 110 |
)
|
| 111 |
clear_btn.click(
|