Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,171 +1,52 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import numpy as np
|
| 3 |
import matplotlib.pyplot as plt
|
| 4 |
-
import cadquery as cq
|
| 5 |
import pyvista as pv
|
| 6 |
-
from ansys.mapdl.core import launch_mapdl
|
| 7 |
-
from reportlab.lib.pagesizes import letter
|
| 8 |
-
from reportlab.pdfgen import canvas
|
| 9 |
import os
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
y_stress = np.array([stress, safety_factor, tool_life])
|
| 58 |
-
|
| 59 |
-
# Multiple parameters (force vs material strength, safety factor vs stress, etc.)
|
| 60 |
-
plt.figure(figsize=(10, 6))
|
| 61 |
-
|
| 62 |
-
# Plot multiple curves
|
| 63 |
-
plt.plot(x, y_stress, label="Stress vs Force and Tool Life")
|
| 64 |
-
plt.plot(x, np.array([safety_factor] * len(x)), label="Safety Factor vs Stress", linestyle='--')
|
| 65 |
-
|
| 66 |
-
# Labels and Titles
|
| 67 |
-
plt.xlabel("Force, Die Width, Die Height, Punch Size")
|
| 68 |
-
plt.ylabel("Values")
|
| 69 |
-
plt.title("Combined Simulation Parameters for Die Design and Stress Analysis")
|
| 70 |
-
|
| 71 |
-
# Adding legend
|
| 72 |
-
plt.legend()
|
| 73 |
-
|
| 74 |
-
# Display the graph
|
| 75 |
-
plt.tight_layout()
|
| 76 |
-
plt.close()
|
| 77 |
-
|
| 78 |
-
return "Graph generated successfully."
|
| 79 |
-
|
| 80 |
-
except Exception as e:
|
| 81 |
-
return f"Error in generating graph: {str(e)}"
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# Tool Optimization Function
|
| 85 |
-
def optimize_tool(speed, feed_rate, depth_of_cut, material):
|
| 86 |
-
try:
|
| 87 |
-
tool_life = 1000 / (speed * feed_rate * depth_of_cut)
|
| 88 |
-
recommended_speed = 0.8 * speed
|
| 89 |
-
recommended_feed_rate = 0.9 * feed_rate
|
| 90 |
-
|
| 91 |
-
return {
|
| 92 |
-
"Estimated Tool Life (hrs)": round(tool_life, 2),
|
| 93 |
-
"Recommended Speed (m/min)": round(recommended_speed, 2),
|
| 94 |
-
"Recommended Feed Rate (mm/rev)": round(recommended_feed_rate, 2)
|
| 95 |
-
}
|
| 96 |
-
except Exception as e:
|
| 97 |
-
return {"Error": str(e)}
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
# Gradio interface for Progressive Die Design and Stress Analysis
|
| 101 |
-
def stress_analysis_interface(force, die_width, die_height, material_strength, simulation_tool):
|
| 102 |
-
if simulation_tool == "Python":
|
| 103 |
-
# Generate and plot combined graph
|
| 104 |
-
result = generate_combined_graph(force, die_width, die_height, material_strength, punch_size=10)
|
| 105 |
-
return result # Show graph or return result for Gradio output
|
| 106 |
-
|
| 107 |
-
elif simulation_tool == "ANSYS":
|
| 108 |
-
# Run ANSYS-based simulation
|
| 109 |
-
result = run_ansys_simulation(force, die_width, die_height, material_strength)
|
| 110 |
-
return result
|
| 111 |
-
|
| 112 |
-
elif simulation_tool == "SolidWorks":
|
| 113 |
-
# Run SolidWorks-based simulation
|
| 114 |
-
result = solidworks_stress_analysis(force, die_width, die_height, material_strength)
|
| 115 |
-
return result
|
| 116 |
-
|
| 117 |
-
else:
|
| 118 |
-
return "Invalid simulation tool selected", None
|
| 119 |
-
|
| 120 |
-
# Gradio Interface for Press Tool AI Suite
|
| 121 |
-
with gr.Blocks() as app:
|
| 122 |
-
gr.Markdown("## Press Tool AI Suite")
|
| 123 |
-
|
| 124 |
-
with gr.Tabs():
|
| 125 |
-
with gr.Tab("Progressive Die Design"):
|
| 126 |
-
# Inputs for Progressive Die Design
|
| 127 |
-
length = gr.Number(label="Length (mm)", value=100)
|
| 128 |
-
width = gr.Number(label="Width (mm)", value=50)
|
| 129 |
-
thickness = gr.Number(label="Thickness (mm)", value=10)
|
| 130 |
-
die_output = gr.Textbox(label="Die Output File")
|
| 131 |
-
visualization_output = gr.Image(label="3D Visualization")
|
| 132 |
-
die_button = gr.Button("Generate Die")
|
| 133 |
-
die_button.click(
|
| 134 |
-
lambda l, w, t: (generate_die(l, w, t), visualize_die(l, w, t)),
|
| 135 |
-
inputs=[length, width, thickness],
|
| 136 |
-
outputs=[die_output, visualization_output],
|
| 137 |
-
)
|
| 138 |
-
|
| 139 |
-
with gr.Tab("Stress Analysis"):
|
| 140 |
-
# Inputs for Stress Analysis
|
| 141 |
-
force = gr.Number(label="Force (N)", value=10000)
|
| 142 |
-
die_width = gr.Number(label="Width (m)", value=0.05)
|
| 143 |
-
die_height = gr.Number(label="Height (m)", value=0.01)
|
| 144 |
-
material_strength = gr.Number(label="Material Strength (MPa)", value=250)
|
| 145 |
-
simulation_tool = gr.Dropdown(choices=["Python", "ANSYS", "SolidWorks"], label="Simulation Tool", value="Python")
|
| 146 |
-
|
| 147 |
-
stress_output = gr.Textbox(label="Simulation Result")
|
| 148 |
-
stress_chart = gr.Plot() # This will display the combined graph
|
| 149 |
-
stress_button = gr.Button("Analyze Stress and Visualize")
|
| 150 |
-
stress_button.click(
|
| 151 |
-
stress_analysis_interface,
|
| 152 |
-
inputs=[force, die_width, die_height, material_strength, simulation_tool],
|
| 153 |
-
outputs=[stress_output, stress_chart],
|
| 154 |
-
)
|
| 155 |
-
|
| 156 |
-
with gr.Tab("Tool Optimization"):
|
| 157 |
-
# Inputs for Tool Optimization
|
| 158 |
-
speed = gr.Number(label="Cutting Speed (m/min)", value=100)
|
| 159 |
-
feed_rate = gr.Number(label="Feed Rate (mm/rev)", value=0.2)
|
| 160 |
-
depth_of_cut = gr.Number(label="Depth of Cut (mm)", value=1.0)
|
| 161 |
-
material = gr.Dropdown(choices=["Steel", "Aluminum", "Titanium"], label="Material", value="Steel")
|
| 162 |
-
optimization_results = gr.JSON(label="Optimization Results")
|
| 163 |
-
optimize_button = gr.Button("Optimize Tool")
|
| 164 |
-
optimize_button.click(
|
| 165 |
-
optimize_tool,
|
| 166 |
-
inputs=[speed, feed_rate, depth_of_cut, material],
|
| 167 |
-
outputs=optimization_results,
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
# Launch the app
|
| 171 |
-
app.launch()
|
|
|
|
| 1 |
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 |
+
# Generate dummy data for simulation results
|
| 10 |
+
x = np.linspace(0, 10, 100)
|
| 11 |
+
y = np.sin(x) * material_yield * part_thickness / safety_factor # Simulated stress distribution
|
| 12 |
+
|
| 13 |
+
# Plotting the stress analysis graph
|
| 14 |
+
plt.figure(figsize=(6, 4))
|
| 15 |
+
plt.plot(x, y, label="Stress Distribution")
|
| 16 |
+
plt.title(f"Stress Analysis: Yield={material_yield}, Thickness={part_thickness}, Force={press_force}, Speed={punch_speed}")
|
| 17 |
+
plt.xlabel("X-axis (Position)")
|
| 18 |
+
plt.ylabel("Stress (MPa)")
|
| 19 |
+
plt.legend()
|
| 20 |
+
plt.grid(True)
|
| 21 |
+
graph_output_path = "/tmp/simulation_output.png"
|
| 22 |
+
plt.savefig(graph_output_path)
|
| 23 |
+
plt.close()
|
| 24 |
+
|
| 25 |
+
# 3D Model Visualization using PyVista
|
| 26 |
+
mesh = pv.Sphere(radius=part_thickness) # Dummy sphere, replace with actual part geometry
|
| 27 |
+
plotter = pv.Plotter()
|
| 28 |
+
plotter.add_mesh(mesh)
|
| 29 |
+
plotter.view_xy()
|
| 30 |
+
plotter_output_path = "/tmp/3d_model_output.png"
|
| 31 |
+
plotter.screenshot(plotter_output_path)
|
| 32 |
+
|
| 33 |
+
return graph_output_path, plotter_output_path
|
| 34 |
+
|
| 35 |
+
# Gradio interface for input parameters and output visualization
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=run_simulation,
|
| 38 |
+
inputs=[
|
| 39 |
+
gr.Slider(minimum=100, maximum=500, default=200, label="Material Yield Strength (MPa)"),
|
| 40 |
+
gr.Slider(minimum=0.5, maximum=10.0, default=3.0, label="Part Thickness (mm)"),
|
| 41 |
+
gr.Slider(minimum=5000, maximum=20000, default=12000, label="Press Force (N)"),
|
| 42 |
+
gr.Slider(minimum=1, maximum=5, default=2, label="Punch Speed (m/s)"),
|
| 43 |
+
gr.Slider(minimum=1.0, maximum=3.0, default=1.5, label="Safety Factor"),
|
| 44 |
+
],
|
| 45 |
+
outputs=[
|
| 46 |
+
gr.Image(type="filepath", label="Stress Analysis Graph"),
|
| 47 |
+
gr.Image(type="filepath", label="3D Model Visualization")
|
| 48 |
+
]
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Launch the interface
|
| 52 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|