Get more control by directly creating the response object.
import fastapi class Model: def predict(self, inputs) -> fastapi.Response: return fastapi.Response(...)
predict
postprocess
StreamingResponse
import time from starlette.responses import StreamingResponse class Model: def predict(self, model_input): def event_stream(): while True: time.sleep(1) yield f"data: Server Time: {time.strftime('%Y-%m-%d %H:%M:%S')}\n\n" return StreamingResponse(event_stream(), media_type="text/event-stream")
Was this page helpful?