namelessai commited on
Commit
58a4da0
·
verified ·
1 Parent(s): 9385b77

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import base64
3
+ import io
4
+ import requests
5
+ from PIL import Image
6
+ import gradio as gr
7
+
8
+ # ------------------------------------------------------------------------------
9
+ # Make sure you export your API token before running this script:
10
+ # export CHUTES_API_TOKEN="your_real_token_here"
11
+ # ------------------------------------------------------------------------------
12
+
13
+ API_TOKEN = os.getenv("CHUTES_API_TOKEN")
14
+ if API_TOKEN is None:
15
+ raise ValueError("Please set the environment variable CHUTES_API_TOKEN before running.")
16
+
17
+ def generate_image(prompt: str,
18
+ resolution: str,
19
+ guidance_scale: float,
20
+ num_inference_steps: int,
21
+ shift: int) -> Image.Image:
22
+ """
23
+ Calls the HiDream (Chutes) API to generate an image from the given prompt
24
+ and parameters. Returns a PIL.Image.
25
+ """
26
+ headers = {
27
+ "Authorization": f"Bearer {API_TOKEN}",
28
+ "Content-Type": "application/json"
29
+ }
30
+ payload = {
31
+ "seed": None, # always random
32
+ "shift": shift,
33
+ "prompt": prompt,
34
+ "resolution": resolution,
35
+ "guidance_scale": guidance_scale,
36
+ "num_inference_steps": num_inference_steps
37
+ }
38
+
39
+ response = requests.post(
40
+ "https://chutes-hidream.chutes.ai/generate",
41
+ headers=headers,
42
+ json=payload
43
+ )
44
+
45
+ if response.status_code != 200:
46
+ # you can customize error handling as needed
47
+ raise RuntimeError(f"API returned {response.status_code}: {response.text}")
48
+
49
+ data = response.json()
50
+ # Typically, the API returns a base64‐encoded image under the key "image".
51
+ # If yours returns a URL (e.g. under "url"), you can fetch that instead.
52
+ b64_img = data.get("image")
53
+ if b64_img:
54
+ decoded = base64.b64decode(b64_img)
55
+ img = Image.open(io.BytesIO(decoded)).convert("RGB")
56
+ return img
57
+
58
+ # Fallback: if the API returns a direct URL
59
+ img_url = data.get("url") or data.get("image_url")
60
+ if img_url:
61
+ img_response = requests.get(img_url)
62
+ img = Image.open(io.BytesIO(img_response.content)).convert("RGB")
63
+ return img
64
+
65
+ raise RuntimeError("No image field found in API response.")
66
+
67
+
68
+ # Build the Gradio interface
69
+ with gr.Blocks(title="HiDream Unlimited") as demo:
70
+ gr.Markdown("## HiDream Unlimited\nGenerate unlimited AI‐driven images powered by HiDream/Chutes.\n\nEnter your prompt and tweak the parameters, then click **Generate**.")
71
+
72
+ with gr.Row():
73
+ with gr.Column(scale=1):
74
+ prompt_in = gr.Textbox(
75
+ label="Prompt",
76
+ placeholder="e.g. a serene sunset over a coral reef, digital painting",
77
+ lines=2
78
+ )
79
+ resolution_in = gr.Dropdown(
80
+ choices=["512x512", "768x768", "1024x1024"],
81
+ value="1024x1024",
82
+ label="Resolution"
83
+ )
84
+ guidance_in = gr.Slider(
85
+ minimum=0.0, maximum=20.0, step=0.5,
86
+ value=5.0, label="Guidance Scale"
87
+ )
88
+ steps_in = gr.Slider(
89
+ minimum=1, maximum=100, step=1,
90
+ value=50, label="Num Inference Steps"
91
+ )
92
+ shift_in = gr.Slider(
93
+ minimum=0, maximum=10, step=1,
94
+ value=3, label="Shift"
95
+ )
96
+ generate_btn = gr.Button("Generate Image", variant="primary")
97
+
98
+ with gr.Column(scale=1):
99
+ output_img = gr.Image(label="Generated Image", interactive=False)
100
+
101
+ # Wire up the button
102
+ generate_btn.click(
103
+ fn=generate_image,
104
+ inputs=[prompt_in, resolution_in, guidance_in, steps_in, shift_in],
105
+ outputs=output_img
106
+ )
107
+
108
+ if __name__ == "__main__":
109
+ demo.launch()