Spaces:
Running
Running
File size: 5,697 Bytes
61e31a5 4af9c3b 44feade 4af9c3b 44feade 4af9c3b 44feade 4af9c3b 44feade 4af9c3b 44feade 4af9c3b 44feade 4af9c3b 44feade 4af9c3b 44feade 4af9c3b 44feade 4af9c3b 44feade 61e31a5 44feade 4af9c3b 61e31a5 44feade 61e31a5 44feade 61e31a5 4af9c3b 44feade 4af9c3b 44feade 4af9c3b 44feade 4af9c3b 44feade 61e31a5 44feade 4af9c3b 61e31a5 44feade 4af9c3b 44feade 61e31a5 44feade 61e31a5 44feade 61e31a5 44feade 61e31a5 44feade 61e31a5 44feade 4af9c3b 44feade 61e31a5 4af9c3b 61e31a5 4af9c3b |
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
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() |