Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,32 @@
|
|
| 1 |
-
from flask import Flask, request,
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
# Load environment variables
|
| 8 |
-
API_KEY = os.getenv("
|
| 9 |
FAKE_KEY = os.getenv("FAKE_KEY", "fake-key") # Default to "fake-key" if not set
|
| 10 |
BASE_URL = "https://api.typegpt.net"
|
| 11 |
|
| 12 |
-
# Proxy endpoint to
|
| 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
|
| 19 |
|
| 20 |
-
# Forward request to
|
| 21 |
headers = {
|
| 22 |
"Authorization": f"Bearer {API_KEY}",
|
| 23 |
"Content-Type": "application/json"
|
| 24 |
}
|
| 25 |
-
data = request.
|
| 26 |
-
response = requests.post(f"{BASE_URL}/{endpoint}", headers=headers,
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
response_json = response.json()
|
| 31 |
-
except requests.exceptions.JSONDecodeError:
|
| 32 |
-
# Log the raw response text for debugging
|
| 33 |
-
print("Non-JSON response:", response.text)
|
| 34 |
-
return jsonify({"error": "Invalid response from OpenAI API", "details": response.text}), response.status_code
|
| 35 |
-
|
| 36 |
-
# Return the OpenAI response if it's JSON
|
| 37 |
-
return jsonify(response_json), response.status_code
|
| 38 |
|
| 39 |
if __name__ == "__main__":
|
| 40 |
app.run(host="0.0.0.0", port=5000)
|
|
|
|
| 1 |
+
from flask import Flask, request, Response
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
| 7 |
# Load environment variables
|
| 8 |
+
API_KEY = os.getenv("TYPEGPT_API_KEY")
|
| 9 |
FAKE_KEY = os.getenv("FAKE_KEY", "fake-key") # Default to "fake-key" if not set
|
| 10 |
BASE_URL = "https://api.typegpt.net"
|
| 11 |
|
| 12 |
+
# Proxy endpoint to TypeGPT'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 Response("{\"error\": \"Unauthorized\"}", status=401, mimetype="application/json")
|
| 19 |
|
| 20 |
+
# Forward the request to TypeGPT
|
| 21 |
headers = {
|
| 22 |
"Authorization": f"Bearer {API_KEY}",
|
| 23 |
"Content-Type": "application/json"
|
| 24 |
}
|
| 25 |
+
data = request.get_data() # Get raw data from the client request
|
| 26 |
+
response = requests.post(f"{BASE_URL}/{endpoint}", headers=headers, data=data)
|
| 27 |
|
| 28 |
+
# Forward the raw response content and headers
|
| 29 |
+
return Response(response.content, status=response.status_code, headers=dict(response.headers))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
if __name__ == "__main__":
|
| 32 |
app.run(host="0.0.0.0", port=5000)
|