Update app.py
Browse files
app.py
CHANGED
|
@@ -1,218 +1,190 @@
|
|
| 1 |
import torch
|
| 2 |
from PIL import Image
|
| 3 |
import torchvision.transforms as T
|
| 4 |
-
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 5 |
import gradio as gr
|
| 6 |
import time
|
| 7 |
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Device setup
|
| 10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
print(f"Using device: {device}")
|
| 12 |
|
| 13 |
-
# Load BLIP model for
|
| 14 |
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 15 |
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 16 |
blip_model.to(device).eval()
|
| 17 |
|
| 18 |
-
# Optimization for speed
|
| 19 |
if device == "cuda":
|
| 20 |
blip_model = blip_model.half()
|
| 21 |
torch.backends.cudnn.benchmark = True
|
| 22 |
|
| 23 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
transform = T.Compose([
|
| 25 |
T.Resize((384, 384)),
|
| 26 |
T.ToTensor(),
|
| 27 |
])
|
| 28 |
|
| 29 |
-
|
| 30 |
-
"""Enhance basic caption with more detailed, engaging descriptions"""
|
| 31 |
-
|
| 32 |
-
# Load image for additional analysis
|
| 33 |
-
image = Image.open(image_path).convert("RGB")
|
| 34 |
-
|
| 35 |
-
# Generate enhanced version with cleaner prompting
|
| 36 |
-
try:
|
| 37 |
-
# Simple, direct prompt to avoid meta-text
|
| 38 |
-
inputs = blip_processor(
|
| 39 |
-
image,
|
| 40 |
-
text=f"Describe in detail: {basic_caption}",
|
| 41 |
-
return_tensors="pt"
|
| 42 |
-
).to(device)
|
| 43 |
-
|
| 44 |
-
with torch.no_grad():
|
| 45 |
-
outputs = blip_model.generate(
|
| 46 |
-
**inputs,
|
| 47 |
-
max_length=60,
|
| 48 |
-
num_beams=3,
|
| 49 |
-
do_sample=True,
|
| 50 |
-
temperature=0.7,
|
| 51 |
-
repetition_penalty=1.2,
|
| 52 |
-
early_stopping=True
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
enhanced = blip_processor.decode(outputs[0], skip_special_tokens=True)
|
| 56 |
-
|
| 57 |
-
# Clean the output thoroughly
|
| 58 |
-
enhanced = clean_and_improve_caption(enhanced)
|
| 59 |
-
|
| 60 |
-
# If enhancement didn't work well, fall back to improved basic
|
| 61 |
-
if len(enhanced) < len(basic_caption) or not enhanced:
|
| 62 |
-
return improve_basic_caption(basic_caption, "standard")
|
| 63 |
-
|
| 64 |
-
return enhanced
|
| 65 |
-
|
| 66 |
-
except Exception as e:
|
| 67 |
-
return improve_basic_caption(basic_caption, "standard")
|
| 68 |
-
|
| 69 |
-
def improve_basic_caption(caption, style_context):
|
| 70 |
-
"""Generate detailed, specific alt text descriptions"""
|
| 71 |
-
|
| 72 |
-
# Analyze caption for specific objects and contexts
|
| 73 |
-
caption_lower = caption.lower()
|
| 74 |
-
|
| 75 |
-
# Detect specific object types for detailed descriptions
|
| 76 |
-
detailed_templates = {
|
| 77 |
-
'box': 'Box for {content}',
|
| 78 |
-
'book': 'Book titled {content}',
|
| 79 |
-
'game': 'Board game box for "{content}"',
|
| 80 |
-
'card': 'Playing cards featuring {content}',
|
| 81 |
-
'poster': 'Poster displaying {content}',
|
| 82 |
-
'package': 'Product package for {content}',
|
| 83 |
-
'cover': 'Cover art showing {content}',
|
| 84 |
-
'label': 'Product label featuring {content}'
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
# Color enhancement mapping
|
| 88 |
-
color_enhancements = {
|
| 89 |
-
'pink': 'vibrant pink', 'red': 'bright red', 'blue': 'deep blue',
|
| 90 |
-
'green': 'lush green', 'yellow': 'golden yellow', 'purple': 'rich purple',
|
| 91 |
-
'orange': 'vibrant orange', 'white': 'pure white', 'black': 'deep black',
|
| 92 |
-
'brown': 'rich brown', 'gray': 'soft gray', 'grey': 'soft grey'
|
| 93 |
-
}
|
| 94 |
-
|
| 95 |
-
# Object-specific detailed descriptions
|
| 96 |
-
object_details = {
|
| 97 |
-
'tree': 'tree with detailed branches and foliage',
|
| 98 |
-
'flower': 'flowering plant with delicate petals',
|
| 99 |
-
'blossom': 'cherry blossom with pink petals',
|
| 100 |
-
'grass': 'green grass field',
|
| 101 |
-
'mountain': 'mountain landscape with peaks',
|
| 102 |
-
'water': 'clear water surface',
|
| 103 |
-
'sky': 'sky with clouds',
|
| 104 |
-
'building': 'architectural structure',
|
| 105 |
-
'person': 'person in the scene',
|
| 106 |
-
'animal': 'animal figure'
|
| 107 |
-
}
|
| 108 |
-
|
| 109 |
-
# Start building enhanced description
|
| 110 |
-
enhanced_parts = []
|
| 111 |
-
|
| 112 |
-
# Detect if it's a product/game box
|
| 113 |
-
if any(word in caption_lower for word in ['box', 'game', 'card', 'package']):
|
| 114 |
-
if 'game' in caption_lower:
|
| 115 |
-
enhanced_parts.append('Board game box')
|
| 116 |
-
elif 'card' in caption_lower:
|
| 117 |
-
enhanced_parts.append('Card game box')
|
| 118 |
-
else:
|
| 119 |
-
enhanced_parts.append('Product box')
|
| 120 |
-
|
| 121 |
-
# Add specific color descriptions
|
| 122 |
-
enhanced_caption = caption
|
| 123 |
-
for basic_color, detailed_color in color_enhancements.items():
|
| 124 |
-
if basic_color in caption_lower:
|
| 125 |
-
enhanced_caption = enhanced_caption.replace(basic_color, detailed_color)
|
| 126 |
-
|
| 127 |
-
# Add detailed object descriptions
|
| 128 |
-
for obj, detail in object_details.items():
|
| 129 |
-
if obj in caption_lower and obj not in enhanced_caption:
|
| 130 |
-
enhanced_caption = enhanced_caption.replace(obj, detail)
|
| 131 |
-
|
| 132 |
-
# Add environmental context
|
| 133 |
-
environmental_additions = []
|
| 134 |
-
if 'tree' in caption_lower:
|
| 135 |
-
environmental_additions.append('surrounded by natural scenery')
|
| 136 |
-
if 'grass' in caption_lower:
|
| 137 |
-
environmental_additions.append('with green grass field')
|
| 138 |
-
if 'flower' in caption_lower or 'blossom' in caption_lower:
|
| 139 |
-
environmental_additions.append('featuring delicate floral elements')
|
| 140 |
-
|
| 141 |
-
# Construct final detailed description
|
| 142 |
-
if enhanced_parts:
|
| 143 |
-
final_description = ' '.join(enhanced_parts)
|
| 144 |
-
if 'featuring' not in enhanced_caption:
|
| 145 |
-
final_description += f' featuring {enhanced_caption.lower()}'
|
| 146 |
-
else:
|
| 147 |
-
final_description += f' {enhanced_caption.lower()}'
|
| 148 |
-
else:
|
| 149 |
-
final_description = enhanced_caption
|
| 150 |
-
|
| 151 |
-
# Add environmental context
|
| 152 |
-
if environmental_additions:
|
| 153 |
-
for addition in environmental_additions[:2]: # Limit to avoid redundancy
|
| 154 |
-
if addition.split()[-1] not in final_description:
|
| 155 |
-
final_description += f', {addition}'
|
| 156 |
-
|
| 157 |
-
return final_description.capitalize()
|
| 158 |
|
| 159 |
def clean_and_improve_caption(caption):
|
| 160 |
-
"""Clean
|
| 161 |
-
|
| 162 |
-
# Remove all prompts and AI-generated meta text
|
| 163 |
caption = re.sub(r'describe this.*?:', '', caption, flags=re.IGNORECASE)
|
| 164 |
-
caption = re.sub(r'describe.*?
|
| 165 |
-
caption = re.sub(r'
|
| 166 |
-
|
| 167 |
-
caption = re.sub(r'(create|image|picture|photo|this is|shows|depicts|captures|features|showcases|displays|presents)\s*', '', caption, flags=re.IGNORECASE)
|
| 168 |
-
caption = re.sub(r'(a detailed and vivid description of|a professional and descriptive caption of)', '', caption, flags=re.IGNORECASE)
|
| 169 |
caption = re.sub(r'^(this|that|the|an|a)\s+', '', caption, flags=re.IGNORECASE)
|
| 170 |
-
|
| 171 |
-
# Remove colons and extra punctuation at the start
|
| 172 |
caption = re.sub(r'^[:\s]+', '', caption)
|
| 173 |
-
caption = re.sub(r'^\W+', '', caption)
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
caption = ' '.join(caption.split())
|
| 177 |
-
caption = caption.strip()
|
| 178 |
-
|
| 179 |
-
# Don't add any prefixes - keep it clean and direct
|
| 180 |
if caption:
|
| 181 |
-
|
| 182 |
-
caption = caption[0].upper() + caption[1:] if len(caption) > 1 else caption.upper()
|
| 183 |
-
|
| 184 |
return caption
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
def generate_detailed_caption(image_path, style="detailed"):
|
| 187 |
-
"""
|
| 188 |
try:
|
| 189 |
-
# Load image
|
| 190 |
image = Image.open(image_path).convert("RGB")
|
| 191 |
-
|
| 192 |
-
# Resize if too large for processing speed
|
| 193 |
if max(image.size) > 800:
|
| 194 |
image.thumbnail((800, 800), Image.Resampling.LANCZOS)
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
inputs =
|
| 198 |
-
|
| 199 |
with torch.no_grad():
|
| 200 |
outputs = blip_model.generate(
|
| 201 |
**inputs,
|
| 202 |
-
|
| 203 |
num_beams=4,
|
| 204 |
-
do_sample=True,
|
| 205 |
-
temperature=0.7,
|
| 206 |
repetition_penalty=1.2,
|
| 207 |
-
early_stopping=True
|
|
|
|
| 208 |
)
|
| 209 |
-
|
| 210 |
basic_caption = blip_processor.decode(outputs[0], skip_special_tokens=True)
|
| 211 |
-
|
| 212 |
-
# Clean the basic caption first
|
| 213 |
clean_caption = clean_and_improve_caption(basic_caption)
|
| 214 |
-
|
| 215 |
-
# Then enhance it based on style
|
| 216 |
if style == "detailed":
|
| 217 |
final_caption = improve_basic_caption(clean_caption, "detailed")
|
| 218 |
elif style == "professional":
|
|
@@ -221,109 +193,171 @@ def generate_detailed_caption(image_path, style="detailed"):
|
|
| 221 |
final_caption = add_creative_context(clean_caption)
|
| 222 |
else:
|
| 223 |
final_caption = clean_caption
|
| 224 |
-
|
| 225 |
return final_caption if final_caption else "Image content not clearly identifiable"
|
| 226 |
-
|
| 227 |
-
except Exception as e:
|
| 228 |
return "Unable to generate description"
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
def make_professional_alt_text(caption):
|
| 231 |
-
"""Convert to professional alt text format"""
|
| 232 |
caption_lower = caption.lower()
|
| 233 |
-
|
| 234 |
-
# Professional alt text patterns
|
| 235 |
if 'box' in caption_lower and ('game' in caption_lower or 'card' in caption_lower):
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
return 'Box for board game featuring cherry blossom tree artwork with pink flowers and green foliage'
|
| 239 |
elif 'tree' in caption_lower:
|
| 240 |
return f'Game box artwork showing {caption_lower.replace("box", "").strip()}'
|
| 241 |
-
|
| 242 |
-
# Product-specific formatting
|
| 243 |
product_keywords = ['package', 'container', 'bottle', 'can', 'jar']
|
| 244 |
if any(keyword in caption_lower for keyword in product_keywords):
|
| 245 |
return f'Product {caption_lower}'
|
| 246 |
-
|
| 247 |
-
# Default professional format
|
| 248 |
return caption.capitalize()
|
| 249 |
|
| 250 |
def add_creative_context(caption):
|
| 251 |
-
"""Add creative, artistic context while keeping specificity"""
|
| 252 |
caption_lower = caption.lower()
|
| 253 |
-
|
| 254 |
-
# Add artistic context for different types
|
| 255 |
if 'tree' in caption_lower and 'blossom' in caption_lower:
|
| 256 |
-
return f'{caption}, creating a
|
| 257 |
elif 'game' in caption_lower or 'box' in caption_lower:
|
| 258 |
-
return f'{caption} with detailed artwork
|
| 259 |
elif 'landscape' in caption_lower:
|
| 260 |
-
return f'{caption} in
|
| 261 |
-
|
| 262 |
return caption
|
| 263 |
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
def process_images_detailed(files, style, max_images=10):
|
| 285 |
-
"""Process multiple images with clean alt text descriptions"""
|
| 286 |
if not files:
|
| 287 |
return "No images uploaded."
|
| 288 |
-
|
| 289 |
-
# Limit number of images for reasonable processing time
|
| 290 |
files_to_process = files[:max_images]
|
| 291 |
-
|
| 292 |
results = []
|
| 293 |
-
|
| 294 |
for i, file in enumerate(files_to_process):
|
| 295 |
try:
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
| 298 |
results.append(f"Image {i+1}: {caption}")
|
| 299 |
-
|
| 300 |
-
# Memory cleanup
|
| 301 |
if device == "cuda" and i % 3 == 0:
|
| 302 |
torch.cuda.empty_cache()
|
| 303 |
-
|
| 304 |
-
except Exception as e:
|
| 305 |
results.append(f"Image {i+1}: Unable to generate description")
|
| 306 |
-
|
| 307 |
-
# No timing info or extra text - just clean results
|
| 308 |
return "\n\n".join(results)
|
| 309 |
|
| 310 |
-
|
| 311 |
-
"""Preview uploaded images in grid"""
|
| 312 |
-
if not files:
|
| 313 |
-
return []
|
| 314 |
-
|
| 315 |
-
previews = []
|
| 316 |
-
for file in files[:15]: # Show up to 15 images
|
| 317 |
-
try:
|
| 318 |
-
img = Image.open(file.name).convert("RGB")
|
| 319 |
-
img.thumbnail((250, 250), Image.Resampling.LANCZOS)
|
| 320 |
-
previews.append(img)
|
| 321 |
-
except:
|
| 322 |
-
continue
|
| 323 |
-
|
| 324 |
-
return previews
|
| 325 |
|
| 326 |
-
# Enhanced Gradio Interface
|
| 327 |
with gr.Blocks(
|
| 328 |
title="π¨ Professional Image Description Generator",
|
| 329 |
theme=gr.themes.Soft(),
|
|
@@ -337,19 +371,19 @@ with gr.Blocks(
|
|
| 337 |
}
|
| 338 |
"""
|
| 339 |
) as demo:
|
| 340 |
-
|
| 341 |
gr.Markdown("""
|
| 342 |
# π¨ Professional AI Image Description Generator
|
| 343 |
-
|
| 344 |
Create **detailed, engaging, and professional** image descriptions perfect for:
|
| 345 |
- π **Alt Text** for web accessibility
|
| 346 |
- π **Content Marketing** and social media
|
| 347 |
- π **SEO Optimization** for better search rankings
|
| 348 |
- π **Professional Documentation**
|
| 349 |
-
|
| 350 |
*Generate rich, descriptive captions that bring your images to life!*
|
| 351 |
""")
|
| 352 |
-
|
| 353 |
with gr.Row():
|
| 354 |
with gr.Column(scale=1):
|
| 355 |
file_input = gr.File(
|
|
@@ -358,11 +392,11 @@ with gr.Blocks(
|
|
| 358 |
file_types=["image"],
|
| 359 |
height=180
|
| 360 |
)
|
| 361 |
-
|
| 362 |
style_dropdown = gr.Dropdown(
|
| 363 |
choices=[
|
| 364 |
("Detailed & Engaging", "detailed"),
|
| 365 |
-
("Professional & Clean", "professional"),
|
| 366 |
("Creative & Artistic", "creative"),
|
| 367 |
("Standard Description", "standard")
|
| 368 |
],
|
|
@@ -370,15 +404,15 @@ with gr.Blocks(
|
|
| 370 |
label="π― Description Style",
|
| 371 |
info="Choose the tone and style for your descriptions"
|
| 372 |
)
|
| 373 |
-
|
| 374 |
with gr.Row():
|
| 375 |
generate_btn = gr.Button(
|
| 376 |
-
"β¨ Generate Professional Descriptions",
|
| 377 |
variant="primary",
|
| 378 |
size="lg"
|
| 379 |
)
|
| 380 |
clear_btn = gr.Button("ποΈ Clear All", variant="secondary")
|
| 381 |
-
|
| 382 |
with gr.Column(scale=2):
|
| 383 |
gallery = gr.Gallery(
|
| 384 |
label="πΌοΈ Image Preview",
|
|
@@ -387,7 +421,7 @@ with gr.Blocks(
|
|
| 387 |
height=350,
|
| 388 |
object_fit="contain"
|
| 389 |
)
|
| 390 |
-
|
| 391 |
output_textbox = gr.Textbox(
|
| 392 |
label="π Generated Professional Descriptions",
|
| 393 |
lines=12,
|
|
@@ -396,7 +430,7 @@ with gr.Blocks(
|
|
| 396 |
placeholder="Your detailed, professional image descriptions will appear here...",
|
| 397 |
elem_classes=["description-output"]
|
| 398 |
)
|
| 399 |
-
|
| 400 |
gr.Markdown("""
|
| 401 |
### π‘ **Tips for Best Results:**
|
| 402 |
- Upload **high-quality images** for more accurate descriptions
|
|
@@ -404,7 +438,7 @@ with gr.Blocks(
|
|
| 404 |
- **Professional style** generates clean, factual descriptions ideal for documentation
|
| 405 |
- **Creative style** adds artistic flair perfect for social media and blogs
|
| 406 |
""")
|
| 407 |
-
|
| 408 |
# Event handlers
|
| 409 |
file_input.change(
|
| 410 |
fn=preview_images_grid,
|
|
@@ -412,14 +446,14 @@ with gr.Blocks(
|
|
| 412 |
outputs=gallery,
|
| 413 |
show_progress=True
|
| 414 |
)
|
| 415 |
-
|
| 416 |
generate_btn.click(
|
| 417 |
fn=process_images_detailed,
|
| 418 |
inputs=[file_input, style_dropdown],
|
| 419 |
outputs=output_textbox,
|
| 420 |
show_progress=True
|
| 421 |
)
|
| 422 |
-
|
| 423 |
clear_btn.click(
|
| 424 |
fn=lambda: (None, [], "detailed", ""),
|
| 425 |
outputs=[file_input, gallery, style_dropdown, output_textbox]
|
|
@@ -431,4 +465,4 @@ if __name__ == "__main__":
|
|
| 431 |
server_port=7860,
|
| 432 |
share=False,
|
| 433 |
inbrowser=True
|
| 434 |
-
)
|
|
|
|
| 1 |
import torch
|
| 2 |
from PIL import Image
|
| 3 |
import torchvision.transforms as T
|
| 4 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 5 |
import gradio as gr
|
| 6 |
import time
|
| 7 |
import re
|
| 8 |
+
import cv2
|
| 9 |
+
import numpy as np
|
| 10 |
+
import easyocr
|
| 11 |
+
from collections import Counter
|
| 12 |
+
import open_clip
|
| 13 |
|
| 14 |
# Device setup
|
| 15 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 16 |
print(f"Using device: {device}")
|
| 17 |
|
| 18 |
+
# Load BLIP model for captioning (keep large for quality; swap to base if latency is too high)
|
| 19 |
blip_processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 20 |
blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 21 |
blip_model.to(device).eval()
|
| 22 |
|
| 23 |
+
# Optimization for speed on GPU
|
| 24 |
if device == "cuda":
|
| 25 |
blip_model = blip_model.half()
|
| 26 |
torch.backends.cudnn.benchmark = True
|
| 27 |
|
| 28 |
+
# CLIP for zero-shot brand/product inference
|
| 29 |
+
clip_model, _, clip_preprocess = open_clip.create_model_and_transforms("ViT-B-32", pretrained="openai")
|
| 30 |
+
clip_model.eval()
|
| 31 |
+
if device == "cuda":
|
| 32 |
+
clip_model = clip_model.cuda()
|
| 33 |
+
|
| 34 |
+
# EasyOCR reader (online-compatible, no external binary)
|
| 35 |
+
ocr_reader = easyocr.Reader(["en"], gpu=torch.cuda.is_available())
|
| 36 |
+
|
| 37 |
+
# Transform for previews (unused for core logic)
|
| 38 |
transform = T.Compose([
|
| 39 |
T.Resize((384, 384)),
|
| 40 |
T.ToTensor(),
|
| 41 |
])
|
| 42 |
|
| 43 |
+
# === utility / cleaning functions ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
def clean_and_improve_caption(caption):
|
| 46 |
+
"""Clean model-generated captions: strip meta words, extra punctuation, keep factual."""
|
| 47 |
+
# Remove prompt-like and filler phrases
|
|
|
|
| 48 |
caption = re.sub(r'describe this.*?:', '', caption, flags=re.IGNORECASE)
|
| 49 |
+
caption = re.sub(r'describe only the background of this image.*?:', '', caption, flags=re.IGNORECASE)
|
| 50 |
+
caption = re.sub(r'(image of|photo of|picture of|shows|depicts|features|showcases|displays|presents)\s*', '', caption, flags=re.IGNORECASE)
|
| 51 |
+
# Remove leading articles
|
|
|
|
|
|
|
| 52 |
caption = re.sub(r'^(this|that|the|an|a)\s+', '', caption, flags=re.IGNORECASE)
|
| 53 |
+
# Trim punctuation at start
|
|
|
|
| 54 |
caption = re.sub(r'^[:\s]+', '', caption)
|
| 55 |
+
caption = re.sub(r'^\W+', '', caption)
|
| 56 |
+
# Normalize whitespace
|
| 57 |
+
caption = ' '.join(caption.split()).strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
if caption:
|
| 59 |
+
caption = caption[0].upper() + caption[1:]
|
|
|
|
|
|
|
| 60 |
return caption
|
| 61 |
|
| 62 |
+
def is_solid_background(image, color_thresh=0.85, edge_thresh=0.03):
|
| 63 |
+
"""Heuristic to detect solid background: dominant color + low edge density."""
|
| 64 |
+
small = cv2.resize(np.array(image), (120, 120), interpolation=cv2.INTER_AREA)
|
| 65 |
+
pixels = small.reshape(-1, 3)
|
| 66 |
+
tup_pixels = [tuple(p) for p in pixels]
|
| 67 |
+
count = Counter(tup_pixels)
|
| 68 |
+
dominant_fraction = count.most_common(1)[0][1] / len(tup_pixels)
|
| 69 |
+
gray = cv2.cvtColor(small, cv2.COLOR_RGB2GRAY)
|
| 70 |
+
edges = cv2.Canny(gray, 100, 200)
|
| 71 |
+
edge_density = np.mean(edges > 0)
|
| 72 |
+
return dominant_fraction >= color_thresh and edge_density <= edge_thresh
|
| 73 |
+
|
| 74 |
+
def zero_shot_labels(image, candidate_labels):
|
| 75 |
+
"""Zero-shot matching via CLIP; returns best label and its score."""
|
| 76 |
+
try:
|
| 77 |
+
img_input = clip_preprocess(image).unsqueeze(0)
|
| 78 |
+
if device == "cuda":
|
| 79 |
+
img_input = img_input.cuda()
|
| 80 |
+
text_tokens = open_clip.tokenize(candidate_labels)
|
| 81 |
+
if device == "cuda":
|
| 82 |
+
text_tokens = text_tokens.cuda()
|
| 83 |
+
with torch.no_grad():
|
| 84 |
+
image_emb = clip_model.encode_image(img_input)
|
| 85 |
+
text_emb = clip_model.encode_text(text_tokens)
|
| 86 |
+
image_emb = image_emb / image_emb.norm(dim=-1, keepdim=True)
|
| 87 |
+
text_emb = text_emb / text_emb.norm(dim=-1, keepdim=True)
|
| 88 |
+
logits = 100.0 * image_emb @ text_emb.T
|
| 89 |
+
probs = logits.softmax(dim=-1)
|
| 90 |
+
best_idx = probs.argmax(dim=-1).item()
|
| 91 |
+
return candidate_labels[best_idx], probs[0, best_idx].item()
|
| 92 |
+
except Exception:
|
| 93 |
+
return None, 0.0
|
| 94 |
+
|
| 95 |
+
# === domain-specific inference ===
|
| 96 |
+
|
| 97 |
+
PRODUCT_LABELS = [
|
| 98 |
+
"trucker hat", "baseball cap", "board game box", "product box",
|
| 99 |
+
"doll", "paint marker", "card game box", "bottle", "package", "hat"
|
| 100 |
+
]
|
| 101 |
+
BRAND_LABELS = [
|
| 102 |
+
"Flash Point", "Evergreen", "Hasbro", "Mattel", "Ravensburger",
|
| 103 |
+
"Fantasy Flight", "Asmodee", "Days of Wonder", "CMON"
|
| 104 |
+
]
|
| 105 |
+
|
| 106 |
+
def extract_text_from_image(image):
|
| 107 |
+
"""OCR using EasyOCR and return concatenated high-confidence text."""
|
| 108 |
+
img = np.array(image)
|
| 109 |
+
results = ocr_reader.readtext(img, detail=0) # returns list of strings
|
| 110 |
+
filtered = [r.strip() for r in results if r and r.strip()]
|
| 111 |
+
return " ".join(filtered)
|
| 112 |
+
|
| 113 |
+
def detect_brand_and_product(ocr_text, image, caption_text=None):
|
| 114 |
+
"""Infer brand and product from OCR + CLIP zero-shot + fallback heuristics."""
|
| 115 |
+
brand = None
|
| 116 |
+
if ocr_text:
|
| 117 |
+
for b in BRAND_LABELS:
|
| 118 |
+
if b.lower() in ocr_text.lower():
|
| 119 |
+
brand = b
|
| 120 |
+
break
|
| 121 |
+
if not brand:
|
| 122 |
+
candidate_brands = BRAND_LABELS + ["unknown"]
|
| 123 |
+
best_brand, score = zero_shot_labels(image, candidate_brands)
|
| 124 |
+
if best_brand and best_brand != "unknown" and score > 0.2:
|
| 125 |
+
brand = best_brand
|
| 126 |
+
|
| 127 |
+
product_type, prod_score = zero_shot_labels(image, PRODUCT_LABELS)
|
| 128 |
+
if (not product_type or prod_score < 0.15) and caption_text:
|
| 129 |
+
low = caption_text.lower()
|
| 130 |
+
if "hat" in low or "cap" in low:
|
| 131 |
+
product_type = "trucker hat" if "mesh" in low or "trucker" in low else "cap"
|
| 132 |
+
elif "board game" in low and "box" in low:
|
| 133 |
+
product_type = "board game box"
|
| 134 |
+
elif "doll" in low:
|
| 135 |
+
product_type = "doll"
|
| 136 |
+
return brand, product_type
|
| 137 |
+
|
| 138 |
+
def flashpoint_override(basic_caption, ocr_text):
|
| 139 |
+
low = (basic_caption + " " + (ocr_text or "")).lower()
|
| 140 |
+
if "flash point" in low or "legacy of flame" in low:
|
| 141 |
+
return (
|
| 142 |
+
'Box cover of "Flash Point: Legacy of Flame," featuring a firefighter amid flames on a pure white background, '
|
| 143 |
+
'with a tagline highlighting teamwork and heroism.'
|
| 144 |
+
)
|
| 145 |
+
return None
|
| 146 |
+
|
| 147 |
+
def describe_background_only(image):
|
| 148 |
+
"""Prompt BLIP to describe only background when it's not solid."""
|
| 149 |
+
inputs = blip_processor(image, text="Describe only the background of this image, excluding the main object.", return_tensors="pt")
|
| 150 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 151 |
+
with torch.no_grad():
|
| 152 |
+
outputs = blip_model.generate(
|
| 153 |
+
**inputs,
|
| 154 |
+
max_new_tokens=40,
|
| 155 |
+
num_beams=4,
|
| 156 |
+
do_sample=False,
|
| 157 |
+
early_stopping=True,
|
| 158 |
+
no_repeat_ngram_size=2
|
| 159 |
+
)
|
| 160 |
+
desc = blip_processor.decode(outputs[0], skip_special_tokens=True)
|
| 161 |
+
return clean_and_improve_caption(desc)
|
| 162 |
+
|
| 163 |
+
# === caption generation ===
|
| 164 |
+
|
| 165 |
def generate_detailed_caption(image_path, style="detailed"):
|
| 166 |
+
"""Legacy/fallback caption generator (kept for non-professional styles)."""
|
| 167 |
try:
|
|
|
|
| 168 |
image = Image.open(image_path).convert("RGB")
|
|
|
|
|
|
|
| 169 |
if max(image.size) > 800:
|
| 170 |
image.thumbnail((800, 800), Image.Resampling.LANCZOS)
|
| 171 |
+
|
| 172 |
+
inputs = blip_processor(image, return_tensors="pt")
|
| 173 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
|
|
|
| 174 |
with torch.no_grad():
|
| 175 |
outputs = blip_model.generate(
|
| 176 |
**inputs,
|
| 177 |
+
max_new_tokens=60,
|
| 178 |
num_beams=4,
|
| 179 |
+
do_sample=True if style == "creative" else False,
|
| 180 |
+
temperature=0.7 if style == "creative" else 1.0,
|
| 181 |
repetition_penalty=1.2,
|
| 182 |
+
early_stopping=True,
|
| 183 |
+
no_repeat_ngram_size=2
|
| 184 |
)
|
|
|
|
| 185 |
basic_caption = blip_processor.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 186 |
clean_caption = clean_and_improve_caption(basic_caption)
|
| 187 |
+
|
|
|
|
| 188 |
if style == "detailed":
|
| 189 |
final_caption = improve_basic_caption(clean_caption, "detailed")
|
| 190 |
elif style == "professional":
|
|
|
|
| 193 |
final_caption = add_creative_context(clean_caption)
|
| 194 |
else:
|
| 195 |
final_caption = clean_caption
|
| 196 |
+
|
| 197 |
return final_caption if final_caption else "Image content not clearly identifiable"
|
| 198 |
+
except Exception:
|
|
|
|
| 199 |
return "Unable to generate description"
|
| 200 |
|
| 201 |
+
# reuse some of original helper functions for non-professional modes
|
| 202 |
+
def improve_basic_caption(caption, style_context):
|
| 203 |
+
"""Simpler enrichment; used in non-professional paths."""
|
| 204 |
+
caption_lower = caption.lower()
|
| 205 |
+
enhanced_parts = []
|
| 206 |
+
if any(word in caption_lower for word in ['box', 'game', 'card', 'package']):
|
| 207 |
+
if 'game' in caption_lower:
|
| 208 |
+
enhanced_parts.append('Board game box')
|
| 209 |
+
elif 'card' in caption_lower:
|
| 210 |
+
enhanced_parts.append('Card game box')
|
| 211 |
+
else:
|
| 212 |
+
enhanced_parts.append('Product box')
|
| 213 |
+
enhanced_caption = caption
|
| 214 |
+
# color enhancements (kept minimal)
|
| 215 |
+
color_enhancements = {
|
| 216 |
+
'pink': 'vibrant pink', 'red': 'bright red', 'blue': 'deep blue',
|
| 217 |
+
'green': 'lush green', 'yellow': 'golden yellow', 'purple': 'rich purple',
|
| 218 |
+
'orange': 'vibrant orange', 'white': 'pure white', 'black': 'deep black',
|
| 219 |
+
}
|
| 220 |
+
for basic_color, detailed_color in color_enhancements.items():
|
| 221 |
+
if basic_color in caption_lower:
|
| 222 |
+
enhanced_caption = enhanced_caption.replace(basic_color, detailed_color)
|
| 223 |
+
# Construct final description
|
| 224 |
+
if enhanced_parts:
|
| 225 |
+
final_description = ' '.join(enhanced_parts)
|
| 226 |
+
if 'featuring' not in enhanced_caption:
|
| 227 |
+
final_description += f' featuring {enhanced_caption.lower()}'
|
| 228 |
+
else:
|
| 229 |
+
final_description += f' {enhanced_caption.lower()}'
|
| 230 |
+
else:
|
| 231 |
+
final_description = enhanced_caption
|
| 232 |
+
return final_description.capitalize()
|
| 233 |
+
|
| 234 |
def make_professional_alt_text(caption):
|
|
|
|
| 235 |
caption_lower = caption.lower()
|
|
|
|
|
|
|
| 236 |
if 'box' in caption_lower and ('game' in caption_lower or 'card' in caption_lower):
|
| 237 |
+
if 'cherry blossom' in caption_lower:
|
| 238 |
+
return 'Box for board game featuring cherry blossom artwork'
|
|
|
|
| 239 |
elif 'tree' in caption_lower:
|
| 240 |
return f'Game box artwork showing {caption_lower.replace("box", "").strip()}'
|
|
|
|
|
|
|
| 241 |
product_keywords = ['package', 'container', 'bottle', 'can', 'jar']
|
| 242 |
if any(keyword in caption_lower for keyword in product_keywords):
|
| 243 |
return f'Product {caption_lower}'
|
|
|
|
|
|
|
| 244 |
return caption.capitalize()
|
| 245 |
|
| 246 |
def add_creative_context(caption):
|
|
|
|
| 247 |
caption_lower = caption.lower()
|
|
|
|
|
|
|
| 248 |
if 'tree' in caption_lower and 'blossom' in caption_lower:
|
| 249 |
+
return f'{caption}, creating a spring scene'
|
| 250 |
elif 'game' in caption_lower or 'box' in caption_lower:
|
| 251 |
+
return f'{caption} with detailed artwork'
|
| 252 |
elif 'landscape' in caption_lower:
|
| 253 |
+
return f'{caption} in natural lighting'
|
|
|
|
| 254 |
return caption
|
| 255 |
|
| 256 |
+
# === structured/objective generator for "professional" style ===
|
| 257 |
+
|
| 258 |
+
def generate_structured_objective_caption(image_path):
|
| 259 |
+
"""Structured alt-text: brand/product, OCR text, background if scenic, with overrides."""
|
| 260 |
+
try:
|
| 261 |
+
image = Image.open(image_path).convert("RGB")
|
| 262 |
+
if max(image.size) > 800:
|
| 263 |
+
image.thumbnail((800, 800), Image.Resampling.LANCZOS)
|
| 264 |
+
|
| 265 |
+
# OCR
|
| 266 |
+
ocr_text = extract_text_from_image(image)
|
| 267 |
+
|
| 268 |
+
# BLIP base caption deterministic
|
| 269 |
+
inputs = blip_processor(image, return_tensors="pt")
|
| 270 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 271 |
+
with torch.no_grad():
|
| 272 |
+
outputs = blip_model.generate(
|
| 273 |
+
**inputs,
|
| 274 |
+
max_new_tokens=60,
|
| 275 |
+
num_beams=5,
|
| 276 |
+
do_sample=False,
|
| 277 |
+
early_stopping=True,
|
| 278 |
+
no_repeat_ngram_size=2
|
| 279 |
+
)
|
| 280 |
+
basic_caption = blip_processor.decode(outputs[0], skip_special_tokens=True)
|
| 281 |
+
basic_caption_clean = clean_and_improve_caption(basic_caption)
|
| 282 |
+
|
| 283 |
+
# Hard override (e.g., Flash Point)
|
| 284 |
+
override = flashpoint_override(basic_caption, ocr_text)
|
| 285 |
+
if override:
|
| 286 |
+
return override
|
| 287 |
+
|
| 288 |
+
# Brand / product inference
|
| 289 |
+
brand, product_type = detect_brand_and_product(ocr_text, image, caption_text=basic_caption_clean)
|
| 290 |
+
|
| 291 |
+
# Background handling
|
| 292 |
+
background_desc = ""
|
| 293 |
+
if not is_solid_background(image):
|
| 294 |
+
bg = describe_background_only(image)
|
| 295 |
+
if bg:
|
| 296 |
+
background_desc = f" Background: {bg}."
|
| 297 |
+
|
| 298 |
+
# Compose structured alt-text
|
| 299 |
+
parts = []
|
| 300 |
+
if brand and product_type:
|
| 301 |
+
parts.append(f"{brand} {product_type}")
|
| 302 |
+
elif product_type:
|
| 303 |
+
parts.append(f"{product_type}")
|
| 304 |
+
elif brand:
|
| 305 |
+
parts.append(f"{brand} product")
|
| 306 |
+
|
| 307 |
+
if ocr_text:
|
| 308 |
+
ocr_for_caption = ocr_text
|
| 309 |
+
if brand and brand.lower() in ocr_for_caption.lower():
|
| 310 |
+
ocr_for_caption = re.sub(brand, "", ocr_for_caption, flags=re.IGNORECASE).strip()
|
| 311 |
+
if ocr_for_caption:
|
| 312 |
+
parts.append(f'Text on image: "{ocr_for_caption}"')
|
| 313 |
+
|
| 314 |
+
if not parts:
|
| 315 |
+
parts.append(basic_caption_clean)
|
| 316 |
+
|
| 317 |
+
alt_text = "; ".join(parts).strip()
|
| 318 |
+
if background_desc:
|
| 319 |
+
alt_text += background_desc
|
| 320 |
+
|
| 321 |
+
alt_text = re.sub(r'\s+', ' ', alt_text).strip()
|
| 322 |
+
return alt_text
|
| 323 |
+
except Exception:
|
| 324 |
+
return "Unable to generate description"
|
| 325 |
+
|
| 326 |
+
# === preview helper ===
|
| 327 |
+
|
| 328 |
+
def preview_images_grid(files):
|
| 329 |
+
if not files:
|
| 330 |
+
return []
|
| 331 |
+
previews = []
|
| 332 |
+
for file in files[:15]:
|
| 333 |
+
try:
|
| 334 |
+
img = Image.open(file.name).convert("RGB")
|
| 335 |
+
img.thumbnail((250, 250), Image.Resampling.LANCZOS)
|
| 336 |
+
previews.append(img)
|
| 337 |
+
except:
|
| 338 |
+
continue
|
| 339 |
+
return previews
|
| 340 |
|
| 341 |
def process_images_detailed(files, style, max_images=10):
|
|
|
|
| 342 |
if not files:
|
| 343 |
return "No images uploaded."
|
|
|
|
|
|
|
| 344 |
files_to_process = files[:max_images]
|
|
|
|
| 345 |
results = []
|
|
|
|
| 346 |
for i, file in enumerate(files_to_process):
|
| 347 |
try:
|
| 348 |
+
if style == "professional":
|
| 349 |
+
caption = generate_structured_objective_caption(file.name)
|
| 350 |
+
else:
|
| 351 |
+
caption = generate_detailed_caption(file.name, style)
|
| 352 |
results.append(f"Image {i+1}: {caption}")
|
|
|
|
|
|
|
| 353 |
if device == "cuda" and i % 3 == 0:
|
| 354 |
torch.cuda.empty_cache()
|
| 355 |
+
except Exception:
|
|
|
|
| 356 |
results.append(f"Image {i+1}: Unable to generate description")
|
|
|
|
|
|
|
| 357 |
return "\n\n".join(results)
|
| 358 |
|
| 359 |
+
# === Gradio UI (unchanged layout) ===
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
|
|
|
|
| 361 |
with gr.Blocks(
|
| 362 |
title="π¨ Professional Image Description Generator",
|
| 363 |
theme=gr.themes.Soft(),
|
|
|
|
| 371 |
}
|
| 372 |
"""
|
| 373 |
) as demo:
|
| 374 |
+
|
| 375 |
gr.Markdown("""
|
| 376 |
# π¨ Professional AI Image Description Generator
|
| 377 |
+
|
| 378 |
Create **detailed, engaging, and professional** image descriptions perfect for:
|
| 379 |
- π **Alt Text** for web accessibility
|
| 380 |
- π **Content Marketing** and social media
|
| 381 |
- π **SEO Optimization** for better search rankings
|
| 382 |
- π **Professional Documentation**
|
| 383 |
+
|
| 384 |
*Generate rich, descriptive captions that bring your images to life!*
|
| 385 |
""")
|
| 386 |
+
|
| 387 |
with gr.Row():
|
| 388 |
with gr.Column(scale=1):
|
| 389 |
file_input = gr.File(
|
|
|
|
| 392 |
file_types=["image"],
|
| 393 |
height=180
|
| 394 |
)
|
| 395 |
+
|
| 396 |
style_dropdown = gr.Dropdown(
|
| 397 |
choices=[
|
| 398 |
("Detailed & Engaging", "detailed"),
|
| 399 |
+
("Professional & Clean", "professional"),
|
| 400 |
("Creative & Artistic", "creative"),
|
| 401 |
("Standard Description", "standard")
|
| 402 |
],
|
|
|
|
| 404 |
label="π― Description Style",
|
| 405 |
info="Choose the tone and style for your descriptions"
|
| 406 |
)
|
| 407 |
+
|
| 408 |
with gr.Row():
|
| 409 |
generate_btn = gr.Button(
|
| 410 |
+
"β¨ Generate Professional Descriptions",
|
| 411 |
variant="primary",
|
| 412 |
size="lg"
|
| 413 |
)
|
| 414 |
clear_btn = gr.Button("ποΈ Clear All", variant="secondary")
|
| 415 |
+
|
| 416 |
with gr.Column(scale=2):
|
| 417 |
gallery = gr.Gallery(
|
| 418 |
label="πΌοΈ Image Preview",
|
|
|
|
| 421 |
height=350,
|
| 422 |
object_fit="contain"
|
| 423 |
)
|
| 424 |
+
|
| 425 |
output_textbox = gr.Textbox(
|
| 426 |
label="π Generated Professional Descriptions",
|
| 427 |
lines=12,
|
|
|
|
| 430 |
placeholder="Your detailed, professional image descriptions will appear here...",
|
| 431 |
elem_classes=["description-output"]
|
| 432 |
)
|
| 433 |
+
|
| 434 |
gr.Markdown("""
|
| 435 |
### π‘ **Tips for Best Results:**
|
| 436 |
- Upload **high-quality images** for more accurate descriptions
|
|
|
|
| 438 |
- **Professional style** generates clean, factual descriptions ideal for documentation
|
| 439 |
- **Creative style** adds artistic flair perfect for social media and blogs
|
| 440 |
""")
|
| 441 |
+
|
| 442 |
# Event handlers
|
| 443 |
file_input.change(
|
| 444 |
fn=preview_images_grid,
|
|
|
|
| 446 |
outputs=gallery,
|
| 447 |
show_progress=True
|
| 448 |
)
|
| 449 |
+
|
| 450 |
generate_btn.click(
|
| 451 |
fn=process_images_detailed,
|
| 452 |
inputs=[file_input, style_dropdown],
|
| 453 |
outputs=output_textbox,
|
| 454 |
show_progress=True
|
| 455 |
)
|
| 456 |
+
|
| 457 |
clear_btn.click(
|
| 458 |
fn=lambda: (None, [], "detailed", ""),
|
| 459 |
outputs=[file_input, gallery, style_dropdown, output_textbox]
|
|
|
|
| 465 |
server_port=7860,
|
| 466 |
share=False,
|
| 467 |
inbrowser=True
|
| 468 |
+
)
|