File size: 6,659 Bytes
3f6c6eb
 
f05400f
21d98e0
 
3f6c6eb
21d98e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8936ae6
21d98e0
8936ae6
 
21d98e0
8936ae6
21d98e0
 
 
 
 
 
 
 
8936ae6
 
3f6c6eb
f05400f
71a9ef0
 
 
 
21d98e0
 
71a9ef0
 
 
8936ae6
 
 
21d98e0
 
 
 
71a9ef0
21d98e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93d08c5
21d98e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93d08c5
3f6c6eb
21d98e0
 
3f6c6eb
 
 
 
 
8936ae6
 
f561a3b
8936ae6
 
 
71a9ef0
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
import gradio as gr
from models import generate_image, MODEL_ID
from config import APPLE_TENCENT_THEME
from typing import Optional, Union
from huggingface_hub import whoami

def verify_pro_status(token: Optional[Union[gr.OAuthToken, str]]) -> bool:
    """Verifies if the user is a Hugging Face PRO user or part of an enterprise org."""
    if not token:
        return False
    
    if isinstance(token, gr.OAuthToken):
        token_str = token.token
    elif isinstance(token, str):
        token_str = token
    else:
        return False
    
    try:
        user_info = whoami(token=token_str)
        return (
            user_info.get("isPro", False) or
            any(org.get("isEnterprise", False) for org in user_info.get("orgs", []))
        )
    except Exception as e:
        print(f"Could not verify user's PRO/Enterprise status: {e}")
        return False

def generate_image_with_pro_check(prompt: str, profile: Optional[gr.OAuthProfile] = None, oauth_token: Optional[gr.OAuthToken] = None):
    """
    Wrapper function that checks if user is PRO before generating image.
    """
    if profile is None:
        raise gr.Error("Please Sign in with Hugging Face to use this app")
    
    # Check if user has PRO status
    if not verify_pro_status(oauth_token):
        raise gr.Error(
            "This app is exclusively for Hugging Face PRO subscribers. "
            "Please upgrade at https://huggingface.co/subscribe/pro to access this feature."
        )
    
    # User is PRO, proceed with image generation
    return generate_image(prompt)

def create_ui():
    with gr.Blocks(title=f"Tencent HunyuanImage-3.0 Demo", theme=APPLE_TENCENT_THEME) as demo:
        gr.HTML(
            f"<div style='text-align: center; max-width: 700px; margin: 0 auto;'>"
            f"<h1>Tencent {MODEL_ID.split('/')[-1]}</h1>"
            f"<p>Generate images using Tencent's state-of-the-art model hosted by FAL AI.</p>"
            f"<p style='color: orange;'>⚠️ This app is exclusively for Hugging Face PRO subscribers.</p>"
            f"<p><a href='https://huggingface.co/subscribe/pro' target='_blank' style='color: #ff6b6b; font-weight: bold;'>πŸš€ Subscribe to PRO</a></p>"
            f"Built with <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>anycoder</a>"
            f"</div>"
        )
        
        # Add login button - required for OAuth
        gr.LoginButton()
        
        # PRO status message area
        pro_message = gr.Markdown(visible=False)
        main_interface = gr.Column(visible=False)

        with main_interface:
            with gr.Row():
                with gr.Column(scale=1):
                    prompt_input = gr.Textbox(
                        label="Prompt",
                        placeholder="e.g., A detailed watercolor painting of a small red fox sleeping on a pile of autumn leaves.",
                        lines=4
                    )
                    generate_btn = gr.Button("🎨 Generate Image", variant="primary")
                    
                with gr.Column(scale=1):
                    output_image = gr.Image(
                        label="Generated Image",
                        height=512,
                        width=512,
                        interactive=False,
                        show_download_button=True
                    )
            
            # Set up the event listener - note the function now takes OAuthProfile and OAuthToken
            generate_btn.click(
                fn=generate_image_with_pro_check,
                inputs=[prompt_input],
                outputs=[output_image],
                queue=False,
                api_name=False,
                show_api=False,
            )
            
            # Example usage guidance with queue features disabled
            gr.Examples(
                examples=[
                    "A detailed watercolor painting of a small red fox sleeping on a pile of autumn leaves.",
                    "A futuristic city skyline at sunset with flying cars",
                    "A mystical forest with glowing mushrooms and fireflies",
                    "An astronaut riding a horse on Mars",
                ],
                inputs=prompt_input,
                outputs=output_image,
                fn=generate_image_with_pro_check,
                cache_examples=False,
                api_name=False,
                show_api=False,
            )
            
            gr.Markdown("<h3 style='text-align: center'>Thank you for being a PRO subscriber! πŸ€—</h3>")

        # Control access based on PRO status
        def control_access(profile: Optional[gr.OAuthProfile] = None, oauth_token: Optional[gr.OAuthToken] = None):
            if not profile:
                return (
                    gr.update(visible=False),  # main_interface
                    gr.update(visible=True, value=(
                        "## πŸ‘‹ Welcome!\n\n"
                        "Please **Sign in with Hugging Face** using the button above to continue."
                    ))  # pro_message
                )
            
            if verify_pro_status(oauth_token):
                return (
                    gr.update(visible=True),   # main_interface
                    gr.update(visible=False)   # pro_message
                )
            else:
                message = (
                    "## ✨ Exclusive Access for PRO Users\n\n"
                    "Thank you for your interest! This app is available exclusively for our Hugging Face **PRO** members.\n\n"
                    "With PRO, you'll get:\n"
                    "- Access to exclusive Spaces like this one\n"
                    "- Increased GPU quotas\n"
                    "- Early access to new features\n"
                    "- Priority support\n\n"
                    "### [**Become a PRO Today!**](https://huggingface.co/subscribe/pro)\n\n"
                    "*Already a PRO member? Try logging out and back in if you're having issues.*"
                )
                return (
                    gr.update(visible=False),  # main_interface
                    gr.update(visible=True, value=message)  # pro_message
                )
        
        # Load event to check access on page load
        demo.load(control_access, inputs=None, outputs=[main_interface, pro_message])

    return demo

if __name__ == "__main__":
    app = create_ui()
    # Launch without special auth parameters
    # OAuth is enabled via Space metadata (hf_oauth: true in README.md)
    app.launch(
        show_api=False,
        enable_monitoring=False,
        quiet=True,
    )