Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import FreeCAD
|
| 3 |
+
import Part
|
| 4 |
+
from FreeCAD import Base
|
| 5 |
+
|
| 6 |
+
# Hugging Face API URL (replace with your own model's endpoint)
|
| 7 |
+
huggingface_api_url = "https://api-inference.huggingface.co/models/your-username/your-model-name"
|
| 8 |
+
|
| 9 |
+
# Set the authorization token (replace with your Hugging Face API token)
|
| 10 |
+
headers = {
|
| 11 |
+
"Authorization": "Bearer your_huggingface_api_token"
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
# Function to query the Hugging Face model
|
| 15 |
+
def query_huggingface_model(inputs):
|
| 16 |
+
# Send data to Hugging Face API and get the response
|
| 17 |
+
response = requests.post(huggingface_api_url, headers=headers, json=inputs)
|
| 18 |
+
|
| 19 |
+
if response.status_code == 200:
|
| 20 |
+
return response.json() # Return the predictions
|
| 21 |
+
else:
|
| 22 |
+
raise Exception(f"Error querying model: {response.text}")
|
| 23 |
+
|
| 24 |
+
# Example design parameters (e.g., dimensions of the part)
|
| 25 |
+
design_parameters = {
|
| 26 |
+
"material": "steel",
|
| 27 |
+
"load": 1000, # Load applied to the part (in N)
|
| 28 |
+
"shape": "cylinder" # Shape of the part
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
# Query the Hugging Face model for optimized dimensions
|
| 32 |
+
predicted_parameters = query_huggingface_model(design_parameters)
|
| 33 |
+
|
| 34 |
+
# Extract the predicted parameters from the model response
|
| 35 |
+
optimized_radius = predicted_parameters.get("radius", 10) # Default to 10 if not returned
|
| 36 |
+
optimized_height = predicted_parameters.get("height", 50) # Default to 50 if not returned
|
| 37 |
+
|
| 38 |
+
# Create a new FreeCAD document
|
| 39 |
+
doc = FreeCAD.newDocument()
|
| 40 |
+
|
| 41 |
+
# Create a cylinder using the predicted dimensions
|
| 42 |
+
cylinder = Part.makeCylinder(optimized_radius, optimized_height)
|
| 43 |
+
|
| 44 |
+
# Add the cylinder to the document
|
| 45 |
+
part_object = doc.addObject("Part::Feature", "OptimizedCylinder")
|
| 46 |
+
part_object.Shape = cylinder
|
| 47 |
+
|
| 48 |
+
# Recompute the document to apply the changes
|
| 49 |
+
doc.recompute()
|
| 50 |
+
|
| 51 |
+
# Optionally save the document
|
| 52 |
+
doc.saveAs("optimized_cylinder.FCStd")
|
| 53 |
+
|
| 54 |
+
print(f"Created optimized cylinder with radius={optimized_radius} and height={optimized_height}")
|