Update app.py
Browse files
app.py
CHANGED
@@ -4,29 +4,28 @@ import requests
|
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
-
# Load
|
8 |
API_KEY = os.getenv("OPENAI_API_KEY")
|
9 |
-
|
|
|
10 |
|
11 |
-
#
|
12 |
@app.route("/<path:endpoint>", methods=["POST"])
|
13 |
def proxy(endpoint):
|
14 |
-
#
|
15 |
client_key = request.headers.get("Authorization")
|
16 |
-
if client_key != "Bearer
|
17 |
return jsonify({"error": "Unauthorized"}), 401
|
18 |
|
19 |
-
# Forward
|
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 |
-
#
|
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__":
|