import random import threading import psutil import fastapi import gradio as gr import uvicorn from viser_proxy_manager import ViserProxyManager from vis_st4rtrack import visualize_st4rtrack, load_trajectory_data, log_memory_usage # Global cache for loaded data global_data_cache = None def check_ram_usage(threshold_percent=90): """Check if RAM usage is above the threshold. Args: threshold_percent: Maximum RAM usage percentage allowed Returns: bool: True if RAM usage is below threshold, False otherwise """ ram_percent = psutil.virtual_memory().percent print(f"Current RAM usage: {ram_percent}%") return ram_percent < threshold_percent def main() -> None: # Load data once at startup using the function from vis_st4rtrack.py global global_data_cache global_data_cache = load_trajectory_data(use_float16=True, max_frames=69, traj_path="480p_train", mask_folder="train", conf_thre_percentile=3) app = fastapi.FastAPI() viser_manager = ViserProxyManager(app) # Create a Gradio interface with title, iframe, and buttons with gr.Blocks(title="Viser Viewer") as demo: # Add the iframe with a border iframe_html = gr.HTML("") status_text = gr.Markdown("") # Add status text component @demo.load(outputs=[iframe_html, status_text]) def start_server(request: gr.Request): assert request.session_hash is not None # Check RAM usage before starting visualization if not check_ram_usage(threshold_percent=100): return """
Please try again later when resources are available.