Spaces:
Build error
Build error
Update Dockerfile
Browse files- Dockerfile +18 -25
Dockerfile
CHANGED
|
@@ -10,20 +10,19 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
| 10 |
ca-certificates \
|
| 11 |
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
|
| 13 |
-
# ---------------- Python deps ----------------
|
| 14 |
-
# Install torch FIRST, pinned, CPU by default (much smaller + stable)
|
| 15 |
RUN pip install --no-cache-dir --upgrade pip && \
|
| 16 |
pip install --no-cache-dir \
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
| 20 |
--index-url https://download.pytorch.org/whl/cpu
|
| 21 |
|
| 22 |
-
# Then the rest
|
| 23 |
RUN pip install --no-cache-dir \
|
| 24 |
-
diffusers
|
| 25 |
-
transformers \
|
| 26 |
-
accelerate \
|
| 27 |
safetensors \
|
| 28 |
flask \
|
| 29 |
flask-cors \
|
|
@@ -34,7 +33,7 @@ ENV HOME=/home/sd
|
|
| 34 |
ENV HF_HOME=/home/sd/.cache
|
| 35 |
ENV OMP_NUM_THREADS=1
|
| 36 |
ENV MKL_NUM_THREADS=1
|
| 37 |
-
ENV
|
| 38 |
|
| 39 |
# ---------------- Storage ----------------
|
| 40 |
RUN mkdir -p /home/sd && chmod -R 777 /home/sd
|
|
@@ -46,6 +45,7 @@ from flask_cors import CORS
|
|
| 46 |
from diffusers import DiffusionPipeline, LCMScheduler
|
| 47 |
import torch, os, json, secrets
|
| 48 |
from io import BytesIO
|
|
|
|
| 49 |
|
| 50 |
app = Flask(__name__)
|
| 51 |
CORS(app)
|
|
@@ -63,32 +63,24 @@ for p in [WL_PATH, USAGE_PATH, LIMITS_PATH]:
|
|
| 63 |
if not os.path.exists(p):
|
| 64 |
open(p, "w").write("{}" if p.endswith(".json") else "")
|
| 65 |
|
| 66 |
-
print(f"Loading {MODEL_ID}
|
| 67 |
|
| 68 |
torch.set_grad_enabled(False)
|
| 69 |
-
torch.backends.cuda.matmul.allow_tf32 = True
|
| 70 |
-
torch.backends.cudnn.allow_tf32 = True
|
| 71 |
|
| 72 |
pipe = DiffusionPipeline.from_pretrained(
|
| 73 |
MODEL_ID,
|
| 74 |
-
torch_dtype=torch.
|
| 75 |
safety_checker=None
|
| 76 |
)
|
| 77 |
|
| 78 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
pipe = pipe.to(device)
|
| 82 |
-
|
| 83 |
-
# ---- SPEED + MEMORY OPTS ----
|
| 84 |
pipe.enable_attention_slicing()
|
| 85 |
pipe.enable_vae_slicing()
|
| 86 |
|
| 87 |
-
|
| 88 |
-
pipe.enable_model_cpu_offload()
|
| 89 |
-
pipe.unet.to(memory_format=torch.channels_last)
|
| 90 |
-
|
| 91 |
-
print(f"Model ready on {device}")
|
| 92 |
|
| 93 |
def whitelist():
|
| 94 |
try:
|
|
@@ -113,12 +105,14 @@ def health():
|
|
| 113 |
def generate_key():
|
| 114 |
data = request.get_json() or {}
|
| 115 |
key = "sk-" + secrets.token_hex(16)
|
|
|
|
| 116 |
with open(WL_PATH, "a") as f:
|
| 117 |
f.write(key + "\n")
|
| 118 |
|
| 119 |
limits = load_json(LIMITS_PATH)
|
| 120 |
limits[key] = "unlimited" if data.get("unlimited") else int(data.get("limit", DEFAULT_LIMIT))
|
| 121 |
save_json(LIMITS_PATH, limits)
|
|
|
|
| 122 |
return jsonify({"key": key, "limit": limits[key]})
|
| 123 |
|
| 124 |
@app.route("/api/generate", methods=["POST"])
|
|
@@ -134,11 +128,10 @@ def generate():
|
|
| 134 |
|
| 135 |
limits = load_json(LIMITS_PATH)
|
| 136 |
usage = load_json(USAGE_PATH)
|
| 137 |
-
limit = limits.get(key, DEFAULT_LIMIT)
|
| 138 |
|
| 139 |
-
from datetime import datetime
|
| 140 |
month = datetime.now().strftime("%Y-%m")
|
| 141 |
used = usage.get(key, {}).get(month, 0)
|
|
|
|
| 142 |
|
| 143 |
if limit != "unlimited" and used >= limit:
|
| 144 |
return jsonify({"error": "Monthly limit reached"}), 429
|
|
|
|
| 10 |
ca-certificates \
|
| 11 |
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
|
| 13 |
+
# ---------------- Python deps (PINNED & COMPATIBLE) ----------------
|
|
|
|
| 14 |
RUN pip install --no-cache-dir --upgrade pip && \
|
| 15 |
pip install --no-cache-dir \
|
| 16 |
+
numpy<2 \
|
| 17 |
+
torch==2.0.1 \
|
| 18 |
+
torchvision==0.15.2 \
|
| 19 |
+
torchaudio==2.0.2 \
|
| 20 |
--index-url https://download.pytorch.org/whl/cpu
|
| 21 |
|
|
|
|
| 22 |
RUN pip install --no-cache-dir \
|
| 23 |
+
diffusers==0.24.0 \
|
| 24 |
+
transformers==4.36.2 \
|
| 25 |
+
accelerate==0.25.0 \
|
| 26 |
safetensors \
|
| 27 |
flask \
|
| 28 |
flask-cors \
|
|
|
|
| 33 |
ENV HF_HOME=/home/sd/.cache
|
| 34 |
ENV OMP_NUM_THREADS=1
|
| 35 |
ENV MKL_NUM_THREADS=1
|
| 36 |
+
ENV NUMPY_EXPERIMENTAL_ARRAY_FUNCTION=0
|
| 37 |
|
| 38 |
# ---------------- Storage ----------------
|
| 39 |
RUN mkdir -p /home/sd && chmod -R 777 /home/sd
|
|
|
|
| 45 |
from diffusers import DiffusionPipeline, LCMScheduler
|
| 46 |
import torch, os, json, secrets
|
| 47 |
from io import BytesIO
|
| 48 |
+
from datetime import datetime
|
| 49 |
|
| 50 |
app = Flask(__name__)
|
| 51 |
CORS(app)
|
|
|
|
| 63 |
if not os.path.exists(p):
|
| 64 |
open(p, "w").write("{}" if p.endswith(".json") else "")
|
| 65 |
|
| 66 |
+
print(f"Loading model: {MODEL_ID}")
|
| 67 |
|
| 68 |
torch.set_grad_enabled(False)
|
|
|
|
|
|
|
| 69 |
|
| 70 |
pipe = DiffusionPipeline.from_pretrained(
|
| 71 |
MODEL_ID,
|
| 72 |
+
torch_dtype=torch.float32,
|
| 73 |
safety_checker=None
|
| 74 |
)
|
| 75 |
|
| 76 |
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
| 77 |
+
pipe = pipe.to("cpu")
|
| 78 |
|
| 79 |
+
# ---- MEMORY + SPEED ----
|
|
|
|
|
|
|
|
|
|
| 80 |
pipe.enable_attention_slicing()
|
| 81 |
pipe.enable_vae_slicing()
|
| 82 |
|
| 83 |
+
print("Model loaded (CPU, optimized)")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
def whitelist():
|
| 86 |
try:
|
|
|
|
| 105 |
def generate_key():
|
| 106 |
data = request.get_json() or {}
|
| 107 |
key = "sk-" + secrets.token_hex(16)
|
| 108 |
+
|
| 109 |
with open(WL_PATH, "a") as f:
|
| 110 |
f.write(key + "\n")
|
| 111 |
|
| 112 |
limits = load_json(LIMITS_PATH)
|
| 113 |
limits[key] = "unlimited" if data.get("unlimited") else int(data.get("limit", DEFAULT_LIMIT))
|
| 114 |
save_json(LIMITS_PATH, limits)
|
| 115 |
+
|
| 116 |
return jsonify({"key": key, "limit": limits[key]})
|
| 117 |
|
| 118 |
@app.route("/api/generate", methods=["POST"])
|
|
|
|
| 128 |
|
| 129 |
limits = load_json(LIMITS_PATH)
|
| 130 |
usage = load_json(USAGE_PATH)
|
|
|
|
| 131 |
|
|
|
|
| 132 |
month = datetime.now().strftime("%Y-%m")
|
| 133 |
used = usage.get(key, {}).get(month, 0)
|
| 134 |
+
limit = limits.get(key, DEFAULT_LIMIT)
|
| 135 |
|
| 136 |
if limit != "unlimited" and used >= limit:
|
| 137 |
return jsonify({"error": "Monthly limit reached"}), 429
|