CooLLaMACEO commited on
Commit
623bb9d
·
verified ·
1 Parent(s): e61971e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -13,7 +13,7 @@ DB_PATH = "/data/keys.db" if os.path.exists("/data") else "keys.db"
13
 
14
  # --- DATABASE SETUP ---
15
  def init_db():
16
- conn = sqlite3.connect(DB_PATH)
17
  c = conn.cursor()
18
  c.execute('CREATE TABLE IF NOT EXISTS api_keys (key TEXT PRIMARY KEY)')
19
  conn.commit()
@@ -21,50 +21,59 @@ def init_db():
21
 
22
  init_db()
23
 
 
24
  class ChatRequest(BaseModel):
25
  message: str
26
  session_id: str = "default_user"
27
 
28
- # --- KEY GENERATION (The Faucet) ---
29
  @app.get("/generate")
30
  async def generate_public_key():
31
  """Anyone can call this to get a new sk- key"""
32
  new_key = f"sk-{secrets.token_urlsafe(24)}"
33
-
34
- conn = sqlite3.connect(DB_PATH)
35
  c = conn.cursor()
36
  c.execute('INSERT INTO api_keys (key) VALUES (?)', (new_key,))
37
  conn.commit()
38
  conn.close()
39
-
40
  return {
41
- "api_key": new_key,
42
  "instructions": "Add this to your 'X-API-Key' header to use /v1/chat"
43
  }
44
 
45
- # --- PROXY LOGIC ---
46
  async def verify_key(x_api_key: str = Header(None)):
47
  if not x_api_key:
48
  raise HTTPException(status_code=401, detail="Missing X-API-Key header")
49
-
50
- conn = sqlite3.connect(DB_PATH)
51
  c = conn.cursor()
52
  c.execute('SELECT 1 FROM api_keys WHERE key = ?', (x_api_key,))
53
  exists = c.fetchone()
54
  conn.close()
55
-
56
  if not exists:
57
  raise HTTPException(status_code=403, detail="Invalid API Key")
58
  return x_api_key
59
 
 
60
  @app.post("/v1/chat")
61
  async def proxy_chat(payload: ChatRequest, api_key: str = Depends(verify_key)):
62
  try:
63
- response = requests.post(TARGET_SPACE_URL, json=payload.dict(), timeout=120)
 
 
 
 
 
 
 
 
64
  return response.json()
65
- except Exception as e:
66
- return {"error": "The 20B engine is busy or offline."}
 
 
67
 
 
68
  @app.get("/")
69
  def home():
70
- return {"message": "Go to /generate to get a key, then POST to /v1/chat"}
 
13
 
14
  # --- DATABASE SETUP ---
15
  def init_db():
16
+ conn = sqlite3.connect(DB_PATH, check_same_thread=False)
17
  c = conn.cursor()
18
  c.execute('CREATE TABLE IF NOT EXISTS api_keys (key TEXT PRIMARY KEY)')
19
  conn.commit()
 
21
 
22
  init_db()
23
 
24
+ # --- DATA MODELS ---
25
  class ChatRequest(BaseModel):
26
  message: str
27
  session_id: str = "default_user"
28
 
29
+ # --- KEY GENERATION ---
30
  @app.get("/generate")
31
  async def generate_public_key():
32
  """Anyone can call this to get a new sk- key"""
33
  new_key = f"sk-{secrets.token_urlsafe(24)}"
34
+ conn = sqlite3.connect(DB_PATH, check_same_thread=False)
 
35
  c = conn.cursor()
36
  c.execute('INSERT INTO api_keys (key) VALUES (?)', (new_key,))
37
  conn.commit()
38
  conn.close()
 
39
  return {
40
+ "api_key": new_key,
41
  "instructions": "Add this to your 'X-API-Key' header to use /v1/chat"
42
  }
43
 
44
+ # --- KEY VERIFICATION ---
45
  async def verify_key(x_api_key: str = Header(None)):
46
  if not x_api_key:
47
  raise HTTPException(status_code=401, detail="Missing X-API-Key header")
48
+ conn = sqlite3.connect(DB_PATH, check_same_thread=False)
 
49
  c = conn.cursor()
50
  c.execute('SELECT 1 FROM api_keys WHERE key = ?', (x_api_key,))
51
  exists = c.fetchone()
52
  conn.close()
 
53
  if not exists:
54
  raise HTTPException(status_code=403, detail="Invalid API Key")
55
  return x_api_key
56
 
57
+ # --- PROXY CHAT ---
58
  @app.post("/v1/chat")
59
  async def proxy_chat(payload: ChatRequest, api_key: str = Depends(verify_key)):
60
  try:
61
+ # Forward to your 20B backend
62
+ headers = {"Content-Type": "application/json"}
63
+ response = requests.post(
64
+ TARGET_SPACE_URL,
65
+ json=payload.dict(),
66
+ headers=headers,
67
+ timeout=120
68
+ )
69
+ response.raise_for_status() # raise exception on HTTP errors
70
  return response.json()
71
+ except requests.exceptions.Timeout:
72
+ return {"error": "The 20B engine timed out."}
73
+ except requests.exceptions.RequestException as e:
74
+ return {"error": f"The 20B engine is offline or returned an error: {str(e)}"}
75
 
76
+ # --- HOME ---
77
  @app.get("/")
78
  def home():
79
+ return {"message": "Go to /generate to get a key, then POST to /v1/chat"}