Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,38 +1,61 @@
|
|
| 1 |
from fastapi import FastAPI, Request, Header, HTTPException
|
|
|
|
| 2 |
import httpx
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
|
|
|
|
| 6 |
REAL_API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
|
| 7 |
BASE_URL = "https://fast.typegpt.net"
|
|
|
|
|
|
|
| 8 |
PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"
|
| 9 |
|
| 10 |
@app.api_route("/{path:path}", methods=["GET", "POST"])
|
| 11 |
async def proxy(request: Request, path: str, authorization: str = Header(None)):
|
| 12 |
-
#
|
| 13 |
if not authorization or not authorization.startswith("Bearer "):
|
| 14 |
raise HTTPException(status_code=401, detail="Missing or malformed Authorization header.")
|
| 15 |
-
|
| 16 |
token = authorization.replace("Bearer ", "").strip()
|
| 17 |
|
| 18 |
-
# Check if it's the correct public token
|
| 19 |
if token != PUBLIC_AUTH_TOKEN:
|
| 20 |
raise HTTPException(status_code=401, detail="Invalid Authorization token. Use 'TypeGPT-Free4ALL'.")
|
| 21 |
|
|
|
|
| 22 |
target_url = f"{BASE_URL}/{path}"
|
| 23 |
|
|
|
|
| 24 |
headers = dict(request.headers)
|
| 25 |
headers["Authorization"] = f"Bearer {REAL_API_KEY}"
|
| 26 |
headers.pop("host", None)
|
| 27 |
|
|
|
|
| 28 |
body = await request.body()
|
| 29 |
|
| 30 |
async with httpx.AsyncClient() as client:
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
)
|
| 37 |
-
|
| 38 |
-
return response.json()
|
|
|
|
| 1 |
from fastapi import FastAPI, Request, Header, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse, Response
|
| 3 |
import httpx
|
| 4 |
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
+
# Your internal real API key for TypeGPT (not exposed to users)
|
| 8 |
REAL_API_KEY = "sk-qO9N6kQEEULMWtF4YGVlTTSjIPllEm1h1wfEBzSmnSbxiXwe"
|
| 9 |
BASE_URL = "https://fast.typegpt.net"
|
| 10 |
+
|
| 11 |
+
# Public token users must send in their Authorization header
|
| 12 |
PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"
|
| 13 |
|
| 14 |
@app.api_route("/{path:path}", methods=["GET", "POST"])
|
| 15 |
async def proxy(request: Request, path: str, authorization: str = Header(None)):
|
| 16 |
+
# Validate the Authorization header
|
| 17 |
if not authorization or not authorization.startswith("Bearer "):
|
| 18 |
raise HTTPException(status_code=401, detail="Missing or malformed Authorization header.")
|
| 19 |
+
|
| 20 |
token = authorization.replace("Bearer ", "").strip()
|
| 21 |
|
|
|
|
| 22 |
if token != PUBLIC_AUTH_TOKEN:
|
| 23 |
raise HTTPException(status_code=401, detail="Invalid Authorization token. Use 'TypeGPT-Free4ALL'.")
|
| 24 |
|
| 25 |
+
# Construct URL to forward to the real backend
|
| 26 |
target_url = f"{BASE_URL}/{path}"
|
| 27 |
|
| 28 |
+
# Prepare headers with real API key
|
| 29 |
headers = dict(request.headers)
|
| 30 |
headers["Authorization"] = f"Bearer {REAL_API_KEY}"
|
| 31 |
headers.pop("host", None)
|
| 32 |
|
| 33 |
+
# Forward the request body
|
| 34 |
body = await request.body()
|
| 35 |
|
| 36 |
async with httpx.AsyncClient() as client:
|
| 37 |
+
try:
|
| 38 |
+
response = await client.request(
|
| 39 |
+
method=request.method,
|
| 40 |
+
url=target_url,
|
| 41 |
+
content=body,
|
| 42 |
+
headers=headers,
|
| 43 |
+
timeout=60 # prevent hanging
|
| 44 |
+
)
|
| 45 |
+
except httpx.RequestError as e:
|
| 46 |
+
raise HTTPException(status_code=502, detail=f"Request to backend failed: {e}")
|
| 47 |
+
|
| 48 |
+
# Log response for debugging
|
| 49 |
+
print("TypeGPT Response Status:", response.status_code)
|
| 50 |
+
print("TypeGPT Response Headers:", response.headers)
|
| 51 |
+
print("TypeGPT Response Content:", response.text[:200]) # limit output
|
| 52 |
+
|
| 53 |
+
# Try to return JSON response, fallback to raw
|
| 54 |
+
try:
|
| 55 |
+
return JSONResponse(content=response.json(), status_code=response.status_code)
|
| 56 |
+
except Exception:
|
| 57 |
+
return Response(
|
| 58 |
+
content=response.content,
|
| 59 |
+
status_code=response.status_code,
|
| 60 |
+
media_type=response.headers.get("content-type", "text/plain")
|
| 61 |
)
|
|
|
|
|
|