File size: 20,576 Bytes
045edbf 9b24c4d 045edbf 9bceb4c 9b24c4d 4a1f3ff 9b24c4d 045edbf 9bceb4c 045edbf 9bceb4c 045edbf 9bceb4c 045edbf 9bceb4c 045edbf 9bceb4c 045edbf 9bceb4c 045edbf 9bceb4c 045edbf 9b24c4d 9bceb4c 9b24c4d 9bceb4c 9b24c4d 9bceb4c 9b24c4d 9bceb4c 9b24c4d 9bceb4c 9b24c4d 9bceb4c 9b24c4d 9bceb4c 9b24c4d 9bceb4c 9b24c4d 045edbf 4a1f3ff 045edbf |
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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 |
"""
Export Utilities for Construction Plans
Handles PDF and JSON export functionality
"""
import json
import logging
from typing import Dict, Any, Optional
from datetime import datetime
from pathlib import Path
logger = logging.getLogger(__name__)
def export_to_json(construction_plan: Any, output_path: Optional[str] = None) -> str:
"""
Export construction plan to JSON format
Args:
construction_plan: Construction plan object
output_path: Optional output file path
Returns:
Path to exported JSON file
"""
try:
# Convert construction plan to dictionary
plan_dict = _construction_plan_to_dict(construction_plan)
# Generate filename if not provided
if not output_path:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_path = f"construction_plan_{timestamp}.json"
# Ensure output directory exists
output_file = Path(output_path)
output_file.parent.mkdir(parents=True, exist_ok=True)
# Write JSON file
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(plan_dict, f, indent=2, ensure_ascii=False)
logger.info(f"Construction plan exported to JSON: {output_file}")
return str(output_file)
except Exception as e:
logger.error(f"Failed to export to JSON: {str(e)}")
raise
def export_to_pdf(construction_plan: Any, output_path: Optional[str] = None) -> str:
"""
Export construction plan to PDF format
Args:
construction_plan: Construction plan object
output_path: Optional output file path
Returns:
Path to exported PDF file
"""
try:
# Generate filename if not provided
if not output_path:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
output_path = f"construction_plan_{timestamp}.pdf"
# Ensure output directory exists
output_file = Path(output_path)
output_file.parent.mkdir(parents=True, exist_ok=True)
# Generate PDF content
pdf_content = _generate_pdf_content(construction_plan)
# For now, create a simple HTML-to-PDF conversion
# In production, use a proper PDF library like reportlab or weasyprint
try:
from weasyprint import HTML
HTML(string=pdf_content).write_pdf(output_file)
except (ImportError, OSError, Exception) as e:
# Fallback: Save as HTML if weasyprint not available or fails
logger.warning(f"weasyprint not available or failed ({type(e).__name__}), saving as HTML instead")
output_file = output_file.with_suffix('.html')
with open(output_file, 'w', encoding='utf-8') as f:
f.write(pdf_content)
logger.info(f"Construction plan exported to PDF: {output_file}")
return str(output_file)
except Exception as e:
logger.error(f"Failed to export to PDF: {str(e)}")
raise
def _construction_plan_to_dict(construction_plan: Any) -> Dict[str, Any]:
"""Convert construction plan object to dictionary"""
from dataclasses import asdict, is_dataclass
# If already a dict, return it
if isinstance(construction_plan, dict):
logger.info("Construction plan is already a dictionary")
if 'visualization' in construction_plan and construction_plan['visualization']:
logger.info("Including visualization data in JSON export")
if 'house_specifications' in construction_plan and construction_plan['house_specifications']:
logger.info("Including house specifications in JSON export")
return construction_plan
elif is_dataclass(construction_plan):
plan_dict = asdict(construction_plan)
# Ensure visualization data is included if present
if hasattr(construction_plan, 'visualization') and construction_plan.visualization:
logger.info("Including visualization data in JSON export")
# Ensure house specifications are included if present
if hasattr(construction_plan, 'house_specifications') and construction_plan.house_specifications:
logger.info("Including house specifications in JSON export")
return plan_dict
elif hasattr(construction_plan, '__dict__'):
return construction_plan.__dict__
else:
return {'data': str(construction_plan)}
def _generate_pdf_content(construction_plan: Any) -> str:
"""Generate HTML content for PDF export"""
# Handle both dict and object formats
if isinstance(construction_plan, dict):
metadata = construction_plan.get('metadata', {})
summary = construction_plan.get('executive_summary', {})
risk = construction_plan.get('risk_assessment', {})
house_specs = construction_plan.get('house_specifications')
recommendations = construction_plan.get('construction_recommendations', {})
costs = construction_plan.get('material_costs', {})
facilities = construction_plan.get('critical_facilities', {})
visualization = construction_plan.get('visualization')
else:
metadata = construction_plan.metadata
summary = construction_plan.executive_summary
risk = construction_plan.risk_assessment
house_specs = construction_plan.house_specifications if hasattr(construction_plan, 'house_specifications') else None
recommendations = construction_plan.construction_recommendations
costs = construction_plan.material_costs
facilities = construction_plan.critical_facilities
visualization = construction_plan.visualization if hasattr(construction_plan, 'visualization') else None
# Helper function to safely get nested values
def get_value(obj, key, default=''):
if isinstance(obj, dict):
return obj.get(key, default)
return getattr(obj, key, default)
# Extract metadata values
building_type = get_value(metadata, 'building_type', 'Unknown')
location = get_value(metadata, 'location', {})
location_name = get_value(location, 'name', 'Unknown')
coordinates = get_value(metadata, 'coordinates', {})
lat = get_value(coordinates, 'latitude', 0)
lon = get_value(coordinates, 'longitude', 0)
building_area = get_value(metadata, 'building_area')
generated_at = get_value(metadata, 'generated_at', 'Unknown')
overall_risk = get_value(summary, 'overall_risk', 'Unknown')
html = f"""
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Construction Plan Report</title>
<style>
body {{
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}}
h1 {{
color: #2563eb;
border-bottom: 3px solid #2563eb;
padding-bottom: 10px;
}}
h2 {{
color: #1e40af;
margin-top: 30px;
border-bottom: 2px solid #dbeafe;
padding-bottom: 5px;
}}
h3 {{
color: #374151;
margin-top: 20px;
}}
.section {{
margin-bottom: 30px;
padding: 15px;
background: #f9fafb;
border-radius: 8px;
}}
.critical {{
background: #fef2f2;
border-left: 5px solid #dc2626;
padding: 15px;
margin: 15px 0;
}}
.warning {{
background: #fef3c7;
border-left: 5px solid #f59e0b;
padding: 15px;
margin: 15px 0;
}}
.info {{
background: #dbeafe;
border-left: 5px solid #2563eb;
padding: 15px;
margin: 15px 0;
}}
table {{
width: 100%;
border-collapse: collapse;
margin: 15px 0;
}}
th, td {{
padding: 10px;
text-align: left;
border-bottom: 1px solid #e5e7eb;
}}
th {{
background: #f3f4f6;
font-weight: bold;
}}
ul, ol {{
margin: 10px 0;
padding-left: 25px;
}}
li {{
margin: 5px 0;
}}
.footer {{
margin-top: 50px;
padding-top: 20px;
border-top: 2px solid #e5e7eb;
font-size: 0.9em;
color: #6b7280;
}}
</style>
</head>
<body>
<h1>🏗️ Disaster Risk Construction Plan</h1>
<div class="section">
<h2>Project Information</h2>
<p><strong>Building Type:</strong> {building_type.replace('_', ' ').title()}</p>
<p><strong>Location:</strong> {location_name}</p>
<p><strong>Coordinates:</strong> {lat:.4f}°N, {lon:.4f}°E</p>
{f"<p><strong>Building Area:</strong> {building_area:.2f} sq meters</p>" if building_area else ""}
<p><strong>Generated:</strong> {generated_at}</p>
</div>
<div class="section">
<h2>Executive Summary</h2>
<p><strong>Overall Risk:</strong> {overall_risk}</p>
</div>
"""
# Critical concerns
critical_concerns = get_value(summary, 'critical_concerns', [])
if critical_concerns:
html += """
<div class="critical">
<h3>⚠️ Critical Concerns</h3>
<ul>
"""
for concern in critical_concerns:
html += f"<li>{concern}</li>"
html += """
</ul>
</div>
"""
# Key recommendations
key_recommendations = get_value(summary, 'key_recommendations', [])
if key_recommendations:
html += """
<div class="warning">
<h3>🎯 Key Recommendations</h3>
<ol>
"""
for rec in key_recommendations:
html += f"<li>{rec}</li>"
html += """
</ol>
</div>
"""
# Visualization
if visualization:
viz_timestamp = get_value(visualization, 'generation_timestamp', 'Unknown')
viz_model = get_value(visualization, 'model_version', 'Unknown')
viz_format = get_value(visualization, 'image_format', 'png')
viz_base64 = get_value(visualization, 'image_base64', '')
viz_features = get_value(visualization, 'features_included', [])
viz_prompt = get_value(visualization, 'prompt_used', '')
html += f"""
<div class="section">
<h2>🎨 Architectural Visualization</h2>
<p><strong>Generated:</strong> {viz_timestamp}</p>
<p><strong>Model:</strong> {viz_model}</p>
"""
# Add the image if available
if viz_base64:
html += f"""
<div style="text-align: center; margin: 20px 0;">
<img src="data:image/{viz_format.lower()};base64,{viz_base64}"
alt="Architectural Visualization"
style="max-width: 100%; height: auto; border: 2px solid #e5e7eb; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1);">
</div>
"""
# Add features list
if viz_features:
html += """
<h3>Disaster-Resistant Features Shown</h3>
<ul>
"""
for feature in viz_features:
html += f"<li>{feature}</li>"
html += """
</ul>
"""
# Add prompt used
if viz_prompt:
html += f"""
<p><strong>Design Prompt:</strong> {viz_prompt}</p>
"""
html += """
</div>
"""
# Risk assessment
html += f"""
<div class="section">
<h2>Risk Assessment</h2>
<p><strong>Overall Risk Level:</strong> {risk.summary.overall_risk_level}</p>
<p><strong>Total Hazards Assessed:</strong> {risk.summary.total_hazards_assessed}</p>
<p><strong>High Risk Hazards:</strong> {risk.summary.high_risk_count}</p>
</div>
"""
# House Specifications
if house_specs:
html += """
<div class="section">
<h2>🏗️ House Specifications</h2>
<h3>Basic Specifications</h3>
<table>
<tr>
<td><strong>Number of Floors</strong></td>
<td>""" + str(house_specs.number_of_floors) + """</td>
</tr>
<tr>
<td><strong>Primary Material</strong></td>
<td>""" + str(house_specs.primary_material).replace('_', ' ').title() + """</td>
</tr>
<tr>
<td><strong>Foundation Type</strong></td>
<td>""" + str(house_specs.foundation_type).replace('_', ' ').title() + """</td>
</tr>
<tr>
<td><strong>Foundation Depth</strong></td>
<td>""" + f"{house_specs.foundation_depth_meters:.2f} meters" + """</td>
</tr>
<tr>
<td><strong>Roof Type</strong></td>
<td>""" + str(house_specs.roof_type).replace('_', ' ').title() + """</td>
</tr>
<tr>
<td><strong>Wall Thickness</strong></td>
<td>""" + f"{house_specs.wall_thickness_mm} mm" + """</td>
</tr>
</table>
"""
# Reinforcement details
if house_specs.reinforcement_details:
html += f"""
<h3>Reinforcement Details</h3>
<p>{house_specs.reinforcement_details}</p>
"""
# Structural features
if house_specs.structural_features:
html += """
<h3>Structural Features</h3>
<table>
<thead>
<tr>
<th>Feature</th>
<th>Specification</th>
<th>Justification</th>
</tr>
</thead>
<tbody>
"""
for feature in house_specs.structural_features:
html += f"""
<tr>
<td><strong>{feature.feature_name}</strong></td>
<td>{feature.specification}</td>
<td>{feature.justification}</td>
</tr>
"""
html += """
</tbody>
</table>
"""
# Compliance codes
if house_specs.compliance_codes:
html += """
<h3>Compliance Codes</h3>
<ul>
"""
for code in house_specs.compliance_codes:
html += f"<li>{code}</li>"
html += """
</ul>
"""
# Decision rationale
if house_specs.decision_rationale:
html += f"""
<h3>Decision Rationale</h3>
<p style="white-space: pre-wrap;">{house_specs.decision_rationale}</p>
"""
html += """
</div>
"""
# Recommendations
if recommendations.general_guidelines:
html += """
<div class="section">
<h2>Construction Recommendations</h2>
<h3>General Guidelines</h3>
<ul>
"""
for guideline in recommendations.general_guidelines:
html += f"<li>{guideline}</li>"
html += """
</ul>
</div>
"""
# Material costs
if costs.materials:
html += """
<div class="section">
<h2>Material Cost Estimates</h2>
"""
if costs.total_estimate:
html += f"""
<p><strong>Total Estimate Range:</strong></p>
<ul>
<li>Low: {costs.total_estimate.currency} {costs.total_estimate.low:,.2f}</li>
<li>Mid: {costs.total_estimate.currency} {costs.total_estimate.mid:,.2f}</li>
<li>High: {costs.total_estimate.currency} {costs.total_estimate.high:,.2f}</li>
</ul>
"""
html += """
<h3>Itemized Materials</h3>
<table>
<thead>
<tr>
<th>Material</th>
<th>Category</th>
<th>Unit Price</th>
<th>Quantity</th>
<th>Total</th>
</tr>
</thead>
<tbody>
"""
for material in costs.materials[:20]: # Limit to 20 items for PDF
quantity_str = f"{material.quantity_needed:.2f} {material.unit}" if material.quantity_needed else "TBD"
total_str = f"{material.currency} {material.total_cost:,.2f}" if material.total_cost else "TBD"
html += f"""
<tr>
<td>{material.material_name}</td>
<td>{material.category}</td>
<td>{material.currency} {material.price_per_unit:,.2f}/{material.unit}</td>
<td>{quantity_str}</td>
<td>{total_str}</td>
</tr>
"""
html += """
</tbody>
</table>
</div>
"""
# Critical facilities
if facilities.schools or facilities.hospitals:
html += """
<div class="section">
<h2>Critical Facilities</h2>
"""
if facilities.schools:
html += "<h3>Schools</h3><ul>"
for school in facilities.schools[:5]:
html += f"<li>{school.name} - {school.distance_meters/1000:.2f} km</li>"
html += "</ul>"
if facilities.hospitals:
html += "<h3>Hospitals</h3><ul>"
for hospital in facilities.hospitals[:5]:
html += f"<li>{hospital.name} - {hospital.distance_meters/1000:.2f} km</li>"
html += "</ul>"
html += """
</div>
"""
# Footer
html += """
<div class="footer">
<p><strong>Disclaimer:</strong> This construction plan is generated for guidance purposes only.
Always consult with licensed engineers, architects, and local authorities for actual construction projects.
Cost estimates are based on current market conditions and may vary.</p>
<p>Generated by Disaster Risk Construction Planner</p>
</div>
</body>
</html>
"""
return html
def create_export_buttons(construction_plan: Any) -> tuple:
"""
Create export buttons and handlers for Gradio interface
Args:
construction_plan: Construction plan object
Returns:
Tuple of (json_button, pdf_button, json_file, pdf_file)
"""
import gradio as gr
def export_json_handler():
try:
file_path = export_to_json(construction_plan)
return file_path
except Exception as e:
logger.error(f"JSON export failed: {str(e)}")
return None
def export_pdf_handler():
try:
file_path = export_to_pdf(construction_plan)
return file_path
except Exception as e:
logger.error(f"PDF export failed: {str(e)}")
return None
json_button = gr.Button("📥 Export as JSON", variant="secondary")
pdf_button = gr.Button("📄 Export as PDF", variant="secondary")
json_file = gr.File(label="JSON Export", visible=False)
pdf_file = gr.File(label="PDF Export", visible=False)
json_button.click(fn=export_json_handler, outputs=json_file)
pdf_button.click(fn=export_pdf_handler, outputs=pdf_file)
return json_button, pdf_button, json_file, pdf_file
|