Spaces:
Sleeping
Sleeping
File size: 5,986 Bytes
dc1bdf2 | 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | # Real-CUGAN API Docs
Base URL:
- Production: `https://huanx-real-cugan-hf-api.hf.space`
- Space page: `https://huggingface.co/spaces/huanx/real-cugan-hf-api`
## Auth
Use one of:
- `X-API-Key: <your_api_key>`
- `Authorization: Bearer <your_api_key>`
API keys are created in the admin panel:
- `https://huanx-real-cugan-hf-api.hf.space/admin`
## Endpoints
### `GET /`
Returns service entrypoints.
Example response:
```json
{
"name": "Real-CUGAN Control Plane",
"admin": "/admin",
"health": "/healthz",
"api": "/api/v1/upscale"
}
```
### `GET /healthz`
Returns current health status.
Example response:
```json
{
"status": "ok",
"busy": false,
"models_loaded": [],
"admin_configured": true
}
```
### `POST /api/v1/upscale`
Upscales one image.
Request type:
- `multipart/form-data`
Form fields:
- `file`: required, input image
- `scale`: optional, `2` | `3` | `4`
- `variant`: optional
- `alpha`: optional, `0.75` to `1.3`
- `tile_mode`: optional, integer
- `cache_mode`: optional, `0` or `1`
- `response_format`: optional, `png` | `jpeg` | `webp`
Current defaults:
- `scale=2`
- `variant=no-denoise`
- `alpha=1.0`
- `tile_mode=3`
- `cache_mode=1`
- `response_format=png`
Supported variants by scale:
- `2x`: `no-denoise`, `denoise1x`, `denoise2x`, `denoise3x`, `conservative`
- `3x`: `no-denoise`, `denoise3x`, `conservative`
- `4x`: `no-denoise`, `denoise3x`, `conservative`
Input limits:
- minimum dimension: `8px`
- max long edge: `1536px`
- max pixels: `2400000`
Response:
- binary image body
- content type matches `response_format`
Useful response headers:
- `X-Job-Id`
- `X-Scale`
- `X-Variant`
- `X-Tile-Mode-Used`
- `X-Cache-Mode-Used`
- `X-Duration-Ms`
Notes:
- Requests are serialized. Only one upscale runs at a time.
- `png` and `webp` preserve alpha when the source image has transparency.
- For very small images, the runtime may automatically downgrade tile settings to avoid upstream Real-CUGAN errors.
## Quick Start
Set your variables first:
```bash
export BASE_URL="https://huanx-real-cugan-hf-api.hf.space"
export API_KEY="replace-with-your-api-key"
```
### curl
```bash
curl -X POST "$BASE_URL/api/v1/upscale" \
-H "X-API-Key: $API_KEY" \
-F "file=@input.png" \
-F "scale=2" \
-F "variant=no-denoise" \
-F "alpha=1.0" \
-F "response_format=png" \
-o output.png
```
If you want response headers too:
```bash
curl -D response.headers.txt -X POST "$BASE_URL/api/v1/upscale" \
-H "X-API-Key: $API_KEY" \
-F "file=@input.png" \
-F "scale=2" \
-F "variant=no-denoise" \
-F "response_format=png" \
-o output.png
```
### Python
```python
import requests
BASE_URL = "https://huanx-real-cugan-hf-api.hf.space"
API_KEY = "replace-with-your-api-key"
with open("input.png", "rb") as f:
response = requests.post(
f"{BASE_URL}/api/v1/upscale",
headers={"X-API-Key": API_KEY},
files={"file": ("input.png", f, "image/png")},
data={
"scale": "2",
"variant": "no-denoise",
"alpha": "1.0",
"response_format": "png",
},
timeout=180,
)
response.raise_for_status()
with open("output.png", "wb") as f:
f.write(response.content)
print("job_id:", response.headers.get("X-Job-Id"))
print("duration_ms:", response.headers.get("X-Duration-Ms"))
```
### Node.js
Node 18+ example:
```js
import fs from "node:fs";
const baseUrl = "https://huanx-real-cugan-hf-api.hf.space";
const apiKey = "replace-with-your-api-key";
const form = new FormData();
form.append("file", new Blob([fs.readFileSync("input.png")], { type: "image/png" }), "input.png");
form.append("scale", "2");
form.append("variant", "no-denoise");
form.append("alpha", "1.0");
form.append("response_format", "png");
const response = await fetch(`${baseUrl}/api/v1/upscale`, {
method: "POST",
headers: {
"X-API-Key": apiKey,
},
body: form,
});
if (!response.ok) {
throw new Error(await response.text());
}
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync("output.png", buffer);
console.log("job_id:", response.headers.get("x-job-id"));
console.log("duration_ms:", response.headers.get("x-duration-ms"));
```
## Example Variants
Typical quality choices:
- strongest general 2x anime upscale: `scale=2`, `variant=no-denoise`
- noisy or heavily compressed source: `scale=2`, `variant=denoise1x` or `denoise2x`
- aggressive cleanup: `scale=2`, `variant=denoise3x`
- safest / least destructive: `scale=2`, `variant=conservative`
For CPU Basic, recommended first try:
```text
scale=2
variant=no-denoise
alpha=1.0
response_format=png
```
## Error Codes
### `400 Bad Request`
Common causes:
- missing `file`
- empty upload
- unsupported `response_format`
- image too small
- image exceeds configured size limits
Example:
```json
{
"detail": "Empty upload."
}
```
### `401 Unauthorized`
Common causes:
- missing API key
- invalid API key
- disabled API key
Examples:
```json
{
"detail": "Missing API key."
}
```
```json
{
"detail": "Invalid API key."
}
```
### `429 Too Many Requests`
The service only runs one upscale at a time.
Example:
```json
{
"detail": "Another inference is already running."
}
```
### `503 Service Unavailable`
Common causes:
- runtime/model load failure
- missing weights
- unsupported model selection
Example:
```json
{
"detail": "Weight for 2x / no-denoise not found in /app/weights."
}
```
### `500 Internal Server Error`
Unexpected inference failure.
Example:
```json
{
"detail": "Upscaling failed."
}
```
## Admin
Admin panel:
- `GET /admin/login`
- `GET /admin`
Functions:
- create / disable / delete API keys
- adjust default runtime settings
- view job history and dashboard metrics
- change admin password
## Minimal Integration Flow
1. Log in to `/admin`
2. Create an API key
3. Store it in your caller as `X-API-Key`
4. Send `multipart/form-data` to `/api/v1/upscale`
5. Save the binary response as an image file
|