YUVALON commited on
Commit
26b35b3
·
verified ·
1 Parent(s): 12b1982

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -246
app.py CHANGED
@@ -4,7 +4,6 @@ from transformers import pipeline, set_seed
4
  import re
5
  import random
6
  import os
7
- import time
8
 
9
  # Set up the text generation pipeline with better error handling
10
  def initialize_model():
@@ -159,27 +158,6 @@ def generate_short_caption(description, max_retries=5):
159
 
160
  return ""
161
 
162
- def generate_instagram_captions_with_loading(description, progress=gr.Progress()):
163
- """Generate 5 short, catchy Instagram captions with loading animation"""
164
- if not description.strip():
165
- return "Please enter a description of your photo! 📸"
166
-
167
- # Show loading messages with progress
168
- loading_messages = [
169
- "🤖 Analyzing your photo description...",
170
- "✨ Generating creative captions...",
171
- "🎯 Finding the perfect words...",
172
- "🔥 Adding some Instagram magic...",
173
- "📱 Finalizing your captions..."
174
- ]
175
-
176
- for i, message in enumerate(loading_messages):
177
- progress((i + 1) / len(loading_messages), desc=message)
178
- time.sleep(0.5) # Small delay to show loading progression
179
-
180
- # Generate captions (using the existing logic)
181
- return generate_instagram_captions(description)
182
-
183
  def generate_instagram_captions(description):
184
  """Generate 5 short, catchy Instagram captions (max 5 words each)"""
185
  if not description.strip():
@@ -314,7 +292,7 @@ def generate_instagram_captions(description):
314
 
315
  return result.strip()
316
 
317
- # Enhanced Instagram-inspired theme with custom CSS and loading animation
318
  instagram_css = """
319
  /* Instagram color scheme and modern design */
320
  :root {
@@ -329,14 +307,6 @@ instagram_css = """
329
  --ig-light: #FFFFFF;
330
  }
331
 
332
- /* Hide default gradio progress indicators */
333
- .gradio-container .progress-bar,
334
- .gradio-container .eta,
335
- .gradio-container .generating,
336
- .gradio-container footer {
337
- display: none !important;
338
- }
339
-
340
  /* Main container styling */
341
  .gradio-container {
342
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
@@ -344,7 +314,7 @@ instagram_css = """
344
  font-family: 'Instagram Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
345
  }
346
 
347
- /* Header styling - Same gradient as generate button */
348
  .main-header {
349
  text-align: center;
350
  background: linear-gradient(45deg, var(--ig-primary), var(--ig-secondary));
@@ -426,74 +396,6 @@ instagram_css = """
426
  transform: translateY(-1px) !important;
427
  }
428
 
429
- /* Loading animation styles */
430
- .loading-overlay {
431
- position: fixed;
432
- top: 0;
433
- left: 0;
434
- right: 0;
435
- bottom: 0;
436
- background: linear-gradient(135deg,
437
- rgba(228, 64, 95, 0.9) 0%,
438
- rgba(131, 58, 180, 0.9) 25%,
439
- rgba(247, 119, 55, 0.9) 50%,
440
- rgba(252, 175, 69, 0.9) 75%,
441
- rgba(64, 93, 230, 0.9) 100%);
442
- z-index: 9999;
443
- display: flex;
444
- flex-direction: column;
445
- justify-content: center;
446
- align-items: center;
447
- backdrop-filter: blur(10px);
448
- }
449
-
450
- .loading-spinner {
451
- width: 60px;
452
- height: 60px;
453
- border: 4px solid rgba(255, 255, 255, 0.3);
454
- border-top: 4px solid white;
455
- border-radius: 50%;
456
- animation: spin 1s linear infinite;
457
- margin-bottom: 20px;
458
- }
459
-
460
- .loading-text {
461
- color: white;
462
- font-size: 1.5em;
463
- font-weight: bold;
464
- text-align: center;
465
- margin-bottom: 10px;
466
- text-shadow: 0 2px 4px rgba(0,0,0,0.3);
467
- }
468
-
469
- .loading-subtext {
470
- color: rgba(255, 255, 255, 0.8);
471
- font-size: 1em;
472
- text-align: center;
473
- text-shadow: 0 1px 2px rgba(0,0,0,0.3);
474
- }
475
-
476
- @keyframes spin {
477
- 0% { transform: rotate(0deg); }
478
- 100% { transform: rotate(360deg); }
479
- }
480
-
481
- /* Instagram-style pulse animation */
482
- .ig-pulse {
483
- animation: igPulse 2s ease-in-out infinite;
484
- }
485
-
486
- @keyframes igPulse {
487
- 0%, 100% {
488
- transform: scale(1);
489
- opacity: 1;
490
- }
491
- 50% {
492
- transform: scale(1.05);
493
- opacity: 0.8;
494
- }
495
- }
496
-
497
  /* Output styling */
