Spaces:
Running
on
L4
Running
on
L4
Add logging
Browse files- app.py +15 -4
- requirements.txt +2 -1
app.py
CHANGED
@@ -12,12 +12,13 @@ The system uses FastRTC for video streaming and Gradio for the web interface.
|
|
12 |
|
13 |
import os
|
14 |
import cv2
|
15 |
-
import uuid
|
16 |
import time
|
17 |
import torch
|
|
|
18 |
import gradio as gr
|
19 |
import numpy as np
|
20 |
|
|
|
21 |
from gradio.utils import get_space
|
22 |
from fastrtc import (
|
23 |
Stream,
|
@@ -163,10 +164,17 @@ class RunningResult:
|
|
163 |
return self.predictions[-1][1] if self.predictions else "Starting..."
|
164 |
|
165 |
|
166 |
-
def process_frames(image: np.ndarray, frames_state: list, result_state: list):
|
167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
# Initialize frames cache if not exists (and put in gradio state)
|
169 |
if not frames_state:
|
|
|
170 |
running_frames_cache = RunningFramesCache(
|
171 |
save_every_k_frame=128 / frames_per_clip,
|
172 |
max_frames=frames_per_clip,
|
@@ -177,6 +185,7 @@ def process_frames(image: np.ndarray, frames_state: list, result_state: list):
|
|
177 |
|
178 |
# Initialize result cache if not exists (and put in gradio state)
|
179 |
if not result_state:
|
|
|
180 |
running_result = RunningResult(4)
|
181 |
result_state.append(running_result)
|
182 |
else:
|
@@ -205,6 +214,7 @@ def process_frames(image: np.ndarray, frames_state: list, result_state: list):
|
|
205 |
# Get top prediction
|
206 |
top_index = logits.argmax(dim=-1).item()
|
207 |
class_name = model.config.id2label[top_index]
|
|
|
208 |
running_result.add_prediction(class_name)
|
209 |
|
210 |
# Get formatted predictions and last prediction
|
@@ -220,13 +230,14 @@ async def get_credentials():
|
|
220 |
|
221 |
frames_cache = gr.State([])
|
222 |
result_cache = gr.State([])
|
|
|
223 |
|
224 |
# Initialize the video stream with processing callback
|
225 |
stream = Stream(
|
226 |
handler=VideoStreamHandler(process_frames, skip_frames=True),
|
227 |
modality="video",
|
228 |
mode="send-receive",
|
229 |
-
additional_inputs=[frames_cache, result_cache],
|
230 |
additional_outputs=[gr.TextArea(label="Actions", value="", lines=5)],
|
231 |
additional_outputs_handler=lambda _, output: output,
|
232 |
rtc_configuration=get_credentials if get_space() else None,
|
|
|
12 |
|
13 |
import os
|
14 |
import cv2
|
|
|
15 |
import time
|
16 |
import torch
|
17 |
+
import random
|
18 |
import gradio as gr
|
19 |
import numpy as np
|
20 |
|
21 |
+
from loguru import logger
|
22 |
from gradio.utils import get_space
|
23 |
from fastrtc import (
|
24 |
Stream,
|
|
|
164 |
return self.predictions[-1][1] if self.predictions else "Starting..."
|
165 |
|
166 |
|
167 |
+
def process_frames(image: np.ndarray, frames_state: list, result_state: list, session_cache: list):
|
168 |
+
|
169 |
+
if not session_cache:
|
170 |
+
session_id = random.randint(1, 1000)
|
171 |
+
session_cache.append(session_id)
|
172 |
+
else:
|
173 |
+
session_id = session_cache[0]
|
174 |
+
|
175 |
# Initialize frames cache if not exists (and put in gradio state)
|
176 |
if not frames_state:
|
177 |
+
logger.info(f"({session_id}) initialized frames cache")
|
178 |
running_frames_cache = RunningFramesCache(
|
179 |
save_every_k_frame=128 / frames_per_clip,
|
180 |
max_frames=frames_per_clip,
|
|
|
185 |
|
186 |
# Initialize result cache if not exists (and put in gradio state)
|
187 |
if not result_state:
|
188 |
+
logger.info(f"({session_id}) initialized result cache")
|
189 |
running_result = RunningResult(4)
|
190 |
result_state.append(running_result)
|
191 |
else:
|
|
|
214 |
# Get top prediction
|
215 |
top_index = logits.argmax(dim=-1).item()
|
216 |
class_name = model.config.id2label[top_index]
|
217 |
+
logger.info(f"({session_id}) action: '{class_name}'")
|
218 |
running_result.add_prediction(class_name)
|
219 |
|
220 |
# Get formatted predictions and last prediction
|
|
|
230 |
|
231 |
frames_cache = gr.State([])
|
232 |
result_cache = gr.State([])
|
233 |
+
session_id = gr.State([])
|
234 |
|
235 |
# Initialize the video stream with processing callback
|
236 |
stream = Stream(
|
237 |
handler=VideoStreamHandler(process_frames, skip_frames=True),
|
238 |
modality="video",
|
239 |
mode="send-receive",
|
240 |
+
additional_inputs=[frames_cache, result_cache, session_id],
|
241 |
additional_outputs=[gr.TextArea(label="Actions", value="", lines=5)],
|
242 |
additional_outputs_handler=lambda _, output: output,
|
243 |
rtc_configuration=get_credentials if get_space() else None,
|
requirements.txt
CHANGED
@@ -3,4 +3,5 @@ transformers @ git+https://github.com/huggingface/transformers
|
|
3 |
torch
|
4 |
torchvision
|
5 |
opencv-python-headless
|
6 |
-
fastrtc>=0.0.28
|
|
|
|
3 |
torch
|
4 |
torchvision
|
5 |
opencv-python-headless
|
6 |
+
fastrtc>=0.0.28
|
7 |
+
loguru
|