Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import tempfile
|
4 |
+
from io import BytesIO
|
5 |
+
from fpdf import FPDF
|
6 |
+
import requests
|
7 |
+
import json
|
8 |
+
from pathlib import Path
|
9 |
+
|
10 |
+
# Groq API endpoint and headers
|
11 |
+
GROQ_API_URL = "https://api.groq.com/v1/query"
|
12 |
+
GROQ_API_KEY = "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"
|
13 |
+
|
14 |
+
# Function to generate a technical report using the AI model (Groq API)
|
15 |
+
def generate_report(cad_data):
|
16 |
+
# Making the API request to Groq API
|
17 |
+
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 and extract relevant data from CAD files
|
37 |
+
def parse_cad_file(file_path):
|
38 |
+
# Placeholder: Extract relevant data from CAD file (This can be improved with actual CAD parsing libraries)
|
39 |
+
cad_data = f"Extracted information from {file_path.name}: \n - Dimensions: 100mm x 50mm\n - Material: Steel\n - Weight: 1.5kg"
|
40 |
+
return cad_data
|
41 |
+
|
42 |
+
# Function to create a PDF report
|
43 |
+
def create_pdf_report(report_text):
|
44 |
+
pdf = FPDF()
|
45 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
46 |
+
pdf.add_page()
|
47 |
+
|
48 |
+
# Set title and content
|
49 |
+
pdf.set_font("Arial", style="B", size=16)
|
50 |
+
pdf.cell(200, 10, txt="Technical Report for CAD Analysis", ln=True, align="C")
|
51 |
+
|
52 |
+
pdf.ln(10) # Line break
|
53 |
+
pdf.set_font("Arial", size=12)
|
54 |
+
pdf.multi_cell(0, 10, report_text)
|
55 |
+
|
56 |
+
# Save PDF to a file
|
57 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
|
58 |
+
pdf.output(temp_file.name)
|
59 |
+
|
60 |
+
return temp_file.name
|
61 |
+
|
62 |
+
# Streamlit UI
|
63 |
+
st.title("CAD File Analysis and Technical Report Generation")
|
64 |
+
|
65 |
+
# File uploader
|
66 |
+
uploaded_file = st.file_uploader("Upload a CAD file", type=["stl", "step", "dxf", "iges"])
|
67 |
+
|
68 |
+
if uploaded_file is not None:
|
69 |
+
# Save uploaded file temporarily
|
70 |
+
temp_file_path = Path(tempfile.mktemp()) / uploaded_file.name
|
71 |
+
with open(temp_file_path, "wb") as f:
|
72 |
+
f.write(uploaded_file.getbuffer())
|
73 |
+
|
74 |
+
# Parse the CAD file and extract relevant data
|
75 |
+
cad_data = parse_cad_file(temp_file_path)
|
76 |
+
|
77 |
+
# Display extracted data as a preview
|
78 |
+
st.write("Extracted Data from CAD File:")
|
79 |
+
st.text(cad_data)
|
80 |
+
|
81 |
+
# Generate report using Groq API
|
82 |
+
report_text = generate_report(cad_data)
|
83 |
+
st.write("Generated Report:")
|
84 |
+
st.text(report_text)
|
85 |
+
|
86 |
+
# Create a PDF report
|
87 |
+
pdf_file_path = create_pdf_report(report_text)
|
88 |
+
|
89 |
+
# Provide a download link for the PDF report
|
90 |
+
with open(pdf_file_path, "rb") as f:
|
91 |
+
pdf_bytes = f.read()
|
92 |
+
st.download_button(
|
93 |
+
label="Download Technical Report",
|
94 |
+
data=pdf_bytes,
|
95 |
+
file_name="technical_report.pdf",
|
96 |
+
mime="application/pdf"
|
97 |
+
)
|
98 |
+
else:
|
99 |
+
st.write("Please upload a CAD file to begin.")
|