Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ from PIL import Image
|
|
| 3 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 4 |
import torch
|
| 5 |
|
| 6 |
-
# Load model
|
| 7 |
processor = BlipProcessor.from_pretrained(
|
| 8 |
"Salesforce/blip-image-captioning-base"
|
| 9 |
)
|
|
@@ -11,7 +11,7 @@ model = BlipForConditionalGeneration.from_pretrained(
|
|
| 11 |
"Salesforce/blip-image-captioning-base"
|
| 12 |
)
|
| 13 |
|
| 14 |
-
def generate_alt_text(image: Image.Image):
|
| 15 |
if image is None:
|
| 16 |
return ""
|
| 17 |
|
|
@@ -20,15 +20,21 @@ def generate_alt_text(image: Image.Image):
|
|
| 20 |
with torch.no_grad():
|
| 21 |
output = model.generate(
|
| 22 |
**inputs,
|
| 23 |
-
max_new_tokens=
|
| 24 |
)
|
| 25 |
|
| 26 |
caption = processor.decode(
|
| 27 |
output[0],
|
| 28 |
skip_special_tokens=True
|
| 29 |
-
)
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
return alt_text
|
| 34 |
|
|
@@ -36,34 +42,37 @@ def generate_alt_text(image: Image.Image):
|
|
| 36 |
with gr.Blocks(title="Alt Text Generator") as demo:
|
| 37 |
gr.Markdown("""
|
| 38 |
# 🖼️ Alt Text Generator
|
| 39 |
-
|
| 40 |
""")
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
generate_btn = gr.Button("Generate Alt Text 🚀")
|
| 55 |
|
| 56 |
generate_btn.click(
|
| 57 |
fn=generate_alt_text,
|
| 58 |
-
inputs=image_input,
|
| 59 |
outputs=alt_text_output
|
| 60 |
)
|
| 61 |
|
| 62 |
gr.Markdown("""
|
| 63 |
-
###
|
| 64 |
-
-
|
| 65 |
-
-
|
| 66 |
-
- Copy & paste into your website's `<img alt="">` attribute
|
| 67 |
""")
|
| 68 |
|
| 69 |
demo.launch()
|
|
|
|
| 3 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 4 |
import torch
|
| 5 |
|
| 6 |
+
# Load model
|
| 7 |
processor = BlipProcessor.from_pretrained(
|
| 8 |
"Salesforce/blip-image-captioning-base"
|
| 9 |
)
|
|
|
|
| 11 |
"Salesforce/blip-image-captioning-base"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
def generate_alt_text(image: Image.Image, seo_mode: bool):
|
| 15 |
if image is None:
|
| 16 |
return ""
|
| 17 |
|
|
|
|
| 20 |
with torch.no_grad():
|
| 21 |
output = model.generate(
|
| 22 |
**inputs,
|
| 23 |
+
max_new_tokens=60 if seo_mode else 40
|
| 24 |
)
|
| 25 |
|
| 26 |
caption = processor.decode(
|
| 27 |
output[0],
|
| 28 |
skip_special_tokens=True
|
| 29 |
+
).strip()
|
| 30 |
|
| 31 |
+
if seo_mode:
|
| 32 |
+
alt_text = (
|
| 33 |
+
f"{caption.capitalize()}, high-quality image suitable for websites, "
|
| 34 |
+
f"blogs, and SEO-optimized content."
|
| 35 |
+
)
|
| 36 |
+
else:
|
| 37 |
+
alt_text = caption.capitalize() + "."
|
| 38 |
|
| 39 |
return alt_text
|
| 40 |
|
|
|
|
| 42 |
with gr.Blocks(title="Alt Text Generator") as demo:
|
| 43 |
gr.Markdown("""
|
| 44 |
# 🖼️ Alt Text Generator
|
| 45 |
+
Generate accessibility-friendly and SEO-optimized alt text for images.
|
| 46 |
""")
|
| 47 |
|
| 48 |
+
image_input = gr.Image(
|
| 49 |
+
type="pil",
|
| 50 |
+
label="Upload Image"
|
| 51 |
+
)
|
|
|
|
| 52 |
|
| 53 |
+
seo_toggle = gr.Checkbox(
|
| 54 |
+
label="SEO Mode (longer, keyword-friendly alt text)",
|
| 55 |
+
value=False
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
alt_text_output = gr.Textbox(
|
| 59 |
+
label="Generated Alt Text",
|
| 60 |
+
lines=4,
|
| 61 |
+
placeholder="Your alt text will appear here..."
|
| 62 |
+
)
|
| 63 |
|
| 64 |
generate_btn = gr.Button("Generate Alt Text 🚀")
|
| 65 |
|
| 66 |
generate_btn.click(
|
| 67 |
fn=generate_alt_text,
|
| 68 |
+
inputs=[image_input, seo_toggle],
|
| 69 |
outputs=alt_text_output
|
| 70 |
)
|
| 71 |
|
| 72 |
gr.Markdown("""
|
| 73 |
+
### ℹ️ Tips
|
| 74 |
+
- **SEO Mode ON** → Best for blog posts, landing pages, e-commerce
|
| 75 |
+
- **SEO Mode OFF** → Best for pure accessibility & clean HTML
|
|
|
|
| 76 |
""")
|
| 77 |
|
| 78 |
demo.launch()
|