Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,65 +1,241 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from models import generate_image, MODEL_ID
|
| 3 |
from config import APPLE_TENCENT_THEME
|
|
|
|
| 4 |
|
| 5 |
def create_ui():
|
|
|
|
| 6 |
with gr.Blocks(title=f"Tencent HunyuanImage-3.0 Demo", theme=APPLE_TENCENT_THEME) as demo:
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
f"Built with <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>anycoder</a>"
|
| 12 |
-
f"</div>"
|
| 13 |
-
)
|
| 14 |
-
|
| 15 |
with gr.Row():
|
| 16 |
-
with gr.Column(scale=
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
)
|
| 22 |
-
generate_btn = gr.Button("π¨ Generate Image", variant="primary")
|
| 23 |
-
|
| 24 |
with gr.Column(scale=1):
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
generate_btn.click(
|
| 35 |
-
fn=
|
| 36 |
-
inputs=[prompt_input],
|
| 37 |
-
outputs=[output_image],
|
| 38 |
-
queue=False,
|
| 39 |
-
|
| 40 |
-
show_api=False, # Don't show this in API docs
|
| 41 |
)
|
| 42 |
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
],
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
return demo
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
if __name__ == "__main__":
|
| 59 |
app = create_ui()
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
| 61 |
app.launch(
|
| 62 |
-
show_api=False,
|
| 63 |
-
enable_monitoring=False,
|
| 64 |
-
quiet=True,
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 5 |
|
| 6 |
def create_ui():
|
| 7 |
+
"""Create the main application UI with Hugging Face OAuth authentication"""
|
| 8 |
with gr.Blocks(title=f"Tencent HunyuanImage-3.0 Demo", theme=APPLE_TENCENT_THEME) as demo:
|
| 9 |
+
# State to track authentication
|
| 10 |
+
user_profile_state = gr.State(value=None)
|
| 11 |
+
|
| 12 |
+
# Header with login button
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
with gr.Row():
|
| 14 |
+
with gr.Column(scale=3):
|
| 15 |
+
gr.HTML(
|
| 16 |
+
f"<div style='text-align: center; max-width: 700px; margin: 0 auto;'>"
|
| 17 |
+
f"<h1>Tencent {MODEL_ID.split('/')[-1]}</h1>"
|
| 18 |
+
f"<p>Generate images using Tencent's state-of-the-art model hosted by FAL AI.</p>"
|
| 19 |
+
f"Built with <a href='https://huggingface.co/spaces/akhaliq/anycoder' target='_blank'>anycoder</a>"
|
| 20 |
+
f"</div>"
|
| 21 |
)
|
|
|
|
|
|
|
| 22 |
with gr.Column(scale=1):
|
| 23 |
+
# Login button - shows "Sign in with Hugging Face" when logged out, "Logout (username)" when logged in
|
| 24 |
+
login_button = gr.LoginButton(
|
| 25 |
+
value="Sign in with Hugging Face",
|
| 26 |
+
logout_value="Logout ({})",
|
| 27 |
+
size="lg",
|
| 28 |
+
variant="huggingface"
|
| 29 |
)
|
| 30 |
+
|
| 31 |
+
# User info display
|
| 32 |
+
user_info = gr.Markdown("", visible=False)
|
| 33 |
+
|
| 34 |
+
# Main interface container - hidden until authenticated
|
| 35 |
+
with gr.Group(visible=False) as main_interface:
|
| 36 |
+
with gr.Row():
|
| 37 |
+
with gr.Column(scale=1):
|
| 38 |
+
prompt_input = gr.Textbox(
|
| 39 |
+
label="Prompt",
|
| 40 |
+
placeholder="e.g., A detailed watercolor painting of a small red fox sleeping on a pile of autumn leaves.",
|
| 41 |
+
lines=4
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
with gr.Row():
|
| 45 |
+
generate_btn = gr.Button("π¨ Generate Image", variant="primary", scale=2)
|
| 46 |
+
clear_btn = gr.Button("ποΈ Clear", variant="secondary", scale=1)
|
| 47 |
+
|
| 48 |
+
# Additional settings in an accordion
|
| 49 |
+
with gr.Accordion("βοΈ Advanced Settings", open=False):
|
| 50 |
+
gr.Markdown("""
|
| 51 |
+
Advanced settings are not implemented in this demo.
|
| 52 |
+
You can add parameters like:
|
| 53 |
+
- Image size
|
| 54 |
+
- Number of steps
|
| 55 |
+
- Guidance scale
|
| 56 |
+
- Seed
|
| 57 |
+
""")
|
| 58 |
+
|
| 59 |
+
with gr.Column(scale=1):
|
| 60 |
+
output_image = gr.Image(
|
| 61 |
+
label="Generated Image",
|
| 62 |
+
height=512,
|
| 63 |
+
width=512,
|
| 64 |
+
interactive=False,
|
| 65 |
+
show_download_button=True
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
# Generation status
|
| 69 |
+
status_text = gr.Markdown("Ready to generate...", visible=True)
|
| 70 |
|
| 71 |
+
# Example prompts
|
| 72 |
+
gr.Examples(
|
| 73 |
+
examples=[
|
| 74 |
+
"A detailed watercolor painting of a small red fox sleeping on a pile of autumn leaves.",
|
| 75 |
+
"A futuristic cityscape at sunset with flying cars and neon lights",
|
| 76 |
+
"A serene Japanese garden with cherry blossoms and a koi pond",
|
| 77 |
+
"A steampunk mechanical owl perched on an ancient tree",
|
| 78 |
+
"An astronaut playing guitar on the moon with Earth in the background"
|
| 79 |
+
],
|
| 80 |
+
inputs=prompt_input,
|
| 81 |
+
outputs=[output_image, status_text],
|
| 82 |
+
fn=lambda prompt: generate_with_status(prompt, user_profile_state.value),
|
| 83 |
+
cache_examples=False,
|
| 84 |
+
show_api=False,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
# Welcome message for non-authenticated users
|
| 88 |
+
welcome_message = gr.HTML(
|
| 89 |
+
"""
|
| 90 |
+
<div style='text-align: center; max-width: 600px; margin: 50px auto; padding: 30px;
|
| 91 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 92 |
+
border-radius: 20px; box-shadow: 0 10px 30px rgba(0,0,0,0.2);'>
|
| 93 |
+
<h2 style='color: white; margin-bottom: 20px;'>π¨ Welcome to HunyuanImage-3.0</h2>
|
| 94 |
+
<p style='color: rgba(255,255,255,0.9); font-size: 16px; line-height: 1.6;'>
|
| 95 |
+
Sign in with your Hugging Face account to start generating amazing images
|
| 96 |
+
with Tencent's state-of-the-art AI model.
|
| 97 |
+
</p>
|
| 98 |
+
<div style='margin-top: 30px; padding: 20px; background: rgba(255,255,255,0.1);
|
| 99 |
+
border-radius: 10px;'>
|
| 100 |
+
<p style='color: white; margin: 0;'>
|
| 101 |
+
<strong>β¨ Features:</strong><br>
|
| 102 |
+
β’ High-quality image generation<br>
|
| 103 |
+
β’ Easy-to-use interface<br>
|
| 104 |
+
β’ Multiple example prompts<br>
|
| 105 |
+
β’ Download generated images
|
| 106 |
+
</p>
|
| 107 |
+
</div>
|
| 108 |
+
<p style='color: rgba(255,255,255,0.8); font-size: 14px; margin-top: 20px;'>
|
| 109 |
+
Click the "Sign in with Hugging Face" button above to get started
|
| 110 |
+
</p>
|
| 111 |
+
</div>
|
| 112 |
+
""",
|
| 113 |
+
visible=True
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
# Authentication handling functions
|
| 117 |
+
def update_ui_on_auth(profile: gr.OAuthProfile | None) -> tuple:
|
| 118 |
+
"""Update UI based on authentication status"""
|
| 119 |
+
if profile is None:
|
| 120 |
+
# User is not authenticated
|
| 121 |
+
return (
|
| 122 |
+
gr.update(visible=False), # main_interface
|
| 123 |
+
gr.update(visible=True), # welcome_message
|
| 124 |
+
"", # user_info
|
| 125 |
+
gr.update(visible=False), # user_info visibility
|
| 126 |
+
None # user_profile_state
|
| 127 |
+
)
|
| 128 |
+
else:
|
| 129 |
+
# User is authenticated
|
| 130 |
+
user_text = f"### π€ Welcome, {profile.name}!\n\n"
|
| 131 |
+
if profile.username:
|
| 132 |
+
user_text += f"**Username:** @{profile.username}\n"
|
| 133 |
+
if hasattr(profile, 'email') and profile.email:
|
| 134 |
+
user_text += f"**Email:** {profile.email}\n"
|
| 135 |
+
|
| 136 |
+
return (
|
| 137 |
+
gr.update(visible=True), # main_interface
|
| 138 |
+
gr.update(visible=False), # welcome_message
|
| 139 |
+
user_text, # user_info
|
| 140 |
+
gr.update(visible=True), # user_info visibility
|
| 141 |
+
profile # user_profile_state
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
def generate_with_status(prompt: str, profile: gr.OAuthProfile | None) -> tuple:
|
| 145 |
+
"""Generate image with status updates and user verification"""
|
| 146 |
+
if profile is None:
|
| 147 |
+
return None, "β οΈ Please sign in to generate images!"
|
| 148 |
+
|
| 149 |
+
if not prompt:
|
| 150 |
+
return None, "β οΈ Please enter a prompt!"
|
| 151 |
+
|
| 152 |
+
try:
|
| 153 |
+
# You can log user activity here if needed
|
| 154 |
+
print(f"User {profile.username} is generating an image with prompt: {prompt[:50]}...")
|
| 155 |
+
|
| 156 |
+
# Generate the image
|
| 157 |
+
image = generate_image(prompt)
|
| 158 |
+
|
| 159 |
+
return image, "β
Image generated successfully!"
|
| 160 |
+
|
| 161 |
+
except Exception as e:
|
| 162 |
+
return None, f"β Error: {str(e)}"
|
| 163 |
+
|
| 164 |
+
def clear_interface():
|
| 165 |
+
"""Clear the interface"""
|
| 166 |
+
return "", None, "Ready to generate..."
|
| 167 |
+
|
| 168 |
+
# Load event to check authentication status
|
| 169 |
+
demo.load(
|
| 170 |
+
fn=update_ui_on_auth,
|
| 171 |
+
inputs=None,
|
| 172 |
+
outputs=[main_interface, welcome_message, user_info, user_info, user_profile_state],
|
| 173 |
+
queue=False,
|
| 174 |
+
show_api=False,
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# Login button click event (also handles logout)
|
| 178 |
+
login_button.click(
|
| 179 |
+
fn=update_ui_on_auth,
|
| 180 |
+
inputs=None,
|
| 181 |
+
outputs=[main_interface, welcome_message, user_info, user_info, user_profile_state],
|
| 182 |
+
queue=False,
|
| 183 |
+
show_api=False,
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
# Generate button event
|
| 187 |
generate_btn.click(
|
| 188 |
+
fn=lambda prompt, profile: generate_with_status(prompt, profile),
|
| 189 |
+
inputs=[prompt_input, user_profile_state],
|
| 190 |
+
outputs=[output_image, status_text],
|
| 191 |
+
queue=False,
|
| 192 |
+
show_api=False,
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
+
# Clear button event
|
| 196 |
+
clear_btn.click(
|
| 197 |
+
fn=clear_interface,
|
| 198 |
+
inputs=[],
|
| 199 |
+
outputs=[prompt_input, output_image, status_text],
|
| 200 |
+
queue=False,
|
| 201 |
+
show_api=False,
|
| 202 |
+
)
|
| 203 |
+
|
| 204 |
+
# Footer
|
| 205 |
+
gr.HTML(
|
| 206 |
+
"""
|
| 207 |
+
<div style='text-align: center; margin-top: 40px; padding: 20px; border-top: 1px solid #e0e0e0;'>
|
| 208 |
+
<p style='color: #666; font-size: 14px;'>
|
| 209 |
+
This demo uses Hugging Face OAuth for secure authentication.
|
| 210 |
+
Your session will remain active until you logout.
|
| 211 |
+
<br>For the best results, provide detailed prompts with specific descriptions.
|
| 212 |
+
</p>
|
| 213 |
+
</div>
|
| 214 |
+
"""
|
| 215 |
)
|
| 216 |
|
| 217 |
return demo
|
| 218 |
|
| 219 |
+
# Additional helper function for logging/tracking (optional)
|
| 220 |
+
def log_user_activity(profile: gr.OAuthProfile, action: str, details: str = ""):
|
| 221 |
+
"""Log user activity for analytics or monitoring"""
|
| 222 |
+
if profile:
|
| 223 |
+
print(f"[{action}] User: {profile.username}, Name: {profile.name}, Details: {details}")
|
| 224 |
+
|
| 225 |
if __name__ == "__main__":
|
| 226 |
app = create_ui()
|
| 227 |
+
|
| 228 |
+
# Launch the app
|
| 229 |
+
# Note: OAuth features work best when deployed on Hugging Face Spaces
|
| 230 |
+
# For local development, it will use the local HF account (via hf auth login)
|
| 231 |
app.launch(
|
| 232 |
+
show_api=False,
|
| 233 |
+
enable_monitoring=False,
|
| 234 |
+
quiet=True,
|
| 235 |
+
share=False, # Set to True if you want to create a public link
|
| 236 |
+
server_name="0.0.0.0", # Change to "0.0.0.0" to allow external access
|
| 237 |
+
server_port=7860, # Default Gradio port
|
| 238 |
+
)
|
| 239 |
+
|
| 240 |
+
# Note: When deploying to Hugging Face Spaces, you can configure OAuth permissions
|
| 241 |
+
# in the README file as described here: https://huggingface.co/docs/hub/en/spaces-oauth
|