Spaces:
Running
Running
Update app.py: Add HF Inference API integration with n8n support
Browse files
app.py
CHANGED
|
@@ -1,47 +1,83 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from transformers import AutoModelForCausalLM
|
| 4 |
import os
|
| 5 |
-
import
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
print("Note: This is a very large model (80B params) and requires significant GPU memory.")
|
| 12 |
-
print("For production use, consider using the FAL API or other inference providers.")
|
| 13 |
|
| 14 |
-
|
| 15 |
-
@spaces.GPU
|
| 16 |
-
def generate_image(prompt, seed=42, diff_infer_steps=50, image_size="auto"):
|
| 17 |
"""
|
| 18 |
-
Generate image using
|
| 19 |
-
|
| 20 |
-
For Spaces, consider using Inference API or providers like FAL.
|
| 21 |
"""
|
| 22 |
try:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
img = Image.new('RGB', (1024, 1024), color=(240, 240, 245))
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
def infer(prompt, seed, randomize_seed, diff_infer_steps, image_size):
|
| 37 |
import random
|
| 38 |
if randomize_seed:
|
| 39 |
seed = random.randint(0, 2**32 - 1)
|
| 40 |
|
| 41 |
-
image, used_seed =
|
| 42 |
-
return image, used_seed
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
# Gradio Interface
|
| 45 |
examples = [
|
| 46 |
"A brown and white dog is running on the grass",
|
| 47 |
"A futuristic city at sunset with flying cars",
|
|
@@ -63,17 +99,16 @@ css = """
|
|
| 63 |
|
| 64 |
with gr.Blocks(css=css) as demo:
|
| 65 |
with gr.Column(elem_id="col-container"):
|
| 66 |
-
gr.Markdown("# π¨ HunyuanImage-3.0 Text-to-Image")
|
| 67 |
gr.Markdown(
|
| 68 |
-
"""### Tencent HunyuanImage-3.0 -
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
3. Deploy on appropriate hardware
|
| 75 |
|
| 76 |
-
|
| 77 |
""",
|
| 78 |
elem_classes="note"
|
| 79 |
)
|
|
@@ -84,11 +119,13 @@ with gr.Blocks(css=css) as demo:
|
|
| 84 |
show_label=True,
|
| 85 |
max_lines=3,
|
| 86 |
placeholder="Enter your prompt for image generation...",
|
|
|
|
| 87 |
)
|
| 88 |
|
| 89 |
run_button = gr.Button("π¨ Generate Image", variant="primary")
|
| 90 |
|
| 91 |
result = gr.Image(label="Generated Image", show_label=True)
|
|
|
|
| 92 |
|
| 93 |
with gr.Accordion("Advanced Settings", open=False):
|
| 94 |
seed = gr.Slider(
|
|
@@ -120,8 +157,26 @@ with gr.Blocks(css=css) as demo:
|
|
| 120 |
run_button.click(
|
| 121 |
fn=infer,
|
| 122 |
inputs=[prompt, seed, randomize_seed, diff_infer_steps, image_size],
|
| 123 |
-
outputs=[result, seed],
|
| 124 |
)
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
if __name__ == "__main__":
|
| 127 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
|
|
|
| 3 |
import os
|
| 4 |
+
import base64
|
| 5 |
+
from io import BytesIO
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import json
|
| 8 |
|
| 9 |
+
# Hugging Face API configuration
|
| 10 |
+
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 11 |
+
API_URL = "https://api-inference.huggingface.co/models/tencent/HunyuanImage-3.0"
|
| 12 |
|
| 13 |
+
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
def generate_image_api(prompt, seed=42, num_inference_steps=50):
|
|
|
|
|
|
|
| 16 |
"""
|
| 17 |
+
Generate image using Hugging Face Inference API
|
| 18 |
+
Uses paid API from your HF account balance
|
|
|
|
| 19 |
"""
|
| 20 |
try:
|
| 21 |
+
payload = {
|
| 22 |
+
"inputs": prompt,
|
| 23 |
+
"parameters": {
|
| 24 |
+
"seed": int(seed),
|
| 25 |
+
"num_inference_steps": int(num_inference_steps)
|
| 26 |
+
}
|
| 27 |
+
}
|
| 28 |
|
| 29 |
+
response = requests.post(API_URL, headers=headers, json=payload, timeout=60)
|
|
|
|
| 30 |
|
| 31 |
+
if response.status_code == 200:
|
| 32 |
+
image = Image.open(BytesIO(response.content))
|
| 33 |
+
return image, seed, "Success!"
|
| 34 |
+
else:
|
| 35 |
+
error_msg = f"API Error: {response.status_code} - {response.text}"
|
| 36 |
+
print(error_msg)
|
| 37 |
+
placeholder = Image.new('RGB', (1024, 1024), color=(240, 240, 245))
|
| 38 |
+
return placeholder, seed, error_msg
|
| 39 |
+
|
| 40 |
except Exception as e:
|
| 41 |
+
error_msg = f"Error: {str(e)}"
|
| 42 |
+
print(error_msg)
|
| 43 |
+
placeholder = Image.new('RGB', (1024, 1024), color=(240, 240, 245))
|
| 44 |
+
return placeholder, seed, error_msg
|
| 45 |
|
| 46 |
def infer(prompt, seed, randomize_seed, diff_infer_steps, image_size):
|
| 47 |
import random
|
| 48 |
if randomize_seed:
|
| 49 |
seed = random.randint(0, 2**32 - 1)
|
| 50 |
|
| 51 |
+
image, used_seed, status = generate_image_api(prompt, seed, diff_infer_steps)
|
| 52 |
+
return image, used_seed, status
|
| 53 |
+
|
| 54 |
+
def api_generate(prompt: str, seed: int = 42, num_inference_steps: int = 50):
|
| 55 |
+
"""
|
| 56 |
+
API endpoint for external integrations like n8n
|
| 57 |
+
Returns base64 encoded image
|
| 58 |
+
"""
|
| 59 |
+
try:
|
| 60 |
+
image, used_seed, status = generate_image_api(prompt, seed, num_inference_steps)
|
| 61 |
+
|
| 62 |
+
buffered = BytesIO()
|
| 63 |
+
image.save(buffered, format="PNG")
|
| 64 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 65 |
+
|
| 66 |
+
return {
|
| 67 |
+
"success": True,
|
| 68 |
+
"image_base64": img_str,
|
| 69 |
+
"seed": used_seed,
|
| 70 |
+
"status": status,
|
| 71 |
+
"prompt": prompt
|
| 72 |
+
}
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return {
|
| 75 |
+
"success": False,
|
| 76 |
+
"error": str(e),
|
| 77 |
+
"seed": seed,
|
| 78 |
+
"prompt": prompt
|
| 79 |
+
}
|
| 80 |
|
|
|
|
| 81 |
examples = [
|
| 82 |
"A brown and white dog is running on the grass",
|
| 83 |
"A futuristic city at sunset with flying cars",
|
|
|
|
| 99 |
|
| 100 |
with gr.Blocks(css=css) as demo:
|
| 101 |
with gr.Column(elem_id="col-container"):
|
| 102 |
+
gr.Markdown("# π¨ HunyuanImage-3.0 Text-to-Image with Inference API")
|
| 103 |
gr.Markdown(
|
| 104 |
+
"""### Tencent HunyuanImage-3.0 - Using Paid Hugging Face Inference API
|
| 105 |
|
| 106 |
+
β
This Space now uses the Hugging Face Inference API (paid from your account balance)
|
| 107 |
+
- Real image generation with HunyuanImage-3.0
|
| 108 |
+
- API endpoint available for n8n integration
|
| 109 |
+
- Set your HF_TOKEN in Space secrets
|
|
|
|
| 110 |
|
| 111 |
+
π For n8n integration: Use the API endpoint at /gradio_api/ with the api_generate function
|
| 112 |
""",
|
| 113 |
elem_classes="note"
|
| 114 |
)
|
|
|
|
| 119 |
show_label=True,
|
| 120 |
max_lines=3,
|
| 121 |
placeholder="Enter your prompt for image generation...",
|
| 122 |
+
value="A serene mountain landscape with a crystal clear lake"
|
| 123 |
)
|
| 124 |
|
| 125 |
run_button = gr.Button("π¨ Generate Image", variant="primary")
|
| 126 |
|
| 127 |
result = gr.Image(label="Generated Image", show_label=True)
|
| 128 |
+
status_text = gr.Textbox(label="Status", interactive=False)
|
| 129 |
|
| 130 |
with gr.Accordion("Advanced Settings", open=False):
|
| 131 |
seed = gr.Slider(
|
|
|
|
| 157 |
run_button.click(
|
| 158 |
fn=infer,
|
| 159 |
inputs=[prompt, seed, randomize_seed, diff_infer_steps, image_size],
|
| 160 |
+
outputs=[result, seed, status_text],
|
| 161 |
)
|
| 162 |
|
| 163 |
+
api_demo = gr.Interface(
|
| 164 |
+
fn=api_generate,
|
| 165 |
+
inputs=[
|
| 166 |
+
gr.Text(label="Prompt"),
|
| 167 |
+
gr.Number(label="Seed", value=42),
|
| 168 |
+
gr.Number(label="Inference Steps", value=50)
|
| 169 |
+
],
|
| 170 |
+
outputs=gr.JSON(label="Response"),
|
| 171 |
+
title="HunyuanImage-3.0 API Endpoint",
|
| 172 |
+
description="API endpoint for n8n and other integrations. Returns base64 encoded image."
|
| 173 |
+
)
|
| 174 |
+
|
| 175 |
+
app = gr.TabbedInterface(
|
| 176 |
+
[demo, api_demo],
|
| 177 |
+
["Interface", "API Endpoint"],
|
| 178 |
+
title="HunyuanImage-3.0 Generator"
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
if __name__ == "__main__":
|
| 182 |
+
app.launch()
|