Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sqlite3
|
| 3 |
+
import secrets
|
| 4 |
+
import requests
|
| 5 |
+
from fastapi import FastAPI, HTTPException, Header, Depends
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
|
| 8 |
+
app = FastAPI(title="ChatGPT OS Key Faucet")
|
| 9 |
+
|
| 10 |
+
# --- CONFIGURATION ---
|
| 11 |
+
TARGET_SPACE_URL = "https://coollamaceo-chatgptopensource1-0.hf.space/chat"
|
| 12 |
+
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()
|
| 20 |
+
conn.close()
|
| 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"}
|