Workpam commited on
Commit
3a727f9
Β·
verified Β·
1 Parent(s): ea537b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +246 -57
app.py CHANGED
@@ -1,70 +1,259 @@
1
  import gradio as gr
 
 
 
 
 
2
  from transformers import BlipProcessor, BlipForConditionalGeneration
3
  import torch
4
- from PIL import Image
5
- import easyocr
6
-
7
- # Device setup
8
- device = "cuda" if torch.cuda.is_available() else "cpu"
9
 
10
- # OCR reader (English)
11
- reader = easyocr.Reader(['en'], gpu=(device=='cuda'))
 
12
 
13
- # Load BLIP (large) model for high-quality image captioning
14
- processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
15
- model = BlipForConditionalGeneration.from_pretrained(
16
- "Salesforce/blip-image-captioning-large",
17
- torch_dtype=torch.float16 if device=='cuda' else torch.float32
18
- ).to(device)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- # Generate alt-text including OCR-detected text
21
- def generate_alt_text(files):
22
- captions = []
23
- for f in files:
24
- path = f.name if hasattr(f, 'name') else f
 
 
 
25
  try:
26
- # Load image
27
- image = Image.open(path).convert('RGB')
28
- # Extract visible text via OCR
29
- ocr_results = reader.readtext(path, detail=0)
30
- visible_text = ' '.join(ocr_results).strip() or '<no text detected>'
31
- # Prepare a prompt instructing model to include visible text
32
- prompt = f"Describe this image in one sentence, and mention this text: '{visible_text}'."
33
- # Tokenize inputs
34
- inputs = processor(images=image, text=prompt, return_tensors="pt").to(device)
35
- # Generate caption
36
- with torch.no_grad():
37
- out = model.generate(**inputs, max_new_tokens=50)
38
- text = processor.decode(out[0], skip_special_tokens=True)
 
 
39
  except Exception as e:
40
- text = f"<Error processing {path}: {e}>"
41
- captions.append(text.strip())
42
- # Numbered list
43
- return "\n".join(f"{i+1}. {cap}" for i, cap in enumerate(captions))
 
 
 
 
44
 
