forked from livepeer/runner-app-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
195 lines (172 loc) · 6.16 KB
/
Copy pathclient.py
File metadata and controls
195 lines (172 loc) · 6.16 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
#!/usr/bin/env python3
"""comfystream client: reserve a session, drive analyze / start_stream, release.
Publishes video frames into the runner's trickle `in` channel. Analyze collects
text via GET /text; start_stream can also swap the workflow mid-session.
Livepeer integration (grep `# Livepeer:`):
1. reserve_session()
2. post_json / MediaPublish through session.app_url (orch injects session headers)
3. stop_runner_session()
"""
from __future__ import annotations
import argparse
import asyncio
import json
import logging
from contextlib import suppress
from pathlib import Path
from typing import Any
import av
from livepeer_gateway.errors import LivepeerGatewayError
from livepeer_gateway.http import get_json, post_json
from livepeer_gateway.live_runner import stop_runner_session
from livepeer_gateway.media_publish import MediaPublish
from livepeer_gateway.selection import reserve_session
APP_ID = "livepeer-example/comfystream"
DEFAULT_DISCOVERY = "https://localhost:8935/discovery"
DEFAULT_WORKFLOW = (
Path(__file__).resolve().parent / "workflows" / "analyze-stub-api.json"
)
log = logging.getLogger("comfystream-client")
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Run the proxied ComfyStream Live Runner demo."
)
parser.add_argument("input", help="Input video file.")
parser.add_argument("--discovery", default=DEFAULT_DISCOVERY)
parser.add_argument(
"--signer",
default="",
help="Remote signer URL for on-chain path.",
)
parser.add_argument(
"--workflow",
default=str(DEFAULT_WORKFLOW),
help="ComfyUI API-format workflow JSON (default: analyze stub).",
)
parser.add_argument(
"--mode",
choices=("analyze", "stream", "both"),
default="analyze",
help="Which surface to exercise (default: analyze).",
)
parser.add_argument("--max-frames", type=int, default=30)
parser.add_argument("--width", type=int, default=512)
parser.add_argument("--height", type=int, default=512)
parser.add_argument(
"--update-workflow",
default="",
help="Optional second workflow JSON for update_stream.",
)
return parser.parse_args()
def _load_workflow(path: str) -> Any:
return json.loads(Path(path).read_text(encoding="utf-8"))
async def _publish_frames(
publish_url: str,
input_path: str,
*,
max_frames: int,
) -> None:
publisher = MediaPublish(publish_url)
try:
container = av.open(input_path)
sent = 0
for frame in container.decode(video=0):
await publisher.write_frame(frame)
sent += 1
if max_frames and sent >= max_frames:
break
container.close()
log.info("published %d frames to %s", sent, publish_url)
finally:
await publisher.close()
async def _run_analyze(args: argparse.Namespace, workflow: Any) -> None:
session = await reserve_session( # Livepeer: 1
discovery_url=args.discovery,
app=APP_ID,
signer_url=args.signer.strip() or None,
)
try:
async with session:
data = await post_json( # Livepeer: 2
f"{session.app_url.rstrip('/')}/analyze",
{
"prompts": workflow,
"width": args.width,
"height": args.height,
},
timeout=120.0,
)
log.info("analyze started: %s", data)
await _publish_frames(data["in"], args.input, max_frames=args.max_frames)
await asyncio.sleep(2.0)
texts = await get_json(
f"{session.app_url.rstrip('/')}/text",
timeout=30.0,
)
log.info("analyze texts: %s", texts)
except LivepeerGatewayError as exc:
raise SystemExit(f"ERROR: {exc}") from exc
finally:
with suppress(Exception):
await stop_runner_session(session) # Livepeer: 3
async def _run_stream(args: argparse.Namespace, workflow: Any) -> None:
session = await reserve_session( # Livepeer: 1
discovery_url=args.discovery,
app=APP_ID,
signer_url=args.signer.strip() or None,
)
try:
async with session:
data = await post_json( # Livepeer: 2
f"{session.app_url.rstrip('/')}/start_stream",
{
"prompts": workflow,
"width": args.width,
"height": args.height,
},
timeout=120.0,
)
log.info("stream started: %s", data)
publish = asyncio.create_task(
_publish_frames(data["in"], args.input, max_frames=args.max_frames)
)
if args.update_workflow:
await asyncio.sleep(1.0)
update = _load_workflow(args.update_workflow)
updated = await post_json(
f"{session.app_url.rstrip('/')}/update_stream",
{"prompts": update},
timeout=60.0,
)
log.info("update_stream: %s", updated)
await publish
await asyncio.sleep(1.0)
except LivepeerGatewayError as exc:
raise SystemExit(f"ERROR: {exc}") from exc
finally:
with suppress(Exception):
await stop_runner_session(session) # Livepeer: 3
log.info("stream session stopped")
async def _amain() -> int:
args = _parse_args()
input_path = Path(args.input).expanduser()
if not input_path.exists():
raise SystemExit(f"input file does not exist: {input_path}")
args.input = str(input_path)
workflow = _load_workflow(args.workflow)
if args.mode in ("analyze", "both"):
await _run_analyze(args, workflow)
if args.mode in ("stream", "both"):
await _run_stream(args, workflow)
return 0
def main() -> None:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s %(message)s",
)
try:
raise SystemExit(asyncio.run(_amain()))
except KeyboardInterrupt:
raise SystemExit(130) from None
if __name__ == "__main__":
main()