Fix Space SSE stream proxy startup
Browse files- brain_app.py +19 -7
brain_app.py
CHANGED
|
@@ -254,13 +254,25 @@ async def api_stream(model: str, request: Request):
|
|
| 254 |
url = MODELS[_m(model)]["stream"]
|
| 255 |
body = await request.body()
|
| 256 |
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
|
| 265 |
return StreamingResponse(gen(), media_type="text/event-stream",
|
| 266 |
headers={"Cache-Control": "no-cache",
|
|
|
|
| 254 |
url = MODELS[_m(model)]["stream"]
|
| 255 |
body = await request.body()
|
| 256 |
|
| 257 |
+
def gen():
|
| 258 |
+
# Send one SSE comment immediately so HF/Gradio/browser proxies open the
|
| 259 |
+
# response before the upstream Modal stream produces its first token.
|
| 260 |
+
yield b": connected\n\n"
|
| 261 |
+
timeout = httpx.Timeout(600.0, connect=30.0, read=None, write=30.0, pool=30.0)
|
| 262 |
+
headers = {"Content-Type": "application/json", "Accept": "text/event-stream"}
|
| 263 |
+
try:
|
| 264 |
+
with httpx.Client(timeout=timeout, follow_redirects=True) as client:
|
| 265 |
+
with client.stream("POST", url, content=body, headers=headers) as r:
|
| 266 |
+
if r.status_code >= 400:
|
| 267 |
+
msg = json.dumps({"type": "error", "message": f"Upstream stream failed: {r.status_code}"})
|
| 268 |
+
yield f"data: {msg}\n\n".encode()
|
| 269 |
+
return
|
| 270 |
+
for chunk in r.iter_raw():
|
| 271 |
+
if chunk:
|
| 272 |
+
yield chunk
|
| 273 |
+
except Exception as e:
|
| 274 |
+
msg = json.dumps({"type": "error", "message": str(e)})
|
| 275 |
+
yield f"data: {msg}\n\n".encode()
|
| 276 |
|
| 277 |
return StreamingResponse(gen(), media_type="text/event-stream",
|
| 278 |
headers={"Cache-Control": "no-cache",
|