mitulagr2 commited on
Commit
553dd69
1 Parent(s): 628c689

Update main.py

Browse files
Files changed (1) hide show
  1. app/main.py +8 -4
app/main.py CHANGED
@@ -2,6 +2,7 @@ import os
2
  import shutil
3
  import json
4
  import asyncio
 
5
  from datetime import datetime
6
  from typing import List
7
  from fastapi import FastAPI, UploadFile, WebSocket, WebSocketDisconnect
@@ -72,10 +73,13 @@ async def websocket_endpoint(websocket: WebSocket, client_id: int):
72
  await manager.broadcast(json.dumps(message))
73
 
74
 
75
- async def fake_data_streamer():
 
76
  for i in range(10):
77
- yield b'some fake data\n\n'
78
- await asyncio.sleep(0.5)
 
 
79
 
80
 
81
  async def astreamer(generator):
@@ -94,7 +98,7 @@ async def process_input(text: str):
94
  if text and len(text.strip()) > 0:
95
  text = text.strip()
96
  streaming_response = session_assistant.ask(text)
97
- return StreamingResponse(fake_data_streamer(), media_type='text/event-stream')
98
  # return StreamingResponse(astreamer(streaming_response.response_gen), media_type='text/event-stream')
99
 
100
 
 
2
  import shutil
3
  import json
4
  import asyncio
5
+ import time
6
  from datetime import datetime
7
  from typing import List
8
  from fastapi import FastAPI, UploadFile, WebSocket, WebSocketDisconnect
 
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):
 
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