Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,10 @@ from io import BytesIO
|
|
| 5 |
from fpdf import FPDF
|
| 6 |
import requests
|
| 7 |
import json
|
|
|
|
| 8 |
from pathlib import Path
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Groq API endpoint and headers
|
| 11 |
GROQ_API_URL = "https://api.groq.com/v1/query"
|
|
@@ -33,10 +36,22 @@ def generate_report(cad_data):
|
|
| 33 |
st.error(f"Error in report generation: {response.text}")
|
| 34 |
return "Error generating report."
|
| 35 |
|
| 36 |
-
# Function to parse
|
| 37 |
def parse_cad_file(file_path):
|
| 38 |
-
#
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
return cad_data
|
| 41 |
|
| 42 |
# Function to create a PDF report
|
|
@@ -74,26 +89,27 @@ if uploaded_file is not None:
|
|
| 74 |
# Parse the CAD file and extract relevant data
|
| 75 |
cad_data = parse_cad_file(temp_file_path)
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
else:
|
| 99 |
st.write("Please upload a CAD file to begin.")
|
|
|
|
| 5 |
from fpdf import FPDF
|
| 6 |
import requests
|
| 7 |
import json
|
| 8 |
+
import pythonocc
|
| 9 |
from pathlib import Path
|
| 10 |
+
from OCC.Extend.DataExchange import read_step_file
|
| 11 |
+
from OCC.Core.STEPControl import STEPControl_Reader
|
| 12 |
|
| 13 |
# Groq API endpoint and headers
|
| 14 |
GROQ_API_URL = "https://api.groq.com/v1/query"
|
|
|
|
| 36 |
st.error(f"Error in report generation: {response.text}")
|
| 37 |
return "Error generating report."
|
| 38 |
|
| 39 |
+
# Function to parse STEP CAD files using pythonOCC
|
| 40 |
def parse_cad_file(file_path):
|
| 41 |
+
# Read the STEP file and extract data using pythonOCC
|
| 42 |
+
step_reader = STEPControl_Reader()
|
| 43 |
+
status = step_reader.ReadFile(str(file_path))
|
| 44 |
+
|
| 45 |
+
if status != 1: # If the file couldn't be read properly
|
| 46 |
+
st.error("Failed to read CAD file.")
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
step_reader.TransferRoot()
|
| 50 |
+
shape = step_reader.Shape()
|
| 51 |
+
|
| 52 |
+
# Extract basic information from the shape
|
| 53 |
+
# Here we can extract dimension, material properties, and more based on the available data
|
| 54 |
+
cad_data = f"Extracted information from {file_path.name}: \n - Shape contains {shape.NbChildren()} components.\n - Additional analysis can be performed."
|
| 55 |
return cad_data
|
| 56 |
|
| 57 |
# Function to create a PDF report
|
|
|
|
| 89 |
# Parse the CAD file and extract relevant data
|
| 90 |
cad_data = parse_cad_file(temp_file_path)
|
| 91 |
|
| 92 |
+
if cad_data:
|
| 93 |
+
# Display extracted data as a preview
|
| 94 |
+
st.write("Extracted Data from CAD File:")
|
| 95 |
+
st.text(cad_data)
|
| 96 |
|
| 97 |
+
# Generate report using Groq API
|
| 98 |
+
report_text = generate_report(cad_data)
|
| 99 |
+
st.write("Generated Report:")
|
| 100 |
+
st.text(report_text)
|
| 101 |
|
| 102 |
+
# Create a PDF report
|
| 103 |
+
pdf_file_path = create_pdf_report(report_text)
|
| 104 |
|
| 105 |
+
# Provide a download link for the PDF report
|
| 106 |
+
with open(pdf_file_path, "rb") as f:
|
| 107 |
+
pdf_bytes = f.read()
|
| 108 |
+
st.download_button(
|
| 109 |
+
label="Download Technical Report",
|
| 110 |
+
data=pdf_bytes,
|
| 111 |
+
file_name="technical_report.pdf",
|
| 112 |
+
mime="application/pdf"
|
| 113 |
+
)
|
| 114 |
else:
|
| 115 |
st.write("Please upload a CAD file to begin.")
|