import gradio as gr
import sys
import os
# Add the dist directory to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'dist'))
# Import obfuscated module - all logic is now in core_logic
try:
    from core_logic import (
        secure_virtual_tryon,
        handle_like_action,
        get_company_info,
        create_footer_html,
        create_instructions_html,
        get_css_styles
    )
    print("Core logic module loaded successfully")
except ImportError as e:
    print(f"Error: Obfuscated module not found: {e}")
    print("Current directory:", os.getcwd())
    print("Files in dist:", os.listdir('dist') if os.path.exists('dist') else 'dist not found')
    sys.exit(1)
# Get company info and styles from core_logic
company_info = get_company_info()
css = get_css_styles()
def virtual_tryon_interface(human_img, garment_img, garment_type, request: gr.Request):
    """Simple interface function that calls the secure core logic"""
    return secure_virtual_tryon(human_img, garment_img, garment_type, request)
def like_button_interface(request: gr.Request):
    """Simple interface function for like button"""
    return handle_like_action(request)
# Create the Gradio interface
with gr.Blocks(css=css, title=f"{company_info['name']} Virtual Try-On", theme=gr.themes.Ocean()) as demo:
    with gr.Row():
        with gr.Column(0.7):
            gr.Markdown(f"""
                
                     
                    
                        {company_info['name']} Virtual Try-On 👕 👗
                        Try on complete outfits with our AI-powered virtual try-on technology
                     
                 
                """)
            gr.HTML(create_instructions_html())
        with gr.Column(0.3):
            gr.Markdown(f"""
                
                """)
    
    with gr.Row(elem_id="col-container"):
        # Step 1: Person Image
        with gr.Column(elem_classes="step-column"):
            gr.HTML("""
            
                1. Upload Person Image
            
            """)
            human_img = gr.Image(
                label="Person Image", 
                sources='upload', 
                type="numpy",
                elem_classes="image-preview",
                height=400
            )
            gr.Examples(
                examples=[
                    "assets/human/1.jpg",
                    "assets/human/2.jpg",
                    "assets/human/3.jpg",
                    "assets/human/4.jpg",
                    "assets/human/5.jpg",
                    "assets/human/6.jpg",
                    "assets/human/7.jpg",
                    "assets/human/8.jpg",
                    "assets/human/9.jpg",
                    "assets/human/10.jpg",
                    "assets/human/11.jpg",
                    "assets/human/12.jpg"
                ],
                inputs=human_img,
                label="Example Person Images",
                examples_per_page=12
            )
        
        # Step 2: Garment Image
        with gr.Column(elem_classes="step-column"):
            gr.HTML("""
            
                2. Upload Garment Image
            
            """)
            garment_img = gr.Image(
                label="Garment Image", 
                sources='upload', 
                type="numpy",
                elem_classes="image-preview",
                height=400
            )
            garment_type = gr.Dropdown(
                choices=["Dress/Suit", "Top", "Bottom"],
                value="Dress/Suit",
                label="Garment Type",
                info="Select the type of garment you want to try on"
            )
            gr.Examples(
                examples=[
                    "assets/garment/1.jpg",
                    "assets/garment/2.jpg",
                    "assets/garment/3.jpg",
                    "assets/garment/4.jpg",
                    "assets/garment/5.jpg",
                    "assets/garment/6.jpg",
                    "assets/garment/7.jpg",
                    "assets/garment/8.jpg",
                    "assets/garment/9.jpg",
                    "assets/garment/10.jpg",
                    "assets/garment/11.jpg",
                    "assets/garment/12.jpg"
                ],
                inputs=garment_img,
                label="Example Garment Images",
                examples_per_page=12
            )
        
        # Step 3: Results
        with gr.Column(elem_classes="step-column"):
            gr.HTML("""
            
                3. Virtual Try-On Result
            
            """)
            result_img = gr.Image(
                label="Try-On Result", 
                interactive=False,
                elem_classes="image-preview",
                height=400
            )
            with gr.Row():
                like_button = gr.Button(
                    "👍 Like this result!", 
                    elem_classes="like-button",
                    visible=False
                )
            try_button = gr.Button(
                "Run Virtual Try-On 🚀",
                elem_classes="button-gradient"
            )
            gr.HTML("""
            
                If you like our Virtual Try-On results, please give us a ⭐ into our space!
             
            """)
    
            signup_prompt = gr.HTML(
                visible=True,
                value=f"""
                    🚀 Want unlimited generations?
                    Please sign up at {company_info['name']}.ai for unlimited access to all our AI tools!
                    
                        SignUp for Free 🚀
                    
                 """
            )
    
    # Examples (if exists)
    if os.path.exists("assets/examples"):
        with gr.Row():
            gr.Examples(
                examples=[
                    ["assets/examples/model1.jpg", "assets/examples/garment1.png", "Dress/Suit", "assets/examples/result1.jpg"],
                    ["assets/examples/model2.png", "assets/examples/garment2.jpg", "Dress/Suit", "assets/examples/result2.jpg"],
                    ["assets/examples/model3.jpg", "assets/examples/garment3.jpg", "Dress/Suit", "assets/examples/result3.jpg"],
                ],
                inputs=[human_img, garment_img, garment_type, result_img],
                label=None,
                examples_per_page=3
            )
    
    # Button actions - Simple interface calls
    try_button.click(
        fn=virtual_tryon_interface,
        inputs=[human_img, garment_img, garment_type],
        outputs=[result_img, like_button],
        api_name="virtual_tryon"
    )
    like_button.click(
        fn=like_button_interface,
        inputs=[],
        outputs=[like_button]
    )
    # Visitor badge
    gr.HTML(f' ')
    # Footer
    gr.HTML(create_footer_html())
    
if __name__ == "__main__":
    demo.launch(
        show_api=False,
        server_name="0.0.0.0",
        server_port=7860
    )
')
    # Footer
    gr.HTML(create_footer_html())
    
if __name__ == "__main__":
    demo.launch(
        show_api=False,
        server_name="0.0.0.0",
        server_port=7860
    )