Update app.py
Browse files
app.py
CHANGED
|
@@ -159,13 +159,18 @@ def improve_basic_caption(caption, style_context):
|
|
| 159 |
def clean_and_improve_caption(caption):
|
| 160 |
"""Clean and improve generated captions"""
|
| 161 |
|
| 162 |
-
# Remove all AI-generated meta
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
caption = re.sub(r'(create|image|picture|photo|this is|shows|depicts|captures|features|showcases|displays|presents)\s*', '', caption, flags=re.IGNORECASE)
|
| 164 |
-
caption = re.sub(r'(a detailed and vivid description of|a professional and descriptive caption of
|
| 165 |
caption = re.sub(r'^(this|that|the|an|a)\s+', '', caption, flags=re.IGNORECASE)
|
| 166 |
|
| 167 |
# Remove colons and extra punctuation at the start
|
| 168 |
caption = re.sub(r'^[:\s]+', '', caption)
|
|
|
|
| 169 |
|
| 170 |
# Clean up extra spaces and normalize
|
| 171 |
caption = ' '.join(caption.split())
|
|
@@ -188,34 +193,26 @@ def generate_detailed_caption(image_path, style="detailed"):
|
|
| 188 |
if max(image.size) > 800:
|
| 189 |
image.thumbnail((800, 800), Image.Resampling.LANCZOS)
|
| 190 |
|
| 191 |
-
# Generate
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
inputs = blip_processor(
|
| 195 |
-
image,
|
| 196 |
-
text=detailed_prompt,
|
| 197 |
-
return_tensors="pt"
|
| 198 |
-
).to(device)
|
| 199 |
|
| 200 |
with torch.no_grad():
|
| 201 |
outputs = blip_model.generate(
|
| 202 |
**inputs,
|
| 203 |
-
max_length=
|
| 204 |
-
num_beams=
|
| 205 |
do_sample=True,
|
| 206 |
-
temperature=0.
|
| 207 |
-
|
| 208 |
-
repetition_penalty=1.3,
|
| 209 |
-
length_penalty=1.1,
|
| 210 |
early_stopping=True
|
| 211 |
)
|
| 212 |
|
| 213 |
-
|
| 214 |
|
| 215 |
-
# Clean
|
| 216 |
-
clean_caption = clean_and_improve_caption(
|
| 217 |
|
| 218 |
-
#
|
| 219 |
if style == "detailed":
|
| 220 |
final_caption = improve_basic_caption(clean_caption, "detailed")
|
| 221 |
elif style == "professional":
|
|
@@ -228,7 +225,7 @@ def generate_detailed_caption(image_path, style="detailed"):
|
|
| 228 |
return final_caption if final_caption else "Image content not clearly identifiable"
|
| 229 |
|
| 230 |
except Exception as e:
|
| 231 |
-
return
|
| 232 |
|
| 233 |
def make_professional_alt_text(caption):
|
| 234 |
"""Convert to professional alt text format"""
|
|
|
|
| 159 |
def clean_and_improve_caption(caption):
|
| 160 |
"""Clean and improve generated captions"""
|
| 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.*?with.*?:', '', caption, flags=re.IGNORECASE)
|
| 165 |
+
caption = re.sub(r'.*specific details.*?:', '', caption, flags=re.IGNORECASE)
|
| 166 |
+
caption = re.sub(r'.*colors.*objects.*text.*:', '', caption, flags=re.IGNORECASE)
|
| 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) # Remove any non-word characters at start
|
| 174 |
|
| 175 |
# Clean up extra spaces and normalize
|
| 176 |
caption = ' '.join(caption.split())
|
|
|
|
| 193 |
if max(image.size) > 800:
|
| 194 |
image.thumbnail((800, 800), Image.Resampling.LANCZOS)
|
| 195 |
|
| 196 |
+
# Generate basic caption first WITHOUT prompting to avoid prompt echo
|
| 197 |
+
inputs = blip_processor(image, return_tensors="pt").to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
with torch.no_grad():
|
| 200 |
outputs = blip_model.generate(
|
| 201 |
**inputs,
|
| 202 |
+
max_length=60,
|
| 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":
|
|
|
|
| 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"""
|