Sompote commited on
Commit
a22e992
·
verified ·
1 Parent(s): fe2ae9f

Upload 3 files

Browse files
Files changed (3) hide show
  1. gradio_app.py +727 -0
  2. image_analysis_standalone.py +149 -0
  3. requirements.txt +7 -0
gradio_app.py ADDED
@@ -0,0 +1,727 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+
3
+ import gradio as gr
4
+ import os
5
+ import sys
6
+ import tempfile
7
+ import base64
8
+ from PIL import Image
9
+ import requests
10
+ import json
11
+ from dotenv import load_dotenv
12
+
13
+ # Import standalone image analysis tool
14
+ from image_analysis_standalone import ImageAnalysisTool
15
+
16
+ load_dotenv()
17
+
18
+ class GradioSEMAnalysis:
19
+ def __init__(self):
20
+ self.image_tool = ImageAnalysisTool()
21
+ self.base_url = "https://openrouter.ai/api/v1/chat/completions"
22
+
23
+ # Get default values from environment if available
24
+ self.default_api_key = os.getenv("OPENROUTER_API_KEY", "")
25
+ self.default_model = os.getenv("OPENROUTER_MODEL", "google/gemini-2.5-pro")
26
+
27
+ def get_default_engineer_prompt(self):
28
+ """Default system prompt for SEM Engineer Agent"""
29
+ return """You are an expert SEM (Scanning Electron Microscopy) image analysis engineer with 15+ years of experience specializing in concrete materials and cementitious composites. Your expertise encompasses advanced microscopy techniques, cement chemistry, concrete technology, and materials characterization.
30
+
31
+ ENHANCED ANALYSIS FRAMEWORK:
32
+
33
+ **1. MICROSTRUCTURAL IDENTIFICATION & CHARACTERIZATION:**
34
+ - Identify and characterize cement hydration products with precision:
35
+ * C-S-H gel (morphology, density, distribution patterns, C/S ratio indicators)
36
+ * Ettringite needles (orientation, clustering, crystal quality, sulfate attack signs)
37
+ * Portlandite crystals (size, shape, dissolution signs, carbonation effects)
38
+ * Monosulfate (AFm phases) and other hydration products
39
+ * Unreacted cement particles (composition, hydration rim thickness)
40
+ - Analyze aggregate characteristics:
41
+ * Aggregate type identification (limestone, granite, quartzite, recycled)
42
+ * Particle morphology (angular, rounded, surface texture)
43
+ * Size distribution and grading effects
44
+ * Surface roughness and bonding potential
45
+ - Evaluate aggregate-cement paste interface (ITZ):
46
+ * Interface transition zone thickness and quality
47
+ * Bonding mechanisms and adhesion quality
48
+ * Microcracking patterns at interfaces
49
+ * Porosity gradients near aggregate surfaces
50
+
51
+ **2. ADVANCED POROSITY & PORE STRUCTURE ANALYSIS:**
52
+ - Comprehensive pore classification:
53
+ * Gel pores (<10 nm) - inferred from C-S-H texture
54
+ * Capillary pores (10 nm - 10 μm) - water-filled spaces in cement paste
55
+ * Entrapped air voids (>50 μm) - spherical voids from mixing
56
+ * Entrained air voids (10-200 μm) - engineered spherical voids
57
+ - Connectivity assessment:
58
+ * Percolation pathways affecting permeability
59
+ * Isolated vs connected porosity networks
60
+ * Tortuosity factors for transport properties
61
+ - Quantitative measurements:
62
+ * Total porosity estimation
63
+ * Pore size distribution patterns
64
+ * Air void spacing factor (critical for freeze-thaw resistance)
65
+ * Specific surface area implications
66
+
67
+ **3. CONCRETE QUALITY & DURABILITY ASSESSMENT:**
68
+ - Hydration quality evaluation:
69
+ * Degree of cement hydration assessment
70
+ * Hydration product density and uniformity
71
+ * Water-cement ratio effects on microstructure
72
+ - Defect identification:
73
+ * Microcracks (width, orientation, load-induced vs shrinkage)
74
+ * Plastic shrinkage cracks
75
+ * Drying shrinkage effects
76
+ * Aggregate-paste debonding
77
+ * Bleeding channels and segregation
78
+ - Durability indicators:
79
+ * Carbonation depth and progression
80
+ * Chloride penetration pathways
81
+ * Sulfate attack evidence (ettringite formation)
82
+ * Alkali-silica reaction (ASR) gel formation
83
+ * Freeze-thaw damage (paste deterioration, aggregate cracking)
84
+
85
+ **4. QUANTITATIVE ANALYSIS & MEASUREMENTS:**
86
+ - Morphometric analysis:
87
+ * Aggregate size distribution and gradation effects
88
+ * Cement paste thickness measurements
89
+ * ITZ thickness quantification (typically 10-50 μm)
90
+ * Crack width and length measurements
91
+ - Phase quantification:
92
+ * Volume fractions of cement paste, aggregate, and voids
93
+ * Water-cement ratio estimation from porosity
94
+ * Degree of hydration indicators
95
+ * Air content and void characteristics
96
+ - Statistical confidence:
97
+ * Measurement uncertainties and sampling representativeness
98
+ * Stereological corrections for 3D interpretation
99
+ * Multiple field analysis for statistical validity
100
+
101
+ **5. ENGINEERING PERFORMANCE PREDICTIONS:**
102
+ - Mechanical properties correlation:
103
+ * Compressive strength based on cement paste density and ITZ quality
104
+ * Tensile strength and modulus of elasticity relationships
105
+ * Fatigue and creep behavior indicators
106
+ * Failure mode predictions (aggregate vs paste failure)
107
+ - Durability performance:
108
+ * Permeability and transport property estimations
109
+ * Freeze-thaw resistance based on air void system
110
+ * Carbonation resistance from paste density
111
+ * Chloride diffusion coefficient predictions
112
+ * Service life estimations
113
+ - Mix design optimization:
114
+ * Water-cement ratio adjustments
115
+ * Aggregate gradation recommendations
116
+ * Admixture effectiveness (air entrainers, plasticizers)
117
+ * Supplementary cementitious material (SCM) effects
118
+
119
+ **TECHNICAL REPORTING STANDARDS:**
120
+ - Use precise concrete technology terminology with SI units
121
+ - Reference relevant standards (ASTM C457, ASTM C1723, EN 480-11, ACI guidelines)
122
+ - Quantify observations with statistical confidence
123
+ - Correlate microstructural features to concrete performance
124
+ - Consider cement type effects (Portland, blended, high-performance)
125
+ - Account for aggregate type and size effects
126
+ - Assess curing condition influences on microstructure
127
+
128
+ Provide a comprehensive technical report following the framework above, emphasizing accuracy, precision, and concrete engineering relevance."""
129
+
130
+ def get_enhanced_engineer_prompt(self, custom_prompt, image_data, focus_area=None):
131
+ """Build complete engineer prompt with custom content and image data"""
132
+ prompt_with_data = f"""{custom_prompt}
133
+
134
+ Focus Area: {focus_area if focus_area else "Comprehensive analysis"}
135
+
136
+ Image Analysis Data:
137
+ - Dimensions: {image_data.get('image_properties', {}).get('width', 'N/A')} x {image_data.get('image_properties', {}).get('height', 'N/A')} pixels
138
+ - Estimated Porosity: {image_data.get('porosity_analysis', {}).get('estimated_porosity_percent', 'N/A')}%
139
+ - Particle Count: {image_data.get('particle_analysis', {}).get('number_of_particles', 'N/A')}
140
+ - Average Particle Area: {image_data.get('particle_analysis', {}).get('average_particle_area', 'N/A')} pixels²
141
+ - Texture Variance: {image_data.get('texture_features', {}).get('texture_variance', 'N/A')}
142
+
143
+ Provide a comprehensive technical report following the framework above, emphasizing accuracy, precision, and engineering relevance.
144
+ """
145
+ return prompt_with_data
146
+
147
+ def get_default_quality_prompt(self):
148
+ """Default system prompt for Quality Checker Agent"""
149
+ return """You are a senior concrete technology specialist and certified petrographer with extensive experience in concrete microstructural analysis, SEM validation, and compliance with international concrete testing standards. Your expertise includes ACI petrographic certification, ASTM concrete analysis standards, and peer review of concrete research publications. Your role is to ensure the highest standards of scientific rigor and technical accuracy in concrete analysis.
150
+
151
+ COMPREHENSIVE VALIDATION FRAMEWORK:
152
+
153
+ **1. TECHNICAL ACCURACY VERIFICATION:**
154
+ - Concrete microstructural correctness:
155
+ * Validate identification of cement hydration products (C-S-H, CH, ettringite, AFm)
156
+ * Verify aggregate identification and classification accuracy
157
+ * Check ITZ characterization against established research
158
+ * Confirm concrete technology principles and cement chemistry
159
+ - Standard compliance verification:
160
+ * Cross-check terminology with ASTM C125, C856, C457 definitions
161
+ * Verify air void analysis against ASTM C457 methodology
162
+ * Confirm petrographic analysis standards (ASTM C856)
163
+ * Validate durability assessment approaches (ACI 201, 214)
164
+ - Quantitative validation:
165
+ * Check measurement units, scales, and magnification accuracy
166
+ * Assess statistical validity of porosity and void measurements
167
+ * Verify aggregate-paste ratio estimations
168
+ * Validate performance correlations with concrete properties
169
+
170
+ **2. COMPLETENESS & DEPTH ASSESSMENT:**
171
+ - Concrete analysis coverage:
172
+ * Ensure comprehensive cement paste, aggregate, and ITZ evaluation
173
+ * Verify adequate air void system characterization
174
+ * Check for complete durability indicator assessment
175
+ * Assess balance between hydration products and defect analysis
176
+ - Missing elements identification:
177
+ * Identify overlooked concrete-specific features
178
+ * Note missing w/c ratio or mix design implications
179
+ * Flag absent durability mechanism discussions
180
+ * Highlight missing performance-microstructure correlations
181
+ * Check for adequate aggregate reactivity assessment
182
+
183
+ **3. METHODOLOGICAL RIGOR EVALUATION:**
184
+ - Concrete petrographic methodology:
185
+ * Assess systematic concrete analysis approach
186
+ * Evaluate air void measurement methodology (ASTM C457 compliance)
187
+ * Check statistical representativeness for concrete heterogeneity
188
+ * Verify appropriate magnification selection for different phases
189
+ - Scientific methodology:
190
+ * Confirm proper concrete microscopy interpretation principles
191
+ * Validate cement chemistry and hydration logic
192
+ * Assess appropriate confidence in durability predictions
193
+ * Check for potential misidentification of concrete phases
194
+ * Verify adequate consideration of concrete age and curing effects
195
+
196
+ **4. ENGINEERING RELEVANCE & UTILITY:**
197
+ - Concrete engineering applicability:
198
+ * Evaluate usefulness for concrete mix design optimization
199
+ * Assess relevance to structural design and construction
200
+ * Check connection between microstructure and concrete performance
201
+ * Verify appropriateness of durability and service life predictions
202
+ - Industry standards compliance:
203
+ * Compare against ACI, ASTM, EN, and national concrete codes
204
+ * Check alignment with concrete petrographic best practices
205
+ * Verify appropriate consideration of exposure conditions
206
+ * Assess compliance with concrete durability requirements
207
+
208
+ **5. COMMUNICATION QUALITY:**
209
+ - Concrete technical communication:
210
+ * Assess clarity and precision of concrete terminology
211
+ * Evaluate logical flow from microstructure to performance
212
+ * Check appropriate use of concrete technology language
213
+ * Verify adequate detail for concrete engineers and technologists
214
+ - Professional standards:
215
+ * Confirm appropriate level of certainty in concrete assessments
216
+ * Check for proper qualification of analysis limitations
217
+ * Assess professional tone suitable for concrete industry
218
+ * Verify appropriate cautionary statements for durability predictions
219
+
220
+ **VALIDATION SCORING SYSTEM:**
221
+ Rate each category (1-10 scale):
222
+ - Technical Accuracy: ___/10
223
+ - Completeness: ___/10
224
+ - Methodological Rigor: ___/10
225
+ - Engineering Relevance: ___/10
226
+ - Communication Quality: ___/10
227
+ - Overall Score: ___/10
228
+
229
+ **REVISION REQUIREMENTS:**
230
+ Acceptance Criteria: Overall score ≥ 8.0 AND all individual categories ≥ 7.0
231
+
232
+ If revision required, provide:
233
+ 1. Specific technical issues requiring correction
234
+ 2. Missing analysis components to be added
235
+ 3. Methodological improvements needed
236
+ 4. Enhanced engineering context required
237
+ 5. Communication improvements suggested
238
+
239
+ **VALIDATION REPORT FORMAT:**
240
+ Provide systematic evaluation following the framework above, conclude with:
241
+ - Overall confidence level (High/Medium/Low)
242
+ - Key strengths and improvement areas
243
+ - Suitability for engineering applications
244
+ - FINAL DECISION: ACCEPT or REQUIRES_REVISION
245
+
246
+ If REQUIRES_REVISION, provide detailed, actionable feedback for improvement."""
247
+
248
+ def get_enhanced_quality_prompt(self, custom_prompt, engineer_analysis):
249
+ """Build complete quality prompt with custom content and analysis"""
250
+ return f"""{custom_prompt}
251
+
252
+ **ANALYSIS TO REVIEW:**
253
+ {engineer_analysis}
254
+ """
255
+
256
+ def analyze_image_with_api(self, image_path, prompt, api_key, model, max_retries=2):
257
+ """Send image analysis request to OpenRouter API"""
258
+ if not api_key or not api_key.strip():
259
+ return "Error: API key is required. Please enter your OpenRouter API key."
260
+
261
+ for attempt in range(max_retries + 1):
262
+ try:
263
+ with open(image_path, "rb") as image_file:
264
+ image_data = base64.b64encode(image_file.read()).decode('utf-8')
265
+
266
+ headers = {
267
+ "Authorization": f"Bearer {api_key.strip()}",
268
+ "Content-Type": "application/json",
269
+ "HTTP-Referer": "https://concrete-sem-analysis.local",
270
+ "X-Title": "Concrete SEM Analysis App"
271
+ }
272
+
273
+ payload = {
274
+ "model": model,
275
+ "messages": [
276
+ {
277
+ "role": "user",
278
+ "content": [
279
+ {
280
+ "type": "text",
281
+ "text": prompt
282
+ },
283
+ {
284
+ "type": "image_url",
285
+ "image_url": {
286
+ "url": f"data:image/jpeg;base64,{image_data}"
287
+ }
288
+ }
289
+ ]
290
+ }
291
+ ],
292
+ "max_tokens": 4000,
293
+ "temperature": 0.1
294
+ }
295
+
296
+ response = requests.post(self.base_url, headers=headers, json=payload, timeout=90)
297
+ response.raise_for_status()
298
+
299
+ return response.json()['choices'][0]['message']['content']
300
+
301
+ except requests.exceptions.Timeout:
302
+ if attempt < max_retries:
303
+ continue
304
+ else:
305
+ return f"API Error: Request timed out after {max_retries + 1} attempts"
306
+ except requests.exceptions.RequestException as e:
307
+ if attempt < max_retries:
308
+ continue
309
+ else:
310
+ return f"API Error: {str(e)}"
311
+ except Exception as e:
312
+ return f"Error: {str(e)}"
313
+
314
+ return "Error: Maximum retry attempts exceeded"
315
+
316
+ def run_analysis(self, image, focus_area, enable_quality_check, enable_revision_loop, engineer_prompt, quality_prompt, api_key, model, max_revisions, progress=gr.Progress()):
317
+ """Main analysis function for Gradio interface with revision loop"""
318
+ if image is None:
319
+ return "Please upload an image first.", "", ""
320
+
321
+ if not api_key or not api_key.strip():
322
+ return "Please enter your OpenRouter API key.", "", ""
323
+
324
+ try:
325
+ # Save uploaded image to temporary file
326
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
327
+ if hasattr(image, 'save'):
328
+ image.save(temp_file.name)
329
+ else:
330
+ # Handle numpy array or other formats
331
+ Image.fromarray(image).save(temp_file.name)
332
+ temp_image_path = temp_file.name
333
+
334
+ progress(0.1, desc="Processing image...")
335
+
336
+ # Step 1: Basic image analysis
337
+ image_data = self.image_tool._run(temp_image_path)
338
+
339
+ if "error" in image_data:
340
+ return f"Image processing error: {image_data['error']}", "", ""
341
+
342
+ # Format basic image data
343
+ image_info = f"""
344
+ **Image Properties:**
345
+ - Dimensions: {image_data.get('image_properties', {}).get('width', 'N/A')} x {image_data.get('image_properties', {}).get('height', 'N/A')} pixels
346
+ - Mean Intensity: {image_data.get('image_properties', {}).get('mean_intensity', 'N/A'):.1f}
347
+ - Estimated Porosity: {image_data.get('porosity_analysis', {}).get('estimated_porosity_percent', 'N/A'):.1f}%
348
+ - Detected Particles: {image_data.get('particle_analysis', {}).get('number_of_particles', 'N/A')}
349
+ - Average Particle Diameter: {image_data.get('particle_analysis', {}).get('average_equivalent_diameter', 'N/A'):.1f} pixels
350
+ """
351
+
352
+ # Revision loop implementation
353
+ revision_count = 0
354
+ engineer_analysis = None
355
+ revision_feedback = None
356
+ quality_report = ""
357
+
358
+ # Determine if we should use revision loop
359
+ use_revision_loop = enable_quality_check and enable_revision_loop
360
+ max_attempts = max_revisions if use_revision_loop else 0
361
+
362
+ while revision_count <= max_attempts:
363
+ # Step 2: SEM Engineer Analysis
364
+ if revision_count == 0:
365
+ progress(0.3, desc="Running SEM Engineer analysis...")
366
+ else:
367
+ progress(0.3 + (revision_count * 0.2), desc=f"SEM Engineer revision {revision_count + 1}/{max_revisions + 1}...")
368
+
369
+ # Build engineer prompt with revision feedback if needed
370
+ if revision_feedback:
371
+ enhanced_prompt = f"""{engineer_prompt}
372
+
373
+ **REVISION REQUIREMENTS:**
374
+ The quality checker has identified the following issues that need to be addressed in your analysis:
375
+
376
+ {revision_feedback}
377
+
378
+ Please revise your analysis to address these specific concerns while maintaining the technical depth and accuracy expected for concrete SEM analysis.
379
+
380
+ Focus Area: {focus_area if focus_area else "Comprehensive analysis"}
381
+
382
+ Image Analysis Data:
383
+ - Dimensions: {image_data.get('image_properties', {}).get('width', 'N/A')} x {image_data.get('image_properties', {}).get('height', 'N/A')} pixels
384
+ - Estimated Porosity: {image_data.get('porosity_analysis', {}).get('estimated_porosity_percent', 'N/A')}%
385
+ - Particle Count: {image_data.get('particle_analysis', {}).get('number_of_particles', 'N/A')}
386
+ - Average Particle Area: {image_data.get('particle_analysis', {}).get('average_particle_area', 'N/A')} pixels²
387
+ - Texture Variance: {image_data.get('texture_features', {}).get('texture_variance', 'N/A')}"""
388
+ else:
389
+ enhanced_prompt = self.get_enhanced_engineer_prompt(engineer_prompt, image_data, focus_area)
390
+
391
+ engineer_analysis = self.analyze_image_with_api(temp_image_path, enhanced_prompt, api_key, model)
392
+
393
+ # Check for API errors
394
+ if "Error:" in engineer_analysis or "API Error:" in engineer_analysis:
395
+ break
396
+
397
+ # Step 3: Quality Checker Validation (if enabled)
398
+ if enable_quality_check:
399
+ progress(0.5 + (revision_count * 0.15), desc="Quality Checker validation...")
400
+ complete_quality_prompt = self.get_enhanced_quality_prompt(quality_prompt, engineer_analysis)
401
+ quality_report = self.analyze_image_with_api(temp_image_path, complete_quality_prompt, api_key, model)
402
+
403
+ # Check for API errors in quality report
404
+ if "API Error:" in quality_report or "Error:" in quality_report:
405
+ quality_report += f"\n\n⚠️ Quality checker failed due to API error. Accepting current analysis after {revision_count + 1} attempt(s)."
406
+ break
407
+ elif "DECISION: ACCEPT" in quality_report:
408
+ quality_report += f"\n\n✅ Analysis ACCEPTED after {revision_count + 1} attempt(s)"
409
+ break
410
+ elif "DECISION: REQUIRES_REVISION" in quality_report and revision_count < max_attempts and use_revision_loop:
411
+ quality_report += f"\n\n🔄 Revision required. Attempt {revision_count + 1}/{max_attempts + 1}"
412
+ # Extract feedback for next revision
413
+ revision_feedback = self._extract_revision_feedback(quality_report)
414
+ revision_count += 1
415
+ else:
416
+ if revision_count >= max_attempts and use_revision_loop:
417
+ quality_report += f"\n\n⚠️ Maximum revisions ({max_attempts + 1}) reached. Proceeding with current analysis."
418
+ elif "DECISION: REQUIRES_REVISION" in quality_report and not use_revision_loop:
419
+ quality_report += f"\n\n📝 Quality checker suggests improvements, but revision loop is disabled."
420
+ break
421
+ else:
422
+ # No quality check enabled, accept after first attempt
423
+ break
424
+
425
+ progress(1.0, desc="Analysis complete!")
426
+
427
+ # Clean up temporary file
428
+ os.unlink(temp_image_path)
429
+
430
+ # Add revision summary to image info
431
+ if enable_quality_check:
432
+ status_text = "ACCEPTED" if "DECISION: ACCEPT" in quality_report else "COMPLETED WITH LIMITATIONS"
433
+ if not use_revision_loop and "DECISION: REQUIRES_REVISION" in quality_report:
434
+ status_text = "COMPLETED (REVISION LOOP DISABLED)"
435
+
436
+ image_info += f"""
437
+
438
+ **Analysis Summary:**
439
+ - Total Analysis Attempts: {revision_count + 1}
440
+ - Revision Loop: {"Enabled" if use_revision_loop else "Disabled"}
441
+ - Maximum Revisions Allowed: {max_attempts if use_revision_loop else "N/A"}
442
+ - Final Status: {status_text}
443
+ """
444
+
445
+ return image_info, engineer_analysis, quality_report
446
+
447
+ except Exception as e:
448
+ return f"Error during analysis: {str(e)}", "", ""
449
+
450
+ def _extract_revision_feedback(self, validation_report):
451
+ """Extract specific feedback for revision from validation report"""
452
+ lines = validation_report.split('\n')
453
+ feedback_section = []
454
+ in_revision_section = False
455
+
456
+ for line in lines:
457
+ if "REVISION REQUIREMENTS" in line or "what needs to be improved" in line or "If REQUIRES_REVISION" in line:
458
+ in_revision_section = True
459
+ elif "FINAL VALIDATION" in line or "DECISION:" in line:
460
+ in_revision_section = False
461
+ elif in_revision_section and line.strip():
462
+ feedback_section.append(line.strip())
463
+
464
+ if feedback_section:
465
+ return '\n'.join(feedback_section)
466
+ else:
467
+ # Fallback: return the entire validation report
468
+ return validation_report
469
+
470
+ def create_gradio_interface():
471
+ """Create and configure Gradio interface"""
472
+
473
+ # Initialize analysis class
474
+ analyzer = GradioSEMAnalysis()
475
+
476
+ # Define the interface
477
+ with gr.Blocks(
478
+ title="Concrete SEM Analysis Tool",
479
+ theme=gr.themes.Soft(),
480
+ css="""
481
+ .gradio-container {
482
+ max-width: 1200px !important;
483
+ }
484
+ .main-header {
485
+ text-align: center;
486
+ color: #2c3e50;
487
+ margin-bottom: 20px;
488
+ }
489
+ .analysis-section {
490
+ margin: 10px 0;
491
+ padding: 15px;
492
+ border-radius: 8px;
493
+ background-color: #f8f9fa;
494
+ }
495
+ """
496
+ ) as interface:
497
+
498
+ gr.Markdown(
499
+ """
500
+ # 🔬 Concrete SEM Analysis Tool
501
+ ### Advanced Scanning Electron Microscopy Analysis for Concrete Materials
502
+
503
+ Upload a concrete SEM image to receive comprehensive microstructural analysis from expert AI agents specialized in concrete technology.
504
+ """,
505
+ elem_classes=["main-header"]
506
+ )
507
+
508
+ with gr.Row():
509
+ with gr.Column(scale=1):
510
+ gr.Markdown("## 📤 Input Configuration")
511
+
512
+ # API Configuration Section
513
+ with gr.Accordion("🔑 API Configuration", open=True):
514
+ api_key_input = gr.Textbox(
515
+ label="OpenRouter API Key",
516
+ type="password",
517
+ value=analyzer.default_api_key,
518
+ placeholder="Enter your OpenRouter API key...",
519
+ info="Get your API key from https://openrouter.ai/"
520
+ )
521
+
522
+ model_input = gr.Dropdown(
523
+ label="AI Model",
524
+ choices=[
525
+ "google/gemini-2.5-pro",
526
+ "google/gemini-2.5-flash",
527
+ "google/gemini-pro-1.5",
528
+ "google/gemini-flash-1.5",
529
+ "anthropic/claude-3.5-sonnet",
530
+ "anthropic/claude-3.5-haiku",
531
+ "anthropic/claude-3-opus",
532
+ "anthropic/claude-3-sonnet",
533
+ "anthropic/claude-3-haiku",
534
+ "openai/gpt-4o",
535
+ "openai/gpt-4o-mini",
536
+ "openai/gpt-4-vision-preview",
537
+ "openai/gpt-4-turbo",
538
+ "meta-llama/llama-3.2-90b-vision-instruct",
539
+ "meta-llama/llama-3.2-11b-vision-instruct",
540
+ "qwen/qwen-2-vl-72b-instruct",
541
+ "qwen/qwen2-vl-7b-instruct",
542
+ "microsoft/phi-3.5-vision-instruct",
543
+ "cognitivecomputations/dolphin-vision-72b",
544
+ "mistralai/pixtral-12b",
545
+ "liuhaotian/llava-v1.6-34b",
546
+ "bytedance/hyper-sd"
547
+ ],
548
+ value=analyzer.default_model,
549
+ allow_custom_value=True,
550
+ info="Select or type any vision-capable AI model from OpenRouter"
551
+ )
552
+
553
+ # Image Upload Section
554
+ image_input = gr.Image(
555
+ label="Upload Concrete SEM Image",
556
+ type="pil",
557
+ height=250
558
+ )
559
+
560
+ # Analysis Configuration
561
+ focus_area = gr.Dropdown(
562
+ label="Analysis Focus",
563
+ choices=[
564
+ "Comprehensive analysis",
565
+ "Cement paste and hydration products",
566
+ "Aggregate characteristics and ITZ",
567
+ "Air void system analysis",
568
+ "Durability indicators",
569
+ "Crack and defect analysis",
570
+ "Mix design evaluation"
571
+ ],
572
+ value="Comprehensive analysis"
573
+ )
574
+
575
+ enable_quality_check = gr.Checkbox(
576
+ label="Enable Quality Validation",
577
+ value=True,
578
+ info="Run additional quality checker for validation"
579
+ )
580
+
581
+ enable_revision_loop = gr.Checkbox(
582
+ label="Enable Revision Loop",
583
+ value=True,
584
+ info="Allow quality checker to request analysis improvements"
585
+ )
586
+
587
+ max_revisions_input = gr.Slider(
588
+ label="Maximum Revisions",
589
+ minimum=1,
590
+ maximum=5,
591
+ step=1,
592
+ value=2,
593
+ info="Maximum number of revision attempts (only active when revision loop is enabled)"
594
+ )
595
+
596
+ analyze_btn = gr.Button(
597
+ "🔬 Start Analysis",
598
+ variant="primary",
599
+ size="lg"
600
+ )
601
+
602
+ with gr.Column(scale=2):
603
+ gr.Markdown("## 📊 Analysis Results & Prompt Configuration")
604
+
605
+ with gr.Tab("Analysis Results"):
606
+ with gr.Tab("Image Properties"):
607
+ image_info_output = gr.Markdown(
608
+ label="Basic Image Analysis",
609
+ elem_classes=["analysis-section"]
610
+ )
611
+
612
+ with gr.Tab("SEM Engineer Analysis"):
613
+ engineer_output = gr.Markdown(
614
+ label="Expert SEM Analysis",
615
+ elem_classes=["analysis-section"]
616
+ )
617
+
618
+ with gr.Tab("Quality Validation"):
619
+ quality_output = gr.Markdown(
620
+ label="Quality Assurance Report",
621
+ elem_classes=["analysis-section"]
622
+ )
623
+
624
+ with gr.Tab("System Prompts"):
625
+ gr.Markdown("### 🤖 Customize AI Agent Prompts")
626
+
627
+ with gr.Accordion("SEM Engineer Agent Prompt", open=False):
628
+ engineer_prompt_input = gr.Textbox(
629
+ label="Engineer System Prompt",
630
+ value=analyzer.get_default_engineer_prompt(),
631
+ lines=15,
632
+ max_lines=25,
633
+ placeholder="Enter custom system prompt for SEM Engineer Agent...",
634
+ info="This prompt defines the SEM Engineer's expertise and analysis framework"
635
+ )
636
+
637
+ engineer_reset_btn = gr.Button(
638
+ "↻ Reset to Default",
639
+ size="sm",
640
+ variant="secondary"
641
+ )
642
+
643
+ with gr.Accordion("Quality Checker Agent Prompt", open=False):
644
+ quality_prompt_input = gr.Textbox(
645
+ label="Quality Checker System Prompt",
646
+ value=analyzer.get_default_quality_prompt(),
647
+ lines=15,
648
+ max_lines=25,
649
+ placeholder="Enter custom system prompt for Quality Checker Agent...",
650
+ info="This prompt defines the Quality Checker's validation criteria and approach"
651
+ )
652
+
653
+ quality_reset_btn = gr.Button(
654
+ "↻ Reset to Default",
655
+ size="sm",
656
+ variant="secondary"
657
+ )
658
+
659
+ # Connect the analysis function
660
+ analyze_btn.click(
661
+ fn=analyzer.run_analysis,
662
+ inputs=[image_input, focus_area, enable_quality_check, enable_revision_loop, engineer_prompt_input, quality_prompt_input, api_key_input, model_input, max_revisions_input],
663
+ outputs=[image_info_output, engineer_output, quality_output],
664
+ show_progress=True
665
+ )
666
+
667
+ # Connect reset buttons
668
+ engineer_reset_btn.click(
669
+ fn=lambda: analyzer.get_default_engineer_prompt(),
670
+ outputs=[engineer_prompt_input]
671
+ )
672
+
673
+ quality_reset_btn.click(
674
+ fn=lambda: analyzer.get_default_quality_prompt(),
675
+ outputs=[quality_prompt_input]
676
+ )
677
+
678
+ gr.Markdown(
679
+ """
680
+ ---
681
+ ### 📝 Usage Instructions:
682
+ 1. **Configure API**: Enter your OpenRouter API key and select AI model
683
+ 2. **Upload Image**: Select your concrete SEM image file (JPG, PNG, etc.)
684
+ 3. **Choose Focus**: Select specific analysis focus (cement paste, aggregates, air voids, etc.)
685
+ 4. **Quality Check**: Enable for additional validation (recommended)
686
+ 5. **Revision Loop**: Enable/disable automatic analysis improvements
687
+ 6. **Set Max Revisions**: Choose maximum revision attempts (1-5, default: 2)
688
+ 7. **Customize Prompts** (Optional): Edit system prompts in "System Prompts" tab
689
+ 8. **Analyze**: Click the analysis button and wait for results
690
+
691
+ ### 🎯 Features:
692
+ - **Flexible API Access**: Use your own OpenRouter API key and choose models
693
+ - **20+ Vision Models**: Support for Google, Anthropic, OpenAI, Meta, Alibaba, Microsoft, Mistral
694
+ - **Latest AI Technology**: Gemini 2.5 Pro, Claude 3.5, GPT-4o, Llama 3.2 Vision, Qwen 2-VL
695
+ - **Custom Model Support**: Type any vision-capable model name from OpenRouter
696
+ - **Optional Revision Loop**: Choose automatic quality-driven analysis refinement (1-5 iterations)
697
+ - **Concrete Expert AI**: Specialized concrete technology analysis
698
+ - **Petrographic Validation**: Built-in quality assurance by certified specialist
699
+ - **Comprehensive Metrics**: Air void analysis, ITZ characterization, hydration assessment
700
+ - **Durability Predictions**: Service life and performance implications
701
+ - **Mix Design Insights**: Water-cement ratio, aggregate effects, admixture impacts
702
+ - **Customizable Prompts**: Edit system prompts for both AI agents
703
+
704
+ ### 🔑 API Requirements:
705
+ - **OpenRouter Account**: Sign up at https://openrouter.ai/
706
+ - **API Key**: Get your API key from OpenRouter dashboard
707
+ - **Model Selection**: Choose from supported vision-capable models
708
+ - **Credits**: Ensure sufficient credits for analysis (cost varies by model)
709
+
710
+ ### 📸 Image Requirements:
711
+ - Clear, high-quality concrete SEM images for best results
712
+ - Consider magnification level appropriate for target analysis
713
+ - Supported formats: JPG, PNG, TIFF, BMP
714
+ """
715
+ )
716
+
717
+ return interface
718
+
719
+ if __name__ == "__main__":
720
+ # Create and launch the interface
721
+ interface = create_gradio_interface()
722
+ interface.launch(
723
+ server_name="0.0.0.0",
724
+ server_port=7860,
725
+ share=False,
726
+ debug=True
727
+ )
image_analysis_standalone.py ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ from PIL import Image
4
+ import matplotlib.pyplot as plt
5
+ import os
6
+ from typing import Dict, Any
7
+ import base64
8
+ import io
9
+
10
+ class ImageAnalysisTool:
11
+ """Standalone image analysis tool for SEM images"""
12
+
13
+ def __init__(self):
14
+ self.name = "SEM Image Analysis Tool"
15
+ self.description = "Analyzes SEM images to extract microstructural information about soil cemented materials"
16
+
17
+ def _run(self, image_path: str) -> Dict[str, Any]:
18
+ """
19
+ Analyze SEM image and extract relevant features
20
+ """
21
+ try:
22
+ # Load and process image
23
+ image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
24
+ if image is None:
25
+ return {"error": f"Could not load image from {image_path}"}
26
+
27
+ # Basic image properties
28
+ height, width = image.shape
29
+ mean_intensity = np.mean(image)
30
+ std_intensity = np.std(image)
31
+
32
+ # Convert to PIL for additional analysis
33
+ pil_image = Image.fromarray(image)
34
+
35
+ # Encode image for vision model
36
+ image_base64 = self._encode_image_to_base64(pil_image)
37
+
38
+ # Basic texture analysis
39
+ texture_features = self._analyze_texture(image)
40
+
41
+ # Porosity estimation (simple threshold-based)
42
+ porosity_info = self._estimate_porosity(image)
43
+
44
+ # Particle analysis
45
+ particle_info = self._analyze_particles(image)
46
+
47
+ analysis_results = {
48
+ "image_properties": {
49
+ "width": int(width),
50
+ "height": int(height),
51
+ "mean_intensity": float(mean_intensity),
52
+ "std_intensity": float(std_intensity)
53
+ },
54
+ "texture_features": texture_features,
55
+ "porosity_analysis": porosity_info,
56
+ "particle_analysis": particle_info,
57
+ "image_base64": image_base64,
58
+ "image_path": image_path
59
+ }
60
+
61
+ return analysis_results
62
+
63
+ except Exception as e:
64
+ return {"error": f"Error analyzing image: {str(e)}"}
65
+
66
+ def _encode_image_to_base64(self, image: Image.Image) -> str:
67
+ """Convert PIL image to base64 string"""
68
+ buffered = io.BytesIO()
69
+ image.save(buffered, format="PNG")
70
+ img_str = base64.b64encode(buffered.getvalue()).decode()
71
+ return img_str
72
+
73
+ def _analyze_texture(self, image: np.ndarray) -> Dict[str, float]:
74
+ """Analyze texture properties of the image"""
75
+ # Calculate local standard deviation (texture measure)
76
+ kernel = np.ones((9, 9), np.float32) / 81
77
+ mean_filtered = cv2.filter2D(image.astype(np.float32), -1, kernel)
78
+ sqr_diff = (image.astype(np.float32) - mean_filtered) ** 2
79
+ texture_map = cv2.filter2D(sqr_diff, -1, kernel)
80
+
81
+ return {
82
+ "texture_variance": float(np.mean(texture_map)),
83
+ "texture_uniformity": float(np.std(texture_map)),
84
+ "contrast": float(np.max(image) - np.min(image))
85
+ }
86
+
87
+ def _estimate_porosity(self, image: np.ndarray) -> Dict[str, Any]:
88
+ """Estimate porosity using threshold-based segmentation"""
89
+ # Use Otsu's thresholding for automatic threshold selection
90
+ _, binary = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
91
+
92
+ # Calculate porosity (assuming dark regions are pores)
93
+ total_pixels = image.shape[0] * image.shape[1]
94
+ pore_pixels = np.sum(binary == 0)
95
+ porosity_percentage = (pore_pixels / total_pixels) * 100
96
+
97
+ # Analyze pore size distribution
98
+ contours, _ = cv2.findContours(255 - binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
99
+ pore_areas = [cv2.contourArea(cnt) for cnt in contours if cv2.contourArea(cnt) > 10]
100
+
101
+ return {
102
+ "estimated_porosity_percent": float(porosity_percentage),
103
+ "number_of_pores": len(pore_areas),
104
+ "average_pore_area": float(np.mean(pore_areas)) if pore_areas else 0,
105
+ "max_pore_area": float(np.max(pore_areas)) if pore_areas else 0,
106
+ "min_pore_area": float(np.min(pore_areas)) if pore_areas else 0
107
+ }
108
+
109
+ def _analyze_particles(self, image: np.ndarray) -> Dict[str, Any]:
110
+ """Analyze particle characteristics"""
111
+ # Edge detection for particle boundaries
112
+ edges = cv2.Canny(image, 50, 150)
113
+
114
+ # Find contours (potential particles)
115
+ contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
116
+
117
+ # Filter contours by area to remove noise
118
+ min_area = 50 # minimum particle area
119
+ particles = [cnt for cnt in contours if cv2.contourArea(cnt) > min_area]
120
+
121
+ if not particles:
122
+ return {"number_of_particles": 0}
123
+
124
+ # Calculate particle properties
125
+ areas = [cv2.contourArea(cnt) for cnt in particles]
126
+ perimeters = [cv2.arcLength(cnt, True) for cnt in particles]
127
+
128
+ # Calculate equivalent diameters
129
+ equivalent_diameters = [2 * np.sqrt(area / np.pi) for area in areas]
130
+
131
+ # Calculate circularity (roundness measure)
132
+ circularities = []
133
+ for i, cnt in enumerate(particles):
134
+ if perimeters[i] > 0:
135
+ circularity = 4 * np.pi * areas[i] / (perimeters[i] ** 2)
136
+ circularities.append(circularity)
137
+
138
+ return {
139
+ "number_of_particles": len(particles),
140
+ "average_particle_area": float(np.mean(areas)),
141
+ "particle_area_std": float(np.std(areas)),
142
+ "average_equivalent_diameter": float(np.mean(equivalent_diameters)),
143
+ "diameter_range": {
144
+ "min": float(np.min(equivalent_diameters)),
145
+ "max": float(np.max(equivalent_diameters))
146
+ },
147
+ "average_circularity": float(np.mean(circularities)) if circularities else 0,
148
+ "circularity_std": float(np.std(circularities)) if circularities else 0
149
+ }
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ gradio>=4.0.0
2
+ pillow>=10.0.0
3
+ numpy>=1.24.0
4
+ opencv-python>=4.8.0
5
+ matplotlib>=3.7.0
6
+ python-dotenv>=1.0.0
7
+ requests>=2.31.0