Update app.py
Browse files
app.py
CHANGED
|
@@ -3,85 +3,174 @@ import torch
|
|
| 3 |
from transformers import pipeline, set_seed
|
| 4 |
import re
|
| 5 |
import random
|
|
|
|
| 6 |
|
| 7 |
-
# Set up the text generation pipeline
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
|
| 33 |
-
set_seed(random.randint(1, 10000))
|
| 34 |
|
| 35 |
-
#
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
| 42 |
]
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
full_text = generated[0]['generated_text']
|
| 59 |
-
caption = full_text.replace(prompt, "").strip()
|
| 60 |
-
|
| 61 |
-
# Clean and limit to 5 words
|
| 62 |
-
if caption:
|
| 63 |
-
words = caption.split()[:5]
|
| 64 |
-
if len(words) >= 2:
|
| 65 |
-
return " ".join(words)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
return ""
|
| 71 |
|
| 72 |
def generate_instagram_captions(description):
|
| 73 |
"""Generate 5 short, catchy Instagram captions (max 5 words each)"""
|
| 74 |
if not description.strip():
|
| 75 |
-
return "Please enter a description of your photo!"
|
| 76 |
|
| 77 |
-
#
|
| 78 |
cool_captions = [
|
| 79 |
"Squad goals right here",
|
| 80 |
"Good vibes only today",
|
| 81 |
"Living our best life",
|
| 82 |
"Making memories with favorites",
|
| 83 |
"Caught in the moment",
|
| 84 |
-
"Friends like these
|
| 85 |
"Energy levels unmatched today",
|
| 86 |
"Vibes on point always",
|
| 87 |
"Good times roll forever",
|
|
@@ -92,7 +181,7 @@ def generate_instagram_captions(description):
|
|
| 92 |
"Good company great memories",
|
| 93 |
"Squad deeper than ocean",
|
| 94 |
"Vibes immaculate as always",
|
| 95 |
-
"Energy infectious spread
|
| 96 |
"Good times never ending",
|
| 97 |
"Making waves together always",
|
| 98 |
"Caught smiling too much",
|
|
@@ -105,23 +194,54 @@ def generate_instagram_captions(description):
|
|
| 105 |
"Squad tight bond stronger",
|
| 106 |
"Good energy good people",
|
| 107 |
"Making memories worth keeping",
|
| 108 |
-
"Caught being absolutely legendary"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
]
|
| 110 |
|
| 111 |
-
# Generate
|
| 112 |
ai_captions = []
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
# Combine AI and predefined captions
|
| 119 |
all_captions = ai_captions + cool_captions
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
# Select 5 random captions
|
| 122 |
-
selected_captions = random.sample(
|
| 123 |
|
| 124 |
-
# Ensure each caption is max 5 words
|
| 125 |
final_captions = []
|
| 126 |
for caption in selected_captions:
|
| 127 |
words = caption.split()[:5]
|
|
@@ -136,16 +256,18 @@ def generate_instagram_captions(description):
|
|
| 136 |
if final_caption not in final_captions:
|
| 137 |
final_captions.append(final_caption)
|
| 138 |
|
| 139 |
-
# Format as numbered list
|
| 140 |
-
result = ""
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
|
| 144 |
return result.strip()
|
| 145 |
|
| 146 |
-
# Instagram-inspired theme with custom CSS
|
| 147 |
instagram_css = """
|
| 148 |
-
/* Instagram color scheme */
|
| 149 |
:root {
|
| 150 |
--ig-primary: #E4405F;
|
| 151 |
--ig-secondary: #833AB4;
|
|
@@ -155,12 +277,14 @@ instagram_css = """
|
|
| 155 |
--ig-bg: #FAFAFA;
|
| 156 |
--ig-text: #262626;
|
| 157 |
--ig-border: #DBDBDB;
|
|
|
|
| 158 |
}
|
| 159 |
|
| 160 |
/* Main container styling */
|
| 161 |
.gradio-container {
|
| 162 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 163 |
min-height: 100vh;
|
|
|
|
| 164 |
}
|
| 165 |
|
| 166 |
/* Header styling */
|
|
@@ -174,6 +298,7 @@ instagram_css = """
|
|
| 174 |
font-weight: 900;
|
| 175 |
margin-bottom: 0.5em;
|
| 176 |
text-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
|
|
| 177 |
}
|
| 178 |
|
| 179 |
.subtitle {
|
|
@@ -182,6 +307,7 @@ instagram_css = """
|
|
| 182 |
font-size: 1.2em;
|
| 183 |
margin-bottom: 2em;
|
| 184 |
text-shadow: 0 1px 2px rgba(0,0,0,0.3);
|
|
|
|
| 185 |
}
|
| 186 |
|
| 187 |
/* Card styling */
|
|
@@ -193,6 +319,12 @@ instagram_css = """
|
|
| 193 |
border: 1px solid rgba(255,255,255,0.2) !important;
|
| 194 |
padding: 2rem !important;
|
| 195 |
margin: 1rem !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
}
|
| 197 |
|
| 198 |
/* Input styling */
|
|
@@ -200,6 +332,8 @@ instagram_css = """
|
|
| 200 |
border-radius: 15px !important;
|
| 201 |
border: 2px solid var(--ig-border) !important;
|
| 202 |
transition: all 0.3s ease !important;
|
|
|
|
|
|
|
| 203 |
}
|
| 204 |
|
| 205 |
.gr-textbox:focus {
|
|
@@ -218,6 +352,7 @@ instagram_css = """
|
|
| 218 |
border-radius: 25px !important;
|
| 219 |
transition: all 0.3s ease !important;
|
| 220 |
box-shadow: 0 4px 15px rgba(228, 64, 95, 0.3) !important;
|
|
|
|
| 221 |
}
|
| 222 |
|
| 223 |
.generate-btn:hover {
|
|
@@ -226,6 +361,10 @@ instagram_css = """
|
|
| 226 |
background: linear-gradient(45deg, var(--ig-secondary), var(--ig-primary)) !important;
|
| 227 |
}
|
| 228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
/* Output styling */
|
| 230 |
.output-box {
|
| 231 |
background: rgba(255, 255, 255, 0.9) !important;
|
|
@@ -233,7 +372,8 @@ instagram_css = """
|
|
| 233 |
border: 2px solid var(--ig-border) !important;
|
| 234 |
font-family: 'Instagram Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
|
| 235 |
font-size: 1.1em !important;
|
| 236 |
-
line-height: 1.
|
|
|
|
| 237 |
}
|
| 238 |
|
| 239 |
/* Examples styling */
|
|
@@ -251,6 +391,7 @@ instagram_css = """
|
|
| 251 |
color: white !important;
|
| 252 |
margin: 0.25rem !important;
|
| 253 |
transition: all 0.3s ease !important;
|
|
|
|
| 254 |
}
|
| 255 |
|
| 256 |
.gr-examples .gr-button:hover {
|
|
@@ -274,6 +415,16 @@ instagram_css = """
|
|
| 274 |
margin: 0.5rem 0 !important;
|
| 275 |
}
|
| 276 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
/* Mobile responsive */
|
| 278 |
@media (max-width: 768px) {
|
| 279 |
.main-header {
|
|
@@ -284,6 +435,21 @@ instagram_css = """
|
|
| 284 |
margin: 0.5rem !important;
|
| 285 |
padding: 1.5rem !important;
|
| 286 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
}
|
| 288 |
"""
|
| 289 |
|
|
@@ -294,17 +460,18 @@ with gr.Blocks(
|
|
| 294 |
secondary_hue="purple",
|
| 295 |
neutral_hue="gray"
|
| 296 |
),
|
| 297 |
-
css=instagram_css
|
|
|
|
| 298 |
) as demo:
|
| 299 |
|
| 300 |
gr.HTML('<h1 class="main-header">πΈ InstaCap β AI Caption Generator</h1>')
|
| 301 |
-
gr.HTML('<p class="subtitle">Create short, catchy Instagram captions in seconds! β¨</p>')
|
| 302 |
|
| 303 |
with gr.Row():
|
| 304 |
with gr.Column(scale=1, elem_classes="card"):
|
| 305 |
description_input = gr.Textbox(
|
| 306 |
label="π Describe your photo",
|
| 307 |
-
placeholder="E.g., group photo with friends
|
| 308 |
lines=3,
|
| 309 |
max_lines=5,
|
| 310 |
elem_classes="gr-textbox"
|
|
@@ -318,25 +485,27 @@ with gr.Blocks(
|
|
| 318 |
|
| 319 |
with gr.Column(scale=1, elem_classes="card"):
|
| 320 |
caption_output = gr.Textbox(
|
| 321 |
-
label="π― Your
|
| 322 |
lines=8,
|
| 323 |
-
max_lines=
|
| 324 |
interactive=False,
|
| 325 |
show_copy_button=True,
|
| 326 |
elem_classes="output-box"
|
| 327 |
)
|
| 328 |
|
| 329 |
-
# Add examples with Instagram-style descriptions
|
| 330 |
gr.Examples(
|
| 331 |
examples=[
|
| 332 |
-
["group photo with best friends"],
|
| 333 |
-
["
|
| 334 |
-
["
|
| 335 |
-
["
|
| 336 |
-
["
|
| 337 |
-
["
|
| 338 |
-
["
|
| 339 |
-
["
|
|
|
|
|
|
|
| 340 |
],
|
| 341 |
inputs=description_input,
|
| 342 |
outputs=caption_output,
|
|
@@ -362,15 +531,28 @@ with gr.Blocks(
|
|
| 362 |
gr.HTML(
|
| 363 |
"""
|
| 364 |
<div class="tips-section">
|
| 365 |
-
<p>π‘ <strong>Pro Tips:</strong></p>
|
| 366 |
-
<p>π― Each caption is
|
| 367 |
-
<p>β‘
|
| 368 |
-
<p>π± Copy and paste directly to Instagram</p>
|
| 369 |
-
<p>π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
</div>
|
| 371 |
"""
|
| 372 |
)
|
| 373 |
|
| 374 |
# Launch the app
|
| 375 |
if __name__ == "__main__":
|
| 376 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
from transformers import pipeline, set_seed
|
| 4 |
import re
|
| 5 |
import random
|
| 6 |
+
import os
|
| 7 |
|
| 8 |
+
# Set up the text generation pipeline with better error handling
|
| 9 |
+
def initialize_model():
|
| 10 |
+
"""Initialize the text generation model with fallback options"""
|
| 11 |
+
try:
|
| 12 |
+
# Check if CUDA is available and model can fit in memory
|
| 13 |
+
device = 0 if torch.cuda.is_available() else -1
|
| 14 |
+
print(f"Using device: {'GPU' if device == 0 else 'CPU'}")
|
| 15 |
+
|
| 16 |
+
# Try GPT2-medium first, fallback to GPT2 if memory issues
|
| 17 |
+
try:
|
| 18 |
+
generator = pipeline(
|
| 19 |
+
"text-generation",
|
| 20 |
+
model="gpt2-medium",
|
| 21 |
+
device=device,
|
| 22 |
+
pad_token_id=50256,
|
| 23 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 24 |
+
)
|
| 25 |
+
print("Loaded GPT2-medium model successfully")
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"GPT2-medium failed, falling back to GPT2: {e}")
|
| 28 |
+
generator = pipeline(
|
| 29 |
+
"text-generation",
|
| 30 |
+
model="gpt2",
|
| 31 |
+
device=device,
|
| 32 |
+
pad_token_id=50256
|
| 33 |
+
)
|
| 34 |
+
print("Loaded GPT2 model successfully")
|
| 35 |
+
|
| 36 |
+
return generator
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Error loading model: {e}")
|
| 39 |
+
# Return None to use fallback captions only
|
| 40 |
+
return None
|
| 41 |
|
| 42 |
+
# Initialize model at startup
|
| 43 |
+
generator = initialize_model()
|
| 44 |
+
|
| 45 |
+
def is_valid_caption(caption):
|
| 46 |
+
"""Check if caption is valid and makes sense"""
|
| 47 |
+
if not caption or len(caption.strip()) < 3:
|
| 48 |
+
return False
|
| 49 |
|
| 50 |
+
words = caption.split()
|
|
|
|
| 51 |
|
| 52 |
+
# Check for unwanted patterns
|
| 53 |
+
invalid_patterns = [
|
| 54 |
+
r'http[s]?://', # URLs
|
| 55 |
+
r'www\.', # Website references
|
| 56 |
+
r'\.com', # Domain extensions
|
| 57 |
+
r'\.org',
|
| 58 |
+
r'\.net',
|
| 59 |
+
r'\d{3,}', # Long numbers
|
| 60 |
+
r'[^\w\s]', # Special characters (except basic punctuation)
|
| 61 |
]
|
| 62 |
|
| 63 |
+
for pattern in invalid_patterns:
|
| 64 |
+
if re.search(pattern, caption, re.IGNORECASE):
|
| 65 |
+
return False
|
| 66 |
|
| 67 |
+
# Check for incomplete sentences (common AI issues)
|
| 68 |
+
incomplete_endings = [
|
| 69 |
+
'i get', 'i am', 'i will', 'i have', 'i do', 'i can',
|
| 70 |
+
'you get', 'you are', 'you will', 'you have', 'you do',
|
| 71 |
+
'we get', 'we are', 'we will', 'we have', 'we do',
|
| 72 |
+
'they get', 'they are', 'they will', 'they have',
|
| 73 |
+
'there is a', 'there are', 'this is a', 'that is',
|
| 74 |
+
'it is a', 'here is', 'when you', 'if you',
|
| 75 |
+
'the best', 'the most', 'the only', 'the first'
|
| 76 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
caption_lower = caption.lower()
|
| 79 |
+
for ending in incomplete_endings:
|
| 80 |
+
if caption_lower.endswith(ending):
|
| 81 |
+
return False
|
| 82 |
+
|
| 83 |
+
# Check for nonsensical word combinations
|
| 84 |
+
nonsensical_words = [
|
| 85 |
+
'lorem', 'ipsum', 'dolor', 'amet', 'consectetur',
|
| 86 |
+
'adipiscing', 'elit', 'sed', 'eiusmod', 'tempor'
|
| 87 |
+
]
|
| 88 |
+
|
| 89 |
+
for word in nonsensical_words:
|
| 90 |
+
if word in caption_lower:
|
| 91 |
+
return False
|
| 92 |
+
|
| 93 |
+
# Must have at least 2 different words
|
| 94 |
+
if len(set(words)) < 2:
|
| 95 |
+
return False
|
| 96 |
+
|
| 97 |
+
# Check for proper word structure (no single letters except 'I' and 'a')
|
| 98 |
+
for word in words:
|
| 99 |
+
if len(word) == 1 and word.lower() not in ['i', 'a']:
|
| 100 |
+
return False
|
| 101 |
+
|
| 102 |
+
return True
|
| 103 |
+
|
| 104 |
+
def generate_short_caption(description, max_retries=5):
|
| 105 |
+
"""Generate a short, catchy caption (max 5 words) with better quality control"""
|
| 106 |
+
if not description.strip() or generator is None:
|
| 107 |
+
return ""
|
| 108 |
+
|
| 109 |
+
for attempt in range(max_retries):
|
| 110 |
+
try:
|
| 111 |
+
# Set random seed for variety
|
| 112 |
+
set_seed(random.randint(1, 10000))
|
| 113 |
+
|
| 114 |
+
# More specific prompt templates for complete captions
|
| 115 |
+
prompts = [
|
| 116 |
+
f"Complete Instagram caption about {description[:40]}:",
|
| 117 |
+
f"Short social media caption: {description[:40]}. Caption:",
|
| 118 |
+
f"Trendy Instagram post about {description[:40]}:",
|
| 119 |
+
f"Cool caption for {description[:40]}:",
|
| 120 |
+
f"Fun social media post: {description[:40]}. Text:"
|
| 121 |
+
]
|
| 122 |
+
|
| 123 |
+
prompt = random.choice(prompts)
|
| 124 |
+
|
| 125 |
+
generated = generator(
|
| 126 |
+
prompt,
|
| 127 |
+
max_length=len(prompt.split()) + 12, # Allow more space for complete thoughts
|
| 128 |
+
num_return_sequences=3, # Generate multiple options
|
| 129 |
+
temperature=0.7, # Slightly lower temperature for more coherent text
|
| 130 |
+
do_sample=True,
|
| 131 |
+
top_p=0.85,
|
| 132 |
+
repetition_penalty=1.4,
|
| 133 |
+
pad_token_id=50256,
|
| 134 |
+
truncation=True
|
| 135 |
+
)
|
| 136 |
+
|
| 137 |
+
# Try each generated option
|
| 138 |
+
for result in generated:
|
| 139 |
+
full_text = result['generated_text']
|
| 140 |
+
caption = full_text.replace(prompt, "").strip()
|
| 141 |
+
|
| 142 |
+
if caption:
|
| 143 |
+
# Clean the caption
|
| 144 |
+
caption = re.sub(r'[^\w\s\']', '', caption) # Keep apostrophes
|
| 145 |
+
caption = ' '.join(caption.split()) # Normalize whitespace
|
| 146 |
+
|
| 147 |
+
# Try different word lengths (3-5 words)
|
| 148 |
+
for max_words in [5, 4, 3]:
|
| 149 |
+
words = caption.split()[:max_words]
|
| 150 |
+
if len(words) >= 2:
|
| 151 |
+
test_caption = " ".join(words)
|
| 152 |
+
if is_valid_caption(test_caption):
|
| 153 |
+
return test_caption
|
| 154 |
+
|
| 155 |
+
except Exception as e:
|
| 156 |
+
print(f"Generation error (attempt {attempt + 1}): {e}")
|
| 157 |
+
continue
|
| 158 |
|
| 159 |
return ""
|
| 160 |
|
| 161 |
def generate_instagram_captions(description):
|
| 162 |
"""Generate 5 short, catchy Instagram captions (max 5 words each)"""
|
| 163 |
if not description.strip():
|
| 164 |
+
return "Please enter a description of your photo! πΈ"
|
| 165 |
|
| 166 |
+
# Enhanced cool captions database with complete, meaningful phrases
|
| 167 |
cool_captions = [
|
| 168 |
"Squad goals right here",
|
| 169 |
"Good vibes only today",
|
| 170 |
"Living our best life",
|
| 171 |
"Making memories with favorites",
|
| 172 |
"Caught in the moment",
|
| 173 |
+
"Friends like these matter",
|
| 174 |
"Energy levels unmatched today",
|
| 175 |
"Vibes on point always",
|
| 176 |
"Good times roll forever",
|
|
|
|
| 181 |
"Good company great memories",
|
| 182 |
"Squad deeper than ocean",
|
| 183 |
"Vibes immaculate as always",
|
| 184 |
+
"Energy infectious spread everywhere",
|
| 185 |
"Good times never ending",
|
| 186 |
"Making waves together always",
|
| 187 |
"Caught smiling too much",
|
|
|
|
| 194 |
"Squad tight bond stronger",
|
| 195 |
"Good energy good people",
|
| 196 |
"Making memories worth keeping",
|
| 197 |
+
"Caught being absolutely legendary",
|
| 198 |
+
"Weekend warriors in action",
|
| 199 |
+
"Sunshine mixed with chaos",
|
| 200 |
+
"Creating our own magic",
|
| 201 |
+
"Adventure awaits us always",
|
| 202 |
+
"Blessed with the best",
|
| 203 |
+
"Chaos and we prosper",
|
| 204 |
+
"Dreams do come true",
|
| 205 |
+
"Forever young at heart",
|
| 206 |
+
"Golden hour golden friends",
|
| 207 |
+
"Happiness looks like this",
|
| 208 |
+
"Peace love and yoga",
|
| 209 |
+
"Zen mode activated today",
|
| 210 |
+
"Morning ritual pure bliss",
|
| 211 |
+
"Mindful moments matter most",
|
| 212 |
+
"Grateful for this peace",
|
| 213 |
+
"Serenity found in stillness",
|
| 214 |
+
"Balance restored through practice",
|
| 215 |
+
"Inner peace outer strength",
|
| 216 |
+
"Breathe deeply live fully",
|
| 217 |
+
"Namaste all day long"
|
| 218 |
]
|
| 219 |
|
| 220 |
+
# Generate AI captions if model is available
|
| 221 |
ai_captions = []
|
| 222 |
+
if generator is not None:
|
| 223 |
+
attempts = 0
|
| 224 |
+
while len(ai_captions) < 2 and attempts < 10: # Try to get at least 2 good AI captions
|
| 225 |
+
caption = generate_short_caption(description)
|
| 226 |
+
if caption and caption not in ai_captions and is_valid_caption(caption):
|
| 227 |
+
ai_captions.append(caption)
|
| 228 |
+
attempts += 1
|
| 229 |
|
| 230 |
# Combine AI and predefined captions
|
| 231 |
all_captions = ai_captions + cool_captions
|
| 232 |
|
| 233 |
+
# Remove duplicates while preserving order
|
| 234 |
+
seen = set()
|
| 235 |
+
unique_captions = []
|
| 236 |
+
for caption in all_captions:
|
| 237 |
+
if caption not in seen:
|
| 238 |
+
seen.add(caption)
|
| 239 |
+
unique_captions.append(caption)
|
| 240 |
+
|
| 241 |
# Select 5 random captions
|
| 242 |
+
selected_captions = random.sample(unique_captions, min(5, len(unique_captions)))
|
| 243 |
|
| 244 |
+
# Ensure each caption is max 5 words and properly formatted
|
| 245 |
final_captions = []
|
| 246 |
for caption in selected_captions:
|
| 247 |
words = caption.split()[:5]
|
|
|
|
| 256 |
if final_caption not in final_captions:
|
| 257 |
final_captions.append(final_caption)
|
| 258 |
|
| 259 |
+
# Format as numbered list with emojis
|
| 260 |
+
result = "β¨ Your Instagram Captions:\n\n"
|
| 261 |
+
emojis = ["π―", "β‘", "π", "π₯", "π«"]
|
| 262 |
+
|
| 263 |
+
for i, caption in enumerate(final_captions[:5]):
|
| 264 |
+
result += f"{emojis[i]} {caption}\n"
|
| 265 |
|
| 266 |
return result.strip()
|
| 267 |
|
| 268 |
+
# Enhanced Instagram-inspired theme with custom CSS
|
| 269 |
instagram_css = """
|
| 270 |
+
/* Instagram color scheme and modern design */
|
| 271 |
:root {
|
| 272 |
--ig-primary: #E4405F;
|
| 273 |
--ig-secondary: #833AB4;
|
|
|
|
| 277 |
--ig-bg: #FAFAFA;
|
| 278 |
--ig-text: #262626;
|
| 279 |
--ig-border: #DBDBDB;
|
| 280 |
+
--ig-light: #FFFFFF;
|
| 281 |
}
|
| 282 |
|
| 283 |
/* Main container styling */
|
| 284 |
.gradio-container {
|
| 285 |
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 286 |
min-height: 100vh;
|
| 287 |
+
font-family: 'Instagram Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
|
| 288 |
}
|
| 289 |
|
| 290 |
/* Header styling */
|
|
|
|
| 298 |
font-weight: 900;
|
| 299 |
margin-bottom: 0.5em;
|
| 300 |
text-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
| 301 |
+
padding: 1rem 0;
|
| 302 |
}
|
| 303 |
|
| 304 |
.subtitle {
|
|
|
|
| 307 |
font-size: 1.2em;
|
| 308 |
margin-bottom: 2em;
|
| 309 |
text-shadow: 0 1px 2px rgba(0,0,0,0.3);
|
| 310 |
+
font-weight: 300;
|
| 311 |
}
|
| 312 |
|
| 313 |
/* Card styling */
|
|
|
|
| 319 |
border: 1px solid rgba(255,255,255,0.2) !important;
|
| 320 |
padding: 2rem !important;
|
| 321 |
margin: 1rem !important;
|
| 322 |
+
transition: all 0.3s ease !important;
|
| 323 |
+
}
|
| 324 |
+
|
| 325 |
+
.card:hover {
|
| 326 |
+
transform: translateY(-5px) !important;
|
| 327 |
+
box-shadow: 0 12px 40px rgba(0,0,0,0.15) !important;
|
| 328 |
}
|
| 329 |
|
| 330 |
/* Input styling */
|
|
|
|
| 332 |
border-radius: 15px !important;
|
| 333 |
border: 2px solid var(--ig-border) !important;
|
| 334 |
transition: all 0.3s ease !important;
|
| 335 |
+
font-size: 1.1em !important;
|
| 336 |
+
padding: 1rem !important;
|
| 337 |
}
|
| 338 |
|
| 339 |
.gr-textbox:focus {
|
|
|
|
| 352 |
border-radius: 25px !important;
|
| 353 |
transition: all 0.3s ease !important;
|
| 354 |
box-shadow: 0 4px 15px rgba(228, 64, 95, 0.3) !important;
|
| 355 |
+
cursor: pointer !important;
|
| 356 |
}
|
| 357 |
|
| 358 |
.generate-btn:hover {
|
|
|
|
| 361 |
background: linear-gradient(45deg, var(--ig-secondary), var(--ig-primary)) !important;
|
| 362 |
}
|
| 363 |
|
| 364 |
+
.generate-btn:active {
|
| 365 |
+
transform: translateY(-1px) !important;
|
| 366 |
+
}
|
| 367 |
+
|
| 368 |
/* Output styling */
|
| 369 |
.output-box {
|
| 370 |
background: rgba(255, 255, 255, 0.9) !important;
|
|
|
|
| 372 |
border: 2px solid var(--ig-border) !important;
|
| 373 |
font-family: 'Instagram Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
|
| 374 |
font-size: 1.1em !important;
|
| 375 |
+
line-height: 1.8 !important;
|
| 376 |
+
padding: 1rem !important;
|
| 377 |
}
|
| 378 |
|
| 379 |
/* Examples styling */
|
|
|
|
| 391 |
color: white !important;
|
| 392 |
margin: 0.25rem !important;
|
| 393 |
transition: all 0.3s ease !important;
|
| 394 |
+
font-weight: 600 !important;
|
| 395 |
}
|
| 396 |
|
| 397 |
.gr-examples .gr-button:hover {
|
|
|
|
| 415 |
margin: 0.5rem 0 !important;
|
| 416 |
}
|
| 417 |
|
| 418 |
+
/* Footer styling */
|
| 419 |
+
.footer {
|
| 420 |
+
text-align: center;
|
| 421 |
+
color: white;
|
| 422 |
+
font-size: 0.9em;
|
| 423 |
+
margin-top: 2rem;
|
| 424 |
+
padding: 1rem;
|
| 425 |
+
opacity: 0.8;
|
| 426 |
+
}
|
| 427 |
+
|
| 428 |
/* Mobile responsive */
|
| 429 |
@media (max-width: 768px) {
|
| 430 |
.main-header {
|
|
|
|
| 435 |
margin: 0.5rem !important;
|
| 436 |
padding: 1.5rem !important;
|
| 437 |
}
|
| 438 |
+
|
| 439 |
+
.generate-btn {
|
| 440 |
+
font-size: 1.1em !important;
|
| 441 |
+
padding: 12px 25px !important;
|
| 442 |
+
}
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
/* Loading animation */
|
| 446 |
+
@keyframes pulse {
|
| 447 |
+
0%, 100% { opacity: 1; }
|
| 448 |
+
50% { opacity: 0.5; }
|
| 449 |
+
}
|
| 450 |
+
|
| 451 |
+
.loading {
|
| 452 |
+
animation: pulse 2s infinite;
|
| 453 |
}
|
| 454 |
"""
|
| 455 |
|
|
|
|
| 460 |
secondary_hue="purple",
|
| 461 |
neutral_hue="gray"
|
| 462 |
),
|
| 463 |
+
css=instagram_css,
|
| 464 |
+
title="InstaCap - AI Caption Generator"
|
| 465 |
) as demo:
|
| 466 |
|
| 467 |
gr.HTML('<h1 class="main-header">πΈ InstaCap β AI Caption Generator</h1>')
|
| 468 |
+
gr.HTML('<p class="subtitle">Create short, catchy Instagram captions in seconds! Perfect for your social media posts β¨</p>')
|
| 469 |
|
| 470 |
with gr.Row():
|
| 471 |
with gr.Column(scale=1, elem_classes="card"):
|
| 472 |
description_input = gr.Textbox(
|
| 473 |
label="π Describe your photo",
|
| 474 |
+
placeholder="E.g., group photo with friends at sunset, coffee date with bestie, hiking adventure with squad...",
|
| 475 |
lines=3,
|
| 476 |
max_lines=5,
|
| 477 |
elem_classes="gr-textbox"
|
|
|
|
| 485 |
|
| 486 |
with gr.Column(scale=1, elem_classes="card"):
|
| 487 |
caption_output = gr.Textbox(
|
| 488 |
+
label="π― Your Instagram Captions",
|
| 489 |
lines=8,
|
| 490 |
+
max_lines=12,
|
| 491 |
interactive=False,
|
| 492 |
show_copy_button=True,
|
| 493 |
elem_classes="output-box"
|
| 494 |
)
|
| 495 |
|
| 496 |
+
# Add examples with diverse Instagram-style descriptions
|
| 497 |
gr.Examples(
|
| 498 |
examples=[
|
| 499 |
+
["group photo with best friends at sunset"],
|
| 500 |
+
["coffee date with my bestie"],
|
| 501 |
+
["hiking adventure with the squad"],
|
| 502 |
+
["beach day with good vibes"],
|
| 503 |
+
["cozy movie night with favorites"],
|
| 504 |
+
["birthday celebration with everyone"],
|
| 505 |
+
["weekend brunch with the crew"],
|
| 506 |
+
["road trip memories being made"],
|
| 507 |
+
["concert night with amazing energy"],
|
| 508 |
+
["graduation day with family pride"]
|
| 509 |
],
|
| 510 |
inputs=description_input,
|
| 511 |
outputs=caption_output,
|
|
|
|
| 531 |
gr.HTML(
|
| 532 |
"""
|
| 533 |
<div class="tips-section">
|
| 534 |
+
<p>π‘ <strong>Pro Tips for Better Captions:</strong></p>
|
| 535 |
+
<p>π― Each caption is maximum 5 words for maximum impact</p>
|
| 536 |
+
<p>β‘ Be specific in your description for better AI-generated captions</p>
|
| 537 |
+
<p>π± Copy and paste directly to Instagram, TikTok, or any social platform</p>
|
| 538 |
+
<p>π Click generate again for fresh new options</p>
|
| 539 |
+
<p>β¨ Mix AI-generated captions with trending phrases</p>
|
| 540 |
+
</div>
|
| 541 |
+
"""
|
| 542 |
+
)
|
| 543 |
+
|
| 544 |
+
gr.HTML(
|
| 545 |
+
"""
|
| 546 |
+
<div class="footer">
|
| 547 |
+
<p>Made with β€οΈ for social media creators β’ Perfect for Instagram, TikTok, and more!</p>
|
| 548 |
</div>
|
| 549 |
"""
|
| 550 |
)
|
| 551 |
|
| 552 |
# Launch the app
|
| 553 |
if __name__ == "__main__":
|
| 554 |
+
demo.launch(
|
| 555 |
+
server_name="0.0.0.0",
|
| 556 |
+
server_port=7860,
|
| 557 |
+
share=False
|
| 558 |
+
)
|