guydffdsdsfd commited on
Commit
4a04142
·
verified ·
1 Parent(s): 761ff31

Create Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +74 -0
Dockerfile ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use a lightweight Python base
2
+ FROM python:3.10-slim
3
+
4
+ # Install system dependencies for OpenVINO and Image processing
5
+ RUN apt-get update && apt-get install -y \
6
+ libgl1-mesa-glx libglib2.0-0 git \
7
+ && rm -rf /var/lib/apt/lists/*
8
+
9
+ # Install optimized CPU libraries
10
+ # optimum[openvino] is the key for fast CPU inference
11
+ RUN pip install --no-cache-dir \
12
+ flask flask-cors requests \
13
+ "optimum[openvino,diffusers]" \
14
+ transformers accelerate torch --extra-index-url https://download.pytorch.org/whl/cpu
15
+
16
+ # Set up environment
17
+ ENV HOME=/home/user
18
+ WORKDIR $HOME
19
+ RUN mkdir -p $HOME/.cache && chmod -R 777 $HOME
20
+
21
+ # --- 1. The Python Guard + Inference Script ---
22
+ RUN cat <<EOF > $HOME/app.py
23
+ from flask import Flask, request, jsonify, send_file
24
+ from optimum.intel import OVStableDiffusionPipeline
25
+ from flask_cors import CORS
26
+ import json, os, datetime, io, torch
27
+
28
+ app = Flask(__name__)
29
+ CORS(app)
30
+
31
+ DB_PATH = "/home/user/usage.json"
32
+ WL_PATH = "/home/user/whitelist.txt"
33
+ LIMIT = 500
34
+ UNLIMITED_KEY = "sk-ess4l0ri37"
35
+
36
+ # Load Model Optimized for CPU
37
+ # Using SD 1.5 (Small/Fast) converted to OpenVINO format
38
+ print("Loading Optimized CPU Model...")
39
+ model_id = "heifai/stable-diffusion-v1-5-openvino"
40
+ pipe = OVStableDiffusionPipeline.from_pretrained(model_id, compile=True)
41
+ print("Model Ready.")
42
+
43
+ def get_whitelist():
44
+ if not os.path.exists(WL_PATH):
45
+ with open(WL_PATH, "w") as f:
46
+ f.write(f"{UNLIMITED_KEY}\n")
47
+ return {UNLIMITED_KEY}
48
+ with open(WL_PATH, "r") as f:
49
+ return set(line.strip() for line in f.readlines())
50
+
51
+ @app.route("/api/generate", methods=["POST"])
52
+ def generate():
53
+ user_key = request.headers.get("x-api-key", "")
54
+ if user_key not in get_whitelist():
55
+ return jsonify({"error": "Unauthorized"}), 401
56
+
57
+ # Add your usage tracking logic here (from your Ollama script)
58
+
59
+ data = request.json
60
+ prompt = data.get("prompt", "a simple cat")
61
+
62
+ # CPU Optimization: Fewer steps for speed
63
+ image = pipe(prompt, num_inference_steps=20).images[0]
64
+
65
+ img_io = io.BytesIO()
66
+ image.save(img_io, 'PNG')
67
+ img_io.seek(0)
68
+ return send_file(img_io, mimetype='image/png')
69
+
70
+ if __name__ == "__main__":
71
+ app.run(host="0.0.0.0", port=7860)
72
+ EOF
73
+
74
+ ENTRYPOINT ["python3", "app.py"]