-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathstream.py
More file actions
280 lines (225 loc) · 8.01 KB
/
Copy pathstream.py
File metadata and controls
280 lines (225 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
from enum import Enum
from typing import (
TYPE_CHECKING,
Any,
AsyncIterator,
Dict,
Iterator,
List,
Optional,
Union,
)
import httpx
from typing_extensions import Unpack
from replicate import identifier
from replicate.exceptions import ReplicateError
from replicate.helpers import transform_output
try:
from pydantic import v1 as pydantic # type: ignore
except ImportError:
import pydantic # type: ignore
if TYPE_CHECKING:
from replicate.client import Client
from replicate.identifier import ModelVersionIdentifier
from replicate.model import Model
from replicate.prediction import Predictions
from replicate.version import Version
class ServerSentEvent(pydantic.BaseModel): # type: ignore
"""
A server-sent event.
"""
class EventType(Enum):
"""
A server-sent event type.
"""
OUTPUT = "output"
LOGS = "logs"
ERROR = "error"
DONE = "done"
event: EventType
data: str
id: str
retry: Optional[int]
def __str__(self) -> str:
if self.event == ServerSentEvent.EventType.OUTPUT:
return self.data
return ""
class EventSource:
"""
A server-sent event source.
"""
client: "Client"
response: "httpx.Response"
use_file_output: bool
def __init__(
self,
client: "Client",
response: "httpx.Response",
*,
use_file_output: Optional[bool] = True,
) -> None:
self.client = client
self.response = response
self.use_file_output = use_file_output or True
content_type, _, _ = response.headers["content-type"].partition(";")
if content_type != "text/event-stream":
raise ValueError(
"Expected response Content-Type to be 'text/event-stream', "
f"got {content_type!r}"
)
class Decoder:
"""
A decoder for server-sent events.
"""
event: Optional["ServerSentEvent.EventType"]
data: List[str]
last_event_id: Optional[str]
retry: Optional[int]
def __init__(self) -> None:
self.event = None
self.data = []
self.last_event_id = None
self.retry = None
def decode(self, line: str) -> Optional[ServerSentEvent]:
"""
Decode a line and return a server-sent event if applicable.
"""
if not line:
if (
not any([self.event, self.data, self.last_event_id, self.retry])
or self.event is None
or self.last_event_id is None
):
return None
sse = ServerSentEvent(
event=self.event,
data="\n".join(self.data),
id=self.last_event_id,
retry=self.retry,
)
self.event = None
self.data = []
self.retry = None
return sse
if line.startswith(":"):
return None
fieldname, _, value = line.partition(":")
value = value[1:] if value.startswith(" ") else value
if fieldname == "event":
if event := ServerSentEvent.EventType(value):
self.event = event
elif fieldname == "data":
self.data.append(value)
elif fieldname == "id":
if "\0" not in value:
self.last_event_id = value
elif fieldname == "retry":
try:
self.retry = int(value)
except (TypeError, ValueError):
pass
return None
def __iter__(self) -> Iterator[ServerSentEvent]:
decoder = EventSource.Decoder()
for line in self.response.iter_lines():
line = line.rstrip("\n")
sse = decoder.decode(line)
if sse is not None:
if sse.event == ServerSentEvent.EventType.ERROR:
raise RuntimeError(sse.data)
if (
self.use_file_output
and sse.event == ServerSentEvent.EventType.OUTPUT
):
sse.data = transform_output(sse.data, client=self.client)
yield sse
if sse.event == ServerSentEvent.EventType.DONE:
return
async def __aiter__(self) -> AsyncIterator[ServerSentEvent]:
decoder = EventSource.Decoder()
async for line in self.response.aiter_lines():
line = line.rstrip("\n")
sse = decoder.decode(line)
if sse is not None:
if sse.event == ServerSentEvent.EventType.ERROR:
raise RuntimeError(sse.data)
if (
self.use_file_output
and sse.event == ServerSentEvent.EventType.OUTPUT
):
sse.data = transform_output(sse.data, client=self.client)
yield sse
if sse.event == ServerSentEvent.EventType.DONE:
return
def stream(
client: "Client",
ref: Union["Model", "Version", "ModelVersionIdentifier", str],
input: Optional[Dict[str, Any]] = None,
*,
use_file_output: Optional[bool] = True,
**params: Unpack["Predictions.CreatePredictionParams"],
) -> Iterator[ServerSentEvent]:
"""
Run a model and stream its output.
"""
params = params or {}
params["stream"] = True
version, owner, name, version_id = identifier._resolve(ref)
if version or version_id:
prediction = client.predictions.create(
version=(version or version_id), input=input or {}, **params
)
elif owner and name:
prediction = client.models.predictions.create(
model=(owner, name), input=input or {}, **params
)
else:
raise ValueError(
f"Invalid argument: {ref}. Expected model, version, or reference in the format owner/name or owner/name:version"
)
url = prediction.urls and prediction.urls.get("stream", None)
if not url or not isinstance(url, str):
raise ReplicateError("Model does not support streaming")
headers = {}
headers["Accept"] = "text/event-stream"
headers["Cache-Control"] = "no-store"
with client._client.stream("GET", url, headers=headers) as response:
yield from EventSource(client, response, use_file_output=use_file_output)
async def async_stream(
client: "Client",
ref: Union["Model", "Version", "ModelVersionIdentifier", str],
input: Optional[Dict[str, Any]] = None,
*,
use_file_output: Optional[bool] = True,
**params: Unpack["Predictions.CreatePredictionParams"],
) -> AsyncIterator[ServerSentEvent]:
"""
Run a model and stream its output asynchronously.
"""
params = params or {}
params["stream"] = True
version, owner, name, version_id = identifier._resolve(ref)
if version or version_id:
prediction = await client.predictions.async_create(
version=(version or version_id), input=input or {}, **params
)
elif owner and name:
prediction = await client.models.predictions.async_create(
model=(owner, name), input=input or {}, **params
)
else:
raise ValueError(
f"Invalid argument: {ref}. Expected model, version, or reference in the format owner/name or owner/name:version"
)
url = prediction.urls and prediction.urls.get("stream", None)
if not url or not isinstance(url, str):
raise ReplicateError("Model does not support streaming")
headers = {}
headers["Accept"] = "text/event-stream"
headers["Cache-Control"] = "no-store"
async with client._async_client.stream("GET", url, headers=headers) as response:
async for event in EventSource(
client, response, use_file_output=use_file_output
):
yield event
__all__ = ["ServerSentEvent"]