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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f35fb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3100614
 
 
 
 
 
 
 
 
 
 
 
 
 
2c2d1c2
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
from flask import Flask, render_template, request, jsonify, send_file
import google.generativeai as genai
import base64
import logging
from weasyprint import HTML
import os
from datetime import datetime
import tempfile
from io import BytesIO
import jinja2
from dotenv import load_dotenv
from tenacity import retry, stop_after_attempt, wait_exponential

app = Flask(__name__)

# Load environment variables
load_dotenv()

# Configure logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

# Configure Gemini API with error handling
api_key = os.getenv('GOOGLE_API_KEY')
if not api_key:
    error_msg = ("No Google API key found. "
                 "For Hugging Face deployment, please add GOOGLE_API_KEY "
                 "in your Space's Settings -> Repository Secrets")
    logger.error(error_msg)
    raise ValueError(error_msg)

try:
    genai.configure(api_key=api_key)
    
    # Configure the model with safety settings
    generation_config = {
        "temperature": 0.9,
        "top_p": 1,
        "top_k": 1,
        "max_output_tokens": 2048,
    }

    safety_settings = [
        {
            "category": "HARM_CATEGORY_HARASSMENT",
            "threshold": "BLOCK_MEDIUM_AND_ABOVE"
        },
        {
            "category": "HARM_CATEGORY_HATE_SPEECH",
            "threshold": "BLOCK_MEDIUM_AND_ABOVE"
        },
        {
            "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
            "threshold": "BLOCK_MEDIUM_AND_ABOVE"
        },
        {
            "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
            "threshold": "BLOCK_MEDIUM_AND_ABOVE"
        }
    ]

    model = genai.GenerativeModel(
        model_name="gemini-1.5-flash",
        generation_config=generation_config,
        safety_settings=safety_settings
    )
    
    # Test the connection
    response = model.generate_content("Test connection")
    logger.info("Successfully configured Gemini API")
except Exception as e:
    logger.error(f"Failed to configure Gemini API: {str(e)}")
    raise

# Updated prompt for dual-format report
prompt = """You are a professional campus facility inspector with over 15 years of experience in infrastructure assessment in India. Analyze the campus with total area of ${college_area} acres. Generate two reports based on the provided campus images:
+ The campus includes ${ground_count} grounds with a total area of ${ground_area} acres.
  
  REPORT 1: EXECUTIVE SUMMARY TABLES
  
  Table 1: Campus Overview
  | Aspect | Grade | Key Observations | Priority Level |
  |--------|-------|-----------------|----------------|
  | Overall Infrastructure | (A+/A/B+/B/C) | • Key points | High/Medium/Low |
  | Buildings | (A+/A/B+/B/C) | • Key points | High/Medium/Low |
  | Roads & Parking | (A+/A/B+/B/C) | • Key points | High/Medium/Low |
  | Sports Grounds (${ground_area} acres) | (A+/A/B+/B/C) | • Key points | High/Medium/Low |
  | Canteens | (A+/A/B+/B/C) | • Key points | High/Medium/Low |
  | Entry/Exit & Security | (A+/A/B+/B/C) | • Key points | High/Medium/Low |
  | Environmental | (A+/A/B+/B/C) | • Key points | High/Medium/Low |

Table 2: Critical Issues and Solutions
| Area | Issue | Proposed Solution/Measures |
|------|-------|---------------------------|
| Area 1 | Description of issue | • Detailed solution steps |
| Area 2 | Description of issue | • Detailed solution steps |

REPORT 2: DETAILED ASSESSMENT

1. Overall Campus Analysis
   A. Infrastructure Overview
      - General campus layout and planning
      - Common areas and circulation
      - Campus-wide systems (drainage, lighting)
      - Shared facilities condition
   
   B. Safety & Security Assessment
      - Boundary security
      - Emergency systems
      - Lighting and surveillance
      - Fire safety measures
   
   C. Environmental Analysis
      - Green spaces and landscaping
      - Water management
      - Waste management
      - Natural lighting and ventilation
   
   D. Accessibility & Connectivity
      - Internal roads and pathways
      - Parking facilities
      - Emergency access
      - Campus connectivity

2. Area-wise Assessment
   A. Buildings
      - Overall condition
      - Key features
      - Notable issues
      - Maintenance status

   B. Roads & Parking
      - Surface condition
      - Traffic flow
      - Parking adequacy
      - Safety features

   C. Sports Facilities
      - Ground conditions
      - Equipment status
      - Safety measures
      - Maintenance level

   D. Canteens
      - Structure condition
      - Hygiene standards
      - Ventilation
      - Seating capacity

   E. Entry/Exit Points
      - Security measures
      - Traffic management
      - Access control
      - Emergency preparedness

3. Campus Strengths
   A. Infrastructure Strengths
      - Notable features
      - Well-maintained areas
      - Effective systems
      - Best practices observed

   B. Enhancement Potential
      - Areas showing excellence
      - Opportunities for showcase
      - Positive aspects to build upon
      - Innovative features

4. Areas of Concern & Recommendations
   A. Critical Issues
      - Infrastructure gaps
      - Safety concerns
      - Maintenance needs
      - Operational challenges

   B. Improvement Measures
      - Specific solutions
      - Practical steps
      - Enhancement strategies
      - Preventive measures

5. Final Assessment
   A. Overall Grade: [A+/A/B+/B/C]
      Brief justification of the grade based on:
      - Infrastructure quality
      - Maintenance standards
      - Safety measures
      - Environmental aspects

   B. Concluding Remarks
      - Key takeaways
      - Critical focus areas
      - Positive highlights
      - Path forward

Please focus on significant issues only and ignore minor cosmetic concerns. Consider local weather patterns (monsoon, summer) and regional building practices. Present information in clear, concise formats."""

