import gradio as gr import requests import os import base64 from io import BytesIO from PIL import Image import json # Hugging Face API configuration HF_TOKEN = os.environ.get("HF_TOKEN", "") API_URL = "https://api-inference.huggingface.co/models/tencent/HunyuanImage-3.0" headers = {"Authorization": f"Bearer {HF_TOKEN}"} def generate_image_api(prompt, seed=42, num_inference_steps=50): """ Generate image using Hugging Face Inference API Uses paid API from your HF account balance """ try: payload = { "inputs": prompt, "parameters": { "seed": int(seed), "num_inference_steps": int(num_inference_steps) } } response = requests.post(API_URL, headers=headers, json=payload, timeout=60) if response.status_code == 200: image = Image.open(BytesIO(response.content)) return image, seed, "Success!" else: error_msg = f"API Error: {response.status_code} - {response.text}" print(error_msg) placeholder = Image.new('RGB', (1024, 1024), color=(240, 240, 245)) return placeholder, seed, error_msg except Exception as e: error_msg = f"Error: {str(e)}" print(error_msg) placeholder = Image.new('RGB', (1024, 1024), color=(240, 240, 245)) return placeholder, seed, error_msg def infer(prompt, seed, randomize_seed, diff_infer_steps, image_size): import random if randomize_seed: seed = random.randint(0, 2**32 - 1) image, used_seed, status = generate_image_api(prompt, seed, diff_infer_steps) return image, used_seed, status def api_generate(prompt: str, seed: int = 42, num_inference_steps: int = 50): """ API endpoint for external integrations like n8n Returns base64 encoded image """ try: image, used_seed, status = generate_image_api(prompt, seed, num_inference_steps) buffered = BytesIO() image.save(buffered, format="PNG") img_str = base64.b64encode(buffered.getvalue()).decode() return { "success": True, "image_base64": img_str, "seed": used_seed, "status": status, "prompt": prompt } except Exception as e: return { "success": False, "error": str(e), "seed": seed, "prompt": prompt } examples = [ "A brown and white dog is running on the grass", "A futuristic city at sunset with flying cars", "A serene mountain landscape with a crystal clear lake", ] css = """ #col-container { margin: 0 auto; max-width: 800px; } .note { background: #fff3cd; padding: 15px; border-radius: 8px; margin: 10px 0; } """ with gr.Blocks(css=css) as demo: with gr.Column(elem_id="col-container"): gr.Markdown("# 🎨 HunyuanImage-3.0 Text-to-Image with Inference API") gr.Markdown( """### Tencent HunyuanImage-3.0 - Using Paid Hugging Face Inference API ✅ This Space now uses the Hugging Face Inference API (paid from your account balance) - Real image generation with HunyuanImage-3.0 - API endpoint available for n8n integration - Set your HF_TOKEN in Space secrets 🔗 For n8n integration: Use the API endpoint at /gradio_api/ with the api_generate function """, elem_classes="note" ) with gr.Row(): prompt = gr.Text( label="Prompt", show_label=True, max_lines=3, placeholder="Enter your prompt for image generation...", value="A serene mountain landscape with a crystal clear lake" ) run_button = gr.Button("🎨 Generate Image", variant="primary") result = gr.Image(label="Generated Image", show_label=True) status_text = gr.Textbox(label="Status", interactive=False) with gr.Accordion("Advanced Settings", open=False): seed = gr.Slider( label="Seed", minimum=0, maximum=2**32 - 1, step=1, value=42, ) randomize_seed = gr.Checkbox(label="Randomize seed", value=True) diff_infer_steps = gr.Slider( label="Diffusion inference steps", minimum=10, maximum=100, step=10, value=50, ) image_size = gr.Radio( label="Image Size", choices=["auto", "1024x1024", "1280x768", "768x1280"], value="auto", ) gr.Examples(examples=examples, inputs=[prompt]) run_button.click( fn=infer, inputs=[prompt, seed, randomize_seed, diff_infer_steps, image_size], outputs=[result, seed, status_text], ) api_demo = gr.Interface( fn=api_generate, inputs=[ gr.Text(label="Prompt"), gr.Number(label="Seed", value=42), gr.Number(label="Inference Steps", value=50) ], outputs=gr.JSON(label="Response"), title="HunyuanImage-3.0 API Endpoint", description="API endpoint for n8n and other integrations. Returns base64 encoded image." ) app = gr.TabbedInterface( [demo, api_demo], ["Interface", "API Endpoint"], title="HunyuanImage-3.0 Generator" ) if __name__ == "__main__": app.launch()