MuhammadSajid commited on
Commit
9258ded
Β·
verified Β·
1 Parent(s): 2626333

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -0
app.py CHANGED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Parse the DXF file and extract basic entities
11
+ def analyze_autocad_project(file_path):
12
+ try:
13
+ doc = ezdxf.readfile(file_path)
14
+ msp = doc.modelspace()
15
+
16
+ components = []
17
+ connections = []
18
+
19
+ for entity in msp:
20
+ if isinstance(entity, Insert):
21
+ name = entity.dxf.name
22
+ location = entity.dxf.insert
23
+ components.append({"type": "BLOCK", "name": name, "location": (location.x, location.y)})
24
+ elif isinstance(entity, Line):
25
+ start = (entity.dxf.start.x, entity.dxf.start.y)
26
+ end = (entity.dxf.end.x, entity.dxf.end.y)
27
+ connections.append({"type": "LINE", "start": start, "end": end})
28
+ elif isinstance(entity, LWPolyline):
29
+ points = entity.get_points("xy")
30
+ connections.append({"type": "POLYLINE", "points": points})
31
+ elif isinstance(entity, Text):
32
+ components.append({"type": "TEXT", "content": entity.dxf.text, "location": (entity.dxf.insert.x, entity.dxf.insert.y)})
33
+
34
+ summary = f"πŸ” Found {len(components)} components and {len(connections)} connections."
35
+ return summary, {"components": components, "connections": connections}
36
+ except Exception as e:
37
+ return f"❌ Error: {str(e)}", None
38
+
39
+ # RAG + Groq: Ask questions about specific components
40
+ def ask_groq_question(context_dict, query):
41
+ try:
42
+ # Provide summarized context to Groq
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
+ )
46
+ connection_text = "\n".join(
47
+ [f"{conn['type']} from {conn.get('start', '')} to {conn.get('end', '')}" for conn in context_dict["connections"]]
48
+ )
49
+ context = f"COMPONENTS:\n{component_text}\n\nCONNECTIONS:\n{connection_text}\n\nQUESTION:\n{query}"
50
+
51
+ response = client.chat.completions.create(
52
+ model="llama3-8b-8192",
53
+ messages=[{"role": "user", "content": context}]
54
+ )
55
+ return response.choices[0].message.content
56
+ except Exception as e:
57
+ return f"Groq Error: {str(e)}"
58
+
59
+ # Global store for RAG context
60
+ rag_context = {}
61
+
62
+ # Gradio Interface
63
+ def upload_and_analyze(file):
64
+ summary, context = analyze_autocad_project(file.name)
65
+ if context:
66
+ global rag_context
67
+ rag_context = context
68
+ return summary
69
+
70
+ def query_component(user_query):
71
+ if not rag_context:
72
+ return "⚠️ Please upload and analyze a DXF file first."
73
+ return ask_groq_question(rag_context, user_query)
74
+
75
+ with gr.Blocks() as app:
76
+ gr.Markdown("# πŸ”Œ AutoCAD Electrical Project Analyzer with Groq RAG")
77
+ gr.Markdown("""
78
+ Upload your AutoCAD DXF file to analyze the electrical project layout. Ask questions about specific components, connections, or trace paths.
79
+ """)
80
+
81
+ with gr.Row():
82
+ file_input = gr.File(label="πŸ“ Upload AutoCAD DXF Project")
83
+ upload_btn = gr.Button("πŸ“Š Analyze Project")
84
+
85
+ analysis_result = gr.Textbox(label="πŸ“ Analysis Summary")
86
+
87
+ upload_btn.click(fn=upload_and_analyze, inputs=file_input, outputs=analysis_result)
88
+
89
+ gr.Markdown("### πŸ’¬ Ask about Component Back-tracing or Details")
90
+ user_query = gr.Textbox(label="πŸ” Enter your question (e.g., trace component R1)")
91
+ answer_box = gr.Textbox(label="🧠 Groq RAG Answer")
92
+
93
+ ask_btn = gr.Button("🧠 Ask Groq")
94
+ ask_btn.click(fn=query_component, inputs=user_query, outputs=answer_box)
95
+
96
+ if __name__ == "__main__":
97
+ app.launch()