Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -5,7 +5,6 @@ import socket
|
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
8 |
-
# Your real backend API key
|
9 |
REAL_API_KEY = "sk-kwWVUQPDsLemvilLcyzSSWRzo8sctCzxzlbdN0ZC5ZUCCv0m"
|
10 |
BASE_URL = "https://fast.typegpt.net"
|
11 |
PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"
|
@@ -33,7 +32,7 @@ async def proxy(request: Request, path: str, authorization: str = Header(None)):
|
|
33 |
headers = {
|
34 |
"Authorization": f"Bearer {REAL_API_KEY}",
|
35 |
"Content-Type": request.headers.get("content-type", "application/json"),
|
36 |
-
"Accept": "
|
37 |
"User-Agent": "FastAPI-Proxy"
|
38 |
}
|
39 |
|
@@ -45,17 +44,17 @@ async def proxy(request: Request, path: str, authorization: str = Header(None)):
|
|
45 |
print(f"π Stream mode: {is_stream}")
|
46 |
|
47 |
if is_stream:
|
48 |
-
# Move the client and stream both inside the generator
|
49 |
async def stream_generator():
|
50 |
-
async with httpx.AsyncClient(timeout=
|
51 |
async with client.stream(
|
52 |
method=request.method,
|
53 |
url=target_url,
|
54 |
headers=headers,
|
55 |
content=body
|
56 |
) as upstream_response:
|
57 |
-
async for chunk in upstream_response.
|
58 |
-
|
|
|
59 |
|
60 |
return StreamingResponse(
|
61 |
stream_generator(),
|
@@ -64,7 +63,6 @@ async def proxy(request: Request, path: str, authorization: str = Header(None)):
|
|
64 |
)
|
65 |
|
66 |
else:
|
67 |
-
# Non-stream (normal JSON response)
|
68 |
async with httpx.AsyncClient(timeout=60) as client:
|
69 |
try:
|
70 |
response = await client.request(
|
|
|
5 |
|
6 |
app = FastAPI()
|
7 |
|
|
|
8 |
REAL_API_KEY = "sk-kwWVUQPDsLemvilLcyzSSWRzo8sctCzxzlbdN0ZC5ZUCCv0m"
|
9 |
BASE_URL = "https://fast.typegpt.net"
|
10 |
PUBLIC_AUTH_TOKEN = "TypeGPT-Free4ALL"
|
|
|
32 |
headers = {
|
33 |
"Authorization": f"Bearer {REAL_API_KEY}",
|
34 |
"Content-Type": request.headers.get("content-type", "application/json"),
|
35 |
+
"Accept": "text/event-stream",
|
36 |
"User-Agent": "FastAPI-Proxy"
|
37 |
}
|
38 |
|
|
|
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(
|
50 |
method=request.method,
|
51 |
url=target_url,
|
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 |
)
|
64 |
|
65 |
else:
|
|
|
66 |
async with httpx.AsyncClient(timeout=60) as client:
|
67 |
try:
|
68 |
response = await client.request(
|