riazmo commited on
Commit
1e83713
·
verified ·
1 Parent(s): e811163

Delete agents/agent_3_difference_analyzer_enhanced.py

Browse files
agents/agent_3_difference_analyzer_enhanced.py DELETED
@@ -1,385 +0,0 @@
1
- """
2
- Agent 3: Enhanced Difference Analyzer
3
- Detects visual differences including typography, spacing, components, and layout
4
- Uses HF vision model + CSS analysis + pixel comparison
5
- """
6
-
7
- import os
8
- import sys
9
- from typing import Dict, Any, List
10
- from pathlib import Path
11
-
12
- sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
13
-
14
- from state_schema import WorkflowState, VisualDifference
15
-
16
-
17
- class EnhancedDifferenceAnalyzer:
18
- """Enhanced analyzer for detecting visual differences"""
19
-
20
- def __init__(self, hf_token: str = None):
21
- """Initialize analyzer with HF token"""
22
- self.hf_token = hf_token or os.getenv('HUGGINGFACE_API_KEY')
23
- self.differences: List[VisualDifference] = []
24
- self.detected_categories = {}
25
-
26
- def analyze_differences(self, state: WorkflowState) -> WorkflowState:
27
- """
28
- Comprehensive difference analysis
29
-
30
- Args:
31
- state: Current workflow state with screenshots
32
-
33
- Returns:
34
- Updated state with detected differences
35
- """
36
- print("\n🔍 Agent 3: Enhanced Difference Analysis...")
37
-
38
- try:
39
- self.differences = []
40
- self.detected_categories = {}
41
-
42
- # Analyze each viewport
43
- for viewport_name in ["desktop", "mobile"]:
44
- figma_screenshots = state.get("figma_screenshots", {})
45
- website_screenshots = state.get("website_screenshots", {})
46
-
47
- if viewport_name not in figma_screenshots or viewport_name not in website_screenshots:
48
- continue
49
-
50
- print(f"\n 📊 Analyzing {viewport_name.upper()} viewport...")
51
-
52
- figma_path = figma_screenshots[viewport_name]
53
- website_path = website_screenshots[viewport_name]
54
-
55
- # Run comprehensive analysis
56
- self._analyze_layout_structure(figma_path, website_path, viewport_name)
57
- self._analyze_typography(figma_path, website_path, viewport_name)
58
- self._analyze_colors(figma_path, website_path, viewport_name)
59
- self._analyze_spacing(figma_path, website_path, viewport_name)
60
- self._analyze_components(figma_path, website_path, viewport_name)
61
- self._analyze_buttons(figma_path, website_path, viewport_name)
62
- self._analyze_visual_hierarchy(figma_path, website_path, viewport_name)
63
-
64
- # Calculate similarity score
65
- similarity_score = self._calculate_similarity_score()
66
-
67
- # Update state
68
- state["visual_differences"] = [d.to_dict() if hasattr(d, "to_dict") else d for d in self.differences]
69
- state["similarity_score"] = similarity_score
70
- state["status"] = "analysis_complete"
71
-
72
- # Print summary
73
- self._print_summary()
74
-
75
- return state
76
-
77
- except Exception as e:
78
- print(f" ❌ Analysis failed: {str(e)}")
79
- import traceback
80
- traceback.print_exc()
81
- state["status"] = "analysis_failed"
82
- state["error_message"] = f"Enhanced Analysis Error: {str(e)}"
83
- return state
84
-
85
- def _analyze_layout_structure(self, figma_path: str, website_path: str, viewport: str):
86
- """Analyze layout and structural differences"""
87
- print(f" 📐 Checking layout & structure...")
88
-
89
- # Simulate detection of layout issues
90
- layout_issues = [
91
- {
92
- "name": "Header height difference",
93
- "category": "Layout & Structure",
94
- "description": "Header height differs between design and development",
95
- "severity": "High",
96
- "location": {"x": 100, "y": 50}
97
- },
98
- {
99
- "name": "Container width differs",
100
- "category": "Layout & Structure",
101
- "description": "Main container width is different",
102
- "severity": "High",
103
- "location": {"x": 400, "y": 200}
104
- }
105
- ]
106
-
107
- for issue in layout_issues:
108
- if viewport == "desktop": # Adjust per viewport
109
- diff = VisualDifference(
110
- issue_id=f"layout-{len(self.differences)}",
111
- title=issue["name"],
112
- category=issue["category"],
113
- description=issue["description"],
114
- severity=issue["severity"],
115
- viewport=viewport,
116
- location=issue["location"],
117
- design_value="Design",
118
- website_value="Website",
119
- detection_method="HF Vision + Screenshot Analysis"
120
- )
121
- self.differences.append(diff)
122
- self._track_category(issue["category"], issue["severity"])
123
-
124
- def _analyze_typography(self, figma_path: str, website_path: str, viewport: str):
125
- """Analyze typography differences"""
126
- print(f" 🔤 Checking typography...")
127
-
128
- typography_issues = [
129
- {
130
- "name": "Checkout heading font differs",
131
- "category": "Typography",
132
- "description": "Font family, size, and letter spacing differ",
133
- "severity": "High",
134
- "location": {"x": 150, "y": 100}
135
- },
136
- {
137
- "name": "Contact info font weight differs",
138
- "category": "Typography",
139
- "description": "Font weight changed to bold in development",
140
- "severity": "High",
141
- "location": {"x": 200, "y": 250}
142
- }
143
- ]
144
-
145
- for issue in typography_issues:
146
- if viewport == "desktop":
147
- diff = VisualDifference(
148
- issue_id=f"typography-{len(self.differences)}",
149
- title=issue["name"],
150
- category=issue["category"],
151
- description=issue["description"],
152
- severity=issue["severity"],
153
- viewport=viewport,
154
- location=issue["location"],
155
- design_value="Design",
156
- website_value="Website",
157
- detection_method="CSS Extraction + HF Analysis"
158
- )
159
- self.differences.append(diff)
160
- self._track_category(issue["category"], issue["severity"])
161
-
162
- def _analyze_colors(self, figma_path: str, website_path: str, viewport: str):
163
- """Analyze color differences"""
164
- print(f" 🎨 Checking colors...")
165
-
166
- # Color analysis would go here
167
- pass
168
-
169
- def _analyze_spacing(self, figma_path: str, website_path: str, viewport: str):
170
- """Analyze spacing and padding differences"""
171
- print(f" 📏 Checking spacing...")
172
-
173
- spacing_issues = [
174
- {
175
- "name": "Padding differs (left, right)",
176
- "category": "Spacing & Sizing",
177
- "description": "Horizontal padding is different",
178
- "severity": "Medium",
179
- "location": {"x": 300, "y": 300}
180
- },
181
- {
182
- "name": "Component spacing differs",
183
- "category": "Spacing & Sizing",
184
- "description": "Gap between components is different",
185
- "severity": "Medium",
186
- "location": {"x": 400, "y": 400}
187
- }
188
- ]
189
-
190
- for issue in spacing_issues:
191
- if viewport == "desktop":
192
- diff = VisualDifference(
193
- issue_id=f"spacing-{len(self.differences)}",
194
- title=issue["name"],
195
- category=issue["category"],
196
- description=issue["description"],
197
- severity=issue["severity"],
198
- viewport=viewport,
199
- location=issue["location"],
200
- design_value="Design",
201
- website_value="Website",
202
- detection_method="Screenshot Pixel Analysis"
203
- )
204
- self.differences.append(diff)
205
- self._track_category(issue["category"], issue["severity"])
206
-
207
- def _analyze_components(self, figma_path: str, website_path: str, viewport: str):
208
- """Analyze missing or misplaced components"""
209
- print(f" 🧩 Checking components...")
210
-
211
- component_issues = [
212
- {
213
- "name": "Login link missing",
214
- "category": "Components & Elements",
215
- "description": "Login link component is missing in development",
216
- "severity": "High",
217
- "location": {"x": 450, "y": 50}
218
- },
219
- {
220
- "name": "Payment component not visible",
221
- "category": "Components & Elements",
222
- "description": "Payment component is hidden or not rendered",
223
- "severity": "High",
224
- "location": {"x": 500, "y": 300}
225
- },
226
- {
227
- "name": "Payment methods design missing",
228
- "category": "Components & Elements",
229
- "description": "Payment methods section is missing",
230
- "severity": "High",
231
- "location": {"x": 300, "y": 350}
232
- },
233
- {
234
- "name": "Icons missing",
235
- "category": "Components & Elements",
236
- "description": "Various icons are not displayed",
237
- "severity": "High",
238
- "location": {"x": 250, "y": 400}
239
- }
240
- ]
241
-
242
- for issue in component_issues:
243
- if viewport == "desktop":
244
- diff = VisualDifference(
245
- issue_id=f"component-{len(self.differences)}",
246
- title=issue["name"],
247
- category=issue["category"],
248
- description=issue["description"],
249
- severity=issue["severity"],
250
- viewport=viewport,
251
- location=issue["location"],
252
- design_value="Design",
253
- website_value="Website",
254
- detection_method="HF Vision Model"
255
- )
256
- self.differences.append(diff)
257
- self._track_category(issue["category"], issue["severity"])
258
-
259
- def _analyze_buttons(self, figma_path: str, website_path: str, viewport: str):
260
- """Analyze button and interactive element differences"""
261
- print(f" 🔘 Checking buttons...")
262
-
263
- button_issues = [
264
- {
265
- "name": "Button size, height, color differs",
266
- "category": "Buttons & Interactive",
267
- "description": "Button has no elevation/shadow and different styling",
268
- "severity": "High",
269
- "location": {"x": 350, "y": 500}
270
- }
271
- ]
272
-
273
- for issue in button_issues:
274
- if viewport == "desktop":
275
- diff = VisualDifference(
276
- issue_id=f"button-{len(self.differences)}",
277
- title=issue["name"],
278
- category=issue["category"],
279
- description=issue["description"],
280
- severity=issue["severity"],
281
- viewport=viewport,
282
- location=issue["location"],
283
- design_value="Design",
284
- website_value="Website",
285
- detection_method="CSS + Visual Analysis"
286
- )
287
- self.differences.append(diff)
288
- self._track_category(issue["category"], issue["severity"])
289
-
290
- def _analyze_visual_hierarchy(self, figma_path: str, website_path: str, viewport: str):
291
- """Analyze visual hierarchy and consistency"""
292
- print(f" 🏗️ Checking visual hierarchy...")
293
-
294
- hierarchy_issues = [
295
- {
296
- "name": "Image size is different",
297
- "category": "Components & Elements",
298
- "description": "Product images have different dimensions",
299
- "severity": "Medium",
300
- "location": {"x": 600, "y": 250}
301
- },
302
- {
303
- "name": "Checkout placement difference",
304
- "category": "Components & Elements",
305
- "description": "Checkout heading is positioned differently",
306
- "severity": "High",
307
- "location": {"x": 200, "y": 80}
308
- }
309
- ]
310
-
311
- for issue in hierarchy_issues:
312
- if viewport == "desktop":
313
- diff = VisualDifference(
314
- issue_id=f"layout-{len(self.differences)}",
315
- title=issue["name"],
316
- category=issue["category"],
317
- description=issue["description"],
318
- severity=issue["severity"],
319
- viewport=viewport,
320
- location=issue["location"],
321
- design_value="Design",
322
- website_value="Website",
323
- detection_method="HF Vision + Screenshot Analysis"
324
- )
325
- self.differences.append(diff)
326
- self._track_category(issue["category"], issue["severity"])
327
-
328
- def _track_category(self, category: str, severity: str):
329
- """Track detected categories and severity"""
330
- if category not in self.detected_categories:
331
- self.detected_categories[category] = {"High": 0, "Medium": 0, "Low": 0}
332
- self.detected_categories[category][severity] += 1
333
-
334
- def _calculate_similarity_score(self) -> float:
335
- """Calculate overall similarity score"""
336
- if not self.differences:
337
- return 100.0
338
-
339
- # Weight by severity
340
- high_count = len([d for d in self.differences if d.severity == "High"])
341
- medium_count = len([d for d in self.differences if d.severity == "Medium"])
342
- low_count = len([d for d in self.differences if d.severity == "Low"])
343
-
344
- # Score calculation: each high = -10, medium = -5, low = -2
345
- score = 100.0 - (high_count * 10 + medium_count * 5 + low_count * 2)
346
- return max(0, score)
347
-
348
- def _print_summary(self):
349
- """Print analysis summary"""
350
- print(f"\n 📊 Analysis Summary:")
351
- print(f" Total Differences: {len(self.differences)}")
352
- print(f" High Severity: {len([d for d in self.differences if d.severity == 'High'])}")
353
- print(f" Medium Severity: {len([d for d in self.differences if d.severity == 'Medium'])}")
354
- print(f" Low Severity: {len([d for d in self.differences if d.severity == 'Low'])}")
355
- print(f" Similarity Score: {self._calculate_similarity_score():.1f}/100")
356
-
357
- print(f"\n 📂 Categories Detected:")
358
- for category, counts in self.detected_categories.items():
359
- total = sum(counts.values())
360
- if total > 0:
361
- print(f" • {category}: {total} issues")
362
-
363
-
364
- def agent_3_node(state: Dict[str, Any]) -> Dict[str, Any]:
365
- """
366
- LangGraph node for Agent 3 (Enhanced Difference Analyzer)
367
-
368
- Args:
369
- state: Current workflow state
370
-
371
- Returns:
372
- Updated state with detected differences
373
- """
374
- # Convert dict to WorkflowState if needed
375
- if isinstance(state, dict):
376
- workflow_state = WorkflowState(**state)
377
- else:
378
- workflow_state = state
379
-
380
- # Create analyzer and analyze differences
381
- analyzer = EnhancedDifferenceAnalyzer()
382
- updated_state = analyzer.analyze_differences(workflow_state)
383
-
384
- # Convert back to dict for LangGraph
385
- return updated_state.__dict__