@app.route('/')
def index():
    return render_template('index.html')

def calculate_grade(grades, weights):
    total_weight = sum(weights.values())
    weighted_score = sum(grades[aspect] * weights[aspect] for aspect in grades)
    average_score = weighted_score / total_weight
    # Convert average score to a grade
    if average_score >= 90:
        return 'A+'
    elif average_score >= 80:
        return 'A'
    elif average_score >= 70:
        return 'B+'
    elif average_score >= 60:
        return 'B'
    else:
        return 'C'

def extract_grades(report_text):
    # Dummy implementation for extracting grades from report text
    # This should be replaced with actual logic to parse the report
    return {
        'Overall Infrastructure': 85,
        'Buildings': 80,
        'Roads & Parking': 75,
        'Sports Facilities': 70,
        'Canteens': 65,
        'Entry/Exit & Security': 80,
        'Environmental': 90
    }

def extract_priority_distribution(report_text):
    """Extract priority distribution from the report text"""
    try:
        # Count priority levels from the Overview table
        priorities = {
            'High': 0,
            'Medium': 0,
            'Low': 0
        }
        
        # Look for priority levels in the Overview table
        lines = report_text.split('\n')
        for line in lines:
            if '|' in line:  # Table row
                if 'High' in line:
                    priorities['High'] += 1
                elif 'Medium' in line:
                    priorities['Medium'] += 1
                elif 'Low' in line:
                    priorities['Low'] += 1
                    
        return [priorities['High'], priorities['Medium'], priorities['Low']]
    except Exception as e:
        logger.error(f"Error extracting priority distribution: {str(e)}")
        return [30, 45, 25]  # Default values if extraction fails

def extract_area_performance(report_text):
    """Extract performance scores for different areas"""
    try:
        # Extract scores from the Overview table
        areas = {
            'Buildings': 0,
            'Roads & Parking': 0,
            'Sports Facilities': 0,
            'Canteens': 0,
            'Security': 0,
            'Environmental': 0
        }
        
        lines = report_text.split('\n')
        for line in lines:
            if '|' in line:  # Table row
                parts = line.split('|')
                if len(parts) >= 2:
                    area = parts[1].strip()
                    if area in areas:
                        # Convert grade to numeric score
                        grade = parts[2].strip() if len(parts) > 2 else ''
                        score = {
                            'A+': 95,
                            'A': 85,
                            'B+': 75,
                            'B': 65,
                            'C': 55
                        }.get(grade, 70)
                        areas[area] = score
                        
        return list(areas.values())
    except Exception as e:
        logger.error(f"Error extracting area performance: {str(e)}")
        return [85, 75, 70, 80, 90, 85]  # Default values if extraction fails

