Spaces:
Sleeping
Sleeping
File size: 3,282 Bytes
15f0a61 c46039f cdee8ac 7bf4bdb b36c04e d77e1ff 110262b cdee8ac 110262b 7bf4bdb 110262b 4b481e1 be66e94 c46039f da9ef79 c63cdfe cdee8ac c63cdfe da9ef79 f778066 da9ef79 c63cdfe da9ef79 d77e1ff da9ef79 d77e1ff da9ef79 c46039f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import os
os.environ["MUJOCO_GL"] = "osmesa" # or "egl" if you switch to EGL support
import gradio as gr
from tray_sim import run_tray_simulation
import time # For unique file naming
# Global counter for unique file naming
run_counter = 0
def simulate_and_render(
seed: int,
azimuth: float = 35,
elevation: float = -20,
distance: float = 1.0
):
"""
Runs a TraySim simulation with the specified camera parameters and renders the result.
Parameters:
seed (int): Random seed for simulation initialization, affects object placement and dynamics.
azimuth (float, optional): Horizontal camera angle in degrees. Default is 35.
elevation (float, optional): Vertical camera angle in degrees. Default is -20.
distance (float, optional): Distance of the camera from the scene. Default is 1.0.
Returns:
str: Path to the rendered GIF file showing the simulation from the specified viewpoint.
"""
global run_counter
run_counter += 1
unique_id = f"{seed}_{run_counter}_{int(time.time())}" # Unique identifier for this run
gif_path, physics_state, json_path = run_tray_simulation(
seed=seed,
azimuth=azimuth,
elevation=elevation,
distance=distance,
unique_id=unique_id # Pass unique_id to avoid file conflicts
)
# Optional debug
print("Physics State: ", physics_state)
print("JSON Path: ", json_path)
return gif_path, gif_path, physics_state, json_path
# Define the custom layout using Blocks
with gr.Blocks(
css="""#simulation_image {min-height: 600px; min-width: 700px;}""", # Set minimum size for the image
theme=gr.themes.Default()
) as iface:
gr.Markdown("# TraySim Simulation Viewer")
gr.Markdown("Simulates object drop on a tray using MuJoCo and shows the result as a GIF.")
with gr.Row():
# Input column (30% width)
with gr.Column(scale=3): # scale=3 for 30% width
seed_input = gr.Number(label="Random Seed", value=0)
azimuth_input = gr.Slider(minimum=0, maximum=360, label="Azimuth", value=35)
elevation_input = gr.Slider(minimum=-90, maximum=90, label="Elevation", value=-20)
distance_input = gr.Slider(minimum=0.2, maximum=5.0, label="Zoom (Distance)", value=2.0)
submit_button = gr.Button("Run Simulation")
# Output column (70% width)
with gr.Column(scale=7): # scale=7 for 70% width
output_image = gr.Image(
type="filepath",
label="Simulation",
elem_id="simulation_image", # Assign an ID for CSS targeting
width=700, # Set default width
height=600 # Set default height
)
output_file = gr.File(label="Download Simulation GIF")
state_output = gr.JSON(label="Physics State")
json_file = gr.File(label="Download Physics State JSON")
# Connect the button to the simulation function
submit_button.click(
fn=simulate_and_render,
inputs=[seed_input, azimuth_input, elevation_input, distance_input],
outputs=[output_image, output_file, state_output, json_file]
)
if __name__ == "__main__":
iface.launch() |