Workpam commited on
Commit
9db3db1
·
verified ·
1 Parent(s): 9f00815

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -80
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
- # 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"""
@@ -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 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
 
@@ -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 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):
 
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):