File size: 3,736 Bytes
58a4da0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
543063c
 
58a4da0
 
 
 
 
 
543063c
58a4da0
 
 
 
 
 
 
 
 
 
 
 
 
09e37fb
58a4da0
543063c
 
 
09e37fb
543063c
09e37fb
543063c
58a4da0
543063c
 
 
 
 
 
58a4da0
 
 
 
09e37fb
 
543063c
09e37fb
 
58a4da0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import io
import requests
from PIL import Image
import gradio as gr

# ------------------------------------------------------------------------------
# Make sure you export your API token before running this script:
#   export CHUTES_API_TOKEN="your_real_token_here"
# ------------------------------------------------------------------------------
API_TOKEN = os.getenv("CHUTES_API_TOKEN")
if API_TOKEN is None:
    raise ValueError("Please set the environment variable CHUTES_API_TOKEN before running.")

def generate_image(prompt: str,
                   resolution: str,
                   guidance_scale: float,
                   num_inference_steps: int,
                   shift: int) -> Image.Image:
    """
    Calls the HiDream (Chutes) API to generate an image from the given prompt
    and parameters. Assumes the API responds with raw image bytes (e.g. PNG or JPEG)
    rather than JSON. Returns a PIL.Image on success. Raises a RuntimeError on failure.
    """
    headers = {
        "Authorization": f"Bearer {API_TOKEN}",
        "Content-Type": "application/json"
    }
    payload = {
        "seed": None,
        "shift": shift,
        "prompt": prompt,
        "resolution": resolution,
        "guidance_scale": guidance_scale,
        "num_inference_steps": num_inference_steps
    }

    response = requests.post(
        "https://chutes-hidream.chutes.ai/generate",
        headers=headers,
        json=payload
    )

    # If the status code isn’t 200, raise immediately
    if response.status_code != 200:
        # Try to show any text body if present
        raw_text = response.text.strip()
        raise RuntimeError(f"API returned HTTP {response.status_code}:\n{raw_text}")

    # At this point we assume response.content is raw image bytes.
    try:
        img = Image.open(io.BytesIO(response.content)).convert("RGB")
        return img
    except Exception as e:
        # If decoding as an image fails, include a hex preview of the first few bytes
        prefix = response.content[:16].hex()
        raise RuntimeError(
            f"Failed to decode image from response content (first 16 bytes: {prefix}...):\n{str(e)}"
        )


# Build the Gradio interface
with gr.Blocks(title="HiDream Unlimited") as demo:
    gr.Markdown(
        "## HiDream Unlimited\n\n"
        "Generate unlimited AI-driven images powered by HiDream.\n\n"
        "Enter your prompt and tweak the parameters, then click **Generate**."
    )

    with gr.Row():
        with gr.Column(scale=1):
            prompt_in = gr.Textbox(
                label="Prompt",
                placeholder="e.g. a serene sunset over a coral reef, digital painting",
                lines=2
            )
            resolution_in = gr.Dropdown(
                choices=["512x512", "768x768", "1024x1024"],
                value="1024x1024",
                label="Resolution"
            )
            guidance_in = gr.Slider(
                minimum=0.0, maximum=20.0, step=0.5,
                value=5.0, label="Guidance Scale"
            )
            steps_in = gr.Slider(
                minimum=1, maximum=100, step=1,
                value=50, label="Num Inference Steps"
            )
            shift_in = gr.Slider(
                minimum=0, maximum=10, step=1,
                value=3, label="Shift"
            )
            generate_btn = gr.Button("Generate Image", variant="primary")

        with gr.Column(scale=1):
            output_img = gr.Image(label="Generated Image", interactive=False)

    generate_btn.click(
        fn=generate_image,
        inputs=[prompt_in, resolution_in, guidance_in, steps_in, shift_in],
        outputs=output_img
    )

if __name__ == "__main__":
    demo.launch()