Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,9 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import replicate
|
| 4 |
import easyocr
|
|
|
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
|
| 7 |
# 1. Ensure the API token is set or crash early
|
| 8 |
if "REPLICATE_API_TOKEN" not in os.environ:
|
|
@@ -11,59 +13,84 @@ if "REPLICATE_API_TOKEN" not in os.environ:
|
|
| 11 |
# 2. Inject it into the replicate module so every call is authenticated
|
| 12 |
replicate.api_token = os.environ["REPLICATE_API_TOKEN"]
|
| 13 |
|
| 14 |
-
# 3. Initialize the Replicate client
|
| 15 |
client = replicate.Client()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
# Initialize EasyOCR for visible text extraction
|
| 18 |
-
detector_langs = ['en'] # add more languages if needed
|
| 19 |
-
reader = easyocr.Reader(detector_langs, gpu=False)
|
| 20 |
|
| 21 |
def generate_alt_text(files):
|
| 22 |
"""
|
| 23 |
-
Generates alt text
|
| 24 |
"""
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
try:
|
| 37 |
-
ocr_list = reader.readtext(path, detail=0)
|
| 38 |
-
visible_text = ' '.join(ocr_list).strip() or '<no text>'
|
| 39 |
-
except Exception:
|
| 40 |
-
visible_text = '<ocr error>'
|
| 41 |
-
|
| 42 |
-
# Generate base caption via Replicate BLIP
|
| 43 |
-
try:
|
| 44 |
-
model = client.models.get("salesforce/blip-image-captioning-base")
|
| 45 |
-
result = model.predict(image=open(path, "rb"))
|
| 46 |
-
base_caption = result[0] if isinstance(result, list) else str(result)
|
| 47 |
-
except Exception as e:
|
| 48 |
-
captions.append(f"Error generating caption: {e}")
|
| 49 |
-
continue
|
| 50 |
-
|
| 51 |
-
# Compose final caption
|
| 52 |
-
final = f"{base_caption.strip()} (Text: {visible_text})"
|
| 53 |
-
captions.append(final)
|
| 54 |
-
|
| 55 |
-
# Format numbered list
|
| 56 |
-
return "\n".join([f"{i+1}. {c}" for i, c in enumerate(captions)])
|
| 57 |
|
| 58 |
# Build Gradio interface
|
| 59 |
demo = gr.Blocks(title="Replicate‑Powered Alt Text Generator")
|
| 60 |
with demo:
|
| 61 |
gr.Markdown("# Alt Text Generator with Replicate BLIP")
|
| 62 |
gr.Markdown(
|
| 63 |
-
"Upload multiple images, and get detailed alt text including visible in‑image text.
|
|
|
|
| 64 |
)
|
| 65 |
|
| 66 |
-
# File upload
|
| 67 |
file_input = gr.File(
|
| 68 |
label="Upload Images",
|
| 69 |
file_count="multiple",
|
|
@@ -79,10 +106,10 @@ with demo:
|
|
| 79 |
clr_btn = gr.Button("Clear All")
|
| 80 |
copy_btn = gr.Button("Copy to Clipboard")
|
| 81 |
|
| 82 |
-
# Output
|
| 83 |
output = gr.Textbox(label="Alt Texts", lines=10, elem_id="output_box")
|
| 84 |
|
| 85 |
-
#
|
| 86 |
file_input.upload(lambda files: files, inputs=file_input, outputs=gallery)
|
| 87 |
gen_btn.click(fn=generate_alt_text, inputs=file_input, outputs=output)
|
| 88 |
clr_btn.click(lambda: ([], [], ""), inputs=None, outputs=[file_input, gallery, output])
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import replicate
|
| 4 |
import easyocr
|
| 5 |
+
import torch
|
| 6 |
from PIL import Image
|
| 7 |
+
from concurrent.futures import ThreadPoolExecutor
|
| 8 |
|
| 9 |
# 1. Ensure the API token is set or crash early
|
| 10 |
if "REPLICATE_API_TOKEN" not in os.environ:
|
|
|
|
| 13 |
# 2. Inject it into the replicate module so every call is authenticated
|
| 14 |
replicate.api_token = os.environ["REPLICATE_API_TOKEN"]
|
| 15 |
|
| 16 |
+
# 3. Initialize the Replicate client and preload the BLIP model
|
| 17 |
client = replicate.Client()
|
| 18 |
+
model = client.models.get("salesforce/blip-image-captioning-base")
|
| 19 |
+
|
| 20 |
+
# 4. Initialize EasyOCR with GPU support if available
|
| 21 |
+
gpu_flag = torch.cuda.is_available()
|
| 22 |
+
reader = easyocr.Reader(["en"], gpu=gpu_flag)
|
| 23 |
+
|
| 24 |
+
# Determine available CPU cores for parallel processing
|
| 25 |
+
cpu_cores = os.cpu_count() or 4
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def process_file(file_obj):
|
| 29 |
+
"""
|
| 30 |
+
Process a single file: open image, extract visible text via OCR,
|
| 31 |
+
generate caption via Replicate BLIP, and compose final output.
|
| 32 |
+
"""
|
| 33 |
+
# Resolve file path
|
| 34 |
+
path = getattr(file_obj, 'name', file_obj)
|
| 35 |
+
|
| 36 |
+
# Open and validate image
|
| 37 |
+
try:
|
| 38 |
+
with Image.open(path).convert('RGB') as img:
|
| 39 |
+
img.verify() # verify it's an image
|
| 40 |
+
except Exception:
|
| 41 |
+
return f"<Error opening file {path}>"
|
| 42 |
+
|
| 43 |
+
# OCR extraction
|
| 44 |
+
try:
|
| 45 |
+
ocr_texts = reader.readtext(path, detail=0)
|
| 46 |
+
visible_text = ' '.join(ocr_texts).strip() or '<no text>'
|
| 47 |
+
except Exception:
|
| 48 |
+
visible_text = '<ocr error>'
|
| 49 |
+
|
| 50 |
+
# Generate base caption via Replicate BLIP with beam search for accuracy
|
| 51 |
+
try:
|
| 52 |
+
with open(path, 'rb') as img_file:
|
| 53 |
+
result = model.predict(
|
| 54 |
+
image=img_file,
|
| 55 |
+
num_beams=5,
|
| 56 |
+
max_length=75,
|
| 57 |
+
min_length=5,
|
| 58 |
+
no_repeat_ngram_size=2
|
| 59 |
+
)
|
| 60 |
+
base_caption = result[0] if isinstance(result, list) else str(result)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
return f"Error generating caption: {e}"
|
| 63 |
+
|
| 64 |
+
# Compose and return final caption
|
| 65 |
+
return f"{base_caption.strip()} (Text: {visible_text})"
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
def generate_alt_text(files):
|
| 69 |
"""
|
| 70 |
+
Generates alt text for multiple files in parallel, improving throughput.
|
| 71 |
"""
|
| 72 |
+
if not files:
|
| 73 |
+
return "No files uploaded."
|
| 74 |
+
|
| 75 |
+
# Limit workers to number of files or CPU cores
|
| 76 |
+
max_workers = min(len(files), cpu_cores)
|
| 77 |
+
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
| 78 |
+
results = list(executor.map(process_file, files))
|
| 79 |
+
|
| 80 |
+
# Format as numbered list
|
| 81 |
+
return "\n".join(f"{i+1}. {caption}" for i, caption in enumerate(results))
|
| 82 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
# Build Gradio interface
|
| 85 |
demo = gr.Blocks(title="Replicate‑Powered Alt Text Generator")
|
| 86 |
with demo:
|
| 87 |
gr.Markdown("# Alt Text Generator with Replicate BLIP")
|
| 88 |
gr.Markdown(
|
| 89 |
+
"Upload multiple images, and get detailed alt text including visible in‑image text."
|
| 90 |
+
" Powered by Replicate BLIP and EasyOCR, with parallel processing for speed."
|
| 91 |
)
|
| 92 |
|
| 93 |
+
# File upload component
|
| 94 |
file_input = gr.File(
|
| 95 |
label="Upload Images",
|
| 96 |
file_count="multiple",
|
|
|
|
| 106 |
clr_btn = gr.Button("Clear All")
|
| 107 |
copy_btn = gr.Button("Copy to Clipboard")
|
| 108 |
|
| 109 |
+
# Output textbox
|
| 110 |
output = gr.Textbox(label="Alt Texts", lines=10, elem_id="output_box")
|
| 111 |
|
| 112 |
+
# Bind events
|
| 113 |
file_input.upload(lambda files: files, inputs=file_input, outputs=gallery)
|
| 114 |
gen_btn.click(fn=generate_alt_text, inputs=file_input, outputs=output)
|
| 115 |
clr_btn.click(lambda: ([], [], ""), inputs=None, outputs=[file_input, gallery, output])
|