Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import numpy as np
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
# Configure logging
|
| 7 |
+
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 8 |
+
logger = logging.getLogger()
|
| 9 |
+
|
| 10 |
+
# Function to generate the die design visualization (with more shapes)
|
| 11 |
+
def generate_die_ANSYS(length, width, thickness, die_shape):
|
| 12 |
+
try:
|
| 13 |
+
# Log inputs
|
| 14 |
+
logger.info(f"Generating die design: Length={length}, Width={width}, Thickness={thickness}, Shape={die_shape}")
|
| 15 |
+
|
| 16 |
+
# Validate input dimensions
|
| 17 |
+
if length <= 0 or width <= 0 or thickness <= 0:
|
| 18 |
+
logger.error("Dimensions must be greater than zero.")
|
| 19 |
+
return "Error: Dimensions must be greater than zero.", None
|
| 20 |
+
|
| 21 |
+
# Simulate a realistic die design with multiple shapes
|
| 22 |
+
fig, ax = plt.subplots(figsize=(8, 8))
|
| 23 |
+
|
| 24 |
+
if die_shape == "Rectangle":
|
| 25 |
+
# Visualize a rectangular die
|
| 26 |
+
ax.add_patch(plt.Rectangle((0, 0), length, width, linewidth=2, edgecolor='blue', facecolor='cyan', label='Rectangle Die'))
|
| 27 |
+
ax.set_xlim(0, length + 20)
|
| 28 |
+
ax.set_ylim(0, width + 20)
|
| 29 |
+
ax.set_title(f"Progressive Die Design - Rectangle {length}x{width}")
|
| 30 |
+
logger.info(f"Created rectangular die with dimensions {length} x {width}.")
|
| 31 |
+
|
| 32 |
+
elif die_shape == "Circle":
|
| 33 |
+
# Visualize a circular die
|
| 34 |
+
circle = plt.Circle((length / 2, width / 2), radius=min(length, width) / 2, color='orange', ec='black', lw=2, label='Circular Die')
|
| 35 |
+
ax.add_patch(circle)
|
| 36 |
+
ax.set_xlim(0, length + 20)
|
| 37 |
+
ax.set_ylim(0, width + 20)
|
| 38 |
+
ax.set_title(f"Progressive Die Design - Circle {length}x{width}")
|
| 39 |
+
logger.info(f"Created circular die with diameter {length} and width {width}.")
|
| 40 |
+
|
| 41 |
+
elif die_shape == "Ellipse":
|
| 42 |
+
# Visualize an elliptical die
|
| 43 |
+
ellipse = plt.Ellipse((length / 2, width / 2), width, length, color='green', ec='black', lw=2, label='Elliptical Die')
|
| 44 |
+
ax.add_patch(ellipse)
|
| 45 |
+
ax.set_xlim(0, length + 20)
|
| 46 |
+
ax.set_ylim(0, width + 20)
|
| 47 |
+
ax.set_title(f"Progressive Die Design - Ellipse {length}x{width}")
|
| 48 |
+
logger.info(f"Created elliptical die with dimensions {length} x {width}.")
|
| 49 |
+
|
| 50 |
+
elif die_shape == "Hexagon":
|
| 51 |
+
# Visualize a hexagonal die
|
| 52 |
+
angle = np.linspace(0, 2 * np.pi, 7)
|
| 53 |
+
x = length / 2 * np.cos(angle) + length / 2
|
| 54 |
+
y = width / 2 * np.sin(angle) + width / 2
|
| 55 |
+
ax.fill(x, y, color='purple', edgecolor='black', lw=2, label='Hexagonal Die')
|
| 56 |
+
ax.set_xlim(0, length + 20)
|
| 57 |
+
ax.set_ylim(0, width + 20)
|
| 58 |
+
ax.set_title(f"Progressive Die Design - Hexagon {length}x{width}")
|
| 59 |
+
logger.info(f"Created hexagonal die with side length {length}.")
|
| 60 |
+
|
| 61 |
+
elif die_shape == "Polygon":
|
| 62 |
+
# Visualize a polygonal die (random polygon with 5 sides)
|
| 63 |
+
num_sides = 5
|
| 64 |
+
angle = np.linspace(0, 2 * np.pi, num_sides + 1)
|
| 65 |
+
radius = min(length, width) / 2
|
| 66 |
+
x = radius * np.cos(angle) + length / 2
|
| 67 |
+
y = radius * np.sin(angle) + width / 2
|
| 68 |
+
ax.fill(x, y, color='red', edgecolor='black', lw=2, label='Polygonal Die')
|
| 69 |
+
ax.set_xlim(0, length + 20)
|
| 70 |
+
ax.set_ylim(0, width + 20)
|
| 71 |
+
ax.set_title(f"Progressive Die Design - Polygon {length}x{width}")
|
| 72 |
+
logger.info(f"Created polygonal die with {num_sides} sides.")
|
| 73 |
+
|
| 74 |
+
ax.set_xlabel("X Dimension (mm)")
|
| 75 |
+
ax.set_ylabel("Y Dimension (mm)")
|
| 76 |
+
ax.legend(loc="upper right")
|
| 77 |
+
|
| 78 |
+
# Log the file saving step
|
| 79 |
+
filename = f"generated_die_{die_shape}_{length}x{width}.bmp"
|
| 80 |
+
plt.savefig(filename, format='bmp') # Save the plot as a BMP file
|
| 81 |
+
logger.info(f"Die design saved as {filename}.")
|
| 82 |
+
|
| 83 |
+
plt.close(fig) # Close the plot to prevent displaying twice
|
| 84 |
+
|
| 85 |
+
# Return success message and file path
|
| 86 |
+
return f"Die design saved successfully as {filename}. You can download the file.", filename
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
logger.error(f"Error generating die design: {str(e)}")
|
| 90 |
+
return f"Error generating die design: {str(e)}", None
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
# Gradio interface functions
|
| 94 |
+
def progressive_die_interface(length, width, thickness, die_shape):
|
| 95 |
+
# Generate die design and return file path and plot
|
| 96 |
+
die_message, filename = generate_die_ANSYS(length, width, thickness, die_shape)
|
| 97 |
+
|
| 98 |
+
# Log the completion of the task
|
| 99 |
+
logger.info(f"Die design process completed for shape {die_shape} with filename {filename}.")
|
| 100 |
+
|
| 101 |
+
return die_message, filename
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
# Create the Gradio Interface
|
| 105 |
+
with gr.Blocks() as app:
|
| 106 |
+
gr.Markdown("## Progressive Die Design Simulator")
|
| 107 |
+
gr.Markdown("This tool simulates the creation of progressive die designs. Select the die shape and enter dimensions to generate the die design.")
|
| 108 |
+
|
| 109 |
+
with gr.Row():
|
| 110 |
+
length = gr.Number(label="Length (mm)", value=100)
|
| 111 |
+
width = gr.Number(label="Width (mm)", value=50)
|
| 112 |
+
thickness = gr.Number(label="Thickness (mm)", value=10)
|
| 113 |
+
|
| 114 |
+
die_shape = gr.Dropdown(label="Die Shape", choices=["Rectangle", "Circle", "Ellipse", "Hexagon", "Polygon"], value="Rectangle")
|
| 115 |
+
die_button = gr.Button("Generate Die Design")
|
| 116 |
+
die_output = gr.Textbox(label="Die Design Message", interactive=False)
|
| 117 |
+
die_download = gr.File(label="Download Die Design Image")
|
| 118 |
+
|
| 119 |
+
die_button.click(progressive_die_interface, inputs=[length, width, thickness, die_shape], outputs=[die_output, die_download])
|
| 120 |
+
|
| 121 |
+
# Launch the app
|
| 122 |
+
app.launch()
|