aaxaxax commited on
Commit
741450f
·
1 Parent(s): cdb6e97

Use env vars MASTER_API_KEY, OLLAMA_KEY_1/2/3

Browse files
Files changed (1) hide show
  1. app.py +24 -15
app.py CHANGED
@@ -1,34 +1,40 @@
1
  import os
2
- import random
3
  import httpx
4
  from fastapi import FastAPI, Request, HTTPException
5
  from fastapi.responses import JSONResponse, Response
6
 
7
  app = FastAPI()
8
 
9
- BASE_URL = "https://ollama.com"
10
- MASTER_KEY = "ollama-proxy-free"
 
11
 
12
- KEYS = [
13
- "8ca25de51e554c099962b78b7ce0c9e9.Mp5dnqctR2zzq3g-NO_M-cjW",
14
- "dbd1d0c534964684a6d4678348ab8d30.ieDfmSYVnf0MmTjR-AIdNrW9",
15
- "37e81a6be4104fbfbfbe2ecf557a2c10.GoIbzpHebdM9ZcHarQ9A12Cp"
16
- ]
 
17
 
18
  @app.get("/")
19
  def root():
20
- return {"status": "ok", "master": MASTER_KEY[:10]}
 
 
 
 
21
 
22
  @app.post("/v1/chat/completions")
23
  async def chat(req: Request):
24
  # Auth
25
  auth_key = req.headers.get("Authorization", "").replace("Bearer ", "")
26
- if auth_key != MASTER_KEY:
27
- raise HTTPException(401, "Unauthorized")
28
 
29
  body = await req.json()
30
 
31
- for key in KEYS:
 
32
  try:
33
  async with httpx.AsyncClient(timeout=60) as client:
34
  resp = await client.post(
@@ -36,17 +42,20 @@ async def chat(req: Request):
36
  json=body,
37
  headers={"Authorization": f"Bearer {key}"}
38
  )
39
-
40
  if resp.status_code == 200:
41
  return Response(resp.content, status_code=200)
42
  except Exception as e:
43
- print(f"ERR {key[:6]}: {e}")
44
 
45
  return JSONResponse({"error": "all keys failed"}, status_code=500)
46
 
47
  @app.get("/v1/models")
48
  def models():
49
- for key in KEYS:
 
 
 
 
50
  try:
51
  resp = httpx.get(
52
  f"{BASE_URL}/v1/models",
 
1
  import os
 
2
  import httpx
3
  from fastapi import FastAPI, Request, HTTPException
4
  from fastapi.responses import JSONResponse, Response
5
 
6
  app = FastAPI()
7
 
8
+ # Environment variables
9
+ BASE_URL = os.getenv("BASE_URL", "https://ollama.com")
10
+ MASTER_API_KEY = os.getenv("MASTER_API_KEY", "ollama-proxy-free")
11
 
12
+ # Load Ollama keys from env
13
+ OLLAMA_KEYS = []
14
+ for i in range(1, 10):
15
+ key = os.getenv(f"OLLAMA_KEY_{i}")
16
+ if key:
17
+ OLLAMA_KEYS.append(key)
18
 
19
  @app.get("/")
20
  def root():
21
+ return {
22
+ "status": "ok",
23
+ "master_key": MASTER_API_KEY[:10] + "...",
24
+ "ollama_keys": len(OLLAMA_KEYS)
25
+ }
26
 
27
  @app.post("/v1/chat/completions")
28
  async def chat(req: Request):
29
  # Auth
30
  auth_key = req.headers.get("Authorization", "").replace("Bearer ", "")
31
+ if auth_key != MASTER_API_KEY:
32
+ return JSONResponse({"error": "Unauthorized"}, status_code=401)
33
 
34
  body = await req.json()
35
 
36
+ # Try each key
37
+ for key in OLLAMA_KEYS:
38
  try:
39
  async with httpx.AsyncClient(timeout=60) as client:
40
  resp = await client.post(
 
42
  json=body,
43
  headers={"Authorization": f"Bearer {key}"}
44
  )
 
45
  if resp.status_code == 200:
46
  return Response(resp.content, status_code=200)
47
  except Exception as e:
48
+ continue
49
 
50
  return JSONResponse({"error": "all keys failed"}, status_code=500)
51
 
52
  @app.get("/v1/models")
53
  def models():
54
+ auth_key = req.headers.get("Authorization", "").replace("Bearer ", "")
55
+ if auth_key != MASTER_API_KEY:
56
+ return JSONResponse({"error": "Unauthorized"}, status_code=401)
57
+
58
+ for key in OLLAMA_KEYS:
59
  try:
60
  resp = httpx.get(
61
  f"{BASE_URL}/v1/models",