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

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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
+ )
10
+ 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
+
18
+ inputs = processor(image, return_tensors="pt")
19
+
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
+
35
+ # Gradio UI
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()