def extract_maintenance_status(report_text):
    """Extract maintenance status distribution"""
    try:
        status = {
            'Well Maintained': 0,
            'Needs Attention': 0,
            'Critical': 0
        }
        
        # Count maintenance status mentions in the report
        well_maintained = report_text.lower().count('well maintained')
        needs_attention = report_text.lower().count('needs attention') + report_text.lower().count('requires attention')
        critical = report_text.lower().count('critical') + report_text.lower().count('urgent')
        
        total = well_maintained + needs_attention + critical or 1  # Avoid division by zero
        
        return [
            int(well_maintained * 100 / total),
            int(needs_attention * 100 / total),
            int(critical * 100 / total)
        ]
    except Exception as e:
        logger.error(f"Error extracting maintenance status: {str(e)}")
        return [60, 30, 10]  # Default values if extraction fails

@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
def generate_content_with_retry(prompt, images):
    """Generate content with retry logic"""
    try:
        response = model.generate_content(
            [prompt] + images,
            generation_config=genai.types.GenerationConfig(
                # Remove timeout parameter
                # Other generation config parameters can be added here if needed
            )
        )
        return response
    except Exception as e:
        logger.error(f"Error generating content: {str(e)}")
        raise

@app.route('/generate_report', methods=['POST'])
def generate_report():
    try:
        if not api_key:
            raise ValueError("Google API key not configured")

        data = request.json
        images = data.get('images', [])
        basic_info = data.get('basicInfo', {})
        
        # Add file size validation
        MAX_FILE_SIZE = 4 * 1024 * 1024  # 4MB
        for image_data in images:
            if len(base64.b64decode(image_data['data'].split(',')[1])) > MAX_FILE_SIZE:
                return jsonify({'error': 'Image file size exceeds 4MB limit'}), 400
        
        # Create image context with numbering
        image_contexts = []
        all_images = []
        image_count = 1
        
        for image_data in images:
            if image_data['data'].startswith('data:image'):
                image_data['data'] = image_data['data'].split(',')[1]
            image_bytes = base64.b64decode(image_data['data'])
            
            # Store image data for the report
            image_context = {
                'number': image_count,
                'category': image_data['category'],
                'data': image_bytes,
                'mime_type': 'image/jpeg'
            }
            image_contexts.append(image_context)
            
            # Add to list for API
            all_images.append({
                "mime_type": "image/jpeg",
                "data": image_bytes
            })
            
            image_count += 1

        # Update prompt with actual values
        contextualized_prompt = prompt.replace('${college_area}', str(basic_info.get('collegeArea', '')))
        contextualized_prompt = contextualized_prompt.replace('${building_count}', str(basic_info.get('buildingCount', '')))
        contextualized_prompt = contextualized_prompt.replace('${parking_area}', str(basic_info.get('parkingCount', '')))
        contextualized_prompt = contextualized_prompt.replace('${canteen_count}', str(basic_info.get('canteenCount', '')))
        contextualized_prompt = contextualized_prompt.replace('${ground_count}', str(basic_info.get('groundCount', '')))
        contextualized_prompt = contextualized_prompt.replace('${ground_area}', str(basic_info.get('groundArea', '')))
        contextualized_prompt = contextualized_prompt.replace('${gate_count}', str(basic_info.get('gateCount', '')))
        
        # Generate report with image references
        response = generate_content_with_retry(
            contextualized_prompt,
            all_images
        )
        report_text = response.text
        
        # Extract all metrics
        grades = extract_grades(report_text)
        priority_distribution = extract_priority_distribution(report_text)
        area_performance = extract_area_performance(report_text)
        maintenance_status = extract_maintenance_status(report_text)
        
        # Calculate overall grade
        weights = {
            'Overall Infrastructure': 0.2,
            'Buildings': 0.2,
            'Roads & Parking': 0.15,
            'Sports Facilities': 0.1,
            'Canteens': 0.1,
            'Entry/Exit & Security': 0.15,
            'Environmental': 0.1
        }
        overall_grade = calculate_grade(grades, weights)

        # Add metrics to the response
        return jsonify({
            'report': report_text,
            'overallGrade': overall_grade,
            'metrics': {
                'priorityDistribution': priority_distribution,
                'areaPerformance': area_performance,
                'maintenanceStatus': maintenance_status
            },
            'images': [{
                'number': img['number'],
                'category': img['category'],
                'data': base64.b64encode(img['data']).decode('utf-8')
            } for img in image_contexts]
        })
        
    except ValueError as ve:
        logger.error(f"Validation error: {str(ve)}")
        return jsonify({'error': str(ve)}), 400
    except Exception as e:
        logger.error(f"Error generating report: {str(e)}")
        return jsonify({'error': 'Internal server error occurred'}), 500

