Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| import pyvista as pv | |
| import os | |
| # Sample simulation function for illustration purposes | |
| def run_simulation(material_yield, part_thickness, press_force, punch_speed, safety_factor): | |
| try: | |
| # Generate dummy data for simulation results | |
| x = np.linspace(0, 10, 100) | |
| y = np.sin(x) * material_yield * part_thickness / safety_factor # Simulated stress distribution | |
| # Plotting the stress analysis graph | |
| plt.figure(figsize=(6, 4)) | |
| plt.plot(x, y, label="Stress Distribution") | |
| plt.title(f"Stress Analysis: Yield={material_yield}, Thickness={part_thickness}, Force={press_force}, Speed={punch_speed}") | |
| plt.xlabel("X-axis (Position)") | |
| plt.ylabel("Stress (MPa)") | |
| plt.legend() | |
| plt.grid(True) | |
| graph_output_path = "/tmp/simulation_output.png" | |
| plt.savefig(graph_output_path) | |
| plt.close() | |
| print(f"Graph saved at {graph_output_path}") # Debug message | |
| # 3D Model Visualization using PyVista | |
| mesh = pv.Sphere(radius=part_thickness) # Dummy sphere, replace with actual part geometry | |
| plotter = pv.Plotter() | |
| plotter.add_mesh(mesh) | |
| plotter.view_xy() | |
| # Save the screenshot of the 3D model | |
| plotter_output_path = "/tmp/3d_model_output.png" | |
| plotter.screenshot(plotter_output_path) | |
| print(f"3D Model screenshot saved at {plotter_output_path}") # Debug message | |
| return graph_output_path, plotter_output_path | |
| except Exception as e: | |
| print(f"Error during simulation: {e}") | |
| return None, None | |
| # Gradio interface for input parameters and output visualization | |
| iface = gr.Interface( | |
| fn=run_simulation, | |
| inputs=[ | |
| gr.Slider(minimum=100, maximum=500, value=200, label="Material Yield Strength (MPa)"), | |
| gr.Slider(minimum=0.5, maximum=10.0, value=3.0, label="Part Thickness (mm)"), | |
| gr.Slider(minimum=5000, maximum=20000, value=12000, label="Press Force (N)"), | |
| gr.Slider(minimum=1, maximum=5, value=2, label="Punch Speed (m/s)"), | |
| gr.Slider(minimum=1.0, maximum=3.0, value=1.5, label="Safety Factor"), | |
| ], | |
| outputs=[ | |
| gr.Image(type="filepath", label="Stress Analysis Graph"), | |
| gr.Image(type="filepath", label="3D Model Visualization") | |
| ] | |
| ) | |
| # Launch the interface | |
| iface.launch() | |