multimodalart HF Staff commited on
Commit
7e0b27a
·
verified ·
1 Parent(s): 379cab9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -66
app.py CHANGED
@@ -1,85 +1,50 @@
1
- import random
2
- import time
3
  import numpy as np
4
  import gradio as gr
5
- import colorsys
6
  import spaces
7
-
8
  from fastrtc import WebRTC, get_turn_credentials
9
 
10
  @spaces.GPU
11
- def video_generation_handler(prompt):
12
- num_frames = 120
13
- width, height = 832, 480
14
-
15
- for frame_idx in range(num_frames):
16
- hue = (frame_idx * 3) % 360
17
- saturation = 0.7 + 0.3 * np.sin(frame_idx * 0.1)
18
- value = 0.8 + 0.2 * np.sin(frame_idx * 0.2)
19
-
20
- r, g, b = colorsys.hsv_to_rgb(hue/360, saturation, value)
21
-
22
- rgb_frame = np.full((height, width, 3), [r*255, g*255, b*255], dtype=np.uint8)
23
-
24
- noise = np.random.randint(-15, 15, (height, width, 3), dtype=np.int16)
25
- rgb_frame = np.clip(rgb_frame.astype(np.int16) + noise, 0, 255).astype(np.uint8)
26
-
27
- x_grad = np.linspace(0, 1, width)
28
- y_grad = np.linspace(0, 1, height)
29
- X, Y = np.meshgrid(x_grad, y_grad)
30
-
31
- wave = np.sin(X * 4 + frame_idx * 0.2) * np.sin(Y * 4 + frame_idx * 0.3)
32
- wave_normalized = ((wave + 1) / 2 * 40).astype(np.uint8)
33
-
34
- rgb_frame[:, :, (frame_idx // 20) % 3] = np.clip(
35
- rgb_frame[:, :, (frame_idx // 20) % 3].astype(np.int16) + wave_normalized,
36
- 0, 255
37
- ).astype(np.uint8)
38
-
39
- bgr_frame = rgb_frame[:, :, ::-1]
40
-
41
- yield bgr_frame
42
- time.sleep(0.033) # 30 fps
43
-
44
- print("🎉 Dummy stream finished!")
45
 
46
  # --- Gradio UI Layout ---
47
- with gr.Blocks(theme=gr.themes.Soft(), title="Dummy FastRTC Demo") as demo:
48
- gr.Markdown("# 🚀 Dummy Video Generation with FastRTC Streaming")
49
- gr.Markdown("*This is a demo version that generates random colored frames instead of actual video content.*")
50
 
51
  with gr.Row():
52
- with gr.Column(scale=2):
53
- gr.Markdown("### 1. Configure Generation")
54
- with gr.Group():
55
- prompt = gr.Textbox(
56
- label="Prompt",
57
- placeholder="Enter any prompt to change the random seed",
58
- lines=3,
59
- value="A colorful animated demo"
60
- )
61
-
62
- start = gr.Button("🚀 Start Generation", variant="primary")
63
-
64
- with gr.Column(scale=3):
65
- gr.Markdown("### 2. View Live Stream")
66
- gr.Markdown("Click 'Start Generation' to begin streaming frames.")
67
 
68
- webrtc_output = WebRTC(
69
- label="Video Stream",
 
 
 
70
  modality="video",
71
  mode="receive",
72
- height=480,
73
  rtc_configuration=get_turn_credentials(),
74
  )
75
 
76
- webrtc_output.stream(
77
- fn=video_generation_handler,
78
- inputs=[prompt],
79
- outputs=[webrtc_output],
80
- time_limit=120,
81
- trigger=start.click
82
  )
83
 
84
  if __name__ == "__main__":
85
- demo.queue().launch(share=True)
 
 
 
1
  import numpy as np
2
  import gradio as gr
 
3
  import spaces
 
4
  from fastrtc import WebRTC, get_turn_credentials
5
 
6
  @spaces.GPU
7
+ def flip_frame_handler(video_frame):
8
+ """
9
+ Flips the incoming video frame vertically and yields it.
10
+ This needs to be a generator function because the output is a receive-only stream.
11
+ """
12
+ if video_frame:
13
+ yield np.flip(video_frame, axis=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  # --- Gradio UI Layout ---
16
+ with gr.Blocks(theme=gr.themes.Soft(), title="FastRTC Webcam Frame Flipper") as demo:
17
+ gr.Markdown("# 🚀 FastRTC Webcam Frame Flipper (Side-by-Side)")
18
+ gr.Markdown("*This demo takes your webcam feed from the left panel, flips each frame vertically on the server, and streams it back to the right panel in real-time.*")
19
 
20
  with gr.Row():
21
+ with gr.Column():
22
+ gr.Markdown("### 1. Your Webcam Feed (Input)")
23
+ # This component only sends video to the server
24
+ webcam_input = WebRTC(
25
+ label="Webcam Input",
26
+ modality="video",
27
+ mode="send",
28
+ rtc_configuration=get_turn_credentials(),
29
+ )
 
 
 
 
 
 
30
 
31
+ with gr.Column():
32
+ gr.Markdown("### 2. Flipped Video (Output)")
33
+ # This component only receives video from the server
34
+ video_output = WebRTC(
35
+ label="Flipped Output Stream",
36
  modality="video",
37
  mode="receive",
 
38
  rtc_configuration=get_turn_credentials(),
39
  )
40
 
41
+ # The `stream` event is triggered by the input component
42
+ webcam_input.stream(
43
+ fn=flip_frame_handler,
44
+ inputs=[webcam_input], # Input is the webcam feed
45
+ outputs=[video_output], # Output is the separate receive-only component
46
+ time_limit=60,
47
  )
48
 
49
  if __name__ == "__main__":
50
+ demo.queue().launch()