Skip to content

Commit 508f588

Browse files
Merge pull request #1707 from roboflow/feat/switch-modal-to-app-cls
switch modal to app cls
2 parents aa38739 + 19f1e5d commit 508f588

File tree

1 file changed

+27
-21
lines changed
  • inference/core/interfaces/webrtc_worker

1 file changed

+27
-21
lines changed

inference/core/interfaces/webrtc_worker/modal.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@
6868
image=video_processing_image,
6969
)
7070

71-
# https://modal.com/docs/reference/modal.App#function
72-
@app.function(
71+
# https://modal.com/docs/reference/modal.App#cls
72+
@app.cls(
7373
min_containers=WEBRTC_MODAL_FUNCTION_MIN_CONTAINERS,
7474
buffer_containers=WEBRTC_MODAL_FUNCTION_BUFFER_CONTAINERS,
7575
scaledown_window=WEBRTC_MODAL_FUNCTION_SCALEDOWN_WINDOW,
@@ -129,22 +129,25 @@
129129
},
130130
volumes={MODEL_CACHE_DIR: rfcache_volume},
131131
)
132-
def rtc_peer_connection_modal(
133-
webrtc_request: WebRTCWorkerRequest,
134-
q: modal.Queue,
135-
):
136-
logger.info("Received webrtc offer")
132+
class RTCPeerConnectionModal:
133+
@modal.method()
134+
def rtc_peer_connection_modal(
135+
self,
136+
webrtc_request: WebRTCWorkerRequest,
137+
q: modal.Queue,
138+
):
139+
logger.info("Received webrtc offer")
137140

138-
def send_answer(obj: WebRTCWorkerResult):
139-
logger.info("Sending webrtc answer")
140-
q.put(obj)
141+
def send_answer(obj: WebRTCWorkerResult):
142+
logger.info("Sending webrtc answer")
143+
q.put(obj)
141144

142-
asyncio.run(
143-
init_rtc_peer_connection_with_loop(
144-
webrtc_request=webrtc_request,
145-
send_answer=send_answer,
145+
asyncio.run(
146+
init_rtc_peer_connection_with_loop(
147+
webrtc_request=webrtc_request,
148+
send_answer=send_answer,
149+
)
146150
)
147-
)
148151

149152
def spawn_rtc_peer_connection_modal(
150153
webrtc_request: WebRTCWorkerRequest,
@@ -161,19 +164,22 @@ def spawn_rtc_peer_connection_modal(
161164
except modal.exception.NotFoundError:
162165
logger.info("Deploying webrtc modal app %s", WEBRTC_MODAL_APP_NAME)
163166
app.deploy(name=WEBRTC_MODAL_APP_NAME, client=client)
164-
deployed_func = modal.Function.from_name(
165-
app_name=app.name, name=rtc_peer_connection_modal.info.function_name
167+
# https://modal.com/docs/reference/modal.Cls#from_name
168+
deployed_cls = modal.Cls.from_name(
169+
app_name=app.name,
170+
name=RTCPeerConnectionModal.__name__,
166171
)
167-
deployed_func.hydrate(client=client)
172+
deployed_cls.hydrate(client=client)
173+
rtc_modal_obj: RTCPeerConnectionModal = deployed_cls()
168174
# https://modal.com/docs/reference/modal.Queue#ephemeral
169175
with modal.Queue.ephemeral(client=client) as q:
170176
logger.info(
171-
"Spawning webrtc modal function %s into modal app %s",
172-
rtc_peer_connection_modal.info.function_name,
177+
"Spawning webrtc modal function from %s into modal app %s",
178+
RTCPeerConnectionModal.__name__,
173179
app.name,
174180
)
175181
# https://modal.com/docs/reference/modal.Function#spawn
176-
deployed_func.spawn(
182+
rtc_modal_obj.rtc_peer_connection_modal.spawn(
177183
webrtc_request=webrtc_request,
178184
q=q,
179185
)

0 commit comments

Comments
 (0)