multimodalart HF Staff commited on
Commit
77185d8
·
verified ·
1 Parent(s): 5f82f10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -3,6 +3,9 @@ import numpy as np
3
  import spaces
4
  from fastrtc import WebRTC, get_turn_credentials
5
 
 
 
 
6
  @spaces.GPU
7
  def process_frames_on_gpu(frame_stream):
8
  """
@@ -11,6 +14,10 @@ def process_frames_on_gpu(frame_stream):
11
  """
12
  print("🚀 GPU Frame processing loop started.")
13
 
 
 
 
 
14
  # This loop will block until a new frame is available, making it reactive.
15
  for frame in frame_stream:
16
  if frame is not None:
@@ -26,11 +33,11 @@ def process_frames_on_gpu(frame_stream):
26
  with gr.Blocks(theme=gr.themes.Soft(), title="FastRTC ZeroGPU Flipper") as demo:
27
  gr.Markdown("# 🚀 FastRTC Webcam Flipper for ZeroGPU")
28
  gr.Markdown(
29
- "*This version uses Gradio's native stream iterator pattern. The `process_frames_on_gpu` function is a single, decorated process that receives and yields frames, making it ideal for ZeroGPU.*"
30
  )
31
 
32
  with gr.Row():
33
- with gr.Column():
34
  gr.Markdown("### 1. Your Webcam Feed (Input)")
35
  webcam_input = WebRTC(
36
  label="Webcam Input",
@@ -40,8 +47,9 @@ with gr.Blocks(theme=gr.themes.Soft(), title="FastRTC ZeroGPU Flipper") as demo:
40
  height=480,
41
  rtc_configuration=get_turn_credentials(),
42
  )
 
43
 
44
- with gr.Column():
45
  gr.Markdown("### 2. Flipped Video (Output)")
46
  video_output = WebRTC(
47
  label="Flipped Output Stream",
@@ -52,13 +60,13 @@ with gr.Blocks(theme=gr.themes.Soft(), title="FastRTC ZeroGPU Flipper") as demo:
52
  rtc_configuration=get_turn_credentials(),
53
  )
54
 
55
- # When webcam_input is used as a streaming input, the `process_frames_on_gpu`
56
- # function receives an iterator that it can loop over.
57
- webcam_input.stream(
 
58
  fn=process_frames_on_gpu,
59
  inputs=[webcam_input],
60
- outputs=[video_output],
61
- time_limit=120,
62
  )
63
 
64
  if __name__ == "__main__":
 
3
  import spaces
4
  from fastrtc import WebRTC, get_turn_credentials
5
 
6
+ # This is the core processing function that will be assigned to the GPU.
7
+ # It receives a generator (`frame_stream`) that yields frames from the webcam
8
+ # and it yields processed frames back to the output component.
9
  @spaces.GPU
10
  def process_frames_on_gpu(frame_stream):
11
  """
 
14
  """
15
  print("🚀 GPU Frame processing loop started.")
16
 
17
+ if frame_stream is None:
18
+ print("Input stream is None, ending.")
19
+ return
20
+
21
  # This loop will block until a new frame is available, making it reactive.
22
  for frame in frame_stream:
23
  if frame is not None:
 
33
  with gr.Blocks(theme=gr.themes.Soft(), title="FastRTC ZeroGPU Flipper") as demo:
34
  gr.Markdown("# 🚀 FastRTC Webcam Flipper for ZeroGPU")
35
  gr.Markdown(
36
+ "*This version uses a separate button to trigger the stream, correctly using Gradio's streaming iterator pattern for ZeroGPU without conflicting with the `fastrtc` component's internal logic.*"
37
  )
38
 
39
  with gr.Row():
40
+ with gr.Column(scale=1):
41
  gr.Markdown("### 1. Your Webcam Feed (Input)")
42
  webcam_input = WebRTC(
43
  label="Webcam Input",
 
47
  height=480,
48
  rtc_configuration=get_turn_credentials(),
49
  )
50
+ start_button = gr.Button("🚀 Start Processing Stream", variant="primary")
51
 
52
+ with gr.Column(scale=1):
53
  gr.Markdown("### 2. Flipped Video (Output)")
54
  video_output = WebRTC(
55
  label="Flipped Output Stream",
 
60
  rtc_configuration=get_turn_credentials(),
61
  )
62
 
63
+ # By using a button's `click` event to start the stream, we use Gradio's
64
+ # standard streaming API, which correctly provides the iterator to our
65
+ # decorated function without causing a conflict.
66
+ start_button.click(
67
  fn=process_frames_on_gpu,
68
  inputs=[webcam_input],
69
+ outputs=[video_output]
 
70
  )
71
 
72
  if __name__ == "__main__":