Spaces:
Sleeping
Sleeping
- download_celeba_hq script
Browse files- change to docker project
- update README with Space metadata
- Dockerfile +15 -0
- README.md +17 -0
- app.js +5 -0
- assets/celeba_hq_validation_91_256.png +3 -0
- download_celeba_hq.py +80 -0
- index.html +4 -7
- server.py +7 -2
Dockerfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
COPY requirements.txt .
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 8 |
+
|
| 9 |
+
COPY --chown=user:user . .
|
| 10 |
+
RUN mkdir -p /app/models && chown user:user /app/models
|
| 11 |
+
USER user
|
| 12 |
+
ENV HOME=/home/user
|
| 13 |
+
|
| 14 |
+
EXPOSE 8000
|
| 15 |
+
CMD ["python", "-u", "server.py"]
|
README.md
CHANGED
|
@@ -1,3 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Diffusion Lab
|
| 2 |
|
| 3 |
The reverse process runs the pretrained `google/ddpm-celebahq-256` model through Hugging Face Diffusers. This is an unconditional CelebA-HQ face generator: reverse sampling starts with random Gaussian noise and does not denoise the uploaded image. The upload is used only by the browser-side forward-noising demonstration.
|
|
@@ -21,3 +31,10 @@ To generate and display one image without starting the server, run:
|
|
| 21 |
python generate.py
|
| 22 |
python generate.py --seed 1234
|
| 23 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Diffusion
|
| 3 |
+
emoji: 🌍
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
app_port: 8000
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
# Diffusion Lab
|
| 12 |
|
| 13 |
The reverse process runs the pretrained `google/ddpm-celebahq-256` model through Hugging Face Diffusers. This is an unconditional CelebA-HQ face generator: reverse sampling starts with random Gaussian noise and does not denoise the uploaded image. The upload is used only by the browser-side forward-noising demonstration.
|
|
|
|
| 31 |
python generate.py
|
| 32 |
python generate.py --seed 1234
|
| 33 |
```
|
| 34 |
+
|
| 35 |
+
To download a particular zero-based row from the public CelebA-HQ dataset:
|
| 36 |
+
|
| 37 |
+
```sh
|
| 38 |
+
python download_celeba_hq.py 1374
|
| 39 |
+
python download_celeba_hq.py 42 --split train --output face-42.png
|
| 40 |
+
```
|
app.js
CHANGED
|
@@ -161,4 +161,9 @@ function loadImage(image, label) { const canvas = document.createElement('canvas
|
|
| 161 |
elements.applyTimesteps.addEventListener('click', applyTimesteps); elements.timestepsInput.addEventListener('keydown', (event) => { if (event.key === 'Enter') applyTimesteps(); });
|
| 162 |
elements.randomizeSeed.addEventListener('click', () => { resampleNoise(); elements.configError.textContent = ''; forwardRevealCount = 1; forwardCompleted = false; resetProgress('forward'); computeForwardStates(); renderTracks(); });
|
| 163 |
elements.imageUpload.addEventListener('change', (event) => { const file = event.target.files?.[0]; if (!file) return; const image = new Image(); image.onload = () => { loadImage(image, file.name.replace(/\.[^.]+$/, '')); URL.revokeObjectURL(image.src); }; image.src = URL.createObjectURL(file); });
|
|
|
|
|
|
|
| 164 |
sourcePixels = canvasPixels(makeSourceArt()); computeForwardStates(); reverseNoiseState = makeNoiseState(readSeed()); resetProgress('forward'); resetProgress('reverse'); renderTracks();
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
elements.applyTimesteps.addEventListener('click', applyTimesteps); elements.timestepsInput.addEventListener('keydown', (event) => { if (event.key === 'Enter') applyTimesteps(); });
|
| 162 |
elements.randomizeSeed.addEventListener('click', () => { resampleNoise(); elements.configError.textContent = ''; forwardRevealCount = 1; forwardCompleted = false; resetProgress('forward'); computeForwardStates(); renderTracks(); });
|
| 163 |
elements.imageUpload.addEventListener('change', (event) => { const file = event.target.files?.[0]; if (!file) return; const image = new Image(); image.onload = () => { loadImage(image, file.name.replace(/\.[^.]+$/, '')); URL.revokeObjectURL(image.src); }; image.src = URL.createObjectURL(file); });
|
| 164 |
+
// Keep the original generated still life available while the default face loads,
|
| 165 |
+
// and as a fallback if the image asset cannot be read.
|
| 166 |
sourcePixels = canvasPixels(makeSourceArt()); computeForwardStates(); reverseNoiseState = makeNoiseState(readSeed()); resetProgress('forward'); resetProgress('reverse'); renderTracks();
|
| 167 |
+
const defaultSourceImage = new Image();
|
| 168 |
+
defaultSourceImage.onload = () => loadImage(defaultSourceImage, 'Human face');
|
| 169 |
+
defaultSourceImage.src = 'assets/celeba_hq_validation_91_256.png';
|
assets/celeba_hq_validation_91_256.png
ADDED
|
Git LFS Details
|
download_celeba_hq.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import json
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from urllib.error import HTTPError, URLError
|
| 6 |
+
from urllib.parse import urlencode
|
| 7 |
+
from urllib.request import Request, urlopen
|
| 8 |
+
|
| 9 |
+
from PIL import Image
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
DATASET = "korexyz/celeba-hq-256x256"
|
| 13 |
+
API_URL = "https://datasets-server.huggingface.co/rows"
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def download_row(row_index, split="validation", output=None):
|
| 17 |
+
"""Download one CelebA-HQ row and save it as a 256x256 RGB PNG."""
|
| 18 |
+
if row_index < 0:
|
| 19 |
+
raise ValueError("Row index must be zero or greater.")
|
| 20 |
+
|
| 21 |
+
query = urlencode(
|
| 22 |
+
{
|
| 23 |
+
"dataset": DATASET,
|
| 24 |
+
"config": "default",
|
| 25 |
+
"split": split,
|
| 26 |
+
"offset": row_index,
|
| 27 |
+
"length": 1,
|
| 28 |
+
}
|
| 29 |
+
)
|
| 30 |
+
request = Request(f"{API_URL}?{query}", headers={"User-Agent": "diffusion-lab/1.0"})
|
| 31 |
+
with urlopen(request, timeout=30) as response:
|
| 32 |
+
result = json.load(response)
|
| 33 |
+
|
| 34 |
+
rows = result.get("rows", [])
|
| 35 |
+
if not rows:
|
| 36 |
+
total = result.get("num_rows_total", "the available")
|
| 37 |
+
raise ValueError(f"Row {row_index} is outside {split!r} ({total} rows).")
|
| 38 |
+
|
| 39 |
+
image_url = rows[0]["row"]["image"]["src"]
|
| 40 |
+
image_request = Request(image_url, headers={"User-Agent": "diffusion-lab/1.0"})
|
| 41 |
+
with urlopen(image_request, timeout=60) as response:
|
| 42 |
+
image_bytes = response.read()
|
| 43 |
+
|
| 44 |
+
destination = Path(output or f"celeba_hq_{split}_{row_index}_256.png")
|
| 45 |
+
#destination.parent.mkdir(parents=True, exist_ok=True)
|
| 46 |
+
with Image.open(BytesIO(image_bytes)) as image:
|
| 47 |
+
image = image.convert("RGB").resize((256, 256), Image.Resampling.LANCZOS)
|
| 48 |
+
image.save(destination, format="PNG", optimize=True)
|
| 49 |
+
|
| 50 |
+
return destination
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def main():
|
| 54 |
+
parser = argparse.ArgumentParser(
|
| 55 |
+
description="Download a specific row from the CelebA-HQ 256x256 dataset."
|
| 56 |
+
)
|
| 57 |
+
parser.add_argument("row_index", type=int, help="Zero-based row index")
|
| 58 |
+
parser.add_argument(
|
| 59 |
+
"--split",
|
| 60 |
+
choices=("train", "validation"),
|
| 61 |
+
default="validation",
|
| 62 |
+
help="Dataset split (default: validation)",
|
| 63 |
+
)
|
| 64 |
+
parser.add_argument(
|
| 65 |
+
"--output",
|
| 66 |
+
type=Path,
|
| 67 |
+
help="Output PNG path (default: celeba_hq_<split>_<row>_256.png)",
|
| 68 |
+
)
|
| 69 |
+
args = parser.parse_args()
|
| 70 |
+
|
| 71 |
+
try:
|
| 72 |
+
destination = download_row(args.row_index, args.split, args.output)
|
| 73 |
+
except (HTTPError, URLError, TimeoutError, ValueError, KeyError, OSError) as error:
|
| 74 |
+
parser.exit(1, f"Download failed: {error}\n")
|
| 75 |
+
|
| 76 |
+
print(f"Saved row {args.row_index} from {args.split!r} to {destination}")
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
main()
|
index.html
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
<meta charset="utf-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
<meta name="theme-color" content="#f3f2ec">
|
| 7 |
-
<title>Diffusion
|
| 8 |
<link rel="preconnect" href="https://fonts.googleapis.com">
|
| 9 |
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
| 10 |
<link href="https://fonts.googleapis.com/css2?family=DM+Mono:wght@400;500&family=DM+Sans:wght@400;500;600;700&family=Space+Grotesk:wght@400;500;600;700&display=swap" rel="stylesheet">
|
|
@@ -13,7 +13,8 @@
|
|
| 13 |
<body>
|
| 14 |
<div class="app-shell">
|
| 15 |
<header class="topbar">
|
| 16 |
-
<a class="brand" href="#top"><span class="brand-mark"
|
|
|
|
| 17 |
<div class="topbar-right"><span class="live-dot"></span></div>
|
| 18 |
</header>
|
| 19 |
<main id="top">
|
|
@@ -33,10 +34,6 @@
|
|
| 33 |
<button id="randomize-seed" class="row-label-button" type="button" title="Generate a new random seed">Seed</button>
|
| 34 |
<input id="seed-input" type="text" inputmode="numeric" value="42" spellcheck="false" aria-describedby="seed-help config-error">
|
| 35 |
</div>
|
| 36 |
-
<div class="config-help">
|
| 37 |
-
<span id="timesteps-help">Timesteps: comma-separated values from 0 to 1000.</span>
|
| 38 |
-
<span id="seed-help">Select Seed for a new random value.</span>
|
| 39 |
-
</div>
|
| 40 |
<p id="config-error" class="field-error" role="alert"></p>
|
| 41 |
</div>
|
| 42 |
</section>
|
|
@@ -44,7 +41,7 @@
|
|
| 44 |
<div class="process-heading"><div class="panel-heading"><div>
|
| 45 |
<h2 id="diffusion-title">Forward process: image to noise</h2>
|
| 46 |
</div></div>
|
| 47 |
-
<span class="equation">x<sub>t</sub> = √α
|
| 48 |
</div>
|
| 49 |
<output id="forward-progress-label" class="step-progress">0/1000</output>
|
| 50 |
<div id="diffusion-track" class="image-track" aria-label="Forward diffusion checkpoints"></div>
|
|
|
|
| 4 |
<meta charset="utf-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
<meta name="theme-color" content="#f3f2ec">
|
| 7 |
+
<title>Diffusion — A DDPM in motion</title>
|
| 8 |
<link rel="preconnect" href="https://fonts.googleapis.com">
|
| 9 |
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
| 10 |
<link href="https://fonts.googleapis.com/css2?family=DM+Mono:wght@400;500&family=DM+Sans:wght@400;500;600;700&family=Space+Grotesk:wght@400;500;600;700&display=swap" rel="stylesheet">
|
|
|
|
| 13 |
<body>
|
| 14 |
<div class="app-shell">
|
| 15 |
<header class="topbar">
|
| 16 |
+
<a class="brand" href="#top"><span class="brand-mark"
|
| 17 |
+
aria-hidden="true"><i></i><i></i><i></i><i></i></span><span>diffusion model</span></a>
|
| 18 |
<div class="topbar-right"><span class="live-dot"></span></div>
|
| 19 |
</header>
|
| 20 |
<main id="top">
|
|
|
|
| 34 |
<button id="randomize-seed" class="row-label-button" type="button" title="Generate a new random seed">Seed</button>
|
| 35 |
<input id="seed-input" type="text" inputmode="numeric" value="42" spellcheck="false" aria-describedby="seed-help config-error">
|
| 36 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
<p id="config-error" class="field-error" role="alert"></p>
|
| 38 |
</div>
|
| 39 |
</section>
|
|
|
|
| 41 |
<div class="process-heading"><div class="panel-heading"><div>
|
| 42 |
<h2 id="diffusion-title">Forward process: image to noise</h2>
|
| 43 |
</div></div>
|
| 44 |
+
<span class="equation">x<sub>t</sub> = √α<sub>t</sub> x<sub>t−1</sub> + √(1 − α<sub>t</sub>) ε<sub>t</sub></span>
|
| 45 |
</div>
|
| 46 |
<output id="forward-progress-label" class="step-progress">0/1000</output>
|
| 47 |
<div id="diffusion-track" class="image-track" aria-label="Forward diffusion checkpoints"></div>
|
server.py
CHANGED
|
@@ -52,7 +52,12 @@ class DiffusionHandler(BaseHTTPRequestHandler):
|
|
| 52 |
return
|
| 53 |
|
| 54 |
relative_path = "index.html" if path == "/" else path.lstrip("/")
|
| 55 |
-
if relative_path not in {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
self.send_error(404)
|
| 57 |
return
|
| 58 |
|
|
@@ -116,7 +121,7 @@ class DiffusionServer(ThreadingHTTPServer):
|
|
| 116 |
|
| 117 |
|
| 118 |
if __name__ == "__main__":
|
| 119 |
-
server = DiffusionServer(("
|
| 120 |
print(f"Diffusion Lab: http://{server.server_address[0]}:{server.server_address[1]}")
|
| 121 |
print(f"Model: {MODEL_ID} (loaded on first generation request)")
|
| 122 |
server.serve_forever()
|
|
|
|
| 52 |
return
|
| 53 |
|
| 54 |
relative_path = "index.html" if path == "/" else path.lstrip("/")
|
| 55 |
+
if relative_path not in {
|
| 56 |
+
"index.html",
|
| 57 |
+
"app.js",
|
| 58 |
+
"styles.css",
|
| 59 |
+
"assets/celeba_hq_validation_91_256.png",
|
| 60 |
+
}:
|
| 61 |
self.send_error(404)
|
| 62 |
return
|
| 63 |
|
|
|
|
| 121 |
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
+
server = DiffusionServer(("0.0.0.0", 8000), DiffusionHandler)
|
| 125 |
print(f"Diffusion Lab: http://{server.server_address[0]}:{server.server_address[1]}")
|
| 126 |
print(f"Model: {MODEL_ID} (loaded on first generation request)")
|
| 127 |
server.serve_forever()
|