File size: 24,359 Bytes
6365245 8da7a7f c7bd8fd 8da7a7f c59002d c7bd8fd c59002d 6365245 c59002d b6d4759 c59002d b6d4759 c59002d b6d4759 c59002d b6d4759 c59002d b6d4759 c59002d b6d4759 03140c5 b6d4759 8286ccb 8da7a7f c59002d c7bd8fd 8286ccb 326ed5f 8286ccb 326ed5f c59002d b6d4759 c59002d 8286ccb c59002d c7bd8fd 8286ccb 6365245 c59002d 8286ccb c59002d 8286ccb 03140c5 c59002d b6d4759 c7bd8fd 8286ccb 12b1982 ed49820 1af47a8 c59002d 1af47a8 c7bd8fd 8da7a7f 6365245 8286ccb b6d4759 c59002d b6d4759 c59002d b6d4759 c59002d b6d4759 8286ccb b6d4759 8286ccb b6d4759 326ed5f b6d4759 c59002d 8286ccb 326ed5f b6d4759 c59002d b6d4759 c59002d b6d4759 c59002d b6d4759 c59002d b6d4759 c59002d b6d4759 c59002d b6d4759 8286ccb b6d4759 c59002d b6d4759 8da7a7f b6d4759 8da7a7f b6d4759 8da7a7f c59002d 12b1982 8286ccb b6d4759 c59002d 4e2cb5d 8da7a7f c7bd8fd 26b35b3 326ed5f f7eb26d c59002d 6365245 b6d4759 6365245 b6d4759 c59002d 03140c5 b6d4759 6365245 b6d4759 6365245 8da7a7f 03140c5 6365245 b6d4759 8da7a7f c59002d 8da7a7f c59002d 8da7a7f b6d4759 6365245 26b35b3 6365245 26b35b3 6365245 26b35b3 6365245 8da7a7f 6365245 26b35b3 6365245 26b35b3 6365245 8da7a7f 6365245 b6d4759 c59002d 8286ccb c59002d 8286ccb c59002d 8da7a7f 6365245 c59002d |
1 2 3 4 5 6 7 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 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 |
import gradio as gr
import torch
from transformers import pipeline, set_seed
import re
import random
import os
# Set up the text generation pipeline with better error handling
def initialize_model():
"""Initialize the text generation model with fallback options"""
try:
# Check if CUDA is available and model can fit in memory
device = 0 if torch.cuda.is_available() else -1
print(f"Using device: {'GPU' if device == 0 else 'CPU'}")
# Try GPT2-medium first, fallback to GPT2 if memory issues
try:
generator = pipeline(
"text-generation",
model="gpt2-medium",
device=device,
pad_token_id=50256,
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
)
print("Loaded GPT2-medium model successfully")
except Exception as e:
print(f"GPT2-medium failed, falling back to GPT2: {e}")
generator = pipeline(
"text-generation",
model="gpt2",
device=device,
pad_token_id=50256
)
print("Loaded GPT2 model successfully")
return generator
except Exception as e:
print(f"Error loading model: {e}")
# Return None to use fallback captions only
return None
# Initialize model at startup
generator = initialize_model()
def is_valid_caption(caption):
"""Check if caption is valid and makes sense"""
if not caption or len(caption.strip()) < 3:
return False
words = caption.split()
# Check for unwanted patterns
invalid_patterns = [
r'http[s]?://', # URLs
r'www\.', # Website references
r'\.com', # Domain extensions
r'\.org',
r'\.net',
r'\d{3,}', # Long numbers
r'[^\w\s]', # Special characters (except basic punctuation)
]
for pattern in invalid_patterns:
if re.search(pattern, caption, re.IGNORECASE):
return False
# Check for incomplete sentences (common AI issues)
incomplete_endings = [
'i get', 'i am', 'i will', 'i have', 'i do', 'i can',
'you get', 'you are', 'you will', 'you have', 'you do',
'we get', 'we are', 'we will', 'we have', 'we do',
'they get', 'they are', 'they will', 'they have',
'there is a', 'there are', 'this is a', 'that is',
'it is a', 'here is', 'when you', 'if you',
'the best', 'the most', 'the only', 'the first'
]
caption_lower = caption.lower()
for ending in incomplete_endings:
if caption_lower.endswith(ending):
return False
# Check for nonsensical word combinations
nonsensical_words = [
'lorem', 'ipsum', 'dolor', 'amet', 'consectetur',
'adipiscing', 'elit', 'sed', 'eiusmod', 'tempor'
]
for word in nonsensical_words:
if word in caption_lower:
return False
# Must have at least 2 different words
if len(set(words)) < 2:
return False
# Check for proper word structure (no single letters except 'I' and 'a')
for word in words:
if len(word) == 1 and word.lower() not in ['i', 'a']:
return False
return True
def generate_short_caption(description, max_retries=5):
"""Generate a short, catchy caption (max 5 words) with better quality control"""
if not description.strip() or generator is None:
return ""
for attempt in range(max_retries):
try:
# Set random seed for variety
set_seed(random.randint(1, 10000))
# More specific prompt templates for complete captions
prompts = [
f"Complete Instagram caption about {description[:40]}:",
f"Short social media caption: {description[:40]}. Caption:",
f"Trendy Instagram post about {description[:40]}:",
f"Cool caption for {description[:40]}:",
f"Fun social media post: {description[:40]}. Text:"
]
prompt = random.choice(prompts)
generated = generator(
prompt,
max_length=len(prompt.split()) + 12, # Allow more space for complete thoughts
num_return_sequences=3, # Generate multiple options
temperature=0.7, # Slightly lower temperature for more coherent text
do_sample=True,
top_p=0.85,
repetition_penalty=1.4,
pad_token_id=50256,
truncation=True
)
# Try each generated option
for result in generated:
full_text = result['generated_text']
caption = full_text.replace(prompt, "").strip()
if caption:
# Clean the caption
caption = re.sub(r'[^\w\s\']', '', caption) # Keep apostrophes
caption = ' '.join(caption.split()) # Normalize whitespace
# Try different word lengths (3-5 words)
for max_words in [5, 4, 3]:
words = caption.split()[:max_words]
if len(words) >= 2:
test_caption = " ".join(words)
if is_valid_caption(test_caption):
return test_caption
except Exception as e:
print(f"Generation error (attempt {attempt + 1}): {e}")
continue
return ""
def generate_instagram_captions(description):
"""Generate 5 short, catchy Instagram captions (max 5 words each) using cool captions"""
if not description.strip():
return "Please enter a description of your photo! πΈ"
# Massive database of cool modern captions organized by vibe
cool_captions_db = {
"general_vibes": [
"Good vibes only today",
"Living our best life",
"Squad goals right here",
"Making memories with favorites",
"Caught in the moment",
"Energy levels unmatched today",
"Vibes on point always",
"Good times roll forever",
"Living for these moments",
"Making magic happen daily",
"Good company great memories",
"Vibes immaculate as always",
"Energy infectious spread everywhere",
"Making waves together always",
"Living life in color",
"Good vibes attract everything",
"Caught being absolutely legendary",
"Creating our own magic",
"Blessed with the best",
"Dreams do come true",
"Forever young at heart",
"Happiness looks like this"
],
"friendship_squad": [
"Squad deeper than ocean",
"Friends like these matter",
"Squad tight bond stronger",
"Good energy good people",
"Squad up for success",
"Squad goals achieved today",
"Team work makes dreams",
"Squad deeper than family",
"Friends forever and always",
"Squad goals right here",
"Squad assembled ready always",
"Squad till the end",
"Squad stronger than ever",
"Squad vibes unmatched today",
"Squad goals achieved finally",
"Squad energy off charts",
"Squad love runs deep",
"Squad goals never ending",
"Squad till we die",
"Squad brings out best"
],
"confident_boss": [
"Boss moves only today",
"Confidence level maximum reached",
"Main character energy activated",
"That girl energy today",
"Boss babe vibes only",
"Confidence is my outfit",
"Serving looks and attitude",
"Boss energy never fades",
"Confidence on ten always",
"Boss mode permanently on",
"Confidence through the roof",
"Boss vibes all day",
"Confidence is my superpower",
"Boss lady energy strong",
"Confidence level expert mode",
"Boss moves make history",
"Confidence speaks for itself",
"Boss energy infectious spreading",
"Confidence is my weapon",
"Boss vibes never off"
],
"aesthetic_mood": [
"Aesthetic vibes only today",
"Mood captured perfectly here",
"Aesthetic game too strong",
"Vibes aesthetic perfection achieved",
"Mood board inspiration daily",
"Aesthetic energy flowing freely",
"Mood ring says happy",
"Aesthetic goals achieved today",
"Mood boost successfully delivered",
"Aesthetic vibes never ending",
"Mood elevated to maximum",
"Aesthetic perfection in progress",
"Mood lighting hits different",
"Aesthetic energy through roof",
"Mood captured in pixels",
"Aesthetic vibes permanently on",
"Mood board worthy moment",
"Aesthetic goals finally achieved",
"Mood enhanced by beauty",
"Aesthetic vibes multiply daily"
],
"fun_playful": [
"Chaos and we prosper",
"Weekend warriors in action",
"Sunshine mixed with chaos",
"Adventure awaits us always",
"Mischief managed successfully today",
"Chaos coordinator reporting duty",
"Fun police cant stop",
"Chaos with a purpose",
"Mischief level expert achieved",
"Chaos brings us together",
"Fun mode permanently activated",
"Chaos creates best memories",
"Mischief makes life interesting",
"Chaos with good intentions",
"Fun times multiply exponentially",
"Chaos theory in action",
"Mischief planned and executed",
"Chaos brings out creativity",
"Fun vibes spreading everywhere",
"Chaos with positive energy"
],
"motivational_success": [
"Grinding until dreams reality",
"Success tastes so sweet",
"Hustle hard dream bigger",
"Grinding never stops here",
"Success is only option",
"Hustle mode permanently on",
"Grinding toward bigger goals",
"Success follows consistent effort",
"Hustle beats talent consistently",
"Grinding creates diamond pressure",
"Success is earned daily",
"Hustle culture lifestyle choice",
"Grinding through every obstacle",
"Success mindset never rests",
"Hustle harder than yesterday",
"Grinding until goals achieved",
"Success requires relentless pursuit",
"Hustle creates unstoppable momentum",
"Grinding builds character strength",
"Success is inevitable outcome"
],
"chill_relaxed": [
"Zen mode activated today",
"Peace love and positivity",
"Chill vibes only please",
"Zen energy flowing freely",
"Peace found in simplicity",
"Chill mode permanently on",
"Zen master in training",
"Peace within creates harmony",
"Chill vibes multiply naturally",
"Zen state of mind",
"Peace radiates from within",
"Chill energy infectious spreading",
"Zen garden of thoughts",
"Peace is my superpower",
"Chill vibes heal everything",
"Zen moments create clarity",
"Peace found in present",
"Chill mode healing process",
"Zen energy restores balance",
"Peace multiplies when shared"
],
"trendy_gen_z": [
"No cap this slaps",
"Periodt end of discussion",
"This hits different though",
"Absolutely serving looks today",
"This is it chief",
"We understood the assignment",
"This absolutely sends me",
"Living rent free here",
"This is the vibe",
"Absolutely no skips today",
"This is so valid",
"We really did that",
"This is the moment",
"Absolutely understood assignment perfectly",
"This hits every time",
"We said what said",
"This is peak content",
"Absolutely no notes needed",
"This is the energy",
"We chose violence today"
],
"night_out": [
"Night out squad assembled",
"Party mode fully activated",
"Night vibes hit different",
"Party till sunrise today",
"Night out energy unmatched",
"Party like there tomorrow",
"Night out goals achieved",
"Party mode never ends",
"Night vibes absolutely immaculate",
"Party energy through roof",
"Night out memories made",
"Party until legs hurt",
"Night out squad goals",
"Party mode is lifestyle",
"Night vibes multiply exponentially",
"Party energy infectious spreading",
"Night out therapy session",
"Party mode permanently activated",
"Night vibes heal everything",
"Party like world watching"
],
"self_love": [
"Self love Sunday activated",
"Main character energy strong",
"Self care Sunday vibes",
"Self love looks good",
"Main character moment captured",
"Self care never optional",
"Self love is superpower",
"Main character energy activated",
"Self care Sunday ritual",
"Self love journey continues",
"Main character vibes only",
"Self care is healthcare",
"Self love wins always",
"Main character development complete",
"Self care Sunday success",
"Self love radiates outward",
"Main character energy infectious",
"Self care heals everything",
"Self love multiplies daily",
"Main character story continues"
]
}
# Flatten all cool captions into one big list
all_cool_captions = []
for category, captions in cool_captions_db.items():
all_cool_captions.extend(captions)
# Generate AI captions if model is available
ai_captions = []
if generator is not None:
attempts = 0
while len(ai_captions) < 2 and attempts < 10: # Try to get at least 2 good AI captions
caption = generate_short_caption(description)
if caption and caption not in ai_captions and is_valid_caption(caption):
ai_captions.append(caption)
attempts += 1
# Combine AI and predefined cool captions
combined_captions = ai_captions + all_cool_captions
# Remove duplicates while preserving order
seen = set()
unique_captions = []
for caption in combined_captions:
if caption not in seen:
seen.add(caption)
unique_captions.append(caption)
# Select 5 random captions
selected_captions = random.sample(unique_captions, min(5, len(unique_captions)))
# Ensure each caption is max 5 words and properly formatted
final_captions = []
for caption in selected_captions:
words = caption.split()[:5]
if len(words) >= 2:
final_captions.append(" ".join(words))
# Fill with defaults if needed
while len(final_captions) < 5:
fallback = random.choice(all_cool_captions)
words = fallback.split()[:5]
final_caption = " ".join(words)
if final_caption not in final_captions:
final_captions.append(final_caption)
# Format as simple numbered list
result = ""
emojis = ["π―", "β‘", "π", "π₯", "π«"]
for i, caption in enumerate(final_captions[:5]):
result += f"{emojis[i]} {caption}\n"
return result.strip()
# Enhanced Instagram-inspired theme with custom CSS
instagram_css = """
/* Instagram color scheme and modern design */
:root {
--ig-primary: #E4405F;
--ig-secondary: #833AB4;
--ig-tertiary: #F77737;
--ig-quaternary: #FCAF45;
--ig-blue: #405DE6;
--ig-bg: #FAFAFA;
--ig-text: #262626;
--ig-border: #DBDBDB;
--ig-light: #FFFFFF;
}
/* Main container styling */
.gradio-container {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
min-height: 100vh;
font-family: 'Instagram Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
}
/* Header styling - Modified to be regular black text */
.main-header {
text-align: center;
color: #000000 !important;
font-size: 3em;
font-weight: 400;
margin-bottom: 0.5em;
padding: 1rem 0;
background: rgba(255, 255, 255, 0.9);
border-radius: 15px;
margin: 1rem;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
}
.subtitle {
text-align: center;
color: white;
font-size: 1.2em;
margin-bottom: 2em;
text-shadow: 0 1px 2px rgba(0,0,0,0.3);
font-weight: 300;
}
/* Card styling */
.card {
background: rgba(255, 255, 255, 0.95) !important;
border-radius: 20px !important;
box-shadow: 0 8px 32px rgba(0,0,0,0.1) !important;
backdrop-filter: blur(10px) !important;
border: 1px solid rgba(255,255,255,0.2) !important;
padding: 2rem !important;
margin: 1rem !important;
transition: all 0.3s ease !important;
}
.card:hover {
transform: translateY(-5px) !important;
box-shadow: 0 12px 40px rgba(0,0,0,0.15) !important;
}
/* Input styling */
.gr-textbox {
border-radius: 15px !important;
border: 2px solid var(--ig-border) !important;
transition: all 0.3s ease !important;
font-size: 1.1em !important;
padding: 1rem !important;
}
.gr-textbox:focus {
border-color: var(--ig-primary) !important;
box-shadow: 0 0 0 3px rgba(228, 64, 95, 0.1) !important;
}
/* Button styling */
.generate-btn {
background: linear-gradient(45deg, var(--ig-primary), var(--ig-secondary)) !important;
border: none !important;
color: white !important;
font-weight: bold !important;
font-size: 1.2em !important;
padding: 15px 30px !important;
border-radius: 25px !important;
transition: all 0.3s ease !important;
box-shadow: 0 4px 15px rgba(228, 64, 95, 0.3) !important;
cursor: pointer !important;
}
.generate-btn:hover {
transform: translateY(-3px) !important;
box-shadow: 0 8px 25px rgba(228, 64, 95, 0.4) !important;
background: linear-gradient(45deg, var(--ig-secondary), var(--ig-primary)) !important;
}
.generate-btn:active {
transform: translateY(-1px) !important;
}
/* Output styling */
.output-box {
background: rgba(255, 255, 255, 0.9) !important;
border-radius: 15px !important;
border: 2px solid var(--ig-border) !important;
font-family: 'Instagram Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
font-size: 1.1em !important;
line-height: 1.8 !important;
padding: 1rem !important;
}
/* Examples styling */
.gr-examples {
background: rgba(255, 255, 255, 0.8) !important;
border-radius: 15px !important;
padding: 1rem !important;
margin-top: 1rem !important;
}
.gr-examples .gr-button {
background: linear-gradient(45deg, var(--ig-tertiary), var(--ig-quaternary)) !important;
border: none !important;
border-radius: 10px !important;
color: white !important;
margin: 0.25rem !important;
transition: all 0.3s ease !important;
font-weight: 600 !important;
}
.gr-examples .gr-button:hover {
transform: translateY(-2px) !important;
box-shadow: 0 4px 12px rgba(0,0,0,0.2) !important;
}
/* Tips section */
.tips-section {
background: rgba(255, 255, 255, 0.9) !important;
border-radius: 15px !important;
padding: 1.5rem !important;
margin-top: 2rem !important;
text-align: center !important;
box-shadow: 0 4px 20px rgba(0,0,0,0.1) !important;
}
.tips-section p {
color: var(--ig-text) !important;
font-size: 1em !important;
margin: 0.5rem 0 !important;
}
/* Footer styling */
.footer {
text-align: center;
color: white;
font-size: 0.9em;
margin-top: 2rem;
padding: 1rem;
opacity: 0.8;
}
/* Mobile responsive */
@media (max-width: 768px) {
.main-header {
font-size: 2.2em !important;
}
.card {
margin: 0.5rem !important;
padding: 1.5rem !important;
}
.generate-btn {
font-size: 1.1em !important;
padding: 12px 25px !important;
}
}
/* Loading animation */
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
.loading {
animation: pulse 2s infinite;
}
"""
# Create the Gradio interface
with gr.Blocks(
theme=gr.themes.Soft(
primary_hue="red",
secondary_hue="purple",
neutral_hue="gray"
),
css=instagram_css,
title="InstaCap - AI Caption Generator - By Yuval Alon Yoav Barnea and Dana Shpak"
) as demo:
# Header
gr.HTML('<h1 class="main-header">InstaCap β AI Caption Generator</h1>')
gr.HTML('<p class="subtitle">By Yuval Alon Yoav Barnea and Dana Shpak</p>')
gr.HTML('<p class="subtitle">Create short, catchy Instagram captions in seconds! Perfect for your social media posts β¨</p>')
with gr.Row():
with gr.Column(scale=1, elem_classes="card"):
description_input = gr.Textbox(
label="π Describe your photo",
placeholder="E.g., group photo with friends at sunset, coffee date with bestie, hiking adventure with squad...",
lines=3,
max_lines=5,
elem_classes="gr-textbox"
)
generate_btn = gr.Button(
"β¨ Generate Captions",
variant="primary",
elem_classes="generate-btn"
)
with gr.Column(scale=1, elem_classes="card"):
caption_output = gr.Textbox(
label="π― Your Instagram Captions",
lines=8,
max_lines=12,
interactive=False,
show_copy_button=True,
elem_classes="output-box"
)
# Connect the button to the function
generate_btn.click(
fn=generate_instagram_captions,
inputs=description_input,
outputs=caption_output
)
# Also trigger on Enter key
description_input.submit(
fn=generate_instagram_captions,
inputs=description_input,
outputs=caption_output
)
gr.HTML(
"""
<div class="tips-section">
<p>π‘ <strong>Pro Tips for Better Captions:</strong></p>
<p>π― Each caption is maximum 5 words for maximum impact</p>
<p>β‘ Get fresh trendy captions every time you generate</p>
<p>π± Copy and paste directly to Instagram, TikTok, or any social platform</p>
<p>π Click generate again for completely new cool options</p>
<p>β¨ Mix of AI-generated and trending cool captions</p>
</div>
"""
)
gr.HTML(
"""
<div class="footer">
<p>Made with β€οΈ for social media creators β’ Perfect for Instagram, TikTok, and more!</p>
</div>
"""
)
# Launch the app
if __name__ == "__main__":
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
) |