Update app.py
Browse files
app.py
CHANGED
|
@@ -17,27 +17,86 @@ def generate_alt_text(image):
|
|
| 17 |
# Process the image
|
| 18 |
inputs = processor(image, return_tensors="pt")
|
| 19 |
|
| 20 |
-
# Generate caption
|
| 21 |
with torch.no_grad():
|
| 22 |
-
out = model.generate(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Decode the generated text
|
| 25 |
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 26 |
|
|
|
|
|
|
|
|
|
|
| 27 |
# Clean up the caption to make it more suitable as alt text
|
| 28 |
if caption.lower().startswith("a picture of"):
|
| 29 |
caption = caption[12:].strip()
|
| 30 |
elif caption.lower().startswith("an image of"):
|
| 31 |
caption = caption[11:].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
caption
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
return caption
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
return f"Error generating alt text: {str(e)}"
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
def process_images(files):
|
| 42 |
"""Process multiple uploaded images and generate alt text for each"""
|
| 43 |
if not files:
|
|
@@ -72,62 +131,7 @@ def process_images(files):
|
|
| 72 |
|
| 73 |
return results
|
| 74 |
|
| 75 |
-
|
| 76 |
-
"""Create HTML output with images, alt text, and copy buttons"""
|
| 77 |
-
if not results:
|
| 78 |
-
return "<p>No results to display</p>"
|
| 79 |
-
|
| 80 |
-
html = """
|
| 81 |
-
<div style="font-family: Arial, sans-serif;">
|
| 82 |
-
<h3>Generated Alt Text Results</h3>
|
| 83 |
-
<script>
|
| 84 |
-
function copyToClipboard(text, buttonId) {
|
| 85 |
-
navigator.clipboard.writeText(text).then(function() {
|
| 86 |
-
const button = document.getElementById(buttonId);
|
| 87 |
-
const originalText = button.innerHTML;
|
| 88 |
-
button.innerHTML = '✓ Copied!';
|
| 89 |
-
button.style.backgroundColor = '#4CAF50';
|
| 90 |
-
setTimeout(function() {
|
| 91 |
-
button.innerHTML = originalText;
|
| 92 |
-
button.style.backgroundColor = '#008CBA';
|
| 93 |
-
}, 2000);
|
| 94 |
-
});
|
| 95 |
-
}
|
| 96 |
-
</script>
|
| 97 |
-
<style>
|
| 98 |
-
.result-container {
|
| 99 |
-
border: 1px solid #ddd;
|
| 100 |
-
border-radius: 8px;
|
| 101 |
-
padding: 15px;
|
| 102 |
-
margin: 10px 0;
|
| 103 |
-
background-color: #f9f9f9;
|
| 104 |
-
}
|
| 105 |
-
.image-preview {
|
| 106 |
-
max-width: 300px;
|
| 107 |
-
max-height: 200px;
|
| 108 |
-
border-radius: 5px;
|
| 109 |
-
margin-bottom: 10px;
|
| 110 |
-
}
|
| 111 |
-
.alt-text-container {
|
| 112 |
-
background-color: white;
|
| 113 |
-
padding: 10px;
|
| 114 |
-
border-radius: 5px;
|
| 115 |
-
border: 1px solid #eee;
|
| 116 |
-
margin: 10px 0;
|
| 117 |
-
}
|
| 118 |
-
.copy-button {
|
| 119 |
-
background-color: #008CBA;
|
| 120 |
-
color: white;
|
| 121 |
-
border: none;
|
| 122 |
-
padding: 8px 16px;
|
| 123 |
-
border-radius: 4px;
|
| 124 |
-
cursor: pointer;
|
| 125 |
-
font-size: 14px;
|
| 126 |
-
margin-top: 5px;
|
| 127 |
-
}
|
| 128 |
-
.copy-button:hover {
|
| 129 |
-
background-color: #007B9A;
|
| 130 |
-
}
|
| 131 |
.filename {
|
| 132 |
font-weight: bold;
|
| 133 |
color: #333;
|
|
|
|
| 17 |
# Process the image
|
| 18 |
inputs = processor(image, return_tensors="pt")
|
| 19 |
|
| 20 |
+
# Generate caption with better parameters to avoid repetition
|
| 21 |
with torch.no_grad():
|
| 22 |
+
out = model.generate(
|
| 23 |
+
**inputs,
|
| 24 |
+
max_length=50, # Reduced max length
|
| 25 |
+
min_length=5, # Minimum length
|
| 26 |
+
num_beams=3, # Reduced beam search
|
| 27 |
+
early_stopping=True,
|
| 28 |
+
no_repeat_ngram_size=2, # Prevent repetition
|
| 29 |
+
do_sample=True, # Add sampling
|
| 30 |
+
temperature=0.7, # Control randomness
|
| 31 |
+
top_p=0.9, # Nucleus sampling
|
| 32 |
+
repetition_penalty=1.2 # Penalize repetition
|
| 33 |
+
)
|
| 34 |
|
| 35 |
# Decode the generated text
|
| 36 |
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 37 |
|
| 38 |
+
# Clean up repetitive text
|
| 39 |
+
caption = clean_repetitive_text(caption)
|
| 40 |
+
|
| 41 |
# Clean up the caption to make it more suitable as alt text
|
| 42 |
if caption.lower().startswith("a picture of"):
|
| 43 |
caption = caption[12:].strip()
|
| 44 |
elif caption.lower().startswith("an image of"):
|
| 45 |
caption = caption[11:].strip()
|
| 46 |
+
elif caption.lower().startswith("a photo of"):
|
| 47 |
+
caption = caption[10:].strip()
|
| 48 |
+
|
| 49 |
+
# Remove common prefixes and clean up
|
| 50 |
+
caption = caption.strip()
|
| 51 |
|
| 52 |
+
# Ensure it's not empty and capitalize first letter
|
| 53 |
+
if caption and len(caption) > 0:
|
| 54 |
+
caption = caption[0].upper() + caption[1:]
|
| 55 |
+
else:
|
| 56 |
+
caption = "Image content could not be determined"
|
| 57 |
+
|
| 58 |
+
# Final check for repetitive patterns
|
| 59 |
+
if is_repetitive(caption):
|
| 60 |
+
caption = "Image with visual content"
|
| 61 |
|
| 62 |
return caption
|
| 63 |
|
| 64 |
except Exception as e:
|
| 65 |
return f"Error generating alt text: {str(e)}"
|
| 66 |
|
| 67 |
+
def clean_repetitive_text(text):
|
| 68 |
+
"""Clean repetitive text patterns"""
|
| 69 |
+
import re
|
| 70 |
+
|
| 71 |
+
# Remove patterns like "lan lan lan" or "a a a"
|
| 72 |
+
text = re.sub(r'\b(\w+)(\s+\1){2,}\b', r'\1', text)
|
| 73 |
+
|
| 74 |
+
# Remove excessive repetition of short words
|
| 75 |
+
text = re.sub(r'\b(\w{1,3})\s+(\1\s+){3,}', '', text)
|
| 76 |
+
|
| 77 |
+
# Clean up extra spaces
|
| 78 |
+
text = ' '.join(text.split())
|
| 79 |
+
|
| 80 |
+
return text.strip()
|
| 81 |
+
|
| 82 |
+
def is_repetitive(text):
|
| 83 |
+
"""Check if text is repetitive"""
|
| 84 |
+
words = text.split()
|
| 85 |
+
if len(words) < 3:
|
| 86 |
+
return False
|
| 87 |
+
|
| 88 |
+
# Check for repeated words
|
| 89 |
+
word_counts = {}
|
| 90 |
+
for word in words:
|
| 91 |
+
word_counts[word] = word_counts.get(word, 0) + 1
|
| 92 |
+
|
| 93 |
+
# If any word appears more than 3 times, it's likely repetitive
|
| 94 |
+
for count in word_counts.values():
|
| 95 |
+
if count > 3:
|
| 96 |
+
return True
|
| 97 |
+
|
| 98 |
+
return False
|
| 99 |
+
|
| 100 |
def process_images(files):
|
| 101 |
"""Process multiple uploaded images and generate alt text for each"""
|
| 102 |
if not files:
|
|
|
|
| 131 |
|
| 132 |
return results
|
| 133 |
|
| 134 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
.filename {
|
| 136 |
font-weight: bold;
|
| 137 |
color: #333;
|