incognitolm commited on
Commit
6f9206a
·
1 Parent(s): b09e986

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -44
app.py CHANGED
@@ -1093,12 +1093,73 @@ async def websocket_chat(ws: WebSocket):
1093
  # Internal endpoint (avoids Hugging Face proxy redirect)
1094
  internal_url = "http://127.0.0.1:7860/gen/chat/completions"
1095
 
1096
- # Persistent HTTP client for streaming
1097
- async with httpx.AsyncClient(
1098
- timeout=None,
1099
- follow_redirects=False
1100
- ) as client:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1101
 
 
 
 
 
 
1102
  while True:
1103
  try:
1104
  msg = await ws.receive_text()
@@ -1118,46 +1179,15 @@ async def websocket_chat(ws: WebSocket):
1118
  await ws.send_json({"error": "Missing body"})
1119
  continue
1120
 
1121
- try:
1122
- async with client.stream(
1123
- "POST",
1124
- internal_url,
1125
- json=body,
1126
- headers=headers
1127
- ) as response:
1128
 
1129
- # Handle upstream errors
1130
- if response.status_code >= 400:
1131
- error_text = ""
1132
- try:
1133
- error_text = (await response.aread()).decode(
1134
- "utf-8", errors="replace"
1135
- )[:500]
1136
- except Exception:
1137
- pass
1138
-
1139
- await ws.send_json({
1140
- "error": "Upstream request failed",
1141
- "status": response.status_code,
1142
- "detail": error_text
1143
- })
1144
- continue
1145
-
1146
- # Stream tokens/lines to the websocket
1147
- async for line in response.aiter_lines():
1148
- if not line:
1149
- continue
1150
-
1151
- try:
1152
- await ws.send_text(line)
1153
- except RuntimeError:
1154
- break
1155
-
1156
- except Exception as stream_error:
1157
- await ws.send_json({
1158
- "error": "Streaming error",
1159
- "detail": str(stream_error)
1160
- })
1161
 
1162
  except WebSocketDisconnect:
1163
  return
 
1093
  # Internal endpoint (avoids Hugging Face proxy redirect)
1094
  internal_url = "http://127.0.0.1:7860/gen/chat/completions"
1095
 
1096
+ # Queue to ensure sequential request processing
1097
+ import asyncio
1098
+ request_queue = asyncio.Queue()
1099
+
1100
+ async def process_requests():
1101
+ # Persistent HTTP client for streaming
1102
+ async with httpx.AsyncClient(
1103
+ timeout=None,
1104
+ follow_redirects=False
1105
+ ) as client:
1106
+ while True:
1107
+ try:
1108
+ request_id, body, headers = await request_queue.get()
1109
+ except asyncio.CancelledError:
1110
+ break
1111
+
1112
+ try:
1113
+ async with client.stream(
1114
+ "POST",
1115
+ internal_url,
1116
+ json=body,
1117
+ headers=headers
1118
+ ) as response:
1119
+
1120
+ # Handle upstream errors
1121
+ if response.status_code >= 400:
1122
+ error_text = ""
1123
+ try:
1124
+ error_text = (await response.aread()).decode(
1125
+ "utf-8", errors="replace"
1126
+ )[:500]
1127
+ except Exception:
1128
+ pass
1129
+
1130
+ await ws.send_json({
1131
+ "id": request_id,
1132
+ "error": "Upstream request failed",
1133
+ "status": response.status_code,
1134
+ "detail": error_text
1135
+ })
1136
+ continue
1137
+
1138
+ # Stream tokens/lines to the websocket
1139
+ async for line in response.aiter_lines():
1140
+ if not line:
1141
+ continue
1142
+
1143
+ try:
1144
+ await ws.send_text(f"{request_id}:{line}")
1145
+ except RuntimeError:
1146
+ return
1147
+
1148
+ except Exception as stream_error:
1149
+ try:
1150
+ await ws.send_json({
1151
+ "id": request_id,
1152
+ "error": "Streaming error",
1153
+ "detail": str(stream_error)
1154
+ })
1155
+ except Exception:
1156
+ pass
1157
 
1158
+ # Start the request processor task
1159
+ processor_task = asyncio.create_task(process_requests())
1160
+
1161
+ try:
1162
+ request_counter = 0
1163
  while True:
1164
  try:
1165
  msg = await ws.receive_text()
 
1179
  await ws.send_json({"error": "Missing body"})
1180
  continue
1181
 
1182
+ request_counter += 1
1183
+ await request_queue.put((request_counter, body, headers))
 
 
 
 
 
1184
 
1185
+ finally:
1186
+ processor_task.cancel()
1187
+ try:
1188
+ await processor_task
1189
+ except asyncio.CancelledError:
1190
+ pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1191
 
1192
  except WebSocketDisconnect:
1193
  return