Niansuh commited on
Commit
372d7a8
·
verified ·
1 Parent(s): 2963f9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -9
app.py CHANGED
@@ -4,29 +4,28 @@ import requests
4
 
5
  app = Flask(__name__)
6
 
7
- # Load the OpenAI API key from environment variables
8
  API_KEY = os.getenv("OPENAI_API_KEY")
9
- BASE_URL = "https://api.typegpt.net/v1"
 
10
 
11
- # A simple proxy to OpenAI's endpoints
12
  @app.route("/<path:endpoint>", methods=["POST"])
13
  def proxy(endpoint):
14
- # Check for a fake API key from the client
15
  client_key = request.headers.get("Authorization")
16
- if client_key != "Bearer fake-key":
17
  return jsonify({"error": "Unauthorized"}), 401
18
 
19
- # Forward the request to OpenAI with the actual API key
20
  headers = {
21
  "Authorization": f"Bearer {API_KEY}",
22
  "Content-Type": "application/json"
23
  }
24
  data = request.get_json()
25
-
26
- # Make the request to OpenAI API
27
  response = requests.post(f"{BASE_URL}/{endpoint}", headers=headers, json=data)
28
 
29
- # Forward OpenAI's response back to the client
30
  return jsonify(response.json()), response.status_code
31
 
32
  if __name__ == "__main__":
 
4
 
5
  app = Flask(__name__)
6
 
7
+ # Load environment variables
8
  API_KEY = os.getenv("OPENAI_API_KEY")
9
+ FAKE_KEY = os.getenv("FAKE_KEY", "fake-key") # Default to "fake-key" if not set
10
+ BASE_URL = "https://api.openai.com/v1"
11
 
12
+ # Proxy endpoint to OpenAI's API
13
  @app.route("/<path:endpoint>", methods=["POST"])
14
  def proxy(endpoint):
15
+ # Verify the client's fake API key
16
  client_key = request.headers.get("Authorization")
17
+ if client_key != f"Bearer {FAKE_KEY}":
18
  return jsonify({"error": "Unauthorized"}), 401
19
 
20
+ # Forward request to OpenAI
21
  headers = {
22
  "Authorization": f"Bearer {API_KEY}",
23
  "Content-Type": "application/json"
24
  }
25
  data = request.get_json()
 
 
26
  response = requests.post(f"{BASE_URL}/{endpoint}", headers=headers, json=data)
27
 
28
+ # Return the OpenAI response
29
  return jsonify(response.json()), response.status_code
30
 
31
  if __name__ == "__main__":