mitulagr2 commited on
Commit
6eb1c7e
1 Parent(s): 553dd69

Update main.py

Browse files
Files changed (1) hide show
  1. app/main.py +4 -16
app/main.py CHANGED
@@ -73,33 +73,21 @@ async def websocket_endpoint(websocket: WebSocket, client_id: int):
73
  await manager.broadcast(json.dumps(message))
74
 
75
 
76
- def fake_json_streamer():
77
  t0 = time.time()
78
- for i in range(10):
79
  print(f"Chunk being yielded (time {int((time.time()-t0)*1000)}ms)", flush=True)
80
- yield json.dumps( {"message": "Hello World"}) + '\n'
81
  time.sleep(0.5)
82
  print(f"Over (time {int((time.time()-t0)*1000)}ms)", flush=True)
83
 
84
 
85
- async def astreamer(generator):
86
- try:
87
- print("streaming........")
88
- for i in generator:
89
- print(i)
90
- yield (i)
91
- await asyncio.sleep(.1)
92
- except asyncio.CancelledError as e:
93
- yield ('cancelled')
94
-
95
-
96
  @app.get("/query")
97
  async def process_input(text: str):
98
  if text and len(text.strip()) > 0:
99
  text = text.strip()
100
  streaming_response = session_assistant.ask(text)
101
- return StreamingResponse(fake_json_streamer(), media_type='text/event-stream')
102
- # return StreamingResponse(astreamer(streaming_response.response_gen), media_type='text/event-stream')
103
 
104
 
105
  @app.post("/upload")
 
73
  await manager.broadcast(json.dumps(message))
74
 
75
 
76
+ def astreamer(generator):
77
  t0 = time.time()
78
+ for i in generator:
79
  print(f"Chunk being yielded (time {int((time.time()-t0)*1000)}ms)", flush=True)
80
+ yield i
81
  time.sleep(0.5)
82
  print(f"Over (time {int((time.time()-t0)*1000)}ms)", flush=True)
83
 
84
 
 
 
 
 
 
 
 
 
 
 
 
85
  @app.get("/query")
86
  async def process_input(text: str):
87
  if text and len(text.strip()) > 0:
88
  text = text.strip()
89
  streaming_response = session_assistant.ask(text)
90
+ return StreamingResponse(astreamer(streaming_response.response_gen), media_type='text/event-stream')
 
91
 
92
 
93
  @app.post("/upload")