jithenderchoudary commited on
Commit
531e195
·
verified ·
1 Parent(s): bb2dbc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -55
app.py CHANGED
@@ -2,62 +2,89 @@ import gradio as gr
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
  import pyvista as pv
5
- import os
6
 
7
- # Sample simulation function for illustration purposes
8
- def run_simulation(material_yield, part_thickness, press_force, punch_speed, safety_factor):
9
  try:
10
- # Generate dummy data for simulation results
11
- x = np.linspace(0, 10, 100)
12
- y = np.sin(x) * material_yield * part_thickness / safety_factor # Simulated stress distribution
13
-
14
- # Plotting the stress analysis graph
15
- plt.figure(figsize=(6, 4))
16
- plt.plot(x, y, label="Stress Distribution")
17
- plt.title(f"Stress Analysis: Yield={material_yield}, Thickness={part_thickness}, Force={press_force}, Speed={punch_speed}")
18
- plt.xlabel("X-axis (Position)")
19
- plt.ylabel("Stress (MPa)")
20
- plt.legend()
21
- plt.grid(True)
22
- graph_output_path = "/tmp/simulation_output.png"
23
- plt.savefig(graph_output_path)
24
- plt.close()
25
-
26
- print(f"Graph saved at {graph_output_path}") # Debug message
27
-
28
- # 3D Model Visualization using PyVista
29
- mesh = pv.Sphere(radius=part_thickness) # Dummy sphere, replace with actual part geometry
30
- plotter = pv.Plotter()
31
- plotter.add_mesh(mesh)
32
- plotter.view_xy()
33
-
34
- # Save the screenshot of the 3D model
35
- plotter_output_path = "/tmp/3d_model_output.png"
36
- plotter.screenshot(plotter_output_path)
37
-
38
- print(f"3D Model screenshot saved at {plotter_output_path}") # Debug message
39
-
40
- return graph_output_path, plotter_output_path
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  except Exception as e:
43
- print(f"Error during simulation: {e}")
44
- return None, None
45
-
46
- # Gradio interface for input parameters and output visualization
47
- iface = gr.Interface(
48
- fn=run_simulation,
49
- inputs=[
50
- gr.Slider(minimum=100, maximum=500, value=200, label="Material Yield Strength (MPa)"),
51
- gr.Slider(minimum=0.5, maximum=10.0, value=3.0, label="Part Thickness (mm)"),
52
- gr.Slider(minimum=5000, maximum=20000, value=12000, label="Press Force (N)"),
53
- gr.Slider(minimum=1, maximum=5, value=2, label="Punch Speed (m/s)"),
54
- gr.Slider(minimum=1.0, maximum=3.0, value=1.5, label="Safety Factor"),
55
- ],
56
- outputs=[
57
- gr.Image(type="filepath", label="Stress Analysis Graph"),
58
- gr.Image(type="filepath", label="3D Model Visualization")
59
- ]
60
- )
61
-
62
- # Launch the interface
63
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import numpy as np
3
  import matplotlib.pyplot as plt
4
  import pyvista as pv
5
+ import cadquery as cq
6
 
7
+ # Function for Stress Analysis
8
+ def stress_analysis(force, die_width, die_height, material_strength):
9
  try:
10
+ # Calculate parameters
11
+ stress = force / (die_width * die_height) # Stress = Force / Area
12
+ safety_factor = material_strength / stress # Safety Factor = Material Strength / Stress
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ # Data for plotting
15
+ x = ["Stress", "Material Strength", "Safety Factor"]
16
+ y_stress = [stress, material_strength, safety_factor]
17
+
18
+ # Create a combined graph
19
+ fig, ax = plt.subplots()
20
+ ax.plot(x, y_stress, marker='o', label="Simulation Parameters")
21
+ ax.axhline(y=1, color='red', linestyle='--', label="Critical Safety Factor (1)")
22
+ ax.set_ylabel("Value")
23
+ ax.set_title("Stress Analysis Parameters")
24
+ ax.legend()
25
+
26
+ # Save the figure to a file
27
+ fig_path = "/tmp/stress_analysis_graph.png"
28
+ fig.savefig(fig_path)
29
+
30
+ # Close the figure after saving it
31
+ plt.close(fig)
32
+
33
+ return fig_path # Return the image path
34
  except Exception as e:
35
+ return f"Error in stress analysis: {str(e)}"
36
+
37
+ # Function for 3D Visualization
38
+ def visualize_die(length, width, thickness):
39
+ try:
40
+ # Create a basic die shape using CadQuery
41
+ plate = cq.Workplane("XY").box(length, width, thickness)
42
+ punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length / 4, width / 4, thickness / 2))
43
+ die = plate.cut(punch)
44
+
45
+ # Export to STL for visualization
46
+ stl_file = "/tmp/progressive_die.stl"
47
+ cq.exporters.exportShape(die.val(), "STL", stl_file)
48
+
49
+ # Visualize with PyVista
50
+ mesh = pv.read(stl_file)
51
+ plotter = pv.Plotter(off_screen=True)
52
+ plotter.add_mesh(mesh, color="blue")
53
+
54
+ # Save the 3D visualization as a PNG
55
+ screenshot = "/tmp/progressive_die_visualization.png"
56
+ plotter.screenshot(screenshot)
57
+ plotter.close()
58
+
59
+ return screenshot # Return the screenshot path
60
+ except Exception as e:
61
+ return f"Error visualizing die: {str(e)}"
62
+
63
+ # Combined Interface Function
64
+ def interface(force, die_width, die_height, material_strength, length, width, thickness):
65
+ # Stress Analysis
66
+ stress_graph_path = stress_analysis(force, die_width, die_height, material_strength)
67
+
68
+ # 3D Visualization
69
+ die_visualization_path = visualize_die(length, width, thickness)
70
+
71
+ return stress_graph_path, die_visualization_path
72
+
73
+ # Gradio Interface
74
+ inputs = [
75
+ gr.Slider(minimum=100, maximum=500, value=200, label="Material Yield Strength (MPa)"),
76
+ gr.Slider(minimum=100, maximum=500, value=200, label="Die Width (mm)"),
77
+ gr.Slider(minimum=100, maximum=500, value=200, label="Die Height (mm)"),
78
+ gr.Slider(minimum=5000, maximum=20000, value=12000, label="Force (N)"),
79
+ gr.Slider(minimum=100, maximum=500, value=200, label="Length (mm)"),
80
+ gr.Slider(minimum=100, maximum=500, value=200, label="Width (mm)"),
81
+ gr.Slider(minimum=10, maximum=50, value=10, label="Thickness (mm)"),
82
+ ]
83
+
84
+ outputs = [
85
+ gr.Image(type="filepath", label="Stress Analysis Graph"),
86
+ gr.Image(type="filepath", label="3D Visualization"),
87
+ ]
88
+
89
+ app = gr.Interface(fn=interface, inputs=inputs, outputs=outputs)
90
+ app.launch()