Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,8 @@ import easyocr
|
|
| 6 |
|
| 7 |
# --- Configuration ---
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
-
model_name
|
|
|
|
| 10 |
|
| 11 |
# Ensure you have sentencepiece installed: pip install sentencepiece
|
| 12 |
# Load BLIP-2 base model (balanced performance)
|
|
@@ -42,17 +43,11 @@ def generate_alt_text(files, max_tokens):
|
|
| 42 |
visible_text = ''
|
| 43 |
|
| 44 |
# BLIP2 captioning with an enhanced prompt
|
| 45 |
-
# The goal here is to encourage more descriptive and less generic output.
|
| 46 |
-
# We prompt the model to think about context, action, and key elements.
|
| 47 |
blip2_prompt = (
|
| 48 |
"Describe this image in detail for a visually impaired user, "
|
| 49 |
"focusing on the main subjects, actions, and overall scene. "
|
| 50 |
"What is happening and what are the key elements?"
|
| 51 |
)
|
| 52 |
-
# You could also try:
|
| 53 |
-
# "Generate a rich, descriptive alt text for this image that captures its essence and key visual information."
|
| 54 |
-
# "Provide a comprehensive alt text description of this image, including objects, actions, and context."
|
| 55 |
-
|
| 56 |
|
| 57 |
inputs = processor(
|
| 58 |
images=image,
|
|
@@ -63,9 +58,9 @@ def generate_alt_text(files, max_tokens):
|
|
| 63 |
outputs = model.generate(
|
| 64 |
**inputs,
|
| 65 |
max_new_tokens=int(max_tokens),
|
| 66 |
-
num_beams=5,
|
| 67 |
no_repeat_ngram_size=2,
|
| 68 |
-
length_penalty=0.8
|
| 69 |
)
|
| 70 |
caption = processor.decode(outputs[0], skip_special_tokens=True).strip()
|
| 71 |
|
|
@@ -77,14 +72,11 @@ def generate_alt_text(files, max_tokens):
|
|
| 77 |
|
| 78 |
# Combine with OCR text if any
|
| 79 |
if visible_text:
|
| 80 |
-
# Integrate OCR text naturally into the description if possible,
|
| 81 |
-
# or append it clearly.
|
| 82 |
if len(caption) > 0 and not caption.endswith('.'):
|
| 83 |
caption = f"{caption}. Text on image: '{visible_text}'."
|
| 84 |
else:
|
| 85 |
caption = f"{caption} Text on image: '{visible_text}'."
|
| 86 |
|
| 87 |
-
|
| 88 |
captions.append(caption)
|
| 89 |
return captions
|
| 90 |
|
|
@@ -106,7 +98,7 @@ with gr.Blocks(title="Balanced High‑End Alt Text Generator") as demo:
|
|
| 106 |
minimum=16,
|
| 107 |
maximum=128,
|
| 108 |
step=16,
|
| 109 |
-
value=64,
|
| 110 |
label="Max Tokens for Caption"
|
| 111 |
)
|
| 112 |
generate_btn = gr.Button("Generate Alt Text")
|
|
@@ -128,7 +120,7 @@ with gr.Blocks(title="Balanced High‑End Alt Text Generator") as demo:
|
|
| 128 |
queue=True
|
| 129 |
)
|
| 130 |
clear_btn.click(
|
| 131 |
-
lambda: ([], 64, []),
|
| 132 |
inputs=None,
|
| 133 |
outputs=[file_input, max_tokens, gallery, output]
|
| 134 |
)
|
|
|
|
| 6 |
|
| 7 |
# --- Configuration ---
|
| 8 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
# *** FIX HERE: Change model_name to a valid Hugging Face model identifier ***
|
| 10 |
+
model_name = "Salesforce/blip2-flan-t5-xl" # Using the 'xl' version as a common choice
|
| 11 |
|
| 12 |
# Ensure you have sentencepiece installed: pip install sentencepiece
|
| 13 |
# Load BLIP-2 base model (balanced performance)
|
|
|
|
| 43 |
visible_text = ''
|
| 44 |
|
| 45 |
# BLIP2 captioning with an enhanced prompt
|
|
|
|
|
|
|
| 46 |
blip2_prompt = (
|
| 47 |
"Describe this image in detail for a visually impaired user, "
|
| 48 |
"focusing on the main subjects, actions, and overall scene. "
|
| 49 |
"What is happening and what are the key elements?"
|
| 50 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
inputs = processor(
|
| 53 |
images=image,
|
|
|
|
| 58 |
outputs = model.generate(
|
| 59 |
**inputs,
|
| 60 |
max_new_tokens=int(max_tokens),
|
| 61 |
+
num_beams=5,
|
| 62 |
no_repeat_ngram_size=2,
|
| 63 |
+
length_penalty=0.8
|
| 64 |
)
|
| 65 |
caption = processor.decode(outputs[0], skip_special_tokens=True).strip()
|
| 66 |
|
|
|
|
| 72 |
|
| 73 |
# Combine with OCR text if any
|
| 74 |
if visible_text:
|
|
|
|
|
|
|
| 75 |
if len(caption) > 0 and not caption.endswith('.'):
|
| 76 |
caption = f"{caption}. Text on image: '{visible_text}'."
|
| 77 |
else:
|
| 78 |
caption = f"{caption} Text on image: '{visible_text}'."
|
| 79 |
|
|
|
|
| 80 |
captions.append(caption)
|
| 81 |
return captions
|
| 82 |
|
|
|
|
| 98 |
minimum=16,
|
| 99 |
maximum=128,
|
| 100 |
step=16,
|
| 101 |
+
value=64,
|
| 102 |
label="Max Tokens for Caption"
|
| 103 |
)
|
| 104 |
generate_btn = gr.Button("Generate Alt Text")
|
|
|
|
| 120 |
queue=True
|
| 121 |
)
|
| 122 |
clear_btn.click(
|
| 123 |
+
lambda: ([], 64, []),
|
| 124 |
inputs=None,
|
| 125 |
outputs=[file_input, max_tokens, gallery, output]
|
| 126 |
)
|