Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -76,15 +76,15 @@ async def get_completion(payload: Payload, request: Request):
|
|
76 |
# Prepare the payload for streaming
|
77 |
payload_dict = {**payload.dict(), "stream": True}
|
78 |
|
79 |
-
# Define an asynchronous generator to stream the response
|
80 |
async def stream_generator():
|
81 |
async with httpx.AsyncClient() as client:
|
82 |
try:
|
83 |
async with client.stream("POST", secret_api_endpoint, json=payload_dict, timeout=10) as response:
|
84 |
response.raise_for_status()
|
85 |
-
async for
|
86 |
-
if
|
87 |
-
yield
|
88 |
except httpx.RequestError as e:
|
89 |
raise HTTPException(status_code=500, detail=f"Streaming failed: {e}")
|
90 |
|
|
|
76 |
# Prepare the payload for streaming
|
77 |
payload_dict = {**payload.dict(), "stream": True}
|
78 |
|
79 |
+
# Define an asynchronous generator to stream the response line by line
|
80 |
async def stream_generator():
|
81 |
async with httpx.AsyncClient() as client:
|
82 |
try:
|
83 |
async with client.stream("POST", secret_api_endpoint, json=payload_dict, timeout=10) as response:
|
84 |
response.raise_for_status()
|
85 |
+
async for line in response.aiter_lines():
|
86 |
+
if line:
|
87 |
+
yield f"{line}\n" # Add a newline to distinguish each line
|
88 |
except httpx.RequestError as e:
|
89 |
raise HTTPException(status_code=500, detail=f"Streaming failed: {e}")
|
90 |
|