Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from models import generate_image, MODEL_ID
|
| 3 |
from config import APPLE_TENCENT_THEME
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
| 7 |
-
Wrapper function that checks if user is
|
| 8 |
"""
|
| 9 |
if profile is None:
|
| 10 |
-
raise gr.Error("
|
| 11 |
|
| 12 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
return generate_image(prompt)
|
| 14 |
|
| 15 |
def create_ui():
|
|
@@ -18,54 +49,101 @@ def create_ui():
|
|
| 18 |
f"<div style='text-align: center; max-width: 700px; margin: 0 auto;'>"
|
| 19 |
f"<h1>Tencent {MODEL_ID.split('/')[-1]}</h1>"
|
| 20 |
f"<p>Generate images using Tencent's state-of-the-art model hosted by FAL AI.</p>"
|
| 21 |
-
f"<p style='color: orange;'>β οΈ
|
|
|
|
| 22 |
f"Built with <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>anycoder</a>"
|
| 23 |
f"</div>"
|
| 24 |
)
|
| 25 |
|
| 26 |
# Add login button - required for OAuth
|
| 27 |
gr.LoginButton()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
with
|
| 30 |
-
with gr.
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
-
|
| 47 |
-
# Set up the event listener - note the function now takes OAuthProfile
|
| 48 |
-
generate_btn.click(
|
| 49 |
-
fn=generate_image_with_auth,
|
| 50 |
-
inputs=[prompt_input],
|
| 51 |
-
outputs=[output_image],
|
| 52 |
-
queue=False,
|
| 53 |
-
api_name=False,
|
| 54 |
-
show_api=False,
|
| 55 |
-
)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
examples=[
|
| 60 |
-
"A detailed watercolor painting of a small red fox sleeping on a pile of autumn leaves."
|
| 61 |
-
],
|
| 62 |
-
inputs=prompt_input,
|
| 63 |
-
outputs=output_image,
|
| 64 |
-
fn=generate_image, # Examples use the original function
|
| 65 |
-
cache_examples=False,
|
| 66 |
-
api_name=False,
|
| 67 |
-
show_api=False,
|
| 68 |
-
)
|
| 69 |
|
| 70 |
return demo
|
| 71 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from models import generate_image, MODEL_ID
|
| 3 |
from config import APPLE_TENCENT_THEME
|
| 4 |
+
from typing import Optional, Union
|
| 5 |
+
from huggingface_hub import whoami
|
| 6 |
|
| 7 |
+
def verify_pro_status(token: Optional[Union[gr.OAuthToken, str]]) -> bool:
|
| 8 |
+
"""Verifies if the user is a Hugging Face PRO user or part of an enterprise org."""
|
| 9 |
+
if not token:
|
| 10 |
+
return False
|
| 11 |
+
|
| 12 |
+
if isinstance(token, gr.OAuthToken):
|
| 13 |
+
token_str = token.token
|
| 14 |
+
elif isinstance(token, str):
|
| 15 |
+
token_str = token
|
| 16 |
+
else:
|
| 17 |
+
return False
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
user_info = whoami(token=token_str)
|
| 21 |
+
return (
|
| 22 |
+
user_info.get("isPro", False) or
|
| 23 |
+
any(org.get("isEnterprise", False) for org in user_info.get("orgs", []))
|
| 24 |
+
)
|
| 25 |
+
except Exception as e:
|
| 26 |
+
print(f"Could not verify user's PRO/Enterprise status: {e}")
|
| 27 |
+
return False
|
| 28 |
+
|
| 29 |
+
def generate_image_with_pro_check(prompt: str, profile: Optional[gr.OAuthProfile] = None, oauth_token: Optional[gr.OAuthToken] = None):
|
| 30 |
"""
|
| 31 |
+
Wrapper function that checks if user is PRO before generating image.
|
| 32 |
"""
|
| 33 |
if profile is None:
|
| 34 |
+
raise gr.Error("Please Sign in with Hugging Face to use this app")
|
| 35 |
|
| 36 |
+
# Check if user has PRO status
|
| 37 |
+
if not verify_pro_status(oauth_token):
|
| 38 |
+
raise gr.Error(
|
| 39 |
+
"This app is exclusively for Hugging Face PRO subscribers. "
|
| 40 |
+
"Please upgrade at https://huggingface.co/subscribe/pro to access this feature."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# User is PRO, proceed with image generation
|
| 44 |
return generate_image(prompt)
|
| 45 |
|
| 46 |
def create_ui():
|
|
|
|
| 49 |
f"<div style='text-align: center; max-width: 700px; margin: 0 auto;'>"
|
| 50 |
f"<h1>Tencent {MODEL_ID.split('/')[-1]}</h1>"
|
| 51 |
f"<p>Generate images using Tencent's state-of-the-art model hosted by FAL AI.</p>"
|
| 52 |
+
f"<p style='color: orange;'>β οΈ This app is exclusively for Hugging Face PRO subscribers.</p>"
|
| 53 |
+
f"<p><a href='https://huggingface.co/subscribe/pro' target='_blank' style='color: #ff6b6b; font-weight: bold;'>π Subscribe to PRO</a></p>"
|
| 54 |
f"Built with <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>anycoder</a>"
|
| 55 |
f"</div>"
|
| 56 |
)
|
| 57 |
|
| 58 |
# Add login button - required for OAuth
|
| 59 |
gr.LoginButton()
|
| 60 |
+
|
| 61 |
+
# PRO status message area
|
| 62 |
+
pro_message = gr.Markdown(visible=False)
|
| 63 |
+
main_interface = gr.Column(visible=False)
|
| 64 |
|
| 65 |
+
with main_interface:
|
| 66 |
+
with gr.Row():
|
| 67 |
+
with gr.Column(scale=1):
|
| 68 |
+
prompt_input = gr.Textbox(
|
| 69 |
+
label="Prompt",
|
| 70 |
+
placeholder="e.g., A detailed watercolor painting of a small red fox sleeping on a pile of autumn leaves.",
|
| 71 |
+
lines=4
|
| 72 |
+
)
|
| 73 |
+
generate_btn = gr.Button("π¨ Generate Image", variant="primary")
|
| 74 |
+
|
| 75 |
+
with gr.Column(scale=1):
|
| 76 |
+
output_image = gr.Image(
|
| 77 |
+
label="Generated Image",
|
| 78 |
+
height=512,
|
| 79 |
+
width=512,
|
| 80 |
+
interactive=False,
|
| 81 |
+
show_download_button=True
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Set up the event listener - note the function now takes OAuthProfile and OAuthToken
|
| 85 |
+
generate_btn.click(
|
| 86 |
+
fn=generate_image_with_pro_check,
|
| 87 |
+
inputs=[prompt_input],
|
| 88 |
+
outputs=[output_image],
|
| 89 |
+
queue=False,
|
| 90 |
+
api_name=False,
|
| 91 |
+
show_api=False,
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Example usage guidance with queue features disabled
|
| 95 |
+
gr.Examples(
|
| 96 |
+
examples=[
|
| 97 |
+
"A detailed watercolor painting of a small red fox sleeping on a pile of autumn leaves.",
|
| 98 |
+
"A futuristic city skyline at sunset with flying cars",
|
| 99 |
+
"A mystical forest with glowing mushrooms and fireflies",
|
| 100 |
+
"An astronaut riding a horse on Mars",
|
| 101 |
+
],
|
| 102 |
+
inputs=prompt_input,
|
| 103 |
+
outputs=output_image,
|
| 104 |
+
fn=generate_image_with_pro_check,
|
| 105 |
+
cache_examples=False,
|
| 106 |
+
api_name=False,
|
| 107 |
+
show_api=False,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
gr.Markdown("<h3 style='text-align: center'>Thank you for being a PRO subscriber! π€</h3>")
|
| 111 |
+
|
| 112 |
+
# Control access based on PRO status
|
| 113 |
+
def control_access(profile: Optional[gr.OAuthProfile] = None, oauth_token: Optional[gr.OAuthToken] = None):
|
| 114 |
+
if not profile:
|
| 115 |
+
return (
|
| 116 |
+
gr.update(visible=False), # main_interface
|
| 117 |
+
gr.update(visible=True, value=(
|
| 118 |
+
"## π Welcome!\n\n"
|
| 119 |
+
"Please **Sign in with Hugging Face** using the button above to continue."
|
| 120 |
+
)) # pro_message
|
| 121 |
)
|
| 122 |
+
|
| 123 |
+
if verify_pro_status(oauth_token):
|
| 124 |
+
return (
|
| 125 |
+
gr.update(visible=True), # main_interface
|
| 126 |
+
gr.update(visible=False) # pro_message
|
| 127 |
+
)
|
| 128 |
+
else:
|
| 129 |
+
message = (
|
| 130 |
+
"## β¨ Exclusive Access for PRO Users\n\n"
|
| 131 |
+
"Thank you for your interest! This app is available exclusively for our Hugging Face **PRO** members.\n\n"
|
| 132 |
+
"With PRO, you'll get:\n"
|
| 133 |
+
"- Access to exclusive Spaces like this one\n"
|
| 134 |
+
"- Increased GPU quotas\n"
|
| 135 |
+
"- Early access to new features\n"
|
| 136 |
+
"- Priority support\n\n"
|
| 137 |
+
"### [**Become a PRO Today!**](https://huggingface.co/subscribe/pro)\n\n"
|
| 138 |
+
"*Already a PRO member? Try logging out and back in if you're having issues.*"
|
| 139 |
+
)
|
| 140 |
+
return (
|
| 141 |
+
gr.update(visible=False), # main_interface
|
| 142 |
+
gr.update(visible=True, value=message) # pro_message
|
| 143 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
+
# Load event to check access on page load
|
| 146 |
+
demo.load(control_access, inputs=None, outputs=[main_interface, pro_message])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
return demo
|
| 149 |
|