Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import gradio as gr
|
|
| 8 |
import os
|
| 9 |
from ansys.mapdl.core import launch_mapdl # For ANSYS integration
|
| 10 |
|
|
|
|
| 11 |
# Function for Progressive Die Design
|
| 12 |
def generate_die(length, width, thickness):
|
| 13 |
try:
|
|
@@ -20,6 +21,7 @@ def generate_die(length, width, thickness):
|
|
| 20 |
except Exception as e:
|
| 21 |
return f"Error generating die: {str(e)}"
|
| 22 |
|
|
|
|
| 23 |
# Function to visualize die in 3D
|
| 24 |
def visualize_die(length, width, thickness):
|
| 25 |
try:
|
|
@@ -40,31 +42,71 @@ def visualize_die(length, width, thickness):
|
|
| 40 |
except Exception as e:
|
| 41 |
return f"Error visualizing die: {str(e)}"
|
| 42 |
|
| 43 |
-
|
|
|
|
| 44 |
def stress_analysis(force, die_width, die_height, material_strength):
|
| 45 |
try:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
safety_factor = material_strength / stress # Safety Factor = Material Strength / Stress
|
| 49 |
-
|
| 50 |
-
# Data for plotting
|
| 51 |
-
x = ["Stress", "Material Strength", "Safety Factor"]
|
| 52 |
-
y_stress = [stress, material_strength, safety_factor]
|
| 53 |
|
| 54 |
-
# Create a combined graph
|
| 55 |
fig, ax = plt.subplots()
|
| 56 |
-
ax.
|
| 57 |
-
ax.
|
| 58 |
-
ax.
|
| 59 |
-
ax.set_title("Stress Analysis Parameters")
|
| 60 |
-
ax.legend()
|
| 61 |
-
plt.tight_layout()
|
| 62 |
plt.close(fig)
|
| 63 |
|
| 64 |
return f"Safety Factor: {round(safety_factor, 2)}", fig
|
| 65 |
except Exception as e:
|
| 66 |
return f"Error in stress analysis: {str(e)}", None
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Function to generate PDF report
|
| 69 |
def generate_pdf_report(data, filename="report.pdf"):
|
| 70 |
try:
|
|
@@ -77,18 +119,46 @@ def generate_pdf_report(data, filename="report.pdf"):
|
|
| 77 |
except Exception as e:
|
| 78 |
return f"Error generating report: {str(e)}"
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
# Gradio interface functions
|
| 81 |
def stress_analysis_interface(force, die_width, die_height, material_strength, simulation_tool):
|
| 82 |
if simulation_tool == "Python":
|
| 83 |
-
# Python-based stress analysis
|
| 84 |
safety_factor, fig = stress_analysis(force, die_width, die_height, material_strength)
|
| 85 |
data = {"stress": force / (die_width * die_height), "safety_factor": safety_factor}
|
| 86 |
pdf_filename = generate_pdf_report(data)
|
| 87 |
return safety_factor, fig, pdf_filename
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
else:
|
| 90 |
return "Invalid simulation tool selected", None, None
|
| 91 |
|
|
|
|
| 92 |
# Create Gradio App
|
| 93 |
with gr.Blocks() as app:
|
| 94 |
gr.Markdown("## Press Tool AI Suite")
|
|
@@ -112,7 +182,7 @@ with gr.Blocks() as app:
|
|
| 112 |
with gr.Tab("Stress Analysis"):
|
| 113 |
gr.Markdown("### Select Simulation Tool and Enter Parameters for Stress Analysis")
|
| 114 |
simulation_tool = gr.Dropdown(
|
| 115 |
-
choices=["Python"],
|
| 116 |
label="Simulation Tool",
|
| 117 |
value="Python",
|
| 118 |
)
|
|
@@ -129,6 +199,65 @@ with gr.Blocks() as app:
|
|
| 129 |
inputs=[force, die_width, die_height, material_strength, simulation_tool],
|
| 130 |
outputs=[safety_factor_output, stress_chart, pdf_file],
|
| 131 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
# Launch the app
|
| 134 |
app.launch()
|
|
|
|
| 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):
|
| 14 |
try:
|
|
|
|
| 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)
|
| 50 |
+
safety_factor = material_strength / stress
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
|
|
|
| 52 |
fig, ax = plt.subplots()
|
| 53 |
+
ax.bar(["Stress", "Material Strength"], [stress, material_strength])
|
| 54 |
+
ax.set_ylabel("Stress (MPa)")
|
| 55 |
+
ax.set_title("Stress Analysis")
|
|
|
|
|
|
|
|
|
|
| 56 |
plt.close(fig)
|
| 57 |
|
| 58 |
return f"Safety Factor: {round(safety_factor, 2)}", fig
|
| 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)
|
| 127 |
+
recommended_speed = 0.8 * speed
|
| 128 |
+
recommended_feed_rate = 0.9 * feed_rate
|
| 129 |
+
|
| 130 |
+
return {
|
| 131 |
+
"Estimated Tool Life (hrs)": round(tool_life, 2),
|
| 132 |
+
"Recommended Speed (m/min)": round(recommended_speed, 2),
|
| 133 |
+
"Recommended Feed Rate (mm/rev)": round(recommended_feed_rate, 2)
|
| 134 |
+
}
|
| 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")
|
|
|
|
| 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 |
)
|
|
|
|
| 199 |
inputs=[force, die_width, die_height, material_strength, simulation_tool],
|
| 200 |
outputs=[safety_factor_output, stress_chart, pdf_file],
|
| 201 |
)
|
| 202 |
+
# Function for Python-based Stress Analysis with a combined graph
|
| 203 |
+
def stress_analysis(force, die_width, die_height, material_strength):
|
| 204 |
+
try:
|
| 205 |
+
# Calculate parameters
|
| 206 |
+
stress = force / (die_width * die_height) # Stress = Force / Area
|
| 207 |
+
safety_factor = material_strength / stress # Safety Factor = Material Strength / Stress
|
| 208 |
+
|
| 209 |
+
# Data for plotting
|
| 210 |
+
x = ["Stress", "Material Strength", "Safety Factor"]
|
| 211 |
+
y_stress = [stress, material_strength, safety_factor]
|
| 212 |
+
|
| 213 |
+
# Create a combined graph
|
| 214 |
+
fig, ax = plt.subplots()
|
| 215 |
+
ax.plot(x, y_stress, marker='o', label="Simulation Parameters")
|
| 216 |
+
ax.axhline(y=1, color='red', linestyle='--', label="Critical Safety Factor (1)")
|
| 217 |
+
ax.set_ylabel("Value")
|
| 218 |
+
ax.set_title("Stress Analysis Parameters")
|
| 219 |
+
ax.legend()
|
| 220 |
+
plt.tight_layout()
|
| 221 |
+
plt.close(fig)
|
| 222 |
+
|
| 223 |
+
return f"Safety Factor: {round(safety_factor, 2)}", fig
|
| 224 |
+
except Exception as e:
|
| 225 |
+
return f"Error in stress analysis: {str(e)}", None
|
| 226 |
+
def stress_analysis_interface(force, die_width, die_height, material_strength, simulation_tool):
|
| 227 |
+
if simulation_tool == "Python":
|
| 228 |
+
# Python-based stress analysis with combined graph
|
| 229 |
+
safety_factor, fig = stress_analysis(force, die_width, die_height, material_strength)
|
| 230 |
+
data = {"stress": force / (die_width * die_height), "safety_factor": safety_factor}
|
| 231 |
+
pdf_filename = generate_pdf_report(data)
|
| 232 |
+
return safety_factor, fig, pdf_filename
|
| 233 |
+
|
| 234 |
+
elif simulation_tool == "ANSYS":
|
| 235 |
+
# Run ANSYS-based simulation
|
| 236 |
+
result = run_ansys_simulation(force, die_width, die_height, material_strength)
|
| 237 |
+
return result, None, None
|
| 238 |
+
|
| 239 |
+
elif simulation_tool == "SolidWorks":
|
| 240 |
+
# Run SolidWorks-based simulation
|
| 241 |
+
result = solidworks_stress_analysis(force, die_width, die_height, material_strength)
|
| 242 |
+
return result, None, None
|
| 243 |
+
|
| 244 |
+
else:
|
| 245 |
+
return "Invalid simulation tool selected", None, None
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
with gr.Tab("Tool Optimization"):
|
| 249 |
+
gr.Markdown("### Enter Machining Parameters for Tool Optimization")
|
| 250 |
+
speed = gr.Number(label="Cutting Speed (m/min)", value=100)
|
| 251 |
+
feed_rate = gr.Number(label="Feed Rate (mm/rev)", value=0.2)
|
| 252 |
+
depth_of_cut = gr.Number(label="Depth of Cut (mm)", value=1.0)
|
| 253 |
+
material = gr.Dropdown(choices=["Steel", "Aluminum", "Titanium"], label="Material", value="Steel")
|
| 254 |
+
optimization_results = gr.JSON(label="Optimization Results")
|
| 255 |
+
optimize_button = gr.Button("Optimize Tool")
|
| 256 |
+
optimize_button.click(
|
| 257 |
+
optimize_tool,
|
| 258 |
+
inputs=[speed, feed_rate, depth_of_cut, material],
|
| 259 |
+
outputs=optimization_results,
|
| 260 |
+
)
|
| 261 |
|
| 262 |
# Launch the app
|
| 263 |
app.launch()
|