guydffdsdsfd commited on
Commit
8974663
·
verified ·
1 Parent(s): 95cf803

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +23 -11
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,7 +9,7 @@ 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
- # Gatekeeper with Persistent Whitelisting
12
  RUN cat <<EOF > /guard.py
13
  from flask import Flask, request, Response
14
  import requests
@@ -22,9 +23,10 @@ DB_PATH = "/home/ollama/usage.json"
22
  WL_PATH = "/home/ollama/whitelist.txt"
23
  LIMIT = 500
24
 
25
- # Load whitelist from file on startup
26
  if not os.path.exists(WL_PATH):
27
- with open(WL_PATH, "w") as f: f.write("sk-admin-seed-99\nsk-ljlubs0boej\n")
 
28
 
29
  def get_whitelist():
30
  with open(WL_PATH, "r") as f:
@@ -42,33 +44,43 @@ def add_to_whitelist():
42
  @app.route("/api/generate", methods=["POST"])
43
  def proxy():
44
  user_key = request.headers.get("x-api-key", "")
 
 
45
  if user_key not in get_whitelist():
46
  return {"error": "Unauthorized: Key not registered"}, 401
47
 
48
- # Usage tracking logic
49
  now = datetime.datetime.now()
50
  month_key = now.strftime("%Y-%m")
51
- if not os.path.exists(DB_PATH): usage = {}
 
 
52
  else:
53
- with open(DB_PATH, "r") as f: usage = json.load(f)
 
 
54
 
55
  if user_key not in usage: usage[user_key] = {}
56
  if month_key not in usage[user_key]: usage[user_key][month_key] = 0
57
 
 
58
  if usage[user_key][month_key] >= LIMIT:
59
- return {"error": "Limit reached"}, 429
60
 
61
  try:
 
62
  resp = requests.post("http://127.0.0.1:11434/api/generate", json=request.json)
63
  if resp.status_code == 200:
64
  usage[user_key][month_key] += 1
65
- with open(DB_PATH, "w") as f: json.dump(usage, f)
 
66
  return Response(resp.content, resp.status_code, resp.headers.items())
67
- except:
68
- return {"error": "Ollama booting..."}, 503
69
 
70
  if __name__ == "__main__":
71
  app.run(host="0.0.0.0", port=7860)
72
  EOF
73
 
74
- CMD ["/bin/sh", "-c", "ollama serve & sleep 15 && ollama pull phi3 && python3 /guard.py"]
 
 
1
  FROM ollama/ollama:latest
2
 
3
+ # 1. Install Python and dependencies with the system-package override
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
+ # 2. Create the Security Guard (Persistent Whitelist + 500/mo Limit)
13
  RUN cat <<EOF > /guard.py
14
  from flask import Flask, request, Response
15
  import requests
 
23
  WL_PATH = "/home/ollama/whitelist.txt"
24
  LIMIT = 500
25
 
26
+ # Ensure the whitelist file exists and includes your specific key
27
  if not os.path.exists(WL_PATH):
28
+ with open(WL_PATH, "w") as f:
29
+ f.write("sk-admin-seed-99\nsk-ljlubs0boej\n")
30
 
31
  def get_whitelist():
32
  with open(WL_PATH, "r") as f:
 
44
  @app.route("/api/generate", methods=["POST"])
45
  def proxy():
46
  user_key = request.headers.get("x-api-key", "")
47
+
48
+ # Check if key is in the persistent file
49
  if user_key not in get_whitelist():
50
  return {"error": "Unauthorized: Key not registered"}, 401
51
 
52
+ # Monthly usage tracking
53
  now = datetime.datetime.now()
54
  month_key = now.strftime("%Y-%m")
55
+
56
+ if not os.path.exists(DB_PATH):
57
+ usage = {}
58
  else:
59
+ with open(DB_PATH, "r") as f:
60
+ try: usage = json.load(f)
61
+ except: usage = {}
62
 
63
  if user_key not in usage: usage[user_key] = {}
64
  if month_key not in usage[user_key]: usage[user_key][month_key] = 0
65
 
66
+ # Check 500 request limit
67
  if usage[user_key][month_key] >= LIMIT:
68
+ return {"error": f"Monthly limit of {LIMIT} reached"}, 429
69
 
70
  try:
71
+ # Forward request to Llama 3.2
72
  resp = requests.post("http://127.0.0.1:11434/api/generate", json=request.json)
73
  if resp.status_code == 200:
74
  usage[user_key][month_key] += 1
75
+ with open(DB_PATH, "w") as f:
76
+ json.dump(usage, f)
77
  return Response(resp.content, resp.status_code, resp.headers.items())
78
+ except Exception as e:
79
+ return {"error": "AI Engine starting... please wait."}, 503
80
 
81
  if __name__ == "__main__":
82
  app.run(host="0.0.0.0", port=7860)
83
  EOF
84
 
85
+ # 3. Start Ollama, pull Llama 3.2 (Smartest Model), then start Guard
86
+ CMD ["/bin/sh", "-c", "ollama serve & sleep 15 && ollama pull llama3.2 && python3 /guard.py"]