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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""

Shelter Flyer Generator - Main Application

A Gradio-based web app for animal shelters to generate adoption flyers.



Compatible with both local deployment and Hugging Face Spaces.

"""

import os
import sys
import gradio as gr
from PIL import Image
from pathlib import Path
from datetime import datetime

# Load environment variables (for local .env support)
# Priority: Environment variables > .env file
try:
    from dotenv import load_dotenv
    load_dotenv()  # This loads .env if present, but doesn't override existing env vars
except ImportError:
    print("[INFO] python-dotenv not installed. Using environment variables only.")

# Import core modules
try:
    from core.background_removal import remove_background
    from core.compositing import composite_animal_on_scene
    from core.bio_generator import generate_bio
    from core.flyer_builder import build_flyer
    print("[OK] All core modules loaded successfully.")
except ImportError as e:
    print(f"[ERROR] Failed to import core modules: {e}")
    print("Please ensure all core modules are present in the 'core/' directory.")
    sys.exit(1)

# ============================================================================
# ROBUST TOKEN MANAGEMENT
# ============================================================================
def check_hf_token():
    """

    Check for HF_TOKEN with proper priority:

    1. Environment variable (for HF Spaces)

    2. .env file (for local development)

    

    Returns:

        str or None: The token if found, None otherwise

    """
    token = os.getenv("HF_TOKEN")
    
    if token and token != "hf_xxxxxxxxxxxxxxxxxxxxx":
        print("βœ… Token detected")
        return token
    else:
        print("❌ Token not found")
        print("[WARN] Bio generation will use fallback template mode.")
        print("[INFO] To use AI bio generation, set HF_TOKEN environment variable.")
        print("[INFO] Get your token at: https://huggingface.co/settings/tokens")
        return None

# Check token at startup
HF_TOKEN = check_hf_token()

# ============================================================================
# PATH CONSISTENCY (pathlib for cross-platform compatibility)
# ============================================================================
BASE_DIR = Path(__file__).parent.resolve()
BACKGROUNDS_DIR = BASE_DIR / "backgrounds"
OUTPUTS_DIR = BASE_DIR / "outputs"

# Ensure outputs directory exists
OUTPUTS_DIR.mkdir(parents=True, exist_ok=True)

# Pre-defined personality traits
PERSONALITY_TRAITS = [
    "Good with kids",
    "Good with other dogs",
    "Good with cats",
    "High energy",
    "Calm / Relaxed",
    "Playful",
    "Loyal",
    "Trained / Knows commands",
    "Loves cuddles",
    "Independent",
    "Gentle",
    "Protective",
    "Loves walks",
    "House-trained",
    "Special needs"
]

# Available background scenes
BACKGROUND_SCENES = {
    "Living Room": "living_room.jpg",
    "Cozy Home": "cozy_home.jpg",
    "Garden/Yard": "garden.jpg",
    "Forest Park": "park.jpg",
    "Beach Sunset": "beach.jpg"
}


# ============================================================================
# MAIN PIPELINE WITH FILE DOWNLOAD SUPPORT
# ============================================================================
def generate_flyer_pipeline(

    animal_photo,

    scene_choice,

    custom_scene,

    animal_name,

    animal_type,

    breed,

    age,

    traits,

    additional_notes,

    shelter_name,

    shelter_contact

):
    """

    Main pipeline that orchestrates the flyer generation process.

    

    Returns:

        enhanced_photo: PIL.Image - The composited animal on scene

        generated_bio: str - The AI-generated bio

        flyer_file_path: str - Path to downloadable flyer file

        status: str - Status message

    """
    
    try:
        # Validate inputs
        if animal_photo is None:
            return None, None, None, "❌ Please upload an animal photo."
        
        if not animal_name or not animal_type or not breed or not age:
            return None, None, None, "❌ Please fill in all animal information fields."
        
        status_msg = "πŸš€ Starting flyer generation...\n\n"
        
        # Step 1: Select scene
        if custom_scene is not None:
            scene_image = custom_scene
            status_msg += "βœ… Using custom background scene\n"
        elif scene_choice and scene_choice in BACKGROUND_SCENES:
            scene_path = BACKGROUNDS_DIR / BACKGROUND_SCENES[scene_choice]
            if not scene_path.exists():
                return None, None, None, f"❌ Background file not found: {scene_path}"
            scene_image = Image.open(scene_path)
            status_msg += f"βœ… Using {scene_choice} background\n"
        else:
            return None, None, None, "❌ Please select a background scene or upload a custom one."
        
        # Step 2: Remove background from animal photo
        status_msg += "πŸ”„ Removing background from animal photo...\n"
        try:
            animal_cutout = remove_background(animal_photo)
            status_msg += "βœ… Background removed successfully\n"
        except Exception as e:
            status_msg += f"❌ Background removal failed: {str(e)}\n"
            return None, None, None, status_msg
        
        # Step 3: Composite animal onto scene
        status_msg += "πŸ”„ Compositing animal onto scene...\n"
        try:
            enhanced_photo = composite_animal_on_scene(
                animal_rgba=animal_cutout,
                scene=scene_image,
                position="center",
                scale_factor=0.6
            )
            status_msg += "βœ… Enhanced photo created\n"
        except Exception as e:
            status_msg += f"❌ Compositing failed: {str(e)}\n"
            return None, None, None, status_msg
        
        # Step 4: Generate bio with AI error handling & fallback
        status_msg += "πŸ”„ Generating bio...\n"
        try:
            bio_text = generate_bio(
                name=animal_name,
                animal_type=animal_type,
                breed=breed,
                age=age,
                traits=traits if traits else [],
                additional_notes=additional_notes
            )
            
            # Check if fallback was used
            if "Meet" in bio_text and "looking for a forever home" in bio_text:
                status_msg += "βœ… Bio generated (using template fallback)\n"
            else:
                status_msg += "βœ… Bio generated with AI\n"
                
        except Exception as e:
            status_msg += f"⚠️ Bio generation encountered an issue: {str(e)}\n"
            status_msg += "πŸ“ Using template-based bio as fallback...\n"
            
            # Manual fallback if bio_generator fails completely
            bio_text = f"Meet {animal_name}! This {age} {breed} {animal_type.lower()} is looking for a forever home. "
            if traits:
                bio_text += f"{animal_name} is {', '.join(traits[:3]).lower()}. "
            bio_text += f"Come meet {animal_name} at our shelter today!"
            status_msg += "βœ… Template bio created\n"
        
        # Step 5: Build final flyer
        status_msg += "πŸ”„ Building final flyer...\n"
        try:
            flyer_image = build_flyer(
                photo=enhanced_photo,
                name=animal_name,
                animal_type=animal_type,
                breed=breed,
                age=age,
                bio=bio_text,
                shelter_name=shelter_name if shelter_name else "Your Local Animal Shelter",
                shelter_contact=shelter_contact if shelter_contact else "",
                template="template_1"
            )
            status_msg += "βœ… Flyer created successfully\n"
        except Exception as e:
            status_msg += f"❌ Flyer building failed: {str(e)}\n"
            return None, None, None, status_msg
        
        # Step 6: Save flyer to file for download
        status_msg += "πŸ’Ύ Saving flyer...\n"
        try:
            # Create unique filename with timestamp
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            safe_name = "".join(c for c in animal_name if c.isalnum() or c in (' ', '-', '_'))
            safe_name = safe_name.replace(' ', '_')
            flyer_filename = f"flyer_{safe_name}_{timestamp}.jpg"
            flyer_path = OUTPUTS_DIR / flyer_filename
            
            # Save with high quality
            flyer_image.save(flyer_path, "JPEG", quality=95, optimize=True)
            
            status_msg += f"βœ… Flyer saved: {flyer_filename}\n"
            status_msg += "\nπŸŽ‰ Generation complete! You can now download your flyer.\n"
            
            # Return the path as a string (Gradio will handle the file download)
            return enhanced_photo, bio_text, str(flyer_path), status_msg
            
        except Exception as e:
            status_msg += f"❌ Failed to save flyer: {str(e)}\n"
            return enhanced_photo, bio_text, None, status_msg
        
    except Exception as e:
        import traceback
        error_msg = "❌ Unexpected error occurred:\n\n"
        error_msg += f"Error: {str(e)}\n\n"
        error_msg += "Stack trace:\n"
        error_msg += traceback.format_exc()
        return None, None, None, error_msg


def regenerate_bio_only(

    animal_name,

    animal_type,

    breed,

    age,

    traits,

    additional_notes

):
    """

    Regenerates just the bio without reprocessing images.

    Includes proper error handling and fallback.

    """
    
    try:
        if not animal_name or not animal_type:
            return "❌ Please provide at least name and animal type.", "Error"
        
        bio_text = generate_bio(
            name=animal_name,
            animal_type=animal_type,
            breed=breed,
            age=age,
            traits=traits if traits else [],
            additional_notes=additional_notes
        )
        
        # Check if AI or fallback was used
        if "Meet" in bio_text and "looking for a forever home" in bio_text:
            status = "βœ… Bio regenerated (template mode)"
        else:
            status = "βœ… Bio regenerated with AI"
        
        return bio_text, status
        
    except Exception as e:
        error_msg = f"❌ Error generating bio: {str(e)}\n"
        error_msg += "Using fallback template..."
        
        # Fallback bio
        bio_text = f"Meet {animal_name}! This {breed} {animal_type.lower()} is looking for a loving home. "
        if traits:
            bio_text += f"{animal_name} is {', '.join(traits[:2]).lower()}. "
        bio_text += f"Visit our shelter to meet {animal_name}!"
        
        return bio_text, "⚠️ Used fallback template (API unavailable)"


# ============================================================================
# GRADIO 5.0+ COMPATIBLE UI
# ============================================================================
with gr.Blocks(
    title="🐾 Shelter Flyer Generator",
    theme=gr.themes.Soft()
) as app:
    
    gr.Markdown("""

    # 🐾 Shelter Flyer Generator

    

    Create beautiful adoption flyers for shelter animals with AI-enhanced photos and compelling bios.

    

    **How it works:**

    1. Upload a photo of the animal

    2. Choose a background scene

    3. Fill in the animal's information

    4. Generate a professional flyer!

    """)
    
    with gr.Tabs():
        # ====================================================================
        # TAB 1: Upload & Describe
        # ====================================================================
        with gr.Tab("πŸ“Έ Upload & Describe"):
            with gr.Row():
                with gr.Column():
                    gr.Markdown("### Animal Photo")
                    animal_photo = gr.Image(
                        type="pil",
                        label="Upload Animal Photo",
                        height=400
                    )
                    
                    gr.Markdown("### Background Scene")
                    scene_choice = gr.Radio(
                        choices=list(BACKGROUND_SCENES.keys()),
                        label="Choose a Background Scene",
                        value="Living Room"
                    )
                    
                    custom_scene = gr.Image(
                        type="pil",
                        label="Or Upload Your Own Scene (optional)",
                        height=200
                    )
                
                with gr.Column():
                    gr.Markdown("### Animal Information")
                    animal_name = gr.Textbox(
                        label="Animal Name",
                        placeholder="e.g., Bella"
                    )
                    
                    animal_type = gr.Radio(
                        choices=["Dog", "Cat", "Other"],
                        label="Animal Type",
                        value="Dog"
                    )
                    
                    with gr.Row():
                        breed = gr.Textbox(
                            label="Breed",
                            placeholder="e.g., Golden Retriever"
                        )
                        age = gr.Textbox(
                            label="Age",
                            placeholder="e.g., 2 years"
                        )
                    
                    gr.Markdown("### Personality Traits")
                    traits = gr.CheckboxGroup(
                        choices=PERSONALITY_TRAITS,
                        label="Select all that apply"
                    )
                    
                    additional_notes = gr.Textbox(
                        lines=3,
                        label="Additional Notes",
                        placeholder="Any special information about this animal..."
                    )
                    
                    gr.Markdown("### Shelter Information")
                    shelter_name = gr.Textbox(
                        label="Shelter Name",
                        placeholder="e.g., Happy Paws Animal Shelter",
                        value="Your Local Animal Shelter"
                    )
                    
                    shelter_contact = gr.Textbox(
                        label="Contact Info",
                        placeholder="e.g., (555) 123-4567 | www.shelter.org"
                    )
            
            with gr.Row():
                generate_btn = gr.Button(
                    "🐾 Generate Flyer",
                    variant="primary",
                    size="lg"
                )
            
            status_output = gr.Textbox(
                label="Status",
                lines=10,
                interactive=False
            )
        
        # ====================================================================
        # TAB 2: Preview & Edit
        # ====================================================================
        with gr.Tab("πŸ‘οΈ Preview & Edit"):
            gr.Markdown("### Enhanced Photo")
            enhanced_photo_output = gr.Image(
                label="Enhanced Photo Preview",
                type="pil"
            )
            
            gr.Markdown("### Generated Bio")
            with gr.Row():
                bio_output = gr.Textbox(
                    lines=8,
                    label="Generated Bio (Editable)",
                    interactive=True
                )
            
            with gr.Row():
                regenerate_bio_btn = gr.Button("πŸ”„ Regenerate Bio")
                bio_status = gr.Textbox(
                    label="Bio Status",
                    lines=1,
                    interactive=False
                )
        
        # ====================================================================
        # TAB 3: Download (UPDATED FOR FILE DOWNLOADS)
        # ====================================================================
        with gr.Tab("⬇️ Download"):
            gr.Markdown("""

            ### Download Your Flyer

            

            Once your flyer is generated, you can download it here.

            """)
            
            # Main flyer download with file output
            download_flyer = gr.File(
                label="πŸ“₯ Download Final Flyer (JPG)",
                interactive=False
            )
            
            gr.Markdown("""

            ---

            **Tip:** The downloaded JPG is high-quality and ready for:

            - Social media posts

            - Email campaigns  

            - Printing on flyers

            - Posting on your shelter website

            """)
    
    # ========================================================================
    # EVENT HANDLERS
    # ========================================================================
    generate_btn.click(
        fn=generate_flyer_pipeline,
        inputs=[
            animal_photo,
            scene_choice,
            custom_scene,
            animal_name,
            animal_type,
            breed,
            age,
            traits,
            additional_notes,
            shelter_name,
            shelter_contact
        ],
        outputs=[
            enhanced_photo_output,
            bio_output,
            download_flyer,  # Now outputs file path for download
            status_output
        ]
    )
    
    regenerate_bio_btn.click(
        fn=regenerate_bio_only,
        inputs=[
            animal_name,
            animal_type,
            breed,
            age,
            traits,
            additional_notes
        ],
        outputs=[
            bio_output,
            bio_status
        ]
    )
    
    gr.Markdown("""

    ---

    ### About

    

    This app uses:

    - **Background Removal**: RMBG-2.0 (Hugging Face)

    - **Bio Generation**: Zephyr-7B (Hugging Face Inference API)

    - **Image Processing**: Pillow

    

    Built for animal shelters to create compelling adoption flyers. 🐾

    

    **Note:** If HF_TOKEN is not configured, bio generation will use template mode.

    """)


# ============================================================================
# LAUNCH CONFIGURATION
# ============================================================================
if __name__ == "__main__":
    print("\n" + "="*60)
    print("🐾 Shelter Flyer Generator")
    print("="*60)
    print(f"πŸ“ Base directory: {BASE_DIR}")
    print(f"πŸ“ Backgrounds: {BACKGROUNDS_DIR}")
    print(f"πŸ“ Outputs: {OUTPUTS_DIR}")
    print(f"πŸ”‘ HF Token: {'βœ… Configured' if HF_TOKEN else '❌ Not found (using fallback mode)'}")
    print("="*60 + "\n")
    
    # Launch with proper settings for both local and HF Spaces
    app.launch(
        server_name="0.0.0.0",  # Allows external access (needed for HF Spaces)
        server_port=7860,        # Standard port
        share=False              # Set to True for temporary public link
    )