MicroCore-Labs commited on
Commit
67dabf2
Β·
1 Parent(s): 519769d

fix: bump resolution to 768x768 to meet >50KB image threshold

Browse files

- Change width/height from 512x512 to 768x768 in both pipeline stages
- Update API_CONTRACT.md with verified Gradio 5.x API paths:
- Submit: POST /gradio_api/queue/join (with fn_index, session_hash)
- Poll: GET /gradio_api/queue/data (SSE stream)
- Image returned as file URL reference, not inline base64
- Download via output.data[0].url for binary JPEG

Files changed (2) hide show
  1. API_CONTRACT.md +96 -18
  2. app.py +2 -2
API_CONTRACT.md CHANGED
@@ -3,6 +3,8 @@
3
  **Space:** MicroCore-Studio-advanced-cpu-image-gen-v2
4
  **Status:** Active β€” Plug-and-Play Ready
5
  **Last Validated:** 2026-06-10
 
 
6
 
7
  ---
8
 
@@ -11,11 +13,14 @@
11
  | Property | Value |
12
  |----------|-------|
13
  | **Base URL** | `https://{space_id}.hf.space` |
14
- | **Endpoint** | `/gradio_api/call/generate_advanced` |
15
- | **Method** | POST |
 
16
  | **Auth** | None (public) |
17
  | **Content-Type** | `application/json` |
18
 
 
 
19
  ---
20
 
21
  ## 2. Input Payload Order (Deterministic)
@@ -31,7 +36,23 @@ Inputs map left-to-right, top-to-bottom from Gradio interface:
31
  | 4 | `data[4]` | number | `1.5` | Guidance scale (1.0–4.0) |
32
  | 5 | `data[5]` | number | `0.3` | Polish intensity / refiner strength (0.0–0.5) |
33
 
34
- ### Example POST Body
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  ```json
36
  {
37
  "data": [
@@ -51,34 +72,90 @@ Inputs map left-to-right, top-to-bottom from Gradio interface:
51
 
52
  ### Step 1 β€” Submit Generation (POST)
53
  ```
54
- POST /gradio_api/call/generate_advanced
55
  β†’ Response (within 2s): { "event_id": "ev-abc123..." }
56
  ```
 
 
 
 
57
 
58
- ### Step 2 β€” Poll for Result (GET)
59
  ```
60
- GET /queue/status?event_id=ev-abc123...
61
- β†’ Poll every 5s until status == "COMPLETE" (timeout: 120s)
 
 
 
 
62
  ```
 
63
 
64
- #### Success Response
65
  ```json
66
  {
67
- "status": "COMPLETE",
 
68
  "output": {
69
  "data": [
70
- { "image": { "url": "data:image/jpeg;base64,/9j/4AAQ..." } },
71
- "Quality Optimized Generation in 45.2s | Cache Key: a1b2c3d4..."
 
 
 
 
 
 
 
 
72
  ]
73
- }
 
74
  }
75
  ```
76
- - `output.data[0]` β€” Base64-encoded JPEG image (quality=95). **Must decode to >50KB valid JPEG/PNG.**
77
- - `output.data[1]` β€” Status text string.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
 
79
  #### Error Response
80
  ```json
81
- { "status": "ERROR", "output": { "data": ["", "{\"error\": \"...\", \"code\": 400}"] } }
82
  ```
83
 
84
  ---
@@ -90,7 +167,7 @@ GET /queue/status?event_id=ev-abc123...
90
  | **Cache Key** | `SHA-256(JSON.stringify(payload, sort_keys=True))` |
91
  | **Storage Path** | `/tmp/generated_images/{cache_key}.jpg` |
92
  | **TTL** | 24 hours (86400 seconds) |
93
- | **Format** | JPEG binary |
94
  | **Behavior** | Identical payload β†’ instant return from cache, no pipeline invocation |
95
 
96
  ### Cache Hit Detection
@@ -138,12 +215,13 @@ All invalid payloads return graceful JSON errors. **No crashes.**
138
 
139
  ## 6. Integration Checklist for MicroCore Studio
140
 
141
- - [x] Fixed endpoint URL: `/gradio_api/call/generate_advanced`
142
  - [x] No authentication required
143
  - [x] Deterministic input order documented (6 parameters, indexed 0–5)
144
  - [x] POST returns `{event_id}` within 2s
145
- - [x] GET polling returns base64 JPEG >50KB on COMPLETE
146
  - [x] SHA-256 caching with 24h TTL at `/tmp/generated_images/`
147
  - [x] Circuit breaker: 3 failures β†’ 60s pause
148
  - [x] Graceful error responses (HTTP 400-style JSON) for all invalid inputs
149
  - [x] OOM/stress auto-recovery within 2 minutes
 
 
3
  **Space:** MicroCore-Studio-advanced-cpu-image-gen-v2
4
  **Status:** Active β€” Plug-and-Play Ready
5
  **Last Validated:** 2026-06-10
6
+ **Gradio Version:** 5.50.0
7
+ **Image Resolution:** 768Γ—768
8
 
9
  ---
10
 
 
13
  | Property | Value |
14
  |----------|-------|
15
  | **Base URL** | `https://{space_id}.hf.space` |
16
+ | **Submit Endpoint** | `POST /gradio_api/queue/join` |
17
+ | **Polling Endpoint** | `GET /gradio_api/queue/data?session_hash={HASH}` (SSE stream) |
18
+ | **Status Endpoint** | `GET /gradio_api/queue/status?event_id={ID}` |
19
  | **Auth** | None (public) |
20
  | **Content-Type** | `application/json` |
21
 
22
+ > **Note:** Gradio 5.x uses `/gradio_api/queue/join` for submission and SSE streaming via `/gradio_api/queue/data` for results. The legacy `/gradio_api/call/` path also works for submission but returns a simpler response.
23
+
24
  ---