@app.route('/download_pdf', methods=['POST'])
def download_pdf():
    pdf_buffer = None
    try:
        logger.debug("Received PDF download request")
        html_content = request.json.get('html')
        
        if not html_content:
            raise ValueError("No HTML content provided")

        logger.debug("Converting HTML to PDF")
        # Create PDF in memory
        pdf_buffer = BytesIO()
        HTML(string=html_content).write_pdf(pdf_buffer)
        pdf_buffer.seek(0)
        
        logger.debug("Sending PDF file")
        # Create a copy of the buffer contents
        pdf_data = pdf_buffer.getvalue()
        
        # Close the original buffer
        if pdf_buffer:
            pdf_buffer.close()
            
        # Create a new buffer with the copied data
        return_buffer = BytesIO(pdf_data)
        
        return send_file(
            return_buffer,
            mimetype='application/pdf',
            as_attachment=True,
            download_name=f'campus-inspection-report-{datetime.now().strftime("%Y%m%d")}.pdf'
        )

    except Exception as e:
        logger.error(f"Error generating PDF: {str(e)}")
        return jsonify({'error': str(e)}), 500
    finally:
        # Clean up the original buffer if it exists
        if pdf_buffer:
            pdf_buffer.close()

def generate_report(findings, inspector_name, location, weather):
    # Format timestamp
    timestamp = datetime.now().strftime("%B %d, %Y at %I:%M %p")
    
    # Ensure findings have all required fields and proper formatting
    formatted_findings = []
    for finding in findings:
        formatted_finding = {
            'title': finding.get('title', 'Untitled Finding'),
            'description': finding.get('description', 'No description provided'),
            'severity': finding.get('severity', 'Medium').capitalize(),
            'recommendation': finding.get('recommendation', 'No recommendation provided'),
            'image_path': finding.get('image_path', None)
        }
        formatted_findings.append(formatted_finding)

    # Load and render template
    template_loader = jinja2.FileSystemLoader('.')
    template_env = jinja2.Environment(loader=template_loader)
    template = template_env.get_template('campus-inspection-report.html')
    
    html_content = template.render(
        timestamp=timestamp,
        inspector_name=inspector_name,
        location=location,
        weather=weather,
        findings=formatted_findings
    )
    
    return html_content

@app.route('/health')
def health_check():
    try:
        # Simple test to verify API connection
        response = model.generate_content("Test connection")
        return jsonify({
            'status': 'healthy',
            'api_configured': True
        })
    except Exception as e:
        logger.error(f"Health check failed: {str(e)}")
        return jsonify({
            'status': 'unhealthy',
            'error': str(e)
        }), 500

@app.route('/report_status/<task_id>')
def report_status(task_id):
    """Check the status of a report generation task"""
    try:
        # Implement status checking logic
        return jsonify({
            'status': 'processing',
            'progress': 50,  # Example progress percentage
            'message': 'Processing images...'
        })
    except Exception as e:
        logger.error(f"Error checking status: {str(e)}")
        return jsonify({'error': str(e)}), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)