45
- # Build Gradio UI
46
- demo = gr.Blocks()
47
- with demo:
48
- gr.Markdown("# Accurate Alt Text Generator with OCR + BLIP")
49
- gr.Markdown("Upload multiple images, preview them, and get concise descriptions including embedded text.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
52
- gallery = gr.Gallery(label="Image Preview", columns=4)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  with gr.Row():
55
- generate_btn = gr.Button("Generate Alt Text")
56
- clear_btn = gr.Button("Clear All")
57
- copy_btn = gr.Button("Copy to Clipboard")
58
-
59
- output = gr.Textbox(label="Generated Alt Texts", lines=10, elem_id="output_box")
60
-
61
- # Bind events
62
- file_input.upload(lambda files: files, inputs=file_input, outputs=gallery)
63
- generate_btn.click(generate_alt_text, inputs=file_input, outputs=output)
64
- clear_btn.click(lambda: ([], [], ""), inputs=None, outputs=[file_input, gallery, output])
65
- copy_btn.click(None, None, None, js="() => { navigator.clipboard.writeText(document.getElementById('output_box').value); }")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
- # Optional: queueing
68
- demo.queue(max_size=10)
69
- # Launch
70
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
 
 
 
1
  import gradio as gr
2
+ import requests
3
+ import base64
4
+ from PIL import Image
5
+ import io
6
+ import json
7
  from transformers import BlipProcessor, BlipForConditionalGeneration
8
  import torch
 
 
 
 
 
9
 
10
+ # Initialize the BLIP model for image captioning
11
+ processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
12
+ model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
13
 
14
+ def generate_alt_text(image):
15
+ """Generate alt text for a single image using BLIP model"""
16
+ try:
17
+ # Process the image
18
+ inputs = processor(image, return_tensors="pt")
19
+
20
+ # Generate caption
21
+ with torch.no_grad():
22
+ out = model.generate(**inputs, max_length=150, num_beams=5, early_stopping=True)
23
+
24
+ # Decode the generated text
25
+ caption = processor.decode(out[0], skip_special_tokens=True)
26
+
27
+ # Clean up the caption to make it more suitable as alt text
28
+ if caption.lower().startswith("a picture of"):
29
+ caption = caption[12:].strip()
30
+ elif caption.lower().startswith("an image of"):
31
+ caption = caption[11:].strip()
32
+
33
+ # Capitalize first letter
34
+ caption = caption[0].upper() + caption[1:] if caption else "Image content could not be determined"
35
+
36
+ return caption
37
+
38
+ except Exception as e:
39
+ return f"Error generating alt text: {str(e)}"
40
 
41
+ def process_images(files):
42
+ """Process multiple uploaded images and generate alt text for each"""
43
+ if not files:
44
+ return "No images uploaded"
45
+
46
+ results = []
47
+
48
+ for i, file in enumerate(files):
49
  try:
50
+ # Open and process the image
51
+ image = Image.open(file.name).convert('RGB')
52
+
53
+ # Generate alt text
54
+ alt_text = generate_alt_text(image)
55
+
56
+ # Create result dictionary
57
+ result = {
58
+ "image_index": i + 1,
59
+ "filename": file.name.split('/')[-1],
60
+ "alt_text": alt_text,
61
+ "image": image
62
+ }
63
+ results.append(result)
64
+
65
  except Exception as e:
66
+ results.append({
67
+ "image_index": i + 1,
68
+ "filename": file.name.split('/')[-1] if hasattr(file, 'name') else f"Image {i+1}",
69
+ "alt_text": f"Error processing image: {str(e)}",
70
+ "image": None
71
+ })
72
+
73
+ return results
74
 
75
+ def create_result_html(results):
76
+ """Create HTML output with images, alt text, and copy buttons"""
77
+ if not results:
78
+ return "<p>No results to display</p>"
79
+
80
+ html = """
81
+ <div style="font-family: Arial, sans-serif;">
82
+ <h3>Generated Alt Text Results</h3>
83
+ <script>
84
+ function copyToClipboard(text, buttonId) {
85
+ navigator.clipboard.writeText(text).then(function() {
86
+ const button = document.getElementById(buttonId);
87
+ const originalText = button.innerHTML;
88
+ button.innerHTML = 'βœ“ Copied!';
89
+ button.style.backgroundColor = '#4CAF50';
90
+ setTimeout(function() {
91
+ button.innerHTML = originalText;
92
+ button.style.backgroundColor = '#008CBA';
93
+ }, 2000);
94
+ });
95
+ }
96
+ </script>
97
+ <style>
98
+ .result-container {
99
+ border: 1px solid #ddd;
100
+ border-radius: 8px;
101
+ padding: 15px;
102
+ margin: 10px 0;
103
+ background-color: #f9f9f9;
104
+ }
105
+ .image-preview {
106
+ max-width: 300px;
107
+ max-height: 200px;
108
+ border-radius: 5px;
109
+ margin-bottom: 10px;
110
+ }
111
+ .alt-text-container {
112
+ background-color: white;
113
+ padding: 10px;
114
+ border-radius: 5px;
115
+ border: 1px solid #eee;
116
+ margin: 10px 0;
117
+ }
118
+ .copy-button {
119
+ background-color: #008CBA;
120
+ color: white;
121
+ border: none;
122
+ padding: 8px 16px;
123
+ border-radius: 4px;
124
+ cursor: pointer;
125
+ font-size: 14px;
126
+ margin-top: 5px;
127
+ }
128
+ .copy-button:hover {
129
+ background-color: #007B9A;
130
+ }
131
+ .filename {
132
+ font-weight: bold;
133
+ color: #333;
134
+ margin-bottom: 5px;
135
+ }
136
+ </style>
137
+ """
138
+
139
+ for i, result in enumerate(results):
140
+ button_id = f"copy-btn-{i}"
141
+ html += f"""
142
+ <div class="result-container">
143
+ <div class="filename">πŸ“ {result['filename']}</div>
144
+ <div class="alt-text-container">
145
+ <strong>Alt Text:</strong><br>
146
+ <span id="alt-text-{i}">{result['alt_text']}</span>
147
+ </div>
148
+ <button class="copy-button" id="{button_id}" onclick="copyToClipboard('{result['alt_text'].replace("'", "\\'")}', '{button_id}')">
149
+ πŸ“‹ Copy Alt Text
150
+ </button>
151
+ </div>
152
+ """
153
+
154
+ html += "</div>"
155
+ return html
156
 
157
+ def main_process(files):
158
+ """Main processing function for the Gradio interface"""
159
+ if not files:
160
+ return "<p>Please upload at least one image to generate alt text.</p>", []
161
+
162
+ # Process all uploaded images
163
+ results = process_images(files)
164
+
165
+ # Create HTML output
166
+ html_output = create_result_html(results)
167
+
168
+ # Create gallery of images for display
169
+ gallery_images = []
170
+ for result in results:
171
+ if result['image']:
172
+ gallery_images.append(result['image'])
173
+
174
+ return html_output, gallery_images
175
 
176
+ # Create the Gradio interface
177
+ with gr.Blocks(title="Alt Text Generator", theme=gr.themes.Soft()) as demo:
178
+ gr.Markdown("""
179
+ # πŸ–ΌοΈ Alt Text Generator
180
+
181
+ Upload multiple images and generate descriptive alt text for each one.
182
+ The AI will analyze both visual content and any text present in the images.
183
+
184
+ **Features:**
185
+ - Support for multiple image uploads
186
+ - AI-powered alt text generation
187
+ - Copy-to-clipboard functionality for each generated alt text
188
+ - Preview of all uploaded images
189
+ """)
190
+
191
  with gr.Row():
192
+ with gr.Column():
193
+ file_input = gr.Files(
194
+ label="Upload Images",
195
+ file_count="multiple",
196
+ file_types=["image"],
197
+ height=200
198
+ )
199
+
200
+ process_btn = gr.Button(
201
+ "Generate Alt Text",
202
+ variant="primary",
203
+ size="lg"
204
+ )
205
+
206
+ with gr.Column():
207
+ image_gallery = gr.Gallery(
208
+ label="Uploaded Images",
209
+ show_label=True,
210
+ elem_id="gallery",
211
+ columns=3,
212
+ rows=2,
213
+ height="auto"
214
+ )
215
+
216
+ with gr.Row():
217
+ output_html = gr.HTML(
218
+ label="Generated Alt Text Results",
219
+ show_label=True
220
+ )
221
+
222
+ # Event handlers
223
+ process_btn.click(
224
+ fn=main_process,
225
+ inputs=[file_input],
226
+ outputs=[output_html, image_gallery]
227
+ )
228
+
229
+ # Auto-process when files are uploaded
230
+ file_input.change(
231
+ fn=main_process,
232
+ inputs=[file_input],
233
+ outputs=[output_html, image_gallery]
234
+ )
235
+
236
+ gr.Markdown("""
237
+ ### πŸ“ How to Use:
238
+ 1. **Upload Images**: Click "Upload Images" and select one or more image files
239
+ 2. **Generate Alt Text**: Click "Generate Alt Text" or wait for automatic processing
240
+ 3. **Copy Alt Text**: Use the "Copy Alt Text" button next to each generated description
241
+ 4. **Use the Alt Text**: Paste the copied text into your HTML `alt` attributes
242
+
243
+ ### 🎯 About Alt Text:
244
+ Alt text (alternative text) is crucial for web accessibility. It helps screen readers describe images to visually impaired users and provides context when images fail to load.
245
+
246
+ ### πŸ”§ Technical Details:
247
+ - Uses Salesforce's BLIP model for image captioning
248
+ - Processes both visual elements and text within images
249
+ - Generates human-readable descriptions suitable for alt attributes
250
+ """)
251
 
252
+ # Launch the app
253
+ if __name__ == "__main__":
254
+ demo.launch(
255
+ server_name="0.0.0.0",
256
+ server_port=7860,
257
+ share=True,
258
+ debug=True
259
+ )