498
  .output-box {
499
  background: rgba(255, 255, 255, 0.9) !important;
@@ -505,29 +407,6 @@ instagram_css = """
505
  padding: 1rem !important;
506
  }
507
 
508
- /* Examples styling */
509
- .gr-examples {
510
- background: rgba(255, 255, 255, 0.8) !important;
511
- border-radius: 15px !important;
512
- padding: 1rem !important;
513
- margin-top: 1rem !important;
514
- }
515
-
516
- .gr-examples .gr-button {
517
- background: linear-gradient(45deg, var(--ig-tertiary), var(--ig-quaternary)) !important;
518
- border: none !important;
519
- border-radius: 10px !important;
520
- color: white !important;
521
- margin: 0.25rem !important;
522
- transition: all 0.3s ease !important;
523
- font-weight: 600 !important;
524
- }
525
-
526
- .gr-examples .gr-button:hover {
527
- transform: translateY(-2px) !important;
528
- box-shadow: 0 4px 12px rgba(0,0,0,0.2) !important;
529
- }
530
-
531
  /* Tips section */
532
  .tips-section {
533
  background: rgba(255, 255, 255, 0.9) !important;
@@ -569,117 +448,7 @@ instagram_css = """
569
  font-size: 1.1em !important;
570
  padding: 12px 25px !important;
571
  }
572
-
573
- .loading-text {
574
- font-size: 1.2em !important;
575
- }
576
-
577
- .loading-subtext {
578
- font-size: 0.9em !important;
579
- }
580
- }
581
-
582
- /* Loading animation */
583
- @keyframes pulse {
584
- 0%, 100% { opacity: 1; }
585
- 50% { opacity: 0.5; }
586
- }
587
-
588
- .loading {
589
- animation: pulse 2s infinite;
590
- }
591
-
592
- /* Hide progress bar completely */
593
- .wrap.svelte-1116j96 {
594
- display: none !important;
595
  }
