Spaces:
Running
Running
Update Dockerfile
Browse files- Dockerfile +30 -6
Dockerfile
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
FROM ollama/ollama:latest
|
| 2 |
|
|
|
|
| 3 |
RUN apt-get update && apt-get install -y python3 python3-pip && \
|
| 4 |
pip3 install flask flask-cors requests --break-system-packages
|
| 5 |
|
|
@@ -8,36 +9,59 @@ ENV OLLAMA_HOST=127.0.0.1:11434
|
|
| 8 |
ENV HOME=/home/ollama
|
| 9 |
RUN mkdir -p /home/ollama/.ollama && chmod 777 /home/ollama/.ollama
|
| 10 |
|
| 11 |
-
#
|
| 12 |
RUN cat <<EOF > /guard.py
|
| 13 |
from flask import Flask, request, Response
|
| 14 |
import requests
|
| 15 |
from flask_cors import CORS
|
|
|
|
| 16 |
|
| 17 |
app = Flask(__name__)
|
| 18 |
CORS(app)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
@app.route("/whitelist", methods=["POST"])
|
| 24 |
def add_to_whitelist():
|
| 25 |
key = request.json.get("key")
|
| 26 |
if key:
|
| 27 |
whitelist.add(key)
|
| 28 |
-
return {"status": "
|
| 29 |
return {"status": "Error"}, 400
|
| 30 |
|
| 31 |
@app.route("/api/generate", methods=["POST"])
|
| 32 |
def proxy():
|
| 33 |
user_key = request.headers.get("x-api-key", "")
|
| 34 |
if user_key not in whitelist:
|
| 35 |
-
return {"error": "Unauthorized:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
try:
|
| 37 |
resp = requests.post("http://127.0.0.1:11434/api/generate", json=request.json)
|
|
|
|
|
|
|
|
|
|
| 38 |
return Response(resp.content, resp.status_code, resp.headers.items())
|
| 39 |
except:
|
| 40 |
-
return {"error": "
|
| 41 |
|
| 42 |
if __name__ == "__main__":
|
| 43 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
FROM ollama/ollama:latest
|
| 2 |
|
| 3 |
+
# Install Python & Dependencies with PEP 668 fix
|
| 4 |
RUN apt-get update && apt-get install -y python3 python3-pip && \
|
| 5 |
pip3 install flask flask-cors requests --break-system-packages
|
| 6 |
|
|
|
|
| 9 |
ENV HOME=/home/ollama
|
| 10 |
RUN mkdir -p /home/ollama/.ollama && chmod 777 /home/ollama/.ollama
|
| 11 |
|
| 12 |
+
# Create the Security Guard with Rate Limiting and Whitelisting
|
| 13 |
RUN cat <<EOF > /guard.py
|
| 14 |
from flask import Flask, request, Response
|
| 15 |
import requests
|
| 16 |
from flask_cors import CORS
|
| 17 |
+
import json, os, datetime
|
| 18 |
|
| 19 |
app = Flask(__name__)
|
| 20 |
CORS(app)
|
| 21 |
|
| 22 |
+
DB_PATH = "/home/ollama/usage.json"
|
| 23 |
+
LIMIT = 500
|
| 24 |
+
whitelist = set(["sk-admin-seed-99"])
|
| 25 |
+
|
| 26 |
+
def get_usage():
|
| 27 |
+
if not os.path.exists(DB_PATH): return {}
|
| 28 |
+
with open(DB_PATH, "r") as f: return json.load(f)
|
| 29 |
+
|
| 30 |
+
def save_usage(data):
|
| 31 |
+
with open(DB_PATH, "w") as f: json.dump(data, f)
|
| 32 |
|
| 33 |
@app.route("/whitelist", methods=["POST"])
|
| 34 |
def add_to_whitelist():
|
| 35 |
key = request.json.get("key")
|
| 36 |
if key:
|
| 37 |
whitelist.add(key)
|
| 38 |
+
return {"status": "Authorized"}, 200
|
| 39 |
return {"status": "Error"}, 400
|
| 40 |
|
| 41 |
@app.route("/api/generate", methods=["POST"])
|
| 42 |
def proxy():
|
| 43 |
user_key = request.headers.get("x-api-key", "")
|
| 44 |
if user_key not in whitelist:
|
| 45 |
+
return {"error": "Unauthorized: Key not registered"}, 401
|
| 46 |
+
|
| 47 |
+
now = datetime.datetime.now()
|
| 48 |
+
month_key = now.strftime("%Y-%m")
|
| 49 |
+
usage = get_usage()
|
| 50 |
+
|
| 51 |
+
if user_key not in usage: usage[user_key] = {}
|
| 52 |
+
if month_key not in usage[user_key]: usage[user_key][month_key] = 0
|
| 53 |
+
|
| 54 |
+
if usage[user_key][month_key] >= LIMIT:
|
| 55 |
+
return {"error": f"Monthly limit of {LIMIT} reached"}, 429
|
| 56 |
+
|
| 57 |
try:
|
| 58 |
resp = requests.post("http://127.0.0.1:11434/api/generate", json=request.json)
|
| 59 |
+
if resp.status_code == 200:
|
| 60 |
+
usage[user_key][month_key] += 1
|
| 61 |
+
save_usage(usage)
|
| 62 |
return Response(resp.content, resp.status_code, resp.headers.items())
|
| 63 |
except:
|
| 64 |
+
return {"error": "Server booting..."}, 503
|
| 65 |
|
| 66 |
if __name__ == "__main__":
|
| 67 |
app.run(host="0.0.0.0", port=7860)
|