Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
import ezdxf
|
|
|
|
|
|
|
| 4 |
import groq
|
| 5 |
-
from ezdxf.entities import Insert, Line, Text, LWPolyline
|
| 6 |
-
|
| 7 |
-
# Initialize Groq client
|
| 8 |
-
client = groq.Client(api_key=os.getenv("GROQ_API_KEY"))
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
def
|
| 12 |
try:
|
| 13 |
doc = ezdxf.readfile(file_path)
|
| 14 |
msp = doc.modelspace()
|
|
@@ -17,29 +15,35 @@ def analyze_autocad_project(file_path):
|
|
| 17 |
connections = []
|
| 18 |
|
| 19 |
for entity in msp:
|
| 20 |
-
if
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
-
return
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
# RAG + Groq: Ask questions about specific components
|
| 40 |
def ask_groq_question(context_dict, query):
|
| 41 |
try:
|
| 42 |
-
|
|
|
|
| 43 |
component_text = "\n".join(
|
| 44 |
[f"{comp['type']} - {comp.get('name', comp.get('content', ''))} at {comp['location']}" for comp in context_dict["components"]]
|
| 45 |
)
|
|
@@ -56,42 +60,45 @@ def ask_groq_question(context_dict, query):
|
|
| 56 |
except Exception as e:
|
| 57 |
return f"Groq Error: {str(e)}"
|
| 58 |
|
| 59 |
-
#
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
-
file_input = gr.File(label="π Upload AutoCAD DXF
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
analysis_result = gr.Textbox(label="π Analysis Summary")
|
| 86 |
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
-
gr.
|
| 90 |
-
|
| 91 |
-
|
| 92 |
|
| 93 |
-
ask_btn = gr.Button("
|
| 94 |
-
ask_btn.click(
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
-
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
import ezdxf
|
| 4 |
+
import tempfile
|
| 5 |
+
import json
|
| 6 |
import groq
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# Function to parse the DXF and extract simple components and connections
|
| 9 |
+
def analyze_dxf(file_path):
|
| 10 |
try:
|
| 11 |
doc = ezdxf.readfile(file_path)
|
| 12 |
msp = doc.modelspace()
|
|
|
|
| 15 |
connections = []
|
| 16 |
|
| 17 |
for entity in msp:
|
| 18 |
+
if entity.dxftype() == "INSERT":
|
| 19 |
+
components.append({
|
| 20 |
+
"type": "BlockReference",
|
| 21 |
+
"name": entity.dxf.name,
|
| 22 |
+
"location": f"({entity.dxf.insert[0]}, {entity.dxf.insert[1]})"
|
| 23 |
+
})
|
| 24 |
+
elif entity.dxftype() == "TEXT":
|
| 25 |
+
components.append({
|
| 26 |
+
"type": "Text",
|
| 27 |
+
"content": entity.dxf.text,
|
| 28 |
+
"location": f"({entity.dxf.insert[0]}, {entity.dxf.insert[1]})"
|
| 29 |
+
})
|
| 30 |
+
elif entity.dxftype() == "LINE":
|
| 31 |
+
connections.append({
|
| 32 |
+
"type": "Line",
|
| 33 |
+
"start": f"({entity.dxf.start[0]}, {entity.dxf.start[1]})",
|
| 34 |
+
"end": f"({entity.dxf.end[0]}, {entity.dxf.end[1]})"
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
return components, connections
|
| 38 |
except Exception as e:
|
| 39 |
+
return [], []
|
| 40 |
+
|
| 41 |
+
# Function to ask Groq to analyze components
|
| 42 |
|
|
|
|
| 43 |
def ask_groq_question(context_dict, query):
|
| 44 |
try:
|
| 45 |
+
client = groq.Client(api_key=os.getenv("GROQ_API_KEY"))
|
| 46 |
+
|
| 47 |
component_text = "\n".join(
|
| 48 |
[f"{comp['type']} - {comp.get('name', comp.get('content', ''))} at {comp['location']}" for comp in context_dict["components"]]
|
| 49 |
)
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
return f"Groq Error: {str(e)}"
|
| 62 |
|
| 63 |
+
# Gradio interface
|
| 64 |
+
def process_file(file):
|
| 65 |
+
if file is None:
|
| 66 |
+
return "β οΈ Please upload a valid AutoCAD DXF file."
|
| 67 |
+
components, connections = analyze_dxf(file.name)
|
| 68 |
+
context = {
|
| 69 |
+
"components": components,
|
| 70 |
+
"connections": connections
|
| 71 |
+
}
|
| 72 |
+
# Store context in a temp file to retrieve later
|
| 73 |
+
temp_path = os.path.join(tempfile.gettempdir(), "context.json")
|
| 74 |
+
with open(temp_path, "w") as f:
|
| 75 |
+
json.dump(context, f)
|
| 76 |
+
return "β
File processed and understood. Now you can ask questions."
|
| 77 |
+
|
| 78 |
+
def query_groq(user_query):
|
| 79 |
+
temp_path = os.path.join(tempfile.gettempdir(), "context.json")
|
| 80 |
+
if not os.path.exists(temp_path):
|
| 81 |
+
return "β οΈ No DXF context available. Please upload a file first."
|
| 82 |
+
with open(temp_path, "r") as f:
|
| 83 |
+
context = json.load(f)
|
| 84 |
+
return ask_groq_question(context, user_query)
|
| 85 |
+
|
| 86 |
+
with gr.Blocks() as demo:
|
| 87 |
+
gr.Markdown("# π§ RAG-based AutoCAD Analyzer with Groq")
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
+
file_input = gr.File(label="π Upload AutoCAD DXF File", file_types=['.dxf'])
|
| 91 |
+
file_output = gr.Textbox(label="Status", interactive=False)
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
process_btn = gr.Button("π Analyze DXF")
|
| 94 |
+
process_btn.click(process_file, inputs=[file_input], outputs=[file_output])
|
| 95 |
|
| 96 |
+
with gr.Row():
|
| 97 |
+
user_query = gr.Textbox(label="π Ask a Question about the Circuit")
|
| 98 |
+
response = gr.Textbox(label="π¬ Groq Answer")
|
| 99 |
|
| 100 |
+
ask_btn = gr.Button("π€ Ask Groq")
|
| 101 |
+
ask_btn.click(query_groq, inputs=[user_query], outputs=[response])
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
+
demo.launch()
|