Spaces:
Build error
Build error
Update Dockerfile
Browse files- Dockerfile +53 -24
Dockerfile
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
# 1. Use Python base
|
| 2 |
FROM python:3.10-slim
|
| 3 |
|
| 4 |
-
# 2. Install
|
|
|
|
| 5 |
RUN apt-get update && apt-get install -y \
|
| 6 |
libgl1-mesa-glx libglib2.0-0 procps curl && \
|
| 7 |
pip3 install flask flask-cors requests diffusers transformers accelerate torch --extra-index-url https://download.pytorch.org/whl/cpu
|
|
@@ -11,7 +12,7 @@ ENV HOME=/home/user
|
|
| 11 |
WORKDIR $HOME
|
| 12 |
RUN mkdir -p $HOME/.cache && chmod -R 777 $HOME
|
| 13 |
|
| 14 |
-
# --- 4.
|
| 15 |
RUN cat <<EOF > $HOME/app.py
|
| 16 |
from flask import Flask, request, Response, jsonify, send_file
|
| 17 |
import torch
|
|
@@ -22,25 +23,32 @@ import json, os, datetime, io
|
|
| 22 |
app = Flask(__name__)
|
| 23 |
CORS(app)
|
| 24 |
|
|
|
|
| 25 |
DB_PATH = "$HOME/usage.json"
|
| 26 |
-
|
|
|
|
| 27 |
UNLIMITED_KEY = "sk-ess4l0ri37"
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
pipe.to("cpu")
|
| 34 |
-
print("Model
|
| 35 |
|
| 36 |
def get_whitelist():
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
@app.route("/", methods=["GET"])
|
| 42 |
def health():
|
| 43 |
-
return "Stable Diffusion CPU
|
| 44 |
|
| 45 |
@app.route("/api/generate", methods=["POST"])
|
| 46 |
def proxy():
|
|
@@ -48,38 +56,59 @@ def proxy():
|
|
| 48 |
|
| 49 |
# 1. Auth Check
|
| 50 |
if user_key not in get_whitelist():
|
| 51 |
-
return jsonify({"error": "Unauthorized: Key not registered"}), 401
|
| 52 |
|
| 53 |
-
# 2.
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
| 57 |
try:
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
| 59 |
image = pipe(prompt, num_inference_steps=15).images[0]
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
img_io = io.BytesIO()
|
| 62 |
image.save(img_io, 'PNG')
|
| 63 |
img_io.seek(0)
|
| 64 |
-
|
| 65 |
-
# Add your usage.json logging logic here if needed
|
| 66 |
-
|
| 67 |
return send_file(img_io, mimetype='image/png')
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
-
return jsonify({"error": str(e)}), 500
|
| 70 |
|
| 71 |
if __name__ == "__main__":
|
| 72 |
app.run(host="0.0.0.0", port=7860)
|
| 73 |
EOF
|
| 74 |
|
| 75 |
-
# --- 5.
|
| 76 |
RUN cat <<EOF > $HOME/start.sh
|
| 77 |
#!/bin/bash
|
| 78 |
-
echo "Starting Stable Diffusion Guard Service..."
|
| 79 |
python3 $HOME/app.py
|
| 80 |
EOF
|
| 81 |
|
| 82 |
RUN chmod +x $HOME/start.sh
|
| 83 |
|
| 84 |
-
# --- 6.
|
| 85 |
ENTRYPOINT ["/bin/bash", "/home/user/start.sh"]
|
|
|
|
| 1 |
# 1. Use Python base
|
| 2 |
FROM python:3.10-slim
|
| 3 |
|
| 4 |
+
# 2. Install dependencies
|
| 5 |
+
# 'optimum' helps speed up the model on CPU
|
| 6 |
RUN apt-get update && apt-get install -y \
|
| 7 |
libgl1-mesa-glx libglib2.0-0 procps curl && \
|
| 8 |
pip3 install flask flask-cors requests diffusers transformers accelerate torch --extra-index-url https://download.pytorch.org/whl/cpu
|
|
|
|
| 12 |
WORKDIR $HOME
|
| 13 |
RUN mkdir -p $HOME/.cache && chmod -R 777 $HOME
|
| 14 |
|
| 15 |
+
# --- 4. THE PYTHON APP (Guard + SD Model) ---
|
| 16 |
RUN cat <<EOF > $HOME/app.py
|
| 17 |
from flask import Flask, request, Response, jsonify, send_file
|
| 18 |
import torch
|
|
|
|
| 23 |
app = Flask(__name__)
|
| 24 |
CORS(app)
|
| 25 |
|
| 26 |
+
# Note: These files reset every time the Space restarts on the Free Tier
|
| 27 |
DB_PATH = "$HOME/usage.json"
|
| 28 |
+
WL_PATH = "$HOME/whitelist.txt"
|
| 29 |
+
LIMIT = 500
|
| 30 |
UNLIMITED_KEY = "sk-ess4l0ri37"
|
| 31 |
|
| 32 |
+
# Seed the whitelist with your unique SD keys
|
| 33 |
+
if not os.path.exists(WL_PATH):
|
| 34 |
+
with open(WL_PATH, "w") as f:
|
| 35 |
+
f.write(f"sk-sd-user-1\nsk-sd-pro-99\n{UNLIMITED_KEY}\n")
|
| 36 |
+
|
| 37 |
+
print("Loading Stable Diffusion on CPU...")
|
| 38 |
+
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", torch_dtype=torch.float32)
|
| 39 |
pipe.to("cpu")
|
| 40 |
+
print("Model Ready!")
|
| 41 |
|
| 42 |
def get_whitelist():
|
| 43 |
+
try:
|
| 44 |
+
with open(WL_PATH, "r") as f:
|
| 45 |
+
return set(line.strip() for line in f.readlines())
|
| 46 |
+
except:
|
| 47 |
+
return {UNLIMITED_KEY}
|
| 48 |
|
| 49 |
@app.route("/", methods=["GET"])
|
| 50 |
def health():
|
| 51 |
+
return "Stable Diffusion CPU Proxy is Running", 200
|
| 52 |
|
| 53 |
@app.route("/api/generate", methods=["POST"])
|
| 54 |
def proxy():
|
|
|
|
| 56 |
|
| 57 |
# 1. Auth Check
|
| 58 |
if user_key not in get_whitelist():
|
| 59 |
+
return jsonify({"error": "Unauthorized: Key not registered for SD"}), 401
|
| 60 |
|
| 61 |
+
# 2. Usage Check
|
| 62 |
+
is_unlimited = (user_key == UNLIMITED_KEY)
|
| 63 |
+
if not is_unlimited:
|
| 64 |
+
now = datetime.datetime.now()
|
| 65 |
+
month_key = now.strftime("%Y-%m")
|
| 66 |
+
usage = {}
|
| 67 |
+
if os.path.exists(DB_PATH):
|
| 68 |
+
try:
|
| 69 |
+
with open(DB_PATH, "r") as f:
|
| 70 |
+
usage = json.load(f)
|
| 71 |
+
except: pass
|
| 72 |
+
|
| 73 |
+
key_usage = usage.get(user_key, {}).get(month_key, 0)
|
| 74 |
+
if key_usage >= LIMIT:
|
| 75 |
+
return jsonify({"error": f"Monthly limit of {LIMIT} reached"}), 429
|
| 76 |
|
| 77 |
+
# 3. Image Generation
|
| 78 |
try:
|
| 79 |
+
data = request.json
|
| 80 |
+
prompt = data.get("prompt", "a simple drawing of a cat")
|
| 81 |
+
|
| 82 |
+
# CPU speed tweak: Lower steps (15) for faster turnaround
|
| 83 |
image = pipe(prompt, num_inference_steps=15).images[0]
|
| 84 |
|
| 85 |
+
# Log usage
|
| 86 |
+
if not is_unlimited:
|
| 87 |
+
if user_key not in usage: usage[user_key] = {}
|
| 88 |
+
usage[user_key][month_key] = key_usage + 1
|
| 89 |
+
with open(DB_PATH, "w") as f:
|
| 90 |
+
json.dump(usage, f)
|
| 91 |
+
|
| 92 |
+
# Return Image
|
| 93 |
img_io = io.BytesIO()
|
| 94 |
image.save(img_io, 'PNG')
|
| 95 |
img_io.seek(0)
|
|
|
|
|
|
|
|
|
|
| 96 |
return send_file(img_io, mimetype='image/png')
|
| 97 |
+
|
| 98 |
except Exception as e:
|
| 99 |
+
return jsonify({"error": f"Generation Error: {str(e)}"}), 500
|
| 100 |
|
| 101 |
if __name__ == "__main__":
|
| 102 |
app.run(host="0.0.0.0", port=7860)
|
| 103 |
EOF
|
| 104 |
|
| 105 |
+
# --- 5. THE STARTUP SCRIPT ---
|
| 106 |
RUN cat <<EOF > $HOME/start.sh
|
| 107 |
#!/bin/bash
|
|
|
|
| 108 |
python3 $HOME/app.py
|
| 109 |
EOF
|
| 110 |
|
| 111 |
RUN chmod +x $HOME/start.sh
|
| 112 |
|
| 113 |
+
# --- 6. RUN ---
|
| 114 |
ENTRYPOINT ["/bin/bash", "/home/user/start.sh"]
|