sanjay1222 commited on
Commit
a5a5f17
·
verified ·
1 Parent(s): d383737

Upload 5 files

Browse files
Files changed (5) hide show
  1. README.md +15 -12
  2. app.py +80 -0
  3. config.py +21 -0
  4. models.py +60 -0
  5. requirements.txt +4 -0
README.md CHANGED
@@ -1,12 +1,15 @@
1
- ---
2
- title: Image Generator
3
- emoji:
4
- colorFrom: gray
5
- colorTo: red
6
- sdk: gradio
7
- sdk_version: 5.49.0
8
- app_file: app.py
9
- pinned: false
10
- ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
1
+ ---
2
+ title: HunyuanImage-3.0
3
+ emoji: 📊
4
+ colorFrom: purple
5
+ colorTo: yellow
6
+ sdk: gradio
7
+ sdk_version: 5.47.2
8
+ app_file: app.py
9
+ pinned: false
10
+ tags:
11
+ - anycoder
12
+ hf_oauth: true
13
+ ---
14
+
15
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from models import generate_image, MODEL_ID
3
+ from config import APPLE_TENCENT_THEME
4
+
5
+ def generate_image_with_auth(prompt: str, profile: gr.OAuthProfile | None):
6
+ """
7
+ Wrapper function that checks if user is logged in before generating image.
8
+ """
9
+ if profile is None:
10
+ raise gr.Error("Click Sign in with Hugging Face button to use this app for free")
11
+
12
+ # User is logged in, proceed with image generation
13
+ return generate_image(prompt)
14
+
15
+ def create_ui():
16
+ with gr.Blocks(title=f"Tencent HunyuanImage-3.0 Demo", theme=APPLE_TENCENT_THEME) as demo:
17
+ gr.HTML(
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;'>⚠️ You must Sign in with Hugging Face using the button to use this app.</p>"
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 gr.Row():
30
+ with gr.Column(scale=1):
31
+ prompt_input = gr.Textbox(
32
+ label="Prompt",
33
+ placeholder="e.g., A detailed watercolor painting of a small red fox sleeping on a pile of autumn leaves.",
34
+ lines=4
35
+ )
36
+ generate_btn = gr.Button("🎨 Generate Image", variant="primary")
37
+
38
+ with gr.Column(scale=1):
39
+ output_image = gr.Image(
40
+ label="Generated Image",
41
+ height=512,
42
+ width=512,
43
+ interactive=False,
44
+ show_download_button=True
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
+ # Example usage guidance with queue features disabled
58
+ gr.Examples(
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
+
72
+ if __name__ == "__main__":
73
+ app = create_ui()
74
+ # Launch without special auth parameters
75
+ # OAuth is enabled via Space metadata (hf_oauth: true in README.md)
76
+ app.launch(
77
+ show_api=False,
78
+ enable_monitoring=False,
79
+ quiet=True,
80
+ )
config.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ # Define a custom theme based on Soft theme for a modern/Apple aesthetic
4
+ # Using indigo for a deep tech blue (Tencent blue approximation)
5
+ APPLE_TENCENT_THEME = gr.themes.Soft(
6
+ primary_hue=gr.themes.colors.indigo, # FIX: Corrected capitalization to lowercase 'indigo'
7
+ secondary_hue=gr.themes.colors.gray,
8
+ neutral_hue=gr.themes.colors.neutral,
9
+ spacing_size=gr.themes.sizes.spacing_lg,
10
+ radius_size=gr.themes.sizes.radius_lg,
11
+ text_size=gr.themes.sizes.text_md,
12
+ ).set(
13
+ # Custom tweaks for a cleaner, high-contrast look (Apple/Modern aesthetic)
14
+ body_background_fill="#F9F9F9",
15
+ background_fill_primary="#FFFFFF",
16
+ background_fill_secondary="#F0F0F0",
17
+ shadow_drop="0 1px 3px 0 rgba(0, 0, 0, 0.05), 0 1px 2px 0 rgba(0, 0, 0, 0.02)",
18
+ shadow_drop_lg="0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05)",
19
+ button_primary_background_fill="*primary_500",
20
+ button_primary_background_fill_hover="*primary_600",
21
+ )
models.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from huggingface_hub import InferenceClient
3
+ from PIL import Image
4
+ import gradio as gr
5
+ from typing import Union
6
+
7
+ # Load environment variables (useful for local testing)
8
+ from dotenv import load_dotenv
9
+ load_dotenv()
10
+
11
+ # --- Model Configuration ---
12
+ MODEL_ID = "tencent/HunyuanImage-3.0"
13
+ PROVIDER = "fal-ai"
14
+ BILL_TO = "huggingface"
15
+
16
+ # Initialize client
17
+ HF_TOKEN = os.environ.get("HF_TOKEN")
18
+ CLIENT: Union[InferenceClient, None] = None
19
+
20
+ if HF_TOKEN:
21
+ try:
22
+ # Note: FAL AI provider uses HF_TOKEN as its api_key
23
+ CLIENT = InferenceClient(
24
+ provider=PROVIDER,
25
+ api_key=HF_TOKEN,
26
+ bill_to=BILL_TO,
27
+ )
28
+ print(f"✅ InferenceClient initialized for {MODEL_ID} via {PROVIDER}")
29
+ except Exception as e:
30
+ print(f"❌ Error initializing InferenceClient: {e}")
31
+ CLIENT = None
32
+ else:
33
+ print("⚠️ HF_TOKEN environment variable not set. Client will be unavailable.")
34
+
35
+ def generate_image(prompt: str) -> Image.Image:
36
+ """
37
+ Generates an image from a text prompt using the Hugging Face Inference Client.
38
+ """
39
+ if not CLIENT:
40
+ raise gr.Error("API client not available. Please ensure HF_TOKEN is set correctly.")
41
+
42
+ if not prompt:
43
+ raise gr.Error("Please provide a prompt.")
44
+
45
+ print(f"Generating image for prompt: '{prompt[:50]}...'")
46
+
47
+ try:
48
+ # The output is a PIL.Image object directly
49
+ image = CLIENT.text_to_image(
50
+ prompt,
51
+ model=MODEL_ID,
52
+ )
53
+ return image
54
+ except Exception as e:
55
+ print(f"Error during image generation: {e}")
56
+ if "Authentication failed" in str(e):
57
+ raise gr.Error("Authentication failed. Check your HF_TOKEN.")
58
+ if "limit reached" in str(e) or "quota" in str(e):
59
+ raise gr.Error("Rate limit or quota reached for this API endpoint.")
60
+ raise gr.Error(f"Generation failed: {str(e)}")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ huggingface-hub
3
+ Pillow
4
+ python-dotenv