jithenderchoudary commited on
Commit
48a6a16
·
verified ·
1 Parent(s): 9e26e0b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -20
app.py CHANGED
@@ -5,6 +5,9 @@ import pyvista as pv
5
  from reportlab.lib.pagesizes import letter
6
  from reportlab.pdfgen import canvas
7
  import gradio as gr
 
 
 
8
 
9
  # Function for Progressive Die Design
10
  def generate_die(length, width, thickness):
@@ -18,6 +21,7 @@ def generate_die(length, width, thickness):
18
  except Exception as e:
19
  return f"Error generating die: {str(e)}"
20
 
 
21
  # Function to visualize die in 3D
22
  def visualize_die(length, width, thickness):
23
  try:
@@ -38,7 +42,8 @@ def visualize_die(length, width, thickness):
38
  except Exception as e:
39
  return f"Error visualizing die: {str(e)}"
40
 
41
- # Function for Stress Analysis
 
42
  def stress_analysis(force, die_width, die_height, material_strength):
43
  try:
44
  stress = force / (die_width * die_height)
@@ -54,6 +59,54 @@ def stress_analysis(force, die_width, die_height, material_strength):
54
  except Exception as e:
55
  return f"Error in stress analysis: {str(e)}", None
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  # Function to generate PDF report
58
  def generate_pdf_report(data, filename="report.pdf"):
59
  try:
@@ -66,7 +119,8 @@ def generate_pdf_report(data, filename="report.pdf"):
66
  except Exception as e:
67
  return f"Error generating report: {str(e)}"
68
 
69
- # Function for Tool Optimization
 
70
  def optimize_tool(speed, feed_rate, depth_of_cut, material):
71
  try:
72
  tool_life = 1000 / (speed * feed_rate * depth_of_cut)
@@ -81,22 +135,31 @@ def optimize_tool(speed, feed_rate, depth_of_cut, material):
81
  except Exception as e:
82
  return {"Error": str(e)}
83
 
 
84
  # Gradio interface functions
85
- def progressive_die_interface(length, width, thickness):
86
- filename = generate_die(length, width, thickness)
87
- visualization = visualize_die(length, width, thickness)
88
- return filename, visualization
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
- def stress_analysis_interface(force, width, height, material_strength):
91
- safety_factor, fig = stress_analysis(force, width, height, material_strength)
92
- data = {"stress": force / (width * height), "safety_factor": safety_factor}
93
- pdf_filename = generate_pdf_report(data)
94
- return safety_factor, fig, pdf_filename
95
 
96
- def tool_optimization_interface(speed, feed_rate, depth_of_cut, material):
97
- return optimize_tool(speed, feed_rate, depth_of_cut, material)
98
 
99
- # Create the Gradio Interface
100
  with gr.Blocks() as app:
101
  gr.Markdown("## Press Tool AI Suite")
102
  gr.Markdown("Select a tool below to get started:")
@@ -110,19 +173,32 @@ with gr.Blocks() as app:
110
  die_output = gr.Textbox(label="Die Output File")
111
  visualization_output = gr.Image(label="3D Visualization")
112
  die_button = gr.Button("Generate Die")
113
- die_button.click(progressive_die_interface, inputs=[length, width, thickness], outputs=[die_output, visualization_output])
 
 
 
 
114
 
115
  with gr.Tab("Stress Analysis"):
116
- gr.Markdown("### Enter Parameters for Stress Analysis")
 
 
 
 
 
117
  force = gr.Number(label="Force (N)", value=10000)
118
  die_width = gr.Number(label="Width (m)", value=0.05)
119
  die_height = gr.Number(label="Height (m)", value=0.01)
120
  material_strength = gr.Number(label="Material Strength (MPa)", value=250)
121
- safety_factor_output = gr.Textbox(label="Safety Factor")
122
  stress_chart = gr.Plot()
123
- pdf_file = gr.File(label="Download Report")
124
  stress_button = gr.Button("Analyze Stress")
