Spaces:
Sleeping
Sleeping
import streamlit as st | |
import os | |
import tempfile | |
from io import BytesIO | |
from fpdf import FPDF | |
import requests | |
from stl import mesh | |
from pathlib import Path | |
# Directly assign the Groq API key (without using secrets) | |
GROQ_API_KEY = "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" | |
GROQ_API_URL = "https://api.groq.com/v1/query" | |
# 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 STL CAD files using pySTL | |
def parse_stl_file(file_path): | |
# Load the STL file and extract basic information | |
cad_mesh = mesh.Mesh.from_file(file_path) | |
# Example: Get the number of faces and volume | |
num_faces = cad_mesh.vectors.shape[0] | |
volume = cad_mesh.get_mass_properties()[0] # Get volume from mass properties | |
cad_data = f"Extracted information from {file_path.name}:\n - Number of Faces: {num_faces}\n - Volume: {volume} cubic units." | |
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"]) | |
if uploaded_file is not None: | |
# Ensure the temporary directory exists | |
temp_dir = tempfile.mkdtemp() | |
# Save uploaded file temporarily | |
temp_file_path = Path(temp_dir) / uploaded_file.name # Use the temporary directory | |
with open(temp_file_path, "wb") as f: | |
f.write(uploaded_file.getbuffer()) | |
# Parse the STL file and extract relevant data | |
cad_data = parse_stl_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.") | |