engrharis/CAD_Report_Maker
0
1import streamlit as st2import os3import tempfile4from io import BytesIO5from fpdf import FPDF6import requests7from stl import mesh8from pathlib import Path9 10# Directly assign the Groq API key (without using secrets)11GROQ_API_KEY = "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"12GROQ_API_URL = "https://api.groq.com/v1/query"13 14# Function to generate a technical report using the AI model (Groq API)15def generate_report(cad_data):16 # Making the API request to Groq API17 headers = {18 "Authorization": f"Bearer {GROQ_API_KEY}",19 "Content-Type": "application/json"20 }21 22 payload = {23 "model": "llama-3.3-70b-versatile",24 "input": cad_data,25 "output_format": "text"26 }27 28 response = requests.post(GROQ_API_URL, headers=headers, json=payload)29 30 if response.status_code == 200:31 return response.json()['generated_text']32 else:33 st.error(f"Error in report generation: {response.text}")34 return "Error generating report."35 36# Function to parse STL CAD files using pySTL37def parse_stl_file(file_path):38 # Load the STL file and extract basic information39 cad_mesh = mesh.Mesh.from_file(file_path)40 41 # Example: Get the number of faces and volume42 num_faces = cad_mesh.vectors.shape[0]43 volume = cad_mesh.get_mass_properties()[0] # Get volume from mass properties44 45 cad_data = f"Extracted information from {file_path.name}:\n - Number of Faces: {num_faces}\n - Volume: {volume} cubic units."46 return cad_data47 48# Function to create a PDF report49def create_pdf_report(report_text):50 pdf = FPDF()51 pdf.set_auto_page_break(auto=True, margin=15)52 pdf.add_page()53 54 # Set title and content55 pdf.set_font("Arial", style="B", size=16)56 pdf.cell(200, 10, txt="Technical Report for CAD Analysis", ln=True, align="C")57 58 pdf.ln(10) # Line break59 pdf.set_font("Arial", size=12)60 pdf.multi_cell(0, 10, report_text)61 62 # Save PDF to a file63 temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")64 pdf.output(temp_file.name)65 66 return temp_file.name67 68# Streamlit UI69st.title("CAD File Analysis and Technical Report Generation")70 71# File uploader72uploaded_file = st.file_uploader("Upload a CAD file", type=["stl"])73 74if uploaded_file is not None:75 # Ensure the temporary directory exists76 temp_dir = tempfile.mkdtemp()77 78 # Save uploaded file temporarily79 temp_file_path = Path(temp_dir) / uploaded_file.name # Use the temporary directory80 with open(temp_file_path, "wb") as f:81 f.write(uploaded_file.getbuffer())82 83 # Parse the STL file and extract relevant data84 cad_data = parse_stl_file(temp_file_path)85 86 if cad_data:87 # Display extracted data as a preview88 st.write("Extracted Data from CAD File:")89 st.text(cad_data)90 91 # Generate report using Groq API92 report_text = generate_report(cad_data)93 st.write("Generated Report:")94 st.text(report_text)95 96 # Create a PDF report97 pdf_file_path = create_pdf_report(report_text)98 99 # Provide a download link for the PDF report100 with open(pdf_file_path, "rb") as f:101 pdf_bytes = f.read()102 st.download_button(103 label="Download Technical Report",104 data=pdf_bytes,105 file_name="technical_report.pdf",106 mime="application/pdf"107 )108else:109 st.write("Please upload a CAD file to begin.")110 