engrharis's picture
Update app.py
caa3bde verified
raw
history blame
3.73 kB
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.")