from __future__ import annotations import json import os import tempfile from pathlib import Path import gradio as gr from alpha_core import extract_alpha_video def run(video, mode, max_frames): if not video: raise gr.Error("Upload a video first.") work = Path(tempfile.mkdtemp(prefix="simam_alpha_xr_")) webm, preview = work / "simam_alpha_xr.webm", work / "checkerboard_preview.mp4" try: frame, alpha, info = extract_alpha_video(video, str(webm), str(preview), mode, int(max_frames)) except Exception as exc: raise gr.Error(str(exc)) return str(webm), str(preview), frame, alpha, json.dumps(info, indent=2) with gr.Blocks(title="SimamAlphaXR") as demo: gr.Markdown("# SimamAlphaXR\n### Transparent video layers for immersive XR\n\nCreate a foreground-only video that can float inside a virtual environment. This release provides a real VP9 alpha-channel compositor and makes its limitations visible.") with gr.Row(): with gr.Column(): video = gr.Video(label="Input video", sources=["upload", "webcam"], format="mp4") mode = gr.Radio(["Green screen", "Blue screen", "Background contrast baseline"], value="Green screen", label="Matte mode") max_frames = gr.Slider(8, 240, value=96, step=8, label="Maximum processed frames") button = gr.Button("Extract transparent layer", variant="primary") gr.Examples([["examples/sample_subject.mp4"]], inputs=[video], label="Try the included synthetic fixture") with gr.Column(): transparent = gr.File(label="Transparent WebM (VP9 alpha)") preview = gr.Video(label="Checkerboard preview") frame = gr.Image(label="First composited frame") alpha = gr.Image(label="Alpha matte", image_mode="L") diagnostics = gr.Code(label="Diagnostics", language="json") button.click(run, [video, mode, max_frames], [transparent, preview, frame, alpha, diagnostics], api_name="extract_alpha") gr.Markdown("**Roadmap:** Wan-Alpha adapter → temporally stable learned mattes → diffusion-generated transparent clips → WebRTC/WebXR playback. Generated alpha video should be evaluated separately from chroma-key compositing.") if __name__ == "__main__": demo.launch(server_name=os.getenv("GRADIO_SERVER_NAME", "0.0.0.0"), server_port=int(os.getenv("GRADIO_SERVER_PORT", "7860")))