Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import time
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
from fastrtc import WebRTC
|
| 7 |
+
|
| 8 |
+
def video_generation_handler(prompt):
|
| 9 |
+
if seed == -1:
|
| 10 |
+
seed = random.randint(0, 2**32 - 1)
|
| 11 |
+
|
| 12 |
+
random.seed(seed)
|
| 13 |
+
np.random.seed(seed)
|
| 14 |
+
|
| 15 |
+
time.sleep(0.5)
|
| 16 |
+
|
| 17 |
+
num_frames = 60
|
| 18 |
+
width, height = 832, 480
|
| 19 |
+
|
| 20 |
+
for frame_idx in range(num_frames):
|
| 21 |
+
# Create a frame with a random solid color that changes over time
|
| 22 |
+
hue = (frame_idx * 6) % 360 # Cycle through hues
|
| 23 |
+
saturation = 0.7 + 0.3 * np.sin(frame_idx * 0.1) # Vary saturation
|
| 24 |
+
value = 0.8 + 0.2 * np.sin(frame_idx * 0.2) # Vary brightness
|
| 25 |
+
|
| 26 |
+
# Convert HSV to RGB
|
| 27 |
+
import colorsys
|
| 28 |
+
r, g, b = colorsys.hsv_to_rgb(hue/360, saturation, value)
|
| 29 |
+
|
| 30 |
+
# Create frame with some noise for visual interest
|
| 31 |
+
frame_np = np.full((height, width, 3), [r*255, g*255, b*255], dtype=np.uint8)
|
| 32 |
+
|
| 33 |
+
# Add some random noise
|
| 34 |
+
noise = np.random.randint(-20, 20, (height, width, 3), dtype=np.int16)
|
| 35 |
+
frame_np = np.clip(frame_np.astype(np.int16) + noise, 0, 255).astype(np.uint8)
|
| 36 |
+
|
| 37 |
+
# Add some simple animation (moving gradient)
|
| 38 |
+
x_grad = np.linspace(0, 1, width)
|
| 39 |
+
y_grad = np.linspace(0, 1, height)
|
| 40 |
+
X, Y = np.meshgrid(x_grad, y_grad)
|
| 41 |
+
|
| 42 |
+
# Moving wave pattern
|
| 43 |
+
wave = np.sin(X * 4 + frame_idx * 0.2) * np.sin(Y * 4 + frame_idx * 0.3)
|
| 44 |
+
wave_normalized = ((wave + 1) / 2 * 50).astype(np.uint8)
|
| 45 |
+
|
| 46 |
+
# Apply wave to one color channel
|
| 47 |
+
frame_np[:, :, (frame_idx // 10) % 3] = np.clip(
|
| 48 |
+
frame_np[:, :, (frame_idx // 10) % 3].astype(np.int16) + wave_normalized,
|
| 49 |
+
0, 255
|
| 50 |
+
).astype(np.uint8)
|
| 51 |
+
|
| 52 |
+
yield frame_np
|
| 53 |
+
|
| 54 |
+
time.sleep(0.033) #30fps
|
| 55 |
+
|
| 56 |
+
print("🎉 Dummy stream finished!")
|
| 57 |
+
|
| 58 |
+
# --- Gradio UI Layout ---
|
| 59 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Dummy FastRTC Demo") as demo:
|
| 60 |
+
gr.Markdown("# 🚀 Dummy Video Generation with FastRTC Streaming")
|
| 61 |
+
gr.Markdown("*This is a demo version that generates random colored frames instead of actual video content.*")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
with gr.Column(scale=2):
|
| 65 |
+
gr.Markdown("### 1. Configure Generation")
|
| 66 |
+
with gr.Group():
|
| 67 |
+
prompt = gr.Textbox(
|
| 68 |
+
label="Prompt",
|
| 69 |
+
placeholder="Enter any prompt (this is just for demo, colors will be random)",
|
| 70 |
+
lines=3,
|
| 71 |
+
value="A colorful animated demo"
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
start = gr.Button("🚀 Start Generation", variant="primary")
|
| 75 |
+
|
| 76 |
+
with gr.Column(scale=3):
|
| 77 |
+
gr.Markdown("### 2. View Live Stream")
|
| 78 |
+
gr.Markdown("Click 'Start Generation' below to begin streaming random colored frames.")
|
| 79 |
+
|
| 80 |
+
webrtc_output = WebRTC(
|
| 81 |
+
label="Video Stream",
|
| 82 |
+
modality="video",
|
| 83 |
+
mode="receive",
|
| 84 |
+
height=480,
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
webrtc_output.stream(
|
| 88 |
+
fn=video_generation_handler,
|
| 89 |
+
inputs=[prompt],
|
| 90 |
+
outputs=[webrtc_output],
|
| 91 |
+
time_limit=120,
|
| 92 |
+
trigger=start.click
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
demo.queue().launch(share=True)
|