25
 
26
  ## 2. Input Payload Order (Deterministic)
 
36
  | 4 | `data[4]` | number | `1.5` | Guidance scale (1.0–4.0) |
37
  | 5 | `data[5]` | number | `0.3` | Polish intensity / refiner strength (0.0–0.5) |
38
 
39
+ ### Example POST Body (Gradio 5.x β€” queue/join)
40
+ ```json
41
+ {
42
+ "data": [
43
+ "a cat wearing a top hat",
44
+ "blurry, ugly, low quality",
45
+ "Photorealistic",
46
+ 8,
47
+ 1.5,
48
+ 0.3
49
+ ],
50
+ "fn_index": 0,
51
+ "session_hash": "your-session-id"
52
+ }
53
+ ```
54
+
55
+ ### Legacy POST Body (call endpoint)
56
  ```json
57
  {
58
  "data": [
 
72
 
73
  ### Step 1 β€” Submit Generation (POST)
74
  ```
75
+ POST /gradio_api/queue/join
76
  β†’ Response (within 2s): { "event_id": "ev-abc123..." }
77
  ```
78
+ **Headers required for polling:**
79
+ ```
80
+ x-grado-user: api
81
+ ```
82
 
83
+ ### Step 2 β€” Poll for Result (SSE Stream)
84
  ```
85
+ GET /gradio_api/queue/data?session_hash=YOUR_SESSION_HASH
86
+ β†’ Server-Sent Events stream with messages:
87
+ { "msg": "estimation", ... }
88
+ { "msg": "process_starts", "eta": N, ... }
89
+ { "msg": "process_completed", "output": {...}, "success": true }
90
+ { "msg": "close_stream" }
91
  ```
92
+ Poll until `"msg": "process_completed"` is received (timeout: 300s).
93
 
94
+ #### Success Response (`process_completed`)
95
  ```json
96
  {
97
+ "msg": "process_completed",
98
+ "event_id": "...",
99
  "output": {
100
  "data": [
101
+ {
102
+ "path": "/tmp/gradio/{uuid}/image.jpeg",
103
+ "url": "https://...hf.space/gradio_api/file=/tmp/gradio/{uuid}/image.jpeg",
104
+ "size": null,
105
+ "orig_name": "image.jpeg",
106
+ "mime_type": "image/jpeg",
107
+ "is_stream": false,
108
+ "meta": { "_type": "Image" }
109
+ },
110
+ "Quality Optimized Generation in 98.63s | Cache Key: fb304891dc6a85a5..."
111
  ]
112
+ },
113
+ "success": true
114
  }
115
  ```
116
+
117
+ #### Image Retrieval
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 JPEG (768Γ—768, quality=95)
122
+ - **Format:** `image/jpeg`
123
+
124
+ #### Node.js Integration Example
125
+ ```javascript
126
+ const submit = await fetch(SPACE_URL + "/gradio_api/queue/join", {
127
+ method: "POST",
128
+ headers: { "Content-Type": "application/json" },
129
+ body: JSON.stringify({
130
+ data: ["prompt text", "negative", "Photorealistic", 8, 1.5, 0.3],
131
+ fn_index: 0,
132
+ session_hash: "my-session"
133
+ })
134
+ });
135
+ const { event_id } = await submit.json();
136
+
137
+ // Poll via SSE or use gradio_client library
138
+ const result = await new Promise((resolve) => {
139
+ const es = new EventSource(SPACE_URL + "/gradio_api/queue/data?session_hash=my-session");
140
+ es.onmessage = (e) => {
141
+ const msg = JSON.parse(e.data);
142
+ if (msg.msg === "process_completed") {
143
+ es.close();
144
+ resolve(msg.output.data);
145
+ }
146
+ };
147
+ });
148
+
149
+ // result[0].url β†’ download JPEG
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`);
154
+ ```
155
 
156
  #### Error Response
157
  ```json
158
+ { "msg": "process_completed", "success": false, "output": { "data": ["", "{\"error\": \"...\", \"code\": 400}"] } }
159
  ```
160
 
161
  ---
 
167
  | **Cache Key** | `SHA-256(JSON.stringify(payload, sort_keys=True))` |
168
  | **Storage Path** | `/tmp/generated_images/{cache_key}.jpg` |
169
  | **TTL** | 24 hours (86400 seconds) |
170
+ | **Format** | JPEG binary (768Γ—768, quality=95) |
171
  | **Behavior** | Identical payload β†’ instant return from cache, no pipeline invocation |
172
 
173
  ### Cache Hit Detection
 
215
 
216
  ## 6. Integration Checklist for MicroCore Studio
217
 
218
+ - [x] Fixed endpoint URL: `/gradio_api/queue/join` (submit) + `/gradio_api/queue/data` (poll)
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 JPEG >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
226
  - [x] OOM/stress auto-recovery within 2 minutes
227
+ - [x] Image resolution: 768Γ—768 ensuring >50KB output size
app.py CHANGED
@@ -162,8 +162,8 @@ def generate_advanced(prompt, negative_prompt, style, steps, guidance, polish_in
162
  negative_prompt=negative_prompt if negative_prompt else "blurry, low quality, distorted",
163
  num_inference_steps=steps,
164
  guidance_scale=guidance,
165
- width=512,
166
- height=512,
167
  ).images[0]
168
  except Exception as e:
169
  circuit_breaker.record_failure()
 
162
  negative_prompt=negative_prompt if negative_prompt else "blurry, low quality, distorted",
163
  num_inference_steps=steps,
164
  guidance_scale=guidance,
165
+ width=768,
166
+ height=768,
167
  ).images[0]
168
  except Exception as e:
169
  circuit_breaker.record_failure()