Spaces:
Build error
Build error
Create utils/caption_generator.py
Browse files- utils/caption_generator.py +116 -0
utils/caption_generator.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
class CaptionGenerator:
|
| 4 |
+
def __init__(self):
|
| 5 |
+
self.caption_styles = {
|
| 6 |
+
'Bold Yellow with Pop': {
|
| 7 |
+
'color': 'yellow',
|
| 8 |
+
'stroke_color': 'black',
|
| 9 |
+
'stroke_width': 3,
|
| 10 |
+
'fontsize': 70,
|
| 11 |
+
'animation': 'pop'
|
| 12 |
+
},
|
| 13 |
+
'Red Bold with Shake': {
|
| 14 |
+
'color': 'red',
|
| 15 |
+
'stroke_color': 'white',
|
| 16 |
+
'stroke_width': 3,
|
| 17 |
+
'fontsize': 75,
|
| 18 |
+
'animation': 'shake'
|
| 19 |
+
},
|
| 20 |
+
'White Bold with Glow': {
|
| 21 |
+
'color': 'white',
|
| 22 |
+
'stroke_color': 'black',
|
| 23 |
+
'stroke_width': 4,
|
| 24 |
+
'fontsize': 70,
|
| 25 |
+
'animation': 'glow'
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
def generate(self, text, emotion):
|
| 30 |
+
"""Generate caption data for video"""
|
| 31 |
+
# Split text into caption chunks (max 3-4 words per caption)
|
| 32 |
+
words = text.split()
|
| 33 |
+
captions = []
|
| 34 |
+
|
| 35 |
+
chunk_size = 4
|
| 36 |
+
duration_per_chunk = 2.0
|
| 37 |
+
|
| 38 |
+
for i in range(0, len(words), chunk_size):
|
| 39 |
+
chunk = ' '.join(words[i:i+chunk_size])
|
| 40 |
+
captions.append({
|
| 41 |
+
'text': chunk.upper(),
|
| 42 |
+
'start': i * duration_per_chunk / chunk_size,
|
| 43 |
+
'duration': duration_per_chunk
|
| 44 |
+
})
|
| 45 |
+
|
| 46 |
+
style_name = self._get_style_for_emotion(emotion)
|
| 47 |
+
style = self.caption_styles.get(style_name, self.caption_styles['White Bold with Glow'])
|
| 48 |
+
|
| 49 |
+
return {
|
| 50 |
+
'captions': captions,
|
| 51 |
+
'style': style_name,
|
| 52 |
+
**style
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
def _get_style_for_emotion(self, emotion):
|
| 56 |
+
"""Map emotion to caption style"""
|
| 57 |
+
emotion_style_map = {
|
| 58 |
+
'joy': 'Bold Yellow with Pop',
|
| 59 |
+
'surprise': 'Red Bold with Shake',
|
| 60 |
+
'anger': 'Red Bold with Shake',
|
| 61 |
+
'neutral': 'White Bold with Glow'
|
| 62 |
+
}
|
| 63 |
+
return emotion_style_map.get(emotion, 'White Bold with Glow')
|
| 64 |
+
|
| 65 |
+
def generate_description(self, text, hook):
|
| 66 |
+
"""Generate YouTube description with hashtags"""
|
| 67 |
+
# Extract key themes
|
| 68 |
+
text_lower = text.lower()
|
| 69 |
+
|
| 70 |
+
# Base description
|
| 71 |
+
description = f"{hook}\n\n"
|
| 72 |
+
description += "💥 This moment is PURE GOLD! Watch until the end!\n\n"
|
| 73 |
+
|
| 74 |
+
# Add call to action
|
| 75 |
+
cta_options = [
|
| 76 |
+
"❤️ Drop a like if this amazed you!",
|
| 77 |
+
"🔥 Comment your thoughts below!",
|
| 78 |
+
"💬 Tag someone who needs to see this!",
|
| 79 |
+
"⚡ Share this with your friends!"
|
| 80 |
+
]
|
| 81 |
+
description += f"{cta_options[0]}\n\n"
|
| 82 |
+
|
| 83 |
+
# Generate relevant hashtags
|
| 84 |
+
hashtags = self._generate_hashtags(text_lower)
|
| 85 |
+
description += ' '.join(hashtags)
|
| 86 |
+
|
| 87 |
+
return description
|
| 88 |
+
|
| 89 |
+
def _generate_hashtags(self, text):
|
| 90 |
+
"""Generate relevant hashtags based on content"""
|
| 91 |
+
base_tags = ['#shorts', '#viral', '#trending', '#fyp']
|
| 92 |
+
|
| 93 |
+
content_tags = []
|
| 94 |
+
|
| 95 |
+
# Content-based tags
|
| 96 |
+
if any(word in text for word in ['tip', 'how', 'tutorial', 'learn']):
|
| 97 |
+
content_tags.extend(['#tutorial', '#howto', '#tips', '#educational'])
|
| 98 |
+
|
| 99 |
+
if any(word in text for word in ['funny', 'laugh', 'hilarious', 'comedy']):
|
| 100 |
+
content_tags.extend(['#funny', '#comedy', '#lol', '#humor'])
|
| 101 |
+
|
| 102 |
+
if any(word in text for word in ['amazing', 'incredible', 'wow', 'unbelievable']):
|
| 103 |
+
content_tags.extend(['#amazing', '#mindblowing', '#incredible'])
|
| 104 |
+
|
| 105 |
+
if any(word in text for word in ['fail', 'mistake', 'wrong']):
|
| 106 |
+
content_tags.extend(['#fail', '#fails', '#epicfail'])
|
| 107 |
+
|
| 108 |
+
if any(word in text for word in ['game', 'gaming', 'play']):
|
| 109 |
+
content_tags.extend(['#gaming', '#gamer', '#gameplay'])
|
| 110 |
+
|
| 111 |
+
if not content_tags:
|
| 112 |
+
content_tags = ['#content', '#video', '#watch']
|
| 113 |
+
|
| 114 |
+
# Combine and limit to 15 tags
|
| 115 |
+
all_tags = base_tags + content_tags[:11]
|
| 116 |
+
return all_tags[:15]
|