125
- stress_button.click(stress_analysis_interface, inputs=[force, die_width, die_height, material_strength], outputs=[safety_factor_output, stress_chart, pdf_file])
 
 
 
 
126
 
127
  with gr.Tab("Tool Optimization"):
128
  gr.Markdown("### Enter Machining Parameters for Tool Optimization")
@@ -132,7 +208,11 @@ with gr.Blocks() as app:
132
  material = gr.Dropdown(choices=["Steel", "Aluminum", "Titanium"], label="Material", value="Steel")
133
  optimization_results = gr.JSON(label="Optimization Results")
134
  optimize_button = gr.Button("Optimize Tool")
135
- optimize_button.click(tool_optimization_interface, inputs=[speed, feed_rate, depth_of_cut, material], outputs=optimization_results)
 
 
 
 
136
 
137
  # Launch the app
138
  app.launch()
 
5
  from reportlab.lib.pagesizes import letter
6
  from reportlab.pdfgen import canvas
7
  import gradio as gr
8
+ import os
9
+ from ansys.mapdl.core import launch_mapdl # For ANSYS integration
10
+
11
 
12
  # Function for Progressive Die Design
13
  def generate_die(length, width, thickness):
 
21
  except Exception as e:
22
  return f"Error generating die: {str(e)}"
23
 
24
+
25
  # Function to visualize die in 3D
26
  def visualize_die(length, width, thickness):
27
  try:
 
42
  except Exception as e:
43
  return f"Error visualizing die: {str(e)}"
44
 
45
+
46
+ # Function for Python-based Stress Analysis
47
  def stress_analysis(force, die_width, die_height, material_strength):
48
  try:
49
  stress = force / (die_width * die_height)
 
59
  except Exception as e:
60
  return f"Error in stress analysis: {str(e)}", None
61
 
62
+
63
+ # ANSYS Integration for Stress Analysis
64
+ def run_ansys_simulation(force, die_width, die_height, material_strength):
65
+ try:
66
+ # Launch ANSYS MAPDL instance
67
+ mapdl = launch_mapdl()
68
+ mapdl.prep7() # Enter pre-processing module
69
+
70
+ # Define geometry and material properties
71
+ mapdl.rectng(0, die_width, 0, die_height)
72
+ mapdl.mp('EX', 1, material_strength)
73
+ mapdl.et(1, 'PLANE183')
74
+
75
+ # Apply loads
76
+ mapdl.nsel('S', 'LOC', 'X', 0)
77
+ mapdl.d('ALL', 'UX', 0)
78
+ mapdl.f('ALL', 'FY', -force)
79
+
80
+ # Solve
81
+ mapdl.run('/SOLU')
82
+ mapdl.solve()
83
+ mapdl.finish()
84
+
85
+ # Post-processing
86
+ mapdl.post1()
87
+ stress = mapdl.get_value('NODE', 1, 'S', 'EQV') # Retrieve max equivalent stress
88
+ mapdl.exit()
89
+
90
+ return f"Max Stress: {stress:.2f} MPa"
91
+ except Exception as e:
92
+ return f"Error running ANSYS simulation: {str(e)}"
93
+
94
+
95
+ # SolidWorks Placeholder for Stress Analysis
96
+ def solidworks_stress_analysis(force, die_width, die_height, material_strength):
97
+ # Replace this function with SolidWorks API or external script
98
+ try:
99
+ output_file = "/path/to/solidworks/output.txt" # Replace with actual path
100
+ if os.path.exists(output_file):
101
+ with open(output_file, "r") as file:
102
+ result = file.read()
103
+ return result.strip()
104
+ else:
105
+ return "SolidWorks simulation output not found."
106
+ except Exception as e:
107
+ return f"Error running SolidWorks simulation: {str(e)}"
108
+
109
+
110
  # Function to generate PDF report
111
  def generate_pdf_report(data, filename="report.pdf"):
