mughal-88 commited on
Commit
79f0b14
·
verified ·
1 Parent(s): 013f626

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -52
app.py CHANGED
@@ -1,64 +1,70 @@
 
1
  import streamlit as st
2
- import trimesh # For 3D model processing
 
 
3
  import requests
4
 
5
- # Replace with your actual Groq API endpoint URL
6
- GROQ_API_URL = "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"
7
 
 
 
 
 
 
 
 
8
 
9
- def process_model(model_path):
10
- # Load the 3D model using trimesh
11
- try:
12
- mesh = trimesh.load(model_path)
13
- except FileNotFoundError:
14
- st.error(f"Error: File not found at path '{model_path}'.")
15
- return None
16
- except (trimesh.exceptions.ImportError, ValueError) as e:
17
- # Handle potential format-specific errors (e.g., STEP format issues)
18
- st.error(f"Error loading model: {e}")
19
- return None
20
 
21
- # Extract features (replace with relevant features for material estimation)
22
- features = {
23
- "surface_area": mesh.area,
24
- "volume": mesh.volume,
25
- # Add other relevant features based on your requirements
26
- }
27
-
28
- # Encode features (replace with suitable encoding method)
29
- encoded_features = sum(features.values()) # Placeholder, replace with actual encoding
30
-
31
- # Send POST request to Groq API
32
- response = requests.post(GROQ_API_URL, json={"features": encoded_features})
33
-
34
- # Handle potential errors from the API call
35
  if response.status_code == 200:
36
- material_requirements = response.json()["material_requirements"]
37
- return material_requirements
38
- else:
39
- error_message = f"Error: Groq API call failed with status code {response.status_code}"
40
- st.error(error_message)
41
- return None
42
-
43
-
44
- def main():
45
- st.title("RAG-based Material Estimation App (Groq API)")
46
-
47
- uploaded_file = st.file_uploader("Upload 3D CAD Model (Supported formats: .stl, .obj, .stp, etc.)")
48
-
49
- if uploaded_file is not None:
50
- model_path = uploaded_file.name
51
  else:
52
- model_path = None # Set model_path to None if no file is uploaded
 
53
 
54
- # ... (rest of your code for sheet dimensions and quantity)
 
 
 
 
55
 
56
- if st.button("Estimate Material Requirements"):
57
- if model_path:
58
- material_requirements = process_model(model_path)
59
- if material_requirements:
60
- st.success(f"Estimated Material Requirements:\n{material_requirements}")
61
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- if __name__ == "__main__":
64
- main()
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
  import streamlit as st
3
+ from groq import Groq
4
+ from pyvista import read
5
+ from cadquery import exporters
6
  import requests
7
 
8
+ # Initialize Groq API client
9
+ client = Groq(api_key="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")
10
 
11
+ # Case 1: File Upload and DFM Analysis
12
+ def process_uploaded_file(uploaded_file):
13
+ file_path = f"uploaded_{uploaded_file.name}"
14
+ with open(file_path, "wb") as f:
15
+ f.write(uploaded_file.getbuffer())
16
+ st.write(f"File saved as: {file_path}")
17
+ return file_path
18
 
19
+ def perform_dfm_analysis(file_path):
20
+ st.write("Performing DFM Analysis...")
21
+ # Placeholder for actual DFM analysis logic using LLMs
22
+ result = {"CNC_Machinable": True, "Complexity_Score": 0.75} # Mock result
23
+ return result
 
 
 
 
 
 
24
 
25
+ # Case 2: Template Selection and CAD Generation
26
+ def get_template_list():
27
+ dataset_url = "https://huggingface.co/datasets/FreedomIntelligence/CADBench"
28
+ response = requests.get(f"{dataset_url}/resolve/main/metadata.json")
 
 
 
 
 
 
 
 
 
 
29
  if response.status_code == 200:
30
+ templates = response.json().get("templates", [])
31
+ return templates
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  else:
33
+ st.error("Failed to fetch templates.")
34
+ return []
35
 
36
+ def generate_cad_from_template(template_name, parameters):
37
+ st.write(f"Generating CAD for template: {template_name}")
38
+ # Placeholder for template CAD generation logic
39
+ generated_file = f"{template_name}_output.step"
40
+ return generated_file
41
 
42
+ # Streamlit App Interface
43
+ st.title("Quick CAD Model Generator")
44
+ st.sidebar.header("Select Case")
45
+ case = st.sidebar.radio("Choose a Case", ["Upload File for DFM", "Select Predefined Template"])
 
46
 
47
+ if case == "Upload File for DFM":
48
+ uploaded_file = st.file_uploader("Upload your CAD file", type=["step", "stl", "obj", "dwg"])
49
+ if uploaded_file:
50
+ file_path = process_uploaded_file(uploaded_file)
51
+ if st.button("Analyze for CNC"):
52
+ try:
53
+ dfm_result = perform_dfm_analysis(file_path)
54
+ st.success("DFM Analysis Completed!")
55
+ st.json(dfm_result)
56
+ except Exception as e:
57
+ st.error(f"Error in DFM Analysis: {e}")
58
 
59
+ elif case == "Select Predefined Template":
60
+ templates = get_template_list()
61
+ if templates:
62
+ template_name = st.selectbox("Choose a Template", templates)
63
+ parameters = st.text_area("Enter Parameters (JSON format)", placeholder='{"length": 100, "width": 50}')
64
+ if st.button("Generate CAD"):
65
+ try:
66
+ generated_file = generate_cad_from_template(template_name, parameters)
67
+ st.success("CAD Model Generated!")
68
+ st.download_button("Download CAD File", open(generated_file, "rb"), file_name=generated_file)
69
+ except Exception as e:
70
+ st.error(f"Error in CAD Generation: {e}")