Spaces:
Runtime error

Meshyboi commited on
Commit
fbe97a2
Β·
verified Β·
1 Parent(s): 685a08e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -1
app.py CHANGED
@@ -4,6 +4,7 @@ from fastapi.responses import StreamingResponse
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from contextlib import asynccontextmanager
6
  import uvicorn
 
7
  import os
8
  import json
9
  import asyncio
@@ -113,9 +114,31 @@ async def health():
113
  "qdrant": qdrant_status,
114
  }
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  async def stream_pipeline(pipeline, query, ground_truth):
117
  try:
118
- for event in pipeline.run_stream(query, ground_truth):
 
 
119
  yield f"data: {json.dumps(event)}\n\n"
120
  await asyncio.sleep(0.01)
121
  except Exception as e:
 
4
  from fastapi.middleware.cors import CORSMiddleware
5
  from contextlib import asynccontextmanager
6
  import uvicorn
7
+ import anyio
8
  import os
9
  import json
10
  import asyncio
 
114
  "qdrant": qdrant_status,
115
  }
116
 
117
+ _SENTINEL = object()
118
+
119
+ class AsyncIteratorWrapper:
120
+ """Wraps a synchronous iterator using a sentinel to avoid StopIteration leakage."""
121
+ def __init__(self, it):
122
+ self.it = it
123
+ def __aiter__(self):
124
+ return self
125
+ async def __anext__(self):
126
+ def _get_next():
127
+ try:
128
+ return next(self.it)
129
+ except StopIteration:
130
+ return _SENTINEL
131
+
132
+ value = await anyio.to_thread.run_sync(_get_next)
133
+ if value is _SENTINEL:
134
+ raise StopAsyncIteration
135
+ return value
136
+
137
  async def stream_pipeline(pipeline, query, ground_truth):
138
  try:
139
+ # Wrap the synchronous generator with a sentinel-safe async iterator
140
+ async_gen = AsyncIteratorWrapper(pipeline.run_stream(query, ground_truth))
141
+ async for event in async_gen:
142
  yield f"data: {json.dumps(event)}\n\n"
143
  await asyncio.sleep(0.01)
144
  except Exception as e: