Update app.py
Browse files
app.py
CHANGED
|
@@ -1,154 +1,393 @@
|
|
| 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 asyncio
|
| 7 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 8 |
import time
|
|
|
|
| 9 |
|
| 10 |
# Device setup
|
| 11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
print(f"Using device: {device}")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
model.to(device)
|
| 21 |
-
model.eval()
|
| 22 |
-
|
| 23 |
-
# Enable inference optimizations
|
| 24 |
if device == "cuda":
|
| 25 |
-
|
| 26 |
torch.backends.cudnn.benchmark = True
|
| 27 |
|
| 28 |
-
#
|
| 29 |
transform = T.Compose([
|
| 30 |
-
T.Resize((
|
| 31 |
T.ToTensor(),
|
| 32 |
])
|
| 33 |
|
| 34 |
-
def
|
| 35 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
# Load image
|
| 38 |
image = Image.open(image_path).convert("RGB")
|
| 39 |
|
| 40 |
-
# Resize if too large
|
| 41 |
-
if max(image.size) >
|
| 42 |
-
image.thumbnail((
|
| 43 |
|
| 44 |
-
#
|
| 45 |
-
inputs =
|
| 46 |
|
| 47 |
-
# Fast generation with minimal beams
|
| 48 |
with torch.no_grad():
|
| 49 |
-
outputs =
|
| 50 |
**inputs,
|
| 51 |
-
max_length=
|
| 52 |
-
num_beams=
|
| 53 |
-
do_sample=
|
| 54 |
-
|
| 55 |
-
|
| 56 |
)
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
except Exception as e:
|
| 62 |
-
return f"
|
| 63 |
|
| 64 |
-
def
|
| 65 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if not files:
|
| 67 |
return "No images uploaded."
|
| 68 |
|
|
|
|
|
|
|
|
|
|
| 69 |
start_time = time.time()
|
| 70 |
results = []
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
end_time = time.time()
|
| 82 |
processing_time = end_time - start_time
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
return "\n\n".join(results)
|
| 86 |
|
| 87 |
-
def
|
| 88 |
-
"""
|
| 89 |
if not files:
|
| 90 |
return []
|
| 91 |
|
| 92 |
previews = []
|
| 93 |
-
for file in files[:
|
| 94 |
try:
|
| 95 |
img = Image.open(file.name).convert("RGB")
|
| 96 |
-
img.thumbnail((
|
| 97 |
previews.append(img)
|
| 98 |
except:
|
| 99 |
continue
|
| 100 |
|
| 101 |
return previews
|
| 102 |
|
| 103 |
-
#
|
| 104 |
with gr.Blocks(
|
| 105 |
-
title="
|
| 106 |
-
theme=gr.themes.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
) as demo:
|
| 108 |
|
| 109 |
gr.Markdown("""
|
| 110 |
-
#
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
""")
|
| 114 |
|
| 115 |
with gr.Row():
|
| 116 |
-
with gr.Column():
|
| 117 |
file_input = gr.File(
|
| 118 |
-
label="Upload Images",
|
| 119 |
file_count="multiple",
|
| 120 |
-
file_types=["image"]
|
|
|
|
| 121 |
)
|
| 122 |
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
)
|
| 130 |
|
| 131 |
with gr.Row():
|
| 132 |
-
generate_btn = gr.Button(
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
with gr.Column():
|
| 136 |
gallery = gr.Gallery(
|
| 137 |
-
label="Preview",
|
| 138 |
-
columns=
|
| 139 |
-
|
|
|
|
|
|
|
| 140 |
)
|
| 141 |
|
| 142 |
-
|
| 143 |
-
label="
|
| 144 |
-
lines=
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
)
|
| 147 |
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
if __name__ == "__main__":
|
| 154 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
from PIL import Image
|
| 3 |
import torchvision.transforms as T
|
| 4 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration, GPT2LMHeadModel, GPT2Tokenizer
|
| 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 initial captioning
|
| 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 |
+
# Transform for images
|
| 24 |
transform = T.Compose([
|
| 25 |
+
T.Resize((384, 384)),
|
| 26 |
T.ToTensor(),
|
| 27 |
])
|
| 28 |
|
| 29 |
+
def enhance_description(basic_caption, image_path):
|
| 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 |
+
width, height = image.size
|
| 35 |
+
|
| 36 |
+
# Analyze image characteristics
|
| 37 |
+
aspect_ratio = "portrait" if height > width else "landscape" if width > height else "square"
|
| 38 |
+
|
| 39 |
+
# Enhanced prompts for different contexts
|
| 40 |
+
enhancement_prompts = [
|
| 41 |
+
"a detailed and vivid description",
|
| 42 |
+
"a professional and descriptive caption",
|
| 43 |
+
"an engaging and comprehensive description",
|
| 44 |
+
"a rich and expressive portrayal"
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
# Generate multiple enhanced versions
|
| 48 |
+
enhanced_captions = []
|
| 49 |
+
|
| 50 |
+
for prompt in enhancement_prompts[:2]: # Use 2 prompts for variety
|
| 51 |
+
try:
|
| 52 |
+
# Create contextual prompt
|
| 53 |
+
full_prompt = f"Create {prompt} of this image: {basic_caption}"
|
| 54 |
+
|
| 55 |
+
inputs = blip_processor(
|
| 56 |
+
image,
|
| 57 |
+
text=full_prompt,
|
| 58 |
+
return_tensors="pt"
|
| 59 |
+
).to(device)
|
| 60 |
+
|
| 61 |
+
with torch.no_grad():
|
| 62 |
+
outputs = blip_model.generate(
|
| 63 |
+
**inputs,
|
| 64 |
+
max_length=80, # Longer for detailed descriptions
|
| 65 |
+
num_beams=3,
|
| 66 |
+
do_sample=True,
|
| 67 |
+
temperature=0.7,
|
| 68 |
+
top_p=0.9,
|
| 69 |
+
repetition_penalty=1.2,
|
| 70 |
+
length_penalty=1.0,
|
| 71 |
+
early_stopping=True
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
enhanced = blip_processor.decode(outputs[0], skip_special_tokens=True)
|
| 75 |
+
if enhanced and len(enhanced) > len(basic_caption):
|
| 76 |
+
enhanced_captions.append(enhanced)
|
| 77 |
+
|
| 78 |
+
except Exception as e:
|
| 79 |
+
continue
|
| 80 |
+
|
| 81 |
+
# Select best enhanced caption or improve basic one
|
| 82 |
+
if enhanced_captions:
|
| 83 |
+
best_caption = max(enhanced_captions, key=len)
|
| 84 |
+
return clean_and_improve_caption(best_caption)
|
| 85 |
+
else:
|
| 86 |
+
return improve_basic_caption(basic_caption, aspect_ratio)
|
| 87 |
+
|
| 88 |
+
def improve_basic_caption(caption, aspect_ratio):
|
| 89 |
+
"""Improve basic caption with descriptive enhancements"""
|
| 90 |
+
|
| 91 |
+
# Common objects and their enhanced descriptions
|
| 92 |
+
enhancements = {
|
| 93 |
+
'woman': ['elegant woman', 'graceful woman', 'stylish woman'],
|
| 94 |
+
'man': ['handsome man', 'distinguished man', 'confident man'],
|
| 95 |
+
'person': ['individual', 'figure', 'person'],
|
| 96 |
+
'dog': ['adorable dog', 'friendly dog', 'playful dog'],
|
| 97 |
+
'cat': ['beautiful cat', 'elegant feline', 'graceful cat'],
|
| 98 |
+
'car': ['sleek car', 'modern vehicle', 'stylish automobile'],
|
| 99 |
+
'house': ['charming house', 'beautiful home', 'lovely residence'],
|
| 100 |
+
'tree': ['majestic tree', 'lush tree', 'towering tree'],
|
| 101 |
+
'flower': ['vibrant flower', 'beautiful bloom', 'colorful flower'],
|
| 102 |
+
'sky': ['stunning sky', 'dramatic sky', 'beautiful sky'],
|
| 103 |
+
'sunset': ['breathtaking sunset', 'golden sunset', 'spectacular sunset'],
|
| 104 |
+
'beach': ['pristine beach', 'beautiful coastline', 'serene beach'],
|
| 105 |
+
'mountain': ['majestic mountain', 'towering peak', 'scenic mountain'],
|
| 106 |
+
'city': ['bustling city', 'vibrant metropolis', 'urban landscape'],
|
| 107 |
+
'food': ['delicious meal', 'appetizing dish', 'mouth-watering food'],
|
| 108 |
+
'building': ['impressive building', 'architectural structure', 'striking edifice']
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
# Descriptive adjectives to add richness
|
| 112 |
+
descriptive_words = [
|
| 113 |
+
'stunning', 'beautiful', 'elegant', 'vibrant', 'captivating',
|
| 114 |
+
'striking', 'impressive', 'charming', 'graceful', 'magnificent',
|
| 115 |
+
'serene', 'peaceful', 'dynamic', 'colorful', 'artistic'
|
| 116 |
+
]
|
| 117 |
+
|
| 118 |
+
# Enhance the caption
|
| 119 |
+
enhanced = caption.lower()
|
| 120 |
+
|
| 121 |
+
# Replace basic words with enhanced versions
|
| 122 |
+
for basic, alternatives in enhancements.items():
|
| 123 |
+
if basic in enhanced:
|
| 124 |
+
import random
|
| 125 |
+
enhanced = enhanced.replace(basic, random.choice(alternatives))
|
| 126 |
+
|
| 127 |
+
# Add descriptive context
|
| 128 |
+
if 'wearing' in enhanced:
|
| 129 |
+
enhanced = enhanced.replace('wearing', 'elegantly dressed in')
|
| 130 |
+
if 'sitting' in enhanced:
|
| 131 |
+
enhanced = enhanced.replace('sitting', 'comfortably seated')
|
| 132 |
+
if 'standing' in enhanced:
|
| 133 |
+
enhanced = enhanced.replace('standing', 'gracefully positioned')
|
| 134 |
+
if 'holding' in enhanced:
|
| 135 |
+
enhanced = enhanced.replace('holding', 'carefully holding')
|
| 136 |
+
|
| 137 |
+
# Add composition details
|
| 138 |
+
composition_details = {
|
| 139 |
+
'portrait': 'in a portrait composition',
|
| 140 |
+
'landscape': 'in a wide landscape view',
|
| 141 |
+
'square': 'in a balanced square format'
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
if aspect_ratio in composition_details:
|
| 145 |
+
enhanced += f" {composition_details[aspect_ratio]}"
|
| 146 |
+
|
| 147 |
+
return enhanced.capitalize()
|
| 148 |
+
|
| 149 |
+
def clean_and_improve_caption(caption):
|
| 150 |
+
"""Clean and improve generated captions"""
|
| 151 |
+
|
| 152 |
+
# Remove redundant phrases
|
| 153 |
+
caption = re.sub(r'(create|image|picture|photo|this is|shows|depicts)\s+', '', caption, flags=re.IGNORECASE)
|
| 154 |
+
caption = re.sub(r'(a detailed and vivid description of|a professional and descriptive caption of)', '', caption, flags=re.IGNORECASE)
|
| 155 |
+
|
| 156 |
+
# Clean up extra spaces
|
| 157 |
+
caption = ' '.join(caption.split())
|
| 158 |
+
|
| 159 |
+
# Ensure proper capitalization
|
| 160 |
+
caption = caption.strip().capitalize()
|
| 161 |
+
|
| 162 |
+
# Add engaging elements
|
| 163 |
+
engaging_starters = [
|
| 164 |
+
'Features', 'Showcases', 'Displays', 'Presents', 'Captures', 'Depicts'
|
| 165 |
+
]
|
| 166 |
+
|
| 167 |
+
# Sometimes add an engaging starter
|
| 168 |
+
import random
|
| 169 |
+
if len(caption) > 20 and random.random() < 0.3:
|
| 170 |
+
starter = random.choice(engaging_starters)
|
| 171 |
+
caption = f"{starter} {caption.lower()}"
|
| 172 |
+
|
| 173 |
+
return caption
|
| 174 |
+
|
| 175 |
+
def generate_detailed_caption(image_path, style="detailed"):
|
| 176 |
+
"""Generate detailed, engaging captions"""
|
| 177 |
try:
|
| 178 |
# Load image
|
| 179 |
image = Image.open(image_path).convert("RGB")
|
| 180 |
|
| 181 |
+
# Resize if too large for processing speed
|
| 182 |
+
if max(image.size) > 800:
|
| 183 |
+
image.thumbnail((800, 800), Image.Resampling.LANCZOS)
|
| 184 |
|
| 185 |
+
# Generate basic caption first
|
| 186 |
+
inputs = blip_processor(image, return_tensors="pt").to(device)
|
| 187 |
|
|
|
|
| 188 |
with torch.no_grad():
|
| 189 |
+
outputs = blip_model.generate(
|
| 190 |
**inputs,
|
| 191 |
+
max_length=50,
|
| 192 |
+
num_beams=4,
|
| 193 |
+
do_sample=True,
|
| 194 |
+
temperature=0.8,
|
| 195 |
+
early_stopping=True
|
| 196 |
)
|
| 197 |
|
| 198 |
+
basic_caption = blip_processor.decode(outputs[0], skip_special_tokens=True)
|
| 199 |
+
|
| 200 |
+
# Enhance based on style
|
| 201 |
+
if style == "detailed":
|
| 202 |
+
final_caption = enhance_description(basic_caption, image_path)
|
| 203 |
+
elif style == "professional":
|
| 204 |
+
final_caption = improve_basic_caption(basic_caption, "professional")
|
| 205 |
+
elif style == "creative":
|
| 206 |
+
final_caption = enhance_description(basic_caption, image_path)
|
| 207 |
+
# Add more creative elements
|
| 208 |
+
final_caption = add_creative_elements(final_caption)
|
| 209 |
+
else:
|
| 210 |
+
final_caption = improve_basic_caption(basic_caption, "standard")
|
| 211 |
+
|
| 212 |
+
return final_caption
|
| 213 |
|
| 214 |
except Exception as e:
|
| 215 |
+
return f"Unable to generate description: {str(e)}"
|
| 216 |
|
| 217 |
+
def add_creative_elements(caption):
|
| 218 |
+
"""Add creative, artistic elements to descriptions"""
|
| 219 |
+
|
| 220 |
+
creative_additions = [
|
| 221 |
+
"with artistic composition",
|
| 222 |
+
"in beautiful lighting",
|
| 223 |
+
"with stunning visual appeal",
|
| 224 |
+
"creating a captivating scene",
|
| 225 |
+
"with remarkable detail",
|
| 226 |
+
"in perfect harmony",
|
| 227 |
+
"with exceptional clarity"
|
| 228 |
+
]
|
| 229 |
+
|
| 230 |
+
import random
|
| 231 |
+
if len(caption) > 15 and random.random() < 0.4:
|
| 232 |
+
addition = random.choice(creative_additions)
|
| 233 |
+
caption += f" {addition}"
|
| 234 |
+
|
| 235 |
+
return caption
|
| 236 |
+
|
| 237 |
+
def process_images_detailed(files, style, max_images=10):
|
| 238 |
+
"""Process multiple images with detailed descriptions"""
|
| 239 |
if not files:
|
| 240 |
return "No images uploaded."
|
| 241 |
|
| 242 |
+
# Limit number of images for reasonable processing time
|
| 243 |
+
files_to_process = files[:max_images]
|
| 244 |
+
|
| 245 |
start_time = time.time()
|
| 246 |
results = []
|
| 247 |
|
| 248 |
+
for i, file in enumerate(files_to_process):
|
| 249 |
+
try:
|
| 250 |
+
caption = generate_detailed_caption(file.name, style)
|
| 251 |
+
results.append(f"**Image {i+1}:** {caption}")
|
| 252 |
+
|
| 253 |
+
# Memory cleanup
|
| 254 |
+
if device == "cuda" and i % 3 == 0:
|
| 255 |
+
torch.cuda.empty_cache()
|
| 256 |
+
|
| 257 |
+
except Exception as e:
|
| 258 |
+
results.append(f"**Image {i+1}:** Error generating description - {str(e)}")
|
| 259 |
|
| 260 |
end_time = time.time()
|
| 261 |
processing_time = end_time - start_time
|
| 262 |
|
| 263 |
+
if len(files) > max_images:
|
| 264 |
+
results.append(f"\n*Note: Processed first {max_images} images out of {len(files)} uploaded.*")
|
| 265 |
+
|
| 266 |
+
results.append(f"\nβ‘ Generated {len(files_to_process)} detailed descriptions in {processing_time:.1f} seconds")
|
| 267 |
return "\n\n".join(results)
|
| 268 |
|
| 269 |
+
def preview_images_grid(files):
|
| 270 |
+
"""Preview uploaded images in grid"""
|
| 271 |
if not files:
|
| 272 |
return []
|
| 273 |
|
| 274 |
previews = []
|
| 275 |
+
for file in files[:15]: # Show up to 15 images
|
| 276 |
try:
|
| 277 |
img = Image.open(file.name).convert("RGB")
|
| 278 |
+
img.thumbnail((250, 250), Image.Resampling.LANCZOS)
|
| 279 |
previews.append(img)
|
| 280 |
except:
|
| 281 |
continue
|
| 282 |
|
| 283 |
return previews
|
| 284 |
|
| 285 |
+
# Enhanced Gradio Interface
|
| 286 |
with gr.Blocks(
|
| 287 |
+
title="π¨ Professional Image Description Generator",
|
| 288 |
+
theme=gr.themes.Soft(),
|
| 289 |
+
css="""
|
| 290 |
+
.gradio-container {
|
| 291 |
+
max-width: 1400px !important;
|
| 292 |
+
}
|
| 293 |
+
.description-output {
|
| 294 |
+
font-family: 'Georgia', serif;
|
| 295 |
+
line-height: 1.6;
|
| 296 |
+
}
|
| 297 |
+
"""
|
| 298 |
) as demo:
|
| 299 |
|
| 300 |
gr.Markdown("""
|
| 301 |
+
# π¨ Professional AI Image Description Generator
|
| 302 |
|
| 303 |
+
Create **detailed, engaging, and professional** image descriptions perfect for:
|
| 304 |
+
- π **Alt Text** for web accessibility
|
| 305 |
+
- π **Content Marketing** and social media
|
| 306 |
+
- π **SEO Optimization** for better search rankings
|
| 307 |
+
- π **Professional Documentation**
|
| 308 |
+
|
| 309 |
+
*Generate rich, descriptive captions that bring your images to life!*
|
| 310 |
""")
|
| 311 |
|
| 312 |
with gr.Row():
|
| 313 |
+
with gr.Column(scale=1):
|
| 314 |
file_input = gr.File(
|
| 315 |
+
label="π Upload Your Images",
|
| 316 |
file_count="multiple",
|
| 317 |
+
file_types=["image"],
|
| 318 |
+
height=180
|
| 319 |
)
|
| 320 |
|
| 321 |
+
style_dropdown = gr.Dropdown(
|
| 322 |
+
choices=[
|
| 323 |
+
("Detailed & Engaging", "detailed"),
|
| 324 |
+
("Professional & Clean", "professional"),
|
| 325 |
+
("Creative & Artistic", "creative"),
|
| 326 |
+
("Standard Description", "standard")
|
| 327 |
+
],
|
| 328 |
+
value="detailed",
|
| 329 |
+
label="π― Description Style",
|
| 330 |
+
info="Choose the tone and style for your descriptions"
|
| 331 |
)
|
| 332 |
|
| 333 |
with gr.Row():
|
| 334 |
+
generate_btn = gr.Button(
|
| 335 |
+
"β¨ Generate Professional Descriptions",
|
| 336 |
+
variant="primary",
|
| 337 |
+
size="lg"
|
| 338 |
+
)
|
| 339 |
+
clear_btn = gr.Button("ποΈ Clear All", variant="secondary")
|
| 340 |
|
| 341 |
+
with gr.Column(scale=2):
|
| 342 |
gallery = gr.Gallery(
|
| 343 |
+
label="πΌοΈ Image Preview",
|
| 344 |
+
columns=4,
|
| 345 |
+
rows=2,
|
| 346 |
+
height=350,
|
| 347 |
+
object_fit="contain"
|
| 348 |
)
|
| 349 |
|
| 350 |
+
output_textbox = gr.Textbox(
|
| 351 |
+
label="π Generated Professional Descriptions",
|
| 352 |
+
lines=12,
|
| 353 |
+
max_lines=25,
|
| 354 |
+
show_copy_button=True,
|
| 355 |
+
placeholder="Your detailed, professional image descriptions will appear here...",
|
| 356 |
+
elem_classes=["description-output"]
|
| 357 |
+
)
|
| 358 |
+
|
| 359 |
+
gr.Markdown("""
|
| 360 |
+
### π‘ **Tips for Best Results:**
|
| 361 |
+
- Upload **high-quality images** for more accurate descriptions
|
| 362 |
+
- **Detailed style** creates rich, engaging descriptions perfect for marketing
|
| 363 |
+
- **Professional style** generates clean, factual descriptions ideal for documentation
|
| 364 |
+
- **Creative style** adds artistic flair perfect for social media and blogs
|
| 365 |
+
""")
|
| 366 |
+
|
| 367 |
+
# Event handlers
|
| 368 |
+
file_input.change(
|
| 369 |
+
fn=preview_images_grid,
|
| 370 |
+
inputs=file_input,
|
| 371 |
+
outputs=gallery,
|
| 372 |
+
show_progress=True
|
| 373 |
)
|
| 374 |
|
| 375 |
+
generate_btn.click(
|
| 376 |
+
fn=process_images_detailed,
|
| 377 |
+
inputs=[file_input, style_dropdown],
|
| 378 |
+
outputs=output_textbox,
|
| 379 |
+
show_progress=True
|
| 380 |
+
)
|
| 381 |
+
|
| 382 |
+
clear_btn.click(
|
| 383 |
+
fn=lambda: (None, [], "detailed", ""),
|
| 384 |
+
outputs=[file_input, gallery, style_dropdown, output_textbox]
|
| 385 |
+
)
|
| 386 |
|
| 387 |
if __name__ == "__main__":
|
| 388 |
+
demo.launch(
|
| 389 |
+
server_name="0.0.0.0",
|
| 390 |
+
server_port=7860,
|
| 391 |
+
share=False,
|
| 392 |
+
inbrowser=True
|
| 393 |
+
)
|