Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cadquery as cq
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Function for Progressive Die Design
|
| 7 |
+
def generate_die(length, width, thickness):
|
| 8 |
+
try:
|
| 9 |
+
plate = cq.Workplane("XY").box(length, width, thickness)
|
| 10 |
+
punch = cq.Workplane("XY").rect(10, 10).extrude(5).translate((length/4, width/4, thickness/2))
|
| 11 |
+
die = plate.cut(punch)
|
| 12 |
+
filename = "progressive_die.step"
|
| 13 |
+
cq.exporters.export(die, filename)
|
| 14 |
+
return f"Progressive die design saved as {filename}"
|
| 15 |
+
except Exception as e:
|
| 16 |
+
return f"Error generating die: {str(e)}"
|
| 17 |
+
|
| 18 |
+
# Function for Stress Analysis
|
| 19 |
+
def stress_analysis(force, die_width, die_height, material_strength):
|
| 20 |
+
try:
|
| 21 |
+
stress = force / (die_width * die_height)
|
| 22 |
+
safety_factor = material_strength / stress
|
| 23 |
+
|
| 24 |
+
fig, ax = plt.subplots()
|
| 25 |
+
ax.bar(["Stress", "Material Strength"], [stress, material_strength])
|
| 26 |
+
ax.set_ylabel("Stress (MPa)")
|
| 27 |
+
ax.set_title("Stress Analysis")
|
| 28 |
+
plt.close(fig)
|
| 29 |
+
|
| 30 |
+
return f"Safety Factor: {round(safety_factor, 2)}", fig
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error in stress analysis: {str(e)}", None
|
| 33 |
+
|
| 34 |
+
# Function for Tool Optimization
|
| 35 |
+
def optimize_tool(speed, feed_rate, depth_of_cut, material):
|
| 36 |
+
try:
|
| 37 |
+
tool_life = 1000 / (speed * feed_rate * depth_of_cut)
|
| 38 |
+
recommended_speed = 0.8 * speed
|
| 39 |
+
recommended_feed_rate = 0.9 * feed_rate
|
| 40 |
+
|
| 41 |
+
return {
|
| 42 |
+
"Estimated Tool Life (hrs)": round(tool_life, 2),
|
| 43 |
+
"Recommended Speed (m/min)": round(recommended_speed, 2),
|
| 44 |
+
"Recommended Feed Rate (mm/rev)": round(recommended_feed_rate, 2)
|
| 45 |
+
}
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return {"Error": str(e)}
|
| 48 |
+
|
| 49 |
+
# Gradio interface functions
|
| 50 |
+
def progressive_die_interface(length, width, thickness):
|
| 51 |
+
return generate_die(length, width, thickness)
|
| 52 |
+
|
| 53 |
+
def stress_analysis_interface(force, width, height, material_strength):
|
| 54 |
+
return stress_analysis(force, width, height, material_strength)
|
| 55 |
+
|
| 56 |
+
def tool_optimization_interface(speed, feed_rate, depth_of_cut, material):
|
| 57 |
+
return optimize_tool(speed, feed_rate, depth_of_cut, material)
|
| 58 |
+
|
| 59 |
+
# Create the Gradio Interface
|
| 60 |
+
with gr.Blocks() as app:
|
| 61 |
+
gr.Markdown("## Press Tool AI Suite")
|
| 62 |
+
gr.Markdown("Select a tool below to get started:")
|
| 63 |
+
|
| 64 |
+
with gr.Tabs():
|
| 65 |
+
with gr.Tab("Progressive Die Design"):
|
| 66 |
+
gr.Markdown("### Enter Dimensions for Progressive Die")
|
| 67 |
+
length = gr.Number(label="Length (mm)", value=100)
|
| 68 |
+
width = gr.Number(label="Width (mm)", value=50)
|
| 69 |
+
thickness = gr.Number(label="Thickness (mm)", value=10)
|
| 70 |
+
die_output = gr.Textbox(label="Output")
|
| 71 |
+
die_button = gr.Button("Generate Die")
|
| 72 |
+
die_button.click(progressive_die_interface, inputs=[length, width, thickness], outputs=die_output)
|
| 73 |
+
|
| 74 |
+
with gr.Tab("Stress Analysis"):
|
| 75 |
+
gr.Markdown("### Enter Parameters for Stress Analysis")
|
| 76 |
+
force = gr.Number(label="Force (N)", value=10000)
|
| 77 |
+
die_width = gr.Number(label="Width (m)", value=0.05)
|
| 78 |
+
die_height = gr.Number(label="Height (m)", value=0.01)
|
| 79 |
+
material_strength = gr.Number(label="Material Strength (MPa)", value=250)
|
| 80 |
+
safety_factor_output = gr.Textbox(label="Safety Factor")
|
| 81 |
+
stress_chart = gr.Plot()
|
| 82 |
+
stress_button = gr.Button("Analyze Stress")
|
| 83 |
+
stress_button.click(stress_analysis_interface, inputs=[force, die_width, die_height, material_strength], outputs=[safety_factor_output, stress_chart])
|
| 84 |
+
|
| 85 |
+
with gr.Tab("Tool Optimization"):
|
| 86 |
+
gr.Markdown("### Enter Machining Parameters for Tool Optimization")
|
| 87 |
+
speed = gr.Number(label="Cutting Speed (m/min)", value=100)
|
| 88 |
+
feed_rate = gr.Number(label="Feed Rate (mm/rev)", value=0.2)
|
| 89 |
+
depth_of_cut = gr.Number(label="Depth of Cut (mm)", value=1.0)
|
| 90 |
+
material = gr.Dropdown(choices=["Steel", "Aluminum", "Titanium"], label="Material", value="Steel")
|
| 91 |
+
optimization_results = gr.JSON(label="Optimization Results")
|
| 92 |
+
optimize_button = gr.Button("Optimize Tool")
|
| 93 |
+
optimize_button.click(tool_optimization_interface, inputs=[speed, feed_rate, depth_of_cut, material], outputs=optimization_results)
|
| 94 |
+
|
| 95 |
+
# Launch the app
|
| 96 |
+
app.launch()
|