|
|
import os |
|
|
os.environ["MUJOCO_GL"] = "osmesa" |
|
|
|
|
|
import gradio as gr |
|
|
from tray_sim import run_tray_simulation |
|
|
|
|
|
import time |
|
|
|
|
|
|
|
|
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())}" |
|
|
|
|
|
gif_path, stability_flags, physics_state, llm_friendly_output, json_path = run_tray_simulation( |
|
|
seed=seed, |
|
|
azimuth=azimuth, |
|
|
elevation=elevation, |
|
|
distance=distance, |
|
|
unique_id=unique_id |
|
|
) |
|
|
|
|
|
|
|
|
print("Stability: ", stability_flags) |
|
|
print("Physics State: ", physics_state) |
|
|
print("LLM Output: ", llm_friendly_output) |
|
|
print("JSON Path: ", json_path) |
|
|
|
|
|
return gif_path |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
with gr.Blocks( |
|
|
css="""#simulation_image {min-height: 600px; min-width: 700px;}""", |
|
|
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(): |
|
|
|
|
|
with gr.Column(scale=3): |
|
|
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=1.0, label="Zoom (Distance)", value=1.0) |
|
|
submit_button = gr.Button("Run Simulation") |
|
|
|
|
|
|
|
|
with gr.Column(scale=7): |
|
|
output_image = gr.Image( |
|
|
type="filepath", |
|
|
label="Simulation", |
|
|
elem_id="simulation_image", |
|
|
width=700, |
|
|
height=600 |
|
|
) |
|
|
|
|
|
|
|
|
submit_button.click( |
|
|
fn=simulate_and_render, |
|
|
inputs=[seed_input, azimuth_input, elevation_input, distance_input], |
|
|
outputs=output_image |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
iface.launch() |