Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,4 @@
|
|
1 |
import os
|
2 |
-
import base64
|
3 |
import io
|
4 |
import requests
|
5 |
from PIL import Image
|
@@ -20,15 +19,15 @@ def generate_image(prompt: str,
|
|
20 |
shift: int) -> Image.Image:
|
21 |
"""
|
22 |
Calls the HiDream (Chutes) API to generate an image from the given prompt
|
23 |
-
and parameters.
|
24 |
-
|
25 |
"""
|
26 |
headers = {
|
27 |
"Authorization": f"Bearer {API_TOKEN}",
|
28 |
"Content-Type": "application/json"
|
29 |
}
|
30 |
payload = {
|
31 |
-
"seed": None,
|
32 |
"shift": shift,
|
33 |
"prompt": prompt,
|
34 |
"resolution": resolution,
|
@@ -44,41 +43,27 @@ def generate_image(prompt: str,
|
|
44 |
|
45 |
# If the status code isn’t 200, raise immediately
|
46 |
if response.status_code != 200:
|
47 |
-
|
|
|
|
|
48 |
|
49 |
-
#
|
50 |
try:
|
51 |
-
|
52 |
-
except Exception as e:
|
53 |
-
raise RuntimeError(f"Failed to parse JSON (status {response.status_code}):\n{response.text}") from e
|
54 |
-
|
55 |
-
# The API may return base64 under "image", or a URL under "url"/"image_url"
|
56 |
-
b64_img = data.get("image")
|
57 |
-
if b64_img:
|
58 |
-
try:
|
59 |
-
decoded = base64.b64decode(b64_img)
|
60 |
-
img = Image.open(io.BytesIO(decoded)).convert("RGB")
|
61 |
-
return img
|
62 |
-
except Exception as e:
|
63 |
-
raise RuntimeError(f"Error decoding base64 image:\n{str(e)}")
|
64 |
-
|
65 |
-
img_url = data.get("url") or data.get("image_url")
|
66 |
-
if img_url:
|
67 |
-
img_response = requests.get(img_url)
|
68 |
-
if img_response.status_code != 200:
|
69 |
-
raise RuntimeError(f"Failed to fetch image URL ({img_url}): HTTP {img_response.status_code}")
|
70 |
-
img = Image.open(io.BytesIO(img_response.content)).convert("RGB")
|
71 |
return img
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
75 |
|
76 |
|
77 |
# Build the Gradio interface
|
78 |
with gr.Blocks(title="HiDream Unlimited") as demo:
|
79 |
gr.Markdown(
|
80 |
"## HiDream Unlimited\n\n"
|
81 |
-
"Generate unlimited AI
|
82 |
"Enter your prompt and tweak the parameters, then click **Generate**."
|
83 |
)
|
84 |
|
|
|
1 |
import os
|
|
|
2 |
import io
|
3 |
import requests
|
4 |
from PIL import Image
|
|
|
19 |
shift: int) -> Image.Image:
|
20 |
"""
|
21 |
Calls the HiDream (Chutes) API to generate an image from the given prompt
|
22 |
+
and parameters. Assumes the API responds with raw image bytes (e.g. PNG or JPEG)
|
23 |
+
rather than JSON. Returns a PIL.Image on success. Raises a RuntimeError on failure.
|
24 |
"""
|
25 |
headers = {
|
26 |
"Authorization": f"Bearer {API_TOKEN}",
|
27 |
"Content-Type": "application/json"
|
28 |
}
|
29 |
payload = {
|
30 |
+
"seed": None,
|
31 |
"shift": shift,
|
32 |
"prompt": prompt,
|
33 |
"resolution": resolution,
|
|
|
43 |
|
44 |
# If the status code isn’t 200, raise immediately
|
45 |
if response.status_code != 200:
|
46 |
+
# Try to show any text body if present
|
47 |
+
raw_text = response.text.strip()
|
48 |
+
raise RuntimeError(f"API returned HTTP {response.status_code}:\n{raw_text}")
|
49 |
|
50 |
+
# At this point we assume response.content is raw image bytes.
|
51 |
try:
|
52 |
+
img = Image.open(io.BytesIO(response.content)).convert("RGB")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
return img
|
54 |
+
except Exception as e:
|
55 |
+
# If decoding as an image fails, include a hex preview of the first few bytes
|
56 |
+
prefix = response.content[:16].hex()
|
57 |
+
raise RuntimeError(
|
58 |
+
f"Failed to decode image from response content (first 16 bytes: {prefix}...):\n{str(e)}"
|
59 |
+
)
|
60 |
|
61 |
|
62 |
# Build the Gradio interface
|
63 |
with gr.Blocks(title="HiDream Unlimited") as demo:
|
64 |
gr.Markdown(
|
65 |
"## HiDream Unlimited\n\n"
|
66 |
+
"Generate unlimited AI-driven images powered by HiDream.\n\n"
|
67 |
"Enter your prompt and tweak the parameters, then click **Generate**."
|
68 |
)
|
69 |
|