Spaces:
Sleeping
Sleeping
File size: 3,477 Bytes
b722c3b c0ed18c 1873348 b722c3b 6078862 b722c3b c0ed18c caa3bde c0ed18c caa3bde c0ed18c b722c3b c0ed18c b722c3b 1873348 b722c3b 1873348 b722c3b c0ed18c 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 |
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.")
|