AIMaster7 commited on
Commit
aecceec
·
verified ·
1 Parent(s): 9a0435e

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +16 -14
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,14 +22,16 @@ 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}",
34
  "Content-Type": request.headers.get("content-type", "application/json"),
@@ -46,22 +49,21 @@ async def proxy(request: Request, path: str, authorization: str = Header(None)):
46
  async with httpx.AsyncClient(timeout=60) as client:
47
  try:
48
  if is_stream:
49
- async with client.stream(
50
- method=request.method,
51
- url=target_url,
52
- headers=headers,
53
- content=body
54
- ) as upstream_response:
55
-
56
- async def stream_generator():
57
  async for chunk in upstream_response.aiter_bytes():
58
  yield chunk
59
 
60
- return StreamingResponse(
61
- stream_generator(),
62
- status_code=upstream_response.status_code,
63
- media_type="text/event-stream"
64
- )
65
  else:
66
  response = await client.request(
67
  method=request.method,
 
5
 
6
  app = FastAPI()
7
 
8
+ # Replace this with your actual backend 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 token
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
  target_url = f"{BASE_URL}/{path}"
34
+
35
  headers = {
36
  "Authorization": f"Bearer {REAL_API_KEY}",
37
  "Content-Type": request.headers.get("content-type", "application/json"),
 
49
  async with httpx.AsyncClient(timeout=60) as client:
50
  try:
51
  if is_stream:
52
+ async def stream_generator():
53
+ async with client.stream(
54
+ method=request.method,
55
+ url=target_url,
56
+ headers=headers,
57
+ content=body
58
+ ) as upstream_response:
 
59
  async for chunk in upstream_response.aiter_bytes():
60
  yield chunk
61
 
62
+ return StreamingResponse(
63
+ stream_generator(),
64
+ status_code=200,
65
+ media_type="text/event-stream"
66
+ )
67
  else:
68
  response = await client.request(
69
  method=request.method,