Spaces:
Sleeping
Sleeping
Disable buffering for chat streaming
Browse files
app.py
CHANGED
|
@@ -236,7 +236,7 @@ def proxy_chat_stream():
|
|
| 236 |
with requests.post(
|
| 237 |
f"{BACKEND_BASE}/chat/stream",
|
| 238 |
json=payload,
|
| 239 |
-
headers={"Authorization": f"Bearer {HF_TOKEN}"},
|
| 240 |
stream=True,
|
| 241 |
timeout=300,
|
| 242 |
) as r:
|
|
@@ -246,13 +246,18 @@ def proxy_chat_stream():
|
|
| 246 |
yield f"[Backend error {r.status_code}]"
|
| 247 |
return
|
| 248 |
|
| 249 |
-
#
|
| 250 |
-
|
|
|
|
| 251 |
if chunk:
|
| 252 |
yield chunk.decode("utf-8")
|
| 253 |
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
|
| 258 |
# ============================================================
|
|
|
|
| 236 |
with requests.post(
|
| 237 |
f"{BACKEND_BASE}/chat/stream",
|
| 238 |
json=payload,
|
| 239 |
+
headers={"Authorization": f"Bearer {HF_TOKEN}", "Accept": "text/plain"},
|
| 240 |
stream=True,
|
| 241 |
timeout=300,
|
| 242 |
) as r:
|
|
|
|
| 246 |
yield f"[Backend error {r.status_code}]"
|
| 247 |
return
|
| 248 |
|
| 249 |
+
# Use iter_content so we DON'T strip newlines.
|
| 250 |
+
# Smaller chunks improve perceived streaming latency.
|
| 251 |
+
for chunk in r.iter_content(chunk_size=128):
|
| 252 |
if chunk:
|
| 253 |
yield chunk.decode("utf-8")
|
| 254 |
|
| 255 |
+
headers = {
|
| 256 |
+
"Cache-Control": "no-cache",
|
| 257 |
+
"X-Accel-Buffering": "no",
|
| 258 |
+
"Connection": "keep-alive",
|
| 259 |
+
}
|
| 260 |
+
return Response(stream_with_context(generate()), mimetype="text/plain", headers=headers)
|
| 261 |
|
| 262 |
|
| 263 |
# ============================================================
|