Update app.py
Browse files
app.py
CHANGED
|
@@ -7,101 +7,118 @@ from PIL import Image
|
|
| 7 |
from concurrent.futures import ThreadPoolExecutor
|
| 8 |
import pandas as pd
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
gpu_flag = torch.cuda.is_available()
|
| 23 |
ocr_reader = easyocr.Reader(["en"], gpu=gpu_flag)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
if
|
| 28 |
-
yolo_model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def process_file(file_obj, max_len):
|
| 31 |
"""
|
| 32 |
-
Process a single image
|
| 33 |
-
- Open & verify image
|
| 34 |
-
- Extract visible text via EasyOCR
|
| 35 |
-
- Detect objects via YOLOv5
|
| 36 |
-
- Generate base caption via BLIP
|
| 37 |
-
- Combine & truncate to max_len
|
| 38 |
"""
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
#
|
| 43 |
try:
|
| 44 |
-
with Image.open(
|
| 45 |
img.verify()
|
| 46 |
except Exception:
|
| 47 |
return (filename, "<Error opening file>")
|
| 48 |
|
| 49 |
-
#
|
| 50 |
try:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
except Exception:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
try:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
except Exception:
|
| 62 |
-
labels = ''
|
| 63 |
-
|
| 64 |
-
# Generate base caption via BLIP
|
| 65 |
-
try:
|
| 66 |
-
with open(path, 'rb') as img_file:
|
| 67 |
-
result = blip_model.predict(
|
| 68 |
-
image=img_file,
|
| 69 |
num_beams=5,
|
| 70 |
max_length=75,
|
| 71 |
min_length=5,
|
| 72 |
no_repeat_ngram_size=2
|
| 73 |
)
|
| 74 |
-
|
| 75 |
except Exception as e:
|
| 76 |
return (filename, f"Error generating caption: {e}")
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
parts = [
|
| 80 |
if labels:
|
| 81 |
parts.append(f"Objects: {labels}")
|
| 82 |
-
if
|
| 83 |
-
parts.append(f"Text: {
|
| 84 |
caption = '; '.join(parts)
|
| 85 |
|
| 86 |
-
# Truncate
|
| 87 |
if len(caption) > max_len:
|
| 88 |
caption = caption[:max_len].rsplit(' ', 1)[0] + '…'
|
| 89 |
|
| 90 |
return (filename, caption)
|
| 91 |
|
| 92 |
-
# Determine available CPU cores for parallel processing
|
| 93 |
-
cpu_cores = os.cpu_count() or 4
|
| 94 |
|
| 95 |
def generate_alt_text(files, max_len):
|
| 96 |
"""
|
| 97 |
-
|
| 98 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
if not files:
|
| 100 |
return pd.DataFrame(columns=["filename", "alt_text"]), ""
|
| 101 |
|
| 102 |
max_workers = min(len(files), cpu_cores)
|
| 103 |
-
with ThreadPoolExecutor(max_workers=max_workers) as
|
| 104 |
-
results = list(
|
| 105 |
|
| 106 |
df = pd.DataFrame(results, columns=["filename", "alt_text"])
|
| 107 |
return df, df.to_csv(index=False)
|
|
@@ -109,54 +126,37 @@ def generate_alt_text(files, max_len):
|
|
| 109 |
|
| 110 |
def download_csv(csv_str):
|
| 111 |
"""
|
| 112 |
-
Save
|
| 113 |
"""
|
| 114 |
path = "alt_texts.csv"
|
| 115 |
with open(path, 'w', encoding='utf-8') as f:
|
| 116 |
f.write(csv_str)
|
| 117 |
return path
|
| 118 |
|
| 119 |
-
# Build
|
| 120 |
demo = gr.Blocks(title="AI-Powered Alt Text Generator")
|
| 121 |
with demo:
|
| 122 |
-
gr.Markdown("# AI-Powered Alt Text Generator")
|
| 123 |
gr.Markdown(
|
| 124 |
-
"Generate
|
| 125 |
-
"and BLIP, all under a customizable character limit."
|
| 126 |
)
|
| 127 |
|
| 128 |
with gr.Row():
|
| 129 |
-
file_input = gr.File(
|
| 130 |
-
|
| 131 |
-
file_count="multiple",
|
| 132 |
-
file_types=["image"]
|
| 133 |
-
)
|
| 134 |
-
max_len = gr.Slider(
|
| 135 |
-
minimum=50,
|
| 136 |
-
maximum=200,
|
| 137 |
-
step=5,
|
| 138 |
-
value=125,
|
| 139 |
-
label="Max Alt Text Length (chars)"
|
| 140 |
-
)
|
| 141 |
|
| 142 |
-
gallery = gr.Gallery(label="
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
gen_btn = gr.Button("Generate Alt Text")
|
| 146 |
download_btn = gr.Button("Download CSV")
|
| 147 |
|
| 148 |
-
output_df = gr.Dataframe(headers=["filename", "alt_text"], label="Results
|
| 149 |
-
download_file = gr.File(label="
|
| 150 |
|
| 151 |
-
# Event bindings
|
| 152 |
file_input.upload(lambda files: files, inputs=file_input, outputs=gallery)
|
| 153 |
-
gen_btn.click(
|
| 154 |
-
fn=generate_alt_text,
|
| 155 |
-
inputs=[file_input, max_len],
|
| 156 |
-
outputs=[output_df, download_file]
|
| 157 |
-
)
|
| 158 |
download_btn.click(fn=download_csv, inputs=download_file, outputs=download_file)
|
| 159 |
|
| 160 |
-
# Launch the app
|
| 161 |
if __name__ == "__main__":
|
| 162 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
| 7 |
from concurrent.futures import ThreadPoolExecutor
|
| 8 |
import pandas as pd
|
| 9 |
|
| 10 |
+
# Local development: load variables from .env if present
|
| 11 |
+
try:
|
| 12 |
+
from dotenv import load_dotenv
|
| 13 |
+
load_dotenv()
|
| 14 |
+
except ImportError:
|
| 15 |
+
pass # python-dotenv not installed
|
| 16 |
+
|
| 17 |
+
# On Hugging Face Spaces, set this in Settings → Secrets and Variables
|
| 18 |
+
REPLICATE_TOKEN = os.getenv("REPLICATE_API_TOKEN", "")
|
| 19 |
+
replicate.api_token = REPLICATE_TOKEN
|
| 20 |
+
|
| 21 |
+
# Initialize models only if token is present
|
| 22 |
+
token_available = bool(REPLICATE_TOKEN)
|
| 23 |
+
client = replicate.Client() if token_available else None
|
| 24 |
+
blip_model = client.models.get("salesforce/blip-image-captioning-base") if token_available else None
|
| 25 |
+
|
| 26 |
+
# OCR setup
|
| 27 |
gpu_flag = torch.cuda.is_available()
|
| 28 |
ocr_reader = easyocr.Reader(["en"], gpu=gpu_flag)
|
| 29 |
|
| 30 |
+
# (Optional) Object detection using YOLOv5
|
| 31 |
+
use_yolo = True
|
| 32 |
+
if use_yolo:
|
| 33 |
+
yolo_model = torch.hub.load("ultralytics/yolov5", "yolov5s", pretrained=True)
|
| 34 |
+
if gpu_flag:
|
| 35 |
+
yolo_model.to("cuda")
|
| 36 |
+
|
| 37 |
+
# CPU cores for parallelism
|
| 38 |
+
cpu_cores = os.cpu_count() or 4
|
| 39 |
+
|
| 40 |
|
| 41 |
def process_file(file_obj, max_len):
|
| 42 |
"""
|
| 43 |
+
Process a single image: OCR, object detection, BLIP captioning, and truncation.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
"""
|
| 45 |
+
filename = os.path.basename(getattr(file_obj, 'name', file_obj))
|
| 46 |
+
|
| 47 |
+
# If no token, skip processing
|
| 48 |
+
if not token_available:
|
| 49 |
+
return (filename, "Error: REPLICATE_API_TOKEN not set. Add it in Settings → Secrets and Variables.")
|
| 50 |
|
| 51 |
+
# Verify image
|
| 52 |
try:
|
| 53 |
+
with Image.open(getattr(file_obj, 'name', file_obj)).convert('RGB') as img:
|
| 54 |
img.verify()
|
| 55 |
except Exception:
|
| 56 |
return (filename, "<Error opening file>")
|
| 57 |
|
| 58 |
+
# Extract visible text via OCR
|
| 59 |
try:
|
| 60 |
+
texts = ocr_reader.readtext(getattr(file_obj, 'name', file_obj), detail=0)
|
| 61 |
+
visible = ' '.join(texts).strip()
|
| 62 |
except Exception:
|
| 63 |
+
visible = ''
|
| 64 |
+
|
| 65 |
+
# Detect objects via YOLOv5
|
| 66 |
+
labels = ''
|
| 67 |
+
if use_yolo:
|
| 68 |
+
try:
|
| 69 |
+
res = yolo_model(getattr(file_obj, 'name', file_obj))
|
| 70 |
+
labels_list = res.pandas().xyxy[0]['name'].unique().tolist()[:5]
|
| 71 |
+
labels = ', '.join(labels_list)
|
| 72 |
+
except Exception:
|
| 73 |
+
labels = ''
|
| 74 |
+
|
| 75 |
+
# Generate caption via BLIP
|
| 76 |
try:
|
| 77 |
+
with open(getattr(file_obj, 'name', file_obj), 'rb') as img_f:
|
| 78 |
+
output = blip_model.predict(
|
| 79 |
+
image=img_f,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
num_beams=5,
|
| 81 |
max_length=75,
|
| 82 |
min_length=5,
|
| 83 |
no_repeat_ngram_size=2
|
| 84 |
)
|
| 85 |
+
base = output[0] if isinstance(output, list) else str(output)
|
| 86 |
except Exception as e:
|
| 87 |
return (filename, f"Error generating caption: {e}")
|
| 88 |
|
| 89 |
+
# Assemble parts
|
| 90 |
+
parts = [base.strip()]
|
| 91 |
if labels:
|
| 92 |
parts.append(f"Objects: {labels}")
|
| 93 |
+
if visible:
|
| 94 |
+
parts.append(f"Text: {visible}")
|
| 95 |
caption = '; '.join(parts)
|
| 96 |
|
| 97 |
+
# Truncate cleanly
|
| 98 |
if len(caption) > max_len:
|
| 99 |
caption = caption[:max_len].rsplit(' ', 1)[0] + '…'
|
| 100 |
|
| 101 |
return (filename, caption)
|
| 102 |
|
|
|
|
|
|
|
| 103 |
|
| 104 |
def generate_alt_text(files, max_len):
|
| 105 |
"""
|
| 106 |
+
Run processing in parallel and return a DataFrame + CSV for download.
|
| 107 |
"""
|
| 108 |
+
# Handle no-huggingface-token case
|
| 109 |
+
if not token_available:
|
| 110 |
+
df = pd.DataFrame(
|
| 111 |
+
[("", "Error: REPLICATE_API_TOKEN not set. Add it in Settings → Secrets and Variables.")],
|
| 112 |
+
columns=["filename", "alt_text"]
|
| 113 |
+
)
|
| 114 |
+
return df, df.to_csv(index=False)
|
| 115 |
+
|
| 116 |
if not files:
|
| 117 |
return pd.DataFrame(columns=["filename", "alt_text"]), ""
|
| 118 |
|
| 119 |
max_workers = min(len(files), cpu_cores)
|
| 120 |
+
with ThreadPoolExecutor(max_workers=max_workers) as exe:
|
| 121 |
+
results = list(exe.map(lambda f: process_file(f, max_len), files))
|
| 122 |
|
| 123 |
df = pd.DataFrame(results, columns=["filename", "alt_text"])
|
| 124 |
return df, df.to_csv(index=False)
|
|
|
|
| 126 |
|
| 127 |
def download_csv(csv_str):
|
| 128 |
"""
|
| 129 |
+
Save and return path for CSV download.
|
| 130 |
"""
|
| 131 |
path = "alt_texts.csv"
|
| 132 |
with open(path, 'w', encoding='utf-8') as f:
|
| 133 |
f.write(csv_str)
|
| 134 |
return path
|
| 135 |
|
| 136 |
+
# Build Gradio app
|
| 137 |
demo = gr.Blocks(title="AI-Powered Alt Text Generator")
|
| 138 |
with demo:
|
| 139 |
+
gr.Markdown("# AI-Powered Alt Text Generator (Hugging Face Spaces)")
|
| 140 |
gr.Markdown(
|
| 141 |
+
"Generate rich alt text with OCR, object detection, and BLIP—all under your custom length."
|
|
|
|
| 142 |
)
|
| 143 |
|
| 144 |
with gr.Row():
|
| 145 |
+
file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
|
| 146 |
+
max_len = gr.Slider(50, 200, step=5, value=125, label="Max Alt Text Length (chars)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
gallery = gr.Gallery(label="Preview", columns=4).style(grid=[4], height="auto")
|
| 149 |
|
| 150 |
with gr.Row():
|
| 151 |
gen_btn = gr.Button("Generate Alt Text")
|
| 152 |
download_btn = gr.Button("Download CSV")
|
| 153 |
|
| 154 |
+
output_df = gr.Dataframe(headers=["filename", "alt_text"], label="Results")
|
| 155 |
+
download_file = gr.File(label="CSV File")
|
| 156 |
|
|
|
|
| 157 |
file_input.upload(lambda files: files, inputs=file_input, outputs=gallery)
|
| 158 |
+
gen_btn.click(fn=generate_alt_text, inputs=[file_input, max_len], outputs=[output_df, download_file])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
download_btn.click(fn=download_csv, inputs=download_file, outputs=download_file)
|
| 160 |
|
|
|
|
| 161 |
if __name__ == "__main__":
|
| 162 |
demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
|