import tempfile import os import spaces from typing import Optional from ..data.loader import load_simulation_data from .logger import SimulationLogger import rerun as rr @spaces.GPU def visualize_simulation(file, simulation_index: int) -> Optional[str]: if file is None: return None try: simulations, _ = load_simulation_data(file) if simulations is None or simulation_index >= len(simulations): return None # Create temporary file for RRD temp_dir = tempfile.mkdtemp() rrd_path = os.path.join(temp_dir, "simulation.rrd") # Log selected simulation simulation = simulations[simulation_index] logger = SimulationLogger() logger.log_metadata(simulation['instructions']) logger.log_subjects( simulation['subjects'], simulation.get('selectedSubject')) logger.log_camera_trajectory(simulation['cameraFrames']) logger.log_camera_frames(simulation['cameraFrames']) rr.save(rrd_path) return rrd_path except Exception as e: print(f"Error processing simulation: {str(e)}") return None