AIMaster7 commited on
Commit
ef4717a
Β·
verified Β·
1 Parent(s): e85b9cc

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +8 -4
main.py CHANGED
@@ -5,6 +5,7 @@ import socket
5
 
6
  app = FastAPI()
7
 
 
8
  REAL_API_KEY = "sk-kwWVUQPDsLemvilLcyzSSWRzo8sctCzxzlbdN0ZC5ZUCCv0m"
9
  BASE_URL = "https://fast.typegpt.net"
10
  PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"
@@ -21,13 +22,15 @@ async def startup_event():
21
 
22
  @app.api_route("/{path:path}", methods=["GET", "POST"])
23
  async def proxy(request: Request, path: str, authorization: str = Header(None)):
 
24
  if not authorization or not authorization.startswith("Bearer "):
25
  raise HTTPException(status_code=401, detail="Missing or malformed Authorization header.")
26
-
27
  token = authorization.replace("Bearer ", "").strip()
28
  if token != PUBLIC_AUTH_TOKEN:
29
  raise HTTPException(status_code=401, detail="Invalid Authorization token. Use 'TypeGPT-Free4ALL'.")
30
 
 
31
  target_url = f"{BASE_URL}/{path}"
32
  headers = {
33
  "Authorization": f"Bearer {REAL_API_KEY}",
@@ -44,6 +47,7 @@ async def proxy(request: Request, path: str, authorization: str = Header(None)):
44
  print(f"πŸ” Stream mode: {is_stream}")
45
 
46
  if is_stream:
 
47
  async def stream_generator():
48
  async with httpx.AsyncClient(timeout=None) as client:
49
  async with client.stream(
@@ -52,9 +56,8 @@ async def proxy(request: Request, path: str, authorization: str = Header(None)):
52
  headers=headers,
53
  content=body
54
  ) as upstream_response:
55
- async for chunk in upstream_response.aiter_lines():
56
- if chunk.strip(): # skip blank lines
57
- yield f"{chunk}\n"
58
 
59
  return StreamingResponse(
60
  stream_generator(),
@@ -63,6 +66,7 @@ async def proxy(request: Request, path: str, authorization: str = Header(None)):
63
  )
64
 
65
  else:
 
66
  async with httpx.AsyncClient(timeout=60) as client:
67
  try:
68
  response = await client.request(
 
5
 
6
  app = FastAPI()
7
 
8
+ # Your real TypeGPT API key
9
  REAL_API_KEY = "sk-kwWVUQPDsLemvilLcyzSSWRzo8sctCzxzlbdN0ZC5ZUCCv0m"
10
  BASE_URL = "https://fast.typegpt.net"
11
  PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"
 
22
 
23
  @app.api_route("/{path:path}", methods=["GET", "POST"])
24
  async def proxy(request: Request, path: str, authorization: str = Header(None)):
25
+ # Validate Authorization header
26
  if not authorization or not authorization.startswith("Bearer "):
27
  raise HTTPException(status_code=401, detail="Missing or malformed Authorization header.")
28
+
29
  token = authorization.replace("Bearer ", "").strip()
30
  if token != PUBLIC_AUTH_TOKEN:
31
  raise HTTPException(status_code=401, detail="Invalid Authorization token. Use 'TypeGPT-Free4ALL'.")
32
 
33
+ # Build backend request
34
  target_url = f"{BASE_URL}/{path}"
35
  headers = {
36
  "Authorization": f"Bearer {REAL_API_KEY}",
 
47
  print(f"πŸ” Stream mode: {is_stream}")
48
 
49
  if is_stream:
50
+ # Stream response immediately, as chunks arrive
51
  async def stream_generator():
52
  async with httpx.AsyncClient(timeout=None) as client:
53
  async with client.stream(
 
56
  headers=headers,
57
  content=body
58
  ) as upstream_response:
59
+ async for chunk in upstream_response.aiter_raw():
60
+ yield chunk # raw bytes streamed directly
 
61
 
62
  return StreamingResponse(
63
  stream_generator(),
 
66
  )
67
 
68
  else:
69
+ # Normal JSON response
70
  async with httpx.AsyncClient(timeout=60) as client:
71
  try:
72
  response = await client.request(