Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from PyPDF2 import PdfReader
|
| 5 |
+
from groq import Groq
|
| 6 |
+
|
| 7 |
+
# Initialize Groq API client
|
| 8 |
+
client = Groq(api_key="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")
|
| 9 |
+
|
| 10 |
+
# Example CAD metadata
|
| 11 |
+
cad_data = [
|
| 12 |
+
{"id": 1, "name": "Gear", "description": "A gear with specified teeth and diameter."},
|
| 13 |
+
{"id": 2, "name": "Bracket", "description": "A structural bracket for support."}
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
# Function to retrieve CAD templates using Groq API
|
| 17 |
+
def retrieve_cad_template_with_groq(query):
|
| 18 |
+
messages = [
|
| 19 |
+
{
|
| 20 |
+
"role": "user",
|
| 21 |
+
"content": f"Find a CAD template matching this query: {query}",
|
| 22 |
+
}
|
| 23 |
+
]
|
| 24 |
+
chat_completion = client.chat.completions.create(
|
| 25 |
+
messages=messages, model="llama-3.3-70b-versatile"
|
| 26 |
+
)
|
| 27 |
+
# Extract the response
|
| 28 |
+
response = chat_completion.choices[0].message.content
|
| 29 |
+
return response
|
| 30 |
+
|
| 31 |
+
# Function to process PDF input
|
| 32 |
+
def process_pdf(file_path):
|
| 33 |
+
reader = PdfReader(file_path)
|
| 34 |
+
text = ""
|
| 35 |
+
for page in reader.pages:
|
| 36 |
+
text += page.extract_text()
|
| 37 |
+
return text[:500] # Limit to 500 characters for API processing
|
| 38 |
+
|
| 39 |
+
# Function to generate OpenSCAD script for gear
|
| 40 |
+
def generate_gear(teeth, diameter):
|
| 41 |
+
scad_script = f"""
|
| 42 |
+
module gear() {{
|
| 43 |
+
difference() {{
|
| 44 |
+
circle(r={diameter / 2}, $fn=teeth);
|
| 45 |
+
translate([0, 0, -1]) circle(r={diameter / 3}, $fn=teeth);
|
| 46 |
+
}}
|
| 47 |
+
}}
|
| 48 |
+
gear();
|
| 49 |
+
"""
|
| 50 |
+
file_path = "generated_model.scad"
|
| 51 |
+
with open(file_path, "w") as file:
|
| 52 |
+
file.write(scad_script)
|
| 53 |
+
return file_path
|
| 54 |
+
|
| 55 |
+
# Streamlit UI
|
| 56 |
+
st.title("Quick CAD Model Generator")
|
| 57 |
+
st.sidebar.title("Input Options")
|
| 58 |
+
input_option = st.sidebar.radio("Select Input Type", ["Text Description", "Upload PDF", "Google Drive Link"])
|
| 59 |
+
|
| 60 |
+
# Process input
|
| 61 |
+
query = ""
|
| 62 |
+
if input_option == "Text Description":
|
| 63 |
+
query = st.text_input("Describe the part you need (e.g., 'A gear with 20 teeth').")
|
| 64 |
+
elif input_option == "Upload PDF":
|
| 65 |
+
pdf_file = st.file_uploader("Upload a PDF with a CAD drawing", type=["pdf"])
|
| 66 |
+
if pdf_file:
|
| 67 |
+
query = process_pdf(pdf_file)
|
| 68 |
+
st.write("Extracted Text:", query)
|
| 69 |
+
elif input_option == "Google Drive Link":
|
| 70 |
+
drive_link = st.text_input("Enter Google Drive Link for the CAD file")
|
| 71 |
+
if drive_link:
|
| 72 |
+
st.write(f"Processing file from: {drive_link}")
|
| 73 |
+
query = f"File from Google Drive: {drive_link}"
|
| 74 |
+
|
| 75 |
+
# Template suggestion using Groq API
|
| 76 |
+
if st.button("Find Template") and query:
|
| 77 |
+
try:
|
| 78 |
+
suggested_template = retrieve_cad_template_with_groq(query)
|
| 79 |
+
st.success(f"Suggested Template: {suggested_template}")
|
| 80 |
+
except Exception as e:
|
| 81 |
+
st.error(f"Error using Groq API: {e}")
|
| 82 |
+
|
| 83 |
+
# Generate model
|
| 84 |
+
part_type = st.selectbox("Select Part Type", ["Gear", "Bracket"])
|
| 85 |
+
teeth = st.number_input("Number of Teeth (for Gears)", min_value=10, max_value=100, step=1, value=20)
|
| 86 |
+
diameter = st.number_input("Diameter (mm)", min_value=10, max_value=200, step=1, value=50)
|
| 87 |
+
|
| 88 |
+
if st.button("Generate CAD Model"):
|
| 89 |
+
if part_type == "Gear":
|
| 90 |
+
file_path = generate_gear(teeth=teeth, diameter=diameter)
|
| 91 |
+
st.success(f"CAD Model Generated: {file_path}")
|
| 92 |
+
st.download_button("Download Model", open(file_path, "rb").read(), file_name="generated_model.scad")
|