112
  try:
 
119
  except Exception as e:
120
  return f"Error generating report: {str(e)}"
121
 
122
+
123
+ # Tool Optimization Function
124
  def optimize_tool(speed, feed_rate, depth_of_cut, material):
125
  try:
126
  tool_life = 1000 / (speed * feed_rate * depth_of_cut)
 
135
  except Exception as e:
136
  return {"Error": str(e)}
137
 
138
+
139
  # Gradio interface functions
140
+ def stress_analysis_interface(force, die_width, die_height, material_strength, simulation_tool):
141
+ if simulation_tool == "Python":
142
+ # Python-based stress analysis
143
+ safety_factor, fig = stress_analysis(force, die_width, die_height, material_strength)
144
+ data = {"stress": force / (die_width * die_height), "safety_factor": safety_factor}
145
+ pdf_filename = generate_pdf_report(data)
146
+ return safety_factor, fig, pdf_filename
147
+
148
+ elif simulation_tool == "ANSYS":
149
+ # Run ANSYS-based simulation
150
+ result = run_ansys_simulation(force, die_width, die_height, material_strength)
151
+ return result, None, None
152
+
153
+ elif simulation_tool == "SolidWorks":
154
+ # Run SolidWorks-based simulation
155
+ result = solidworks_stress_analysis(force, die_width, die_height, material_strength)
156
+ return result, None, None
157
 
158
+ else:
159
+ return "Invalid simulation tool selected", None, None
 
 
 
160
 
 
 
161
 
162
+ # Create Gradio App
163
  with gr.Blocks() as app:
164
  gr.Markdown("## Press Tool AI Suite")
165
  gr.Markdown("Select a tool below to get started:")
 
173
  die_output = gr.Textbox(label="Die Output File")
174
  visualization_output = gr.Image(label="3D Visualization")
175
  die_button = gr.Button("Generate Die")
176
+ die_button.click(
177
+ lambda l, w, t: (generate_die(l, w, t), visualize_die(l, w, t)),
178
+ inputs=[length, width, thickness],
179
+ outputs=[die_output, visualization_output],
180
+ )
181
 
182
  with gr.Tab("Stress Analysis"):
183
+ gr.Markdown("### Select Simulation Tool and Enter Parameters for Stress Analysis")
184
+ simulation_tool = gr.Dropdown(
185
+ choices=["Python", "ANSYS", "SolidWorks"],
186
+ label="Simulation Tool",
187
+ value="Python",
188
+ )
189
  force = gr.Number(label="Force (N)", value=10000)
190
  die_width = gr.Number(label="Width (m)", value=0.05)
191
  die_height = gr.Number(label="Height (m)", value=0.01)
192
  material_strength = gr.Number(label="Material Strength (MPa)", value=250)
193
+ safety_factor_output = gr.Textbox(label="Safety Factor or Simulation Result")
194
  stress_chart = gr.Plot()
195
+ pdf_file = gr.File(label="Download Report (Python Only)")
196
  stress_button = gr.Button("Analyze Stress")
197
+ stress_button.click(
198
+ stress_analysis_interface,
199
+ inputs=[force, die_width, die_height, material_strength, simulation_tool],
200
+ outputs=[safety_factor_output, stress_chart, pdf_file],
201
+ )
202
 
203
  with gr.Tab("Tool Optimization"):
204
  gr.Markdown("### Enter Machining Parameters for Tool Optimization")
 
208
  material = gr.Dropdown(choices=["Steel", "Aluminum", "Titanium"], label="Material", value="Steel")
209
  optimization_results = gr.JSON(label="Optimization Results")
210
  optimize_button = gr.Button("Optimize Tool")
211
+ optimize_button.click(
212
+ optimize_tool,
213
+ inputs=[speed, feed_rate, depth_of_cut, material],
214
+ outputs=optimization_results,
215
+ )
216
 
217
  # Launch the app
218
  app.launch()