File size: 3,725 Bytes
b722c3b
 
 
 
 
 
 
caa3bde
b722c3b
caa3bde
 
b722c3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caa3bde
b722c3b
caa3bde
 
 
 
 
 
 
 
 
 
 
 
 
 
b722c3b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caa3bde
 
 
 
b722c3b
caa3bde
 
 
 
b722c3b
caa3bde
 
b722c3b
caa3bde
 
 
 
 
 
 
 
 
b722c3b
 
1
2
3
4
5
6
7
8
9
10
11
12
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import streamlit as st
import os
import tempfile
from io import BytesIO
from fpdf import FPDF
import requests
import json
import pythonocc
from pathlib import Path
from OCC.Extend.DataExchange import read_step_file
from OCC.Core.STEPControl import STEPControl_Reader

# Groq API endpoint and headers
GROQ_API_URL = "https://api.groq.com/v1/query"
GROQ_API_KEY = "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"

# Function to generate a technical report using the AI model (Groq API)
def generate_report(cad_data):
    # Making the API request to Groq API
    headers = {
        "Authorization": f"Bearer {GROQ_API_KEY}",
        "Content-Type": "application/json"
    }

    payload = {
        "model": "llama-3.3-70b-versatile",
        "input": cad_data,
        "output_format": "text"
    }

    response = requests.post(GROQ_API_URL, headers=headers, json=payload)

    if response.status_code == 200:
        return response.json()['generated_text']
    else:
        st.error(f"Error in report generation: {response.text}")
        return "Error generating report."

# Function to parse STEP CAD files using pythonOCC
def parse_cad_file(file_path):
    # Read the STEP file and extract data using pythonOCC
    step_reader = STEPControl_Reader()
    status = step_reader.ReadFile(str(file_path))
    
    if status != 1:  # If the file couldn't be read properly
        st.error("Failed to read CAD file.")
        return None
    
    step_reader.TransferRoot()
    shape = step_reader.Shape()
    
    # Extract basic information from the shape
    # Here we can extract dimension, material properties, and more based on the available data
    cad_data = f"Extracted information from {file_path.name}: \n - Shape contains {shape.NbChildren()} components.\n - Additional analysis can be performed."
    return cad_data

# Function to create a PDF report
def create_pdf_report(report_text):
    pdf = FPDF()
    pdf.set_auto_page_break(auto=True, margin=15)
    pdf.add_page()
    
    # Set title and content
    pdf.set_font("Arial", style="B", size=16)
    pdf.cell(200, 10, txt="Technical Report for CAD Analysis", ln=True, align="C")
    
    pdf.ln(10)  # Line break
    pdf.set_font("Arial", size=12)
    pdf.multi_cell(0, 10, report_text)
    
    # Save PDF to a file
    temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
    pdf.output(temp_file.name)
    
    return temp_file.name

# Streamlit UI
st.title("CAD File Analysis and Technical Report Generation")

# File uploader
uploaded_file = st.file_uploader("Upload a CAD file", type=["stl", "step", "dxf", "iges"])

if uploaded_file is not None:
    # Save uploaded file temporarily
    temp_file_path = Path(tempfile.mktemp()) / uploaded_file.name
    with open(temp_file_path, "wb") as f:
        f.write(uploaded_file.getbuffer())

    # Parse the CAD file and extract relevant data
    cad_data = parse_cad_file(temp_file_path)

    if cad_data:
        # Display extracted data as a preview
        st.write("Extracted Data from CAD File:")
        st.text(cad_data)

        # Generate report using Groq API
        report_text = generate_report(cad_data)
        st.write("Generated Report:")
        st.text(report_text)

        # Create a PDF report
        pdf_file_path = create_pdf_report(report_text)

        # Provide a download link for the PDF report
        with open(pdf_file_path, "rb") as f:
            pdf_bytes = f.read()
            st.download_button(
                label="Download Technical Report",
                data=pdf_bytes,
                file_name="technical_report.pdf",
                mime="application/pdf"
            )
else:
    st.write("Please upload a CAD file to begin.")