596
-
597
- /* Additional progress bar hiding */
598
- .gradio-container .progress-text,
599
- .gradio-container .progress-level,
600
- .gradio-container .progress-bar-wrap {
601
- display: none !important;
602
- }
603
- """
604
-
605
- # JavaScript for loading overlay
606
- loading_js = """
607
- <script>
608
- function showLoading() {
609
- // Create loading overlay
610
- const overlay = document.createElement('div');
611
- overlay.id = 'loading-overlay';
612
- overlay.className = 'loading-overlay';
613
- overlay.innerHTML = `
614
- <div class="loading-spinner"></div>
615
- <div class="loading-text ig-pulse">✨ Creating Magic...</div>
616
- <div class="loading-subtext">Generating your perfect Instagram captions</div>
617
- `;
618
- document.body.appendChild(overlay);
619
-
620
- // Update loading text dynamically
621
- const messages = [
622
- "🤖 Analyzing your photo...",
623
- "✨ Crafting catchy captions...",
624
- "🎯 Finding perfect words...",
625
- "🔥 Adding Instagram magic...",
626
- "📱 Almost ready..."
627
- ];
628
-
629
- let index = 0;
630
- const interval = setInterval(() => {
631
- const textElement = overlay.querySelector('.loading-text');
632
- if (textElement && index < messages.length) {
633
- textElement.textContent = messages[index];
634
- index++;
635
- } else {
636
- clearInterval(interval);
637
- }
638
- }, 800);
639
-
640
- return overlay;
641
- }
642
-
643
- function hideLoading() {
644
- const overlay = document.getElementById('loading-overlay');
645
- if (overlay) {
646
- overlay.style.opacity = '0';
647
- setTimeout(() => {
648
- overlay.remove();
649
- }, 300);
650
- }
651
- }
652
-
653
- // Override the generate function to show loading
654
- let originalGenerate = null;
655
-
656
- function setupLoadingEvents() {
657
- const generateBtn = document.querySelector('.generate-btn');
658
- const textInput = document.querySelector('textarea');
659
-
660
- if (generateBtn) {
661
- generateBtn.addEventListener('click', function() {
662
- if (textInput && textInput.value.trim()) {
663
- showLoading();
664
- setTimeout(hideLoading, 4000); // Hide after 4 seconds
665
- }
666
- });
667
- }
668
-
669
- if (textInput) {
670
- textInput.addEventListener('keypress', function(e) {
671
- if (e.key === 'Enter' && !e.shiftKey && textInput.value.trim()) {
672
- showLoading();
673
- setTimeout(hideLoading, 4000); // Hide after 4 seconds
674
- }
675
- });
676
- }
677
- }
678
-
679
- // Initialize when DOM is ready
680
- document.addEventListener('DOMContentLoaded', setupLoadingEvents);
681
- setTimeout(setupLoadingEvents, 1000); // Backup initialization
682
- </script>
683
  """
684
 
685
  # Create the Gradio interface
@@ -690,11 +459,10 @@ with gr.Blocks(
690
  neutral_hue="gray"
691
  ),
692
  css=instagram_css,
693
- title="InstaCap - AI Caption Generator",
694
- js=loading_js
695
  ) as demo:
696
 
697
- # Move title to the front and make it regular black
698
  gr.HTML('<h1 class="main-header">InstaCap – AI Caption Generator</h1>')
699
  gr.HTML('<p class="subtitle">Create short, catchy Instagram captions in seconds! Perfect for your social media posts ✨</p>')
700
 
@@ -724,25 +492,20 @@ with gr.Blocks(
724
  elem_classes="output-box"
725
  )
726
 
727
- # Connect the button to the function with loading
728
  generate_btn.click(
729
- fn=generate_instagram_captions_with_loading,
730
  inputs=description_input,
731
- outputs=caption_output,
732
- show_progress=True
733
  )
734
 
735
  # Also trigger on Enter key
736
  description_input.submit(
737
- fn=generate_instagram_captions_with_loading,
738
  inputs=description_input,
739
- outputs=caption_output,
740
- show_progress=True
741
  )
742
 
743
- # Add JavaScript for loading overlay
744
- gr.HTML(loading_js)
745
-
746
  gr.HTML(
747
  """
748
  <div class="tips-section">
 
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():
 
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():
 
292
 
293
  return result.strip()
294
 
295
+ # Simplified Instagram-inspired theme with custom CSS
296
  instagram_css = """
297
  /* Instagram color scheme and modern design */
298
  :root {
 
307
  --ig-light: #FFFFFF;
308
  }
309
 
 
 
 
 
 
 
 
 
310
  /* Main container styling */
311
  .gradio-container {
312
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
 
314
  font-family: 'Instagram Sans', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif !important;
315
  }
316
 
317
+ /* Header styling */
318
  .main-header {
319
  text-align: center;
320
  background: linear-gradient(45deg, var(--ig-primary), var(--ig-secondary));
 
396
  transform: translateY(-1px) !important;
397
  }
398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399
  /* Output styling */
400
  .output-box {
401
  background: rgba(255, 255, 255, 0.9) !important;
 
407
  padding: 1rem !important;
408
  }
409
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
410
  /* Tips section */
411
  .tips-section {
412
  background: rgba(255, 255, 255, 0.9) !important;
 
448
  font-size: 1.1em !important;
449
  padding: 12px 25px !important;
450
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
451
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
452
  """
453
 
454
  # Create the Gradio interface
 
459
  neutral_hue="gray"
460
  ),
461
  css=instagram_css,
462
+ title="InstaCap - AI Caption Generator"
 
463
  ) as demo:
464
 
465
+ # Header
466
  gr.HTML('<h1 class="main-header">InstaCap – AI Caption Generator</h1>')
467
  gr.HTML('<p class="subtitle">Create short, catchy Instagram captions in seconds! Perfect for your social media posts ✨</p>')
468
 
 
492
  elem_classes="output-box"
493
  )
494
 
495
+ # Connect the button to the function
496
  generate_btn.click(
497
+ fn=generate_instagram_captions,
498
  inputs=description_input,
499
+ outputs=caption_output
 
500
  )
501
 
502
  # Also trigger on Enter key
503
  description_input.submit(
504
+ fn=generate_instagram_captions,
505
  inputs=description_input,
506
+ outputs=caption_output
 
507
  )
508
 
 
 
 
509
  gr.HTML(
510
  """
511
  <div class="tips-section">