Update app.py
Browse files
app.py
CHANGED
|
@@ -31,59 +31,40 @@ def enhance_description(basic_caption, image_path):
|
|
| 31 |
|
| 32 |
# Load image for additional analysis
|
| 33 |
image = Image.open(image_path).convert("RGB")
|
| 34 |
-
width, height = image.size
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 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"""
|
|
@@ -149,26 +130,22 @@ def improve_basic_caption(caption, aspect_ratio):
|
|
| 149 |
def clean_and_improve_caption(caption):
|
| 150 |
"""Clean and improve generated captions"""
|
| 151 |
|
| 152 |
-
# Remove
|
| 153 |
-
caption = re.sub(r'(create|image|picture|photo|this is|shows|depicts)\s
|
| 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 |
-
#
|
| 160 |
-
caption =
|
| 161 |
|
| 162 |
-
#
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
]
|
| 166 |
|
| 167 |
-
#
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
caption = f"{starter} {caption.lower()}"
|
| 172 |
|
| 173 |
return caption
|
| 174 |
|
|
@@ -235,35 +212,29 @@ def add_creative_elements(caption):
|
|
| 235 |
return caption
|
| 236 |
|
| 237 |
def process_images_detailed(files, style, max_images=10):
|
| 238 |
-
"""Process multiple images with
|
| 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 |
-
|
|
|
|
| 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"
|
| 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 |
-
|
| 267 |
return "\n\n".join(results)
|
| 268 |
|
| 269 |
def preview_images_grid(files):
|
|
|
|
| 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, aspect_ratio):
|
| 70 |
"""Improve basic caption with descriptive enhancements"""
|
|
|
|
| 130 |
def clean_and_improve_caption(caption):
|
| 131 |
"""Clean and improve generated captions"""
|
| 132 |
|
| 133 |
+
# Remove all AI-generated meta phrases and prefixes
|
| 134 |
+
caption = re.sub(r'(create|image|picture|photo|this is|shows|depicts|captures|features|showcases|displays|presents)\s*', '', caption, flags=re.IGNORECASE)
|
| 135 |
+
caption = re.sub(r'(a detailed and vivid description of|a professional and descriptive caption of|.*description.*:|.*caption.*:)', '', caption, flags=re.IGNORECASE)
|
| 136 |
+
caption = re.sub(r'^(this|that|the|an|a)\s+', '', caption, flags=re.IGNORECASE)
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
# Remove colons and extra punctuation at the start
|
| 139 |
+
caption = re.sub(r'^[:\s]+', '', caption)
|
| 140 |
|
| 141 |
+
# Clean up extra spaces and normalize
|
| 142 |
+
caption = ' '.join(caption.split())
|
| 143 |
+
caption = caption.strip()
|
|
|
|
| 144 |
|
| 145 |
+
# Don't add any prefixes - keep it clean and direct
|
| 146 |
+
if caption:
|
| 147 |
+
# Just capitalize first letter, keep rest as is
|
| 148 |
+
caption = caption[0].upper() + caption[1:] if len(caption) > 1 else caption.upper()
|
|
|
|
| 149 |
|
| 150 |
return caption
|
| 151 |
|
|
|
|
| 212 |
return caption
|
| 213 |
|
| 214 |
def process_images_detailed(files, style, max_images=10):
|
| 215 |
+
"""Process multiple images with clean alt text descriptions"""
|
| 216 |
if not files:
|
| 217 |
return "No images uploaded."
|
| 218 |
|
| 219 |
# Limit number of images for reasonable processing time
|
| 220 |
files_to_process = files[:max_images]
|
| 221 |
|
|
|
|
| 222 |
results = []
|
| 223 |
|
| 224 |
for i, file in enumerate(files_to_process):
|
| 225 |
try:
|
| 226 |
caption = generate_detailed_caption(file.name, style)
|
| 227 |
+
# Clean format - just the alt text
|
| 228 |
+
results.append(f"Image {i+1}: {caption}")
|
| 229 |
|
| 230 |
# Memory cleanup
|
| 231 |
if device == "cuda" and i % 3 == 0:
|
| 232 |
torch.cuda.empty_cache()
|
| 233 |
|
| 234 |
except Exception as e:
|
| 235 |
+
results.append(f"Image {i+1}: Unable to generate description")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
+
# No timing info or extra text - just clean results
|
| 238 |
return "\n\n".join(results)
|
| 239 |
|
| 240 |
def preview_images_grid(files):
|