Inam65 commited on
Commit
e402baa
·
verified ·
1 Parent(s): a7fae6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -21
app.py CHANGED
@@ -3,7 +3,7 @@ from PIL import Image
3
  from transformers import BlipProcessor, BlipForConditionalGeneration
4
  import torch
5
 
6
- # Load model and processor
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=50
24
  )
25
 
26
  caption = processor.decode(
27
  output[0],
28
  skip_special_tokens=True
29
- )
30
 
31
- alt_text = f"Alt text: {caption.capitalize()}."
 
 
 
 
 
 
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
- Upload an image and instantly generate SEO-friendly and accessibility-ready alt text.
40
  """)
41
 
42
- with gr.Row():
43
- image_input = gr.Image(
44
- type="pil",
45
- label="Upload Image"
46
- )
47
 
48
- alt_text_output = gr.Textbox(
49
- label="Generated Alt Text",
50
- lines=4,
51
- placeholder="Your alt text will appear here..."
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
- ### How to use
64
- - Upload an image
65
- - Click **Generate Alt Text**
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()