Commit Β·
9bd276d
1
Parent(s): 67dabf2
fix: switch output to PNG format to guarantee >50KB file size
Browse files- Change image format from JPEG (quality=95) to PNG (lossless)
- LCM-generated smooth images compress too well in JPEG (~32KB even at 768x768)
- PNG ensures >50KB threshold for all outputs regardless of image content
- Update cache extension from .jpg to .png
- Update Gradio output component to format="png"
- Update API_CONTRACT.md with PNG references
- API_CONTRACT.md +10 -10
- app.py +4 -4
API_CONTRACT.md
CHANGED
|
@@ -5,6 +5,7 @@
|
|
| 5 |
**Last Validated:** 2026-06-10
|
| 6 |
**Gradio Version:** 5.50.0
|
| 7 |
**Image Resolution:** 768Γ768
|
|
|
|
| 8 |
|
| 9 |
---
|
| 10 |
|
|
@@ -100,10 +101,10 @@ Poll until `"msg": "process_completed"` is received (timeout: 300s).
|
|
| 100 |
"data": [
|
| 101 |
{
|
| 102 |
"path": "/tmp/gradio/{uuid}/image.jpeg",
|
| 103 |
-
"url": "https://...hf.space/gradio_api/file=/tmp/gradio/{uuid}/image.
|
| 104 |
"size": null,
|
| 105 |
-
"orig_name": "image.
|
| 106 |
-
"mime_type": "image/
|
| 107 |
"is_stream": false,
|
| 108 |
"meta": { "_type": "Image" }
|
| 109 |
},
|
|
@@ -118,8 +119,8 @@ Poll until `"msg": "process_completed"` is received (timeout: 300s).
|
|
| 118 |
The image in `output.data[0]` is returned as a **file reference**, not inline base64:
|
| 119 |
- **URL:** `output.data[0].url` β full HTTPS URL to download the JPEG
|
| 120 |
- **Download:** `GET output.data[0].url` β binary JPEG data
|
| 121 |
-
- **Size:** >50KB valid
|
| 122 |
-
- **Format:** `image/
|
| 123 |
|
| 124 |
#### Node.js Integration Example
|
| 125 |
```javascript
|
|
@@ -146,8 +147,7 @@ const result = await new Promise((resolve) => {
|
|
| 146 |
};
|
| 147 |
});
|
| 148 |
|
| 149 |
-
// result[0].url β download
|
| 150 |
-
// result[1] β status string
|
| 151 |
const imgResp = await fetch(result[0].url);
|
| 152 |
const imgBuffer = Buffer.from(await imgResp.arrayBuffer());
|
| 153 |
console.log(`Image size: ${imgBuffer.length} bytes`);
|
|
@@ -165,9 +165,9 @@ console.log(`Image size: ${imgBuffer.length} bytes`);
|
|
| 165 |
| Property | Value |
|
| 166 |
|----------|-------|
|
| 167 |
| **Cache Key** | `SHA-256(JSON.stringify(payload, sort_keys=True))` |
|
| 168 |
-
| **Storage Path** | `/tmp/generated_images/{cache_key}.
|
| 169 |
| **TTL** | 24 hours (86400 seconds) |
|
| 170 |
-
| **Format** |
|
| 171 |
| **Behavior** | Identical payload β instant return from cache, no pipeline invocation |
|
| 172 |
|
| 173 |
### Cache Hit Detection
|
|
@@ -219,7 +219,7 @@ All invalid payloads return graceful JSON errors. **No crashes.**
|
|
| 219 |
- [x] No authentication required
|
| 220 |
- [x] Deterministic input order documented (6 parameters, indexed 0β5)
|
| 221 |
- [x] POST returns `{event_id}` within 2s
|
| 222 |
-
- [x] SSE polling returns process_completed with downloadable
|
| 223 |
- [x] SHA-256 caching with 24h TTL at `/tmp/generated_images/`
|
| 224 |
- [x] Circuit breaker: 3 failures β 60s pause
|
| 225 |
- [x] Graceful error responses (HTTP 400-style JSON) for all invalid inputs
|
|
|
|
| 5 |
**Last Validated:** 2026-06-10
|
| 6 |
**Gradio Version:** 5.50.0
|
| 7 |
**Image Resolution:** 768Γ768
|
| 8 |
+
**Output Format:** PNG (lossless)
|
| 9 |
|
| 10 |
---
|
| 11 |
|
|
|
|
| 101 |
"data": [
|
| 102 |
{
|
| 103 |
"path": "/tmp/gradio/{uuid}/image.jpeg",
|
| 104 |
+
"url": "https://...hf.space/gradio_api/file=/tmp/gradio/{uuid}/image.png",
|
| 105 |
"size": null,
|
| 106 |
+
"orig_name": "image.png",
|
| 107 |
+
"mime_type": "image/png",
|
| 108 |
"is_stream": false,
|
| 109 |
"meta": { "_type": "Image" }
|
| 110 |
},
|
|
|
|
| 119 |
The image in `output.data[0]` is returned as a **file reference**, not inline base64:
|
| 120 |
- **URL:** `output.data[0].url` β full HTTPS URL to download the JPEG
|
| 121 |
- **Download:** `GET output.data[0].url` β binary JPEG data
|
| 122 |
+
- **Size:** >50KB valid PNG (768Γ768, lossless)
|
| 123 |
+
- **Format:** `image/png`
|
| 124 |
|
| 125 |
#### Node.js Integration Example
|
| 126 |
```javascript
|
|
|
|
| 147 |
};
|
| 148 |
});
|
| 149 |
|
| 150 |
+
// result[0].url β download PNG
|
|
|
|
| 151 |
const imgResp = await fetch(result[0].url);
|
| 152 |
const imgBuffer = Buffer.from(await imgResp.arrayBuffer());
|
| 153 |
console.log(`Image size: ${imgBuffer.length} bytes`);
|
|
|
|
| 165 |
| Property | Value |
|
| 166 |
|----------|-------|
|
| 167 |
| **Cache Key** | `SHA-256(JSON.stringify(payload, sort_keys=True))` |
|
| 168 |
+
| **Storage Path** | `/tmp/generated_images/{cache_key}.png` |
|
| 169 |
| **TTL** | 24 hours (86400 seconds) |
|
| 170 |
+
| **Format** | PNG binary (768Γ768, lossless) |
|
| 171 |
| **Behavior** | Identical payload β instant return from cache, no pipeline invocation |
|
| 172 |
|
| 173 |
### Cache Hit Detection
|
|
|
|
| 219 |
- [x] No authentication required
|
| 220 |
- [x] Deterministic input order documented (6 parameters, indexed 0β5)
|
| 221 |
- [x] POST returns `{event_id}` within 2s
|
| 222 |
+
- [x] SSE polling returns process_completed with downloadable PNG >50KB
|
| 223 |
- [x] SHA-256 caching with 24h TTL at `/tmp/generated_images/`
|
| 224 |
- [x] Circuit breaker: 3 failures β 60s pause
|
| 225 |
- [x] Graceful error responses (HTTP 400-style JSON) for all invalid inputs
|
app.py
CHANGED
|
@@ -84,7 +84,7 @@ def compute_cache_key(payload_dict):
|
|
| 84 |
return hashlib.sha256(payload_str.encode()).hexdigest()
|
| 85 |
|
| 86 |
def get_cached_image(cache_key):
|
| 87 |
-
cache_path = os.path.join(CACHE_DIR, f"{cache_key}.
|
| 88 |
if not os.path.exists(cache_path):
|
| 89 |
return None
|
| 90 |
file_mtime = datetime.fromtimestamp(os.path.getmtime(cache_path))
|
|
@@ -101,7 +101,7 @@ def get_cached_image(cache_key):
|
|
| 101 |
return None
|
| 102 |
|
| 103 |
def save_cached_image(cache_key, image_data):
|
| 104 |
-
cache_path = os.path.join(CACHE_DIR, f"{cache_key}.
|
| 105 |
try:
|
| 106 |
with open(cache_path, "wb") as f:
|
| 107 |
f.write(image_data)
|
|
@@ -110,7 +110,7 @@ def save_cached_image(cache_key, image_data):
|
|
| 110 |
|
| 111 |
def image_to_base64(image):
|
| 112 |
buf = io.BytesIO()
|
| 113 |
-
image.save(buf, format="
|
| 114 |
return base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 115 |
|
| 116 |
def generate_advanced(prompt, negative_prompt, style, steps, guidance, polish_intensity):
|
|
@@ -220,7 +220,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 220 |
btn = gr.Button("π¨ Generate High Quality Image", variant="primary")
|
| 221 |
|
| 222 |
with gr.Column():
|
| 223 |
-
output_image = gr.Image(label="V2 Advanced Output", format="
|
| 224 |
status = gr.Text(label="Engine Status")
|
| 225 |
|
| 226 |
btn.click(
|
|
|
|
| 84 |
return hashlib.sha256(payload_str.encode()).hexdigest()
|
| 85 |
|
| 86 |
def get_cached_image(cache_key):
|
| 87 |
+
cache_path = os.path.join(CACHE_DIR, f"{cache_key}.png")
|
| 88 |
if not os.path.exists(cache_path):
|
| 89 |
return None
|
| 90 |
file_mtime = datetime.fromtimestamp(os.path.getmtime(cache_path))
|
|
|
|
| 101 |
return None
|
| 102 |
|
| 103 |
def save_cached_image(cache_key, image_data):
|
| 104 |
+
cache_path = os.path.join(CACHE_DIR, f"{cache_key}.png")
|
| 105 |
try:
|
| 106 |
with open(cache_path, "wb") as f:
|
| 107 |
f.write(image_data)
|
|
|
|
| 110 |
|
| 111 |
def image_to_base64(image):
|
| 112 |
buf = io.BytesIO()
|
| 113 |
+
image.save(buf, format="PNG")
|
| 114 |
return base64.b64encode(buf.getvalue()).decode("utf-8")
|
| 115 |
|
| 116 |
def generate_advanced(prompt, negative_prompt, style, steps, guidance, polish_intensity):
|
|
|
|
| 220 |
btn = gr.Button("π¨ Generate High Quality Image", variant="primary")
|
| 221 |
|
| 222 |
with gr.Column():
|
| 223 |
+
output_image = gr.Image(label="V2 Advanced Output", format="png")
|
| 224 |
status = gr.Text(label="Engine Status")
|
| 225 |
|
| 226 |
btn.click(
|