File size: 8,416 Bytes
5369733 |
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 |
"""
Report Generator MCP Tool
Generates formatted reports for trash detection events.
Suitable for city authorities, community groups, or documentation.
"""
from typing import Optional, Any
from datetime import datetime
from trash_model import Detection
import llm_client
def generate_report(
detections: list[Detection],
severity: str,
location: Optional[str] = None,
notes: Optional[str] = None,
event_id: Optional[int] = None,
plan: Optional[Any] = None,
format: str = "email"
):
"""
Generate a formatted report for trash detection event.
Args:
detections: List of trash detections
severity: Severity level
location: Location description
notes: Additional notes
event_id: Database event ID if logged
plan: Optional cleanup plan data
format: "email" | "markdown" | "plain"
Returns:
Dict with report text and metadata
"""
if format == "email":
report_text = _generate_email_report(detections, severity, location, notes, plan)
elif format == "markdown":
report_text = _generate_markdown_report(detections, severity, location, notes, event_id, plan)
else:
report_text = _generate_plain_report(detections, severity, location, notes, plan)
return {
"report": report_text,
"format": format,
"generated_at": datetime.now().isoformat(),
"event_id": event_id
}
def _generate_email_report(
detections: list[Detection],
severity: str,
location: Optional[str],
notes: Optional[str],
plan: Optional[Any]
) -> str:
"""Generate email-formatted report for city authorities."""
location_str = location or "[Location to be specified]"
date_str = datetime.now().strftime("%B %d, %Y")
# Summarize detections
categories = list(set(d["label"] for d in detections))
category_counts = {}
for det in detections:
label = det["label"]
category_counts[label] = category_counts.get(label, 0) + 1
items_list = "\n".join([
f" • {label.replace('_', ' ').title()}: {count} item(s)"
for label, count in sorted(category_counts.items())
])
urgency_text = {
"high": "URGENT - Immediate attention required",
"medium": "Moderate priority - Action needed within 1-3 days",
"low": "Low priority - Routine cleanup recommended"
}
template = f"""Subject: Trash Cleanup Request - {location_str}
Dear City Services / Environmental Department,
I am writing to report significant litter accumulation that requires attention at the following location:
**Location:** {location_str}
**Date Reported:** {date_str}
**Severity Level:** {severity.upper()} ({urgency_text.get(severity, '')})
**Details of Trash Observed:**
Total items detected: {len(detections)}
{items_list}
"""
if notes:
template += f"\n**Additional Context:**\n{notes}\n"
if plan:
template += f"""
**Recommended Action:**
- Estimated cleanup time: {plan.get('estimated_time_minutes', 'N/A')} minutes
- Volunteers needed: {plan.get('recommended_volunteers', 'N/A')}
- Equipment required: {', '.join(plan.get('equipment_needed', []))}
- Urgency: Action within {plan.get('urgency_days', 'N/A')} day(s)
"""
template += """
This accumulation poses environmental and health concerns for the community. I would appreciate a timely response regarding cleanup scheduling.
Thank you for your attention to this matter.
Best regards,
[Your Name / Community Group]
[Contact Information]
"""
return template
def _generate_markdown_report(
detections: list[Detection],
severity: str,
location: Optional[str],
notes: Optional[str],
event_id: Optional[int],
plan: Optional[Any]
) -> str:
"""Generate Markdown-formatted report for documentation."""
location_str = location or "Unspecified location"
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Group detections
category_counts = {}
for det in detections:
label = det["label"]
category_counts[label] = category_counts.get(label, 0) + 1
report = f"""# Trash Detection Report
## Event Information
- **Event ID:** {event_id if event_id else 'Not logged'}
- **Timestamp:** {timestamp}
- **Location:** {location_str}
- **Severity:** {severity.upper()}
## Detection Summary
- **Total Items:** {len(detections)}
- **Unique Categories:** {len(category_counts)}
### Items Breakdown
"""
for label, count in sorted(category_counts.items(), key=lambda x: x[1], reverse=True):
report += f"- **{label.replace('_', ' ').title()}:** {count} item(s)\n"
if plan:
report += f"""
## Cleanup Plan
- **Recommended Volunteers:** {plan.get('recommended_volunteers', 'N/A')}
- **Estimated Time:** {plan.get('estimated_time_minutes', 'N/A')} minutes
- **Urgency:** Within {plan.get('urgency_days', 'N/A')} day(s)
- **Equipment Needed:**
"""
for equipment in plan.get('equipment_needed', []):
report += f" - {equipment}\n"
report += f"\n### Environmental Impact\n{plan.get('environmental_impact', 'N/A')}\n"
if notes:
report += f"\n## Additional Notes\n{notes}\n"
report += "\n---\n*Generated by CleanCity Agent*"
return report
def _generate_plain_report(
detections: list[Detection],
severity: str,
location: Optional[str],
notes: Optional[str],
plan: Optional[Any]
) -> str:
"""Generate plain text report."""
lines = [
"=" * 60,
"TRASH DETECTION REPORT",
"=" * 60,
"",
f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}",
f"Location: {location or 'Unspecified'}",
f"Severity: {severity.upper()}",
"",
f"Total Items Detected: {len(detections)}",
""
]
# Group items
category_counts = {}
for det in detections:
label = det["label"]
category_counts[label] = category_counts.get(label, 0) + 1
lines.append("Items by Category:")
for label, count in sorted(category_counts.items()):
lines.append(f" - {label.replace('_', ' ').title()}: {count}")
if plan:
lines.extend([
"",
"Cleanup Recommendations:",
f" - Volunteers needed: {plan.get('recommended_volunteers', 'N/A')}",
f" - Estimated time: {plan.get('estimated_time_minutes', 'N/A')} minutes",
f" - Action within: {plan.get('urgency_days', 'N/A')} day(s)",
f" - Equipment: {', '.join(plan.get('equipment_needed', []))}"
])
if notes:
lines.extend(["", "Notes:", notes])
lines.extend(["", "=" * 60])
return "\n".join(lines)
def generate_llm_enhanced_report(
detections: list[Detection],
severity: str,
location: Optional[str] = None,
notes: Optional[str] = None,
plan: Optional[Any] = None
) -> str:
"""Use LLM to generate a more sophisticated, context-aware report."""
context = f"""Trash detection event:
- Location: {location or 'unspecified'}
- Items detected: {len(detections)}
- Severity: {severity}
- Categories: {', '.join(set(d['label'] for d in detections))}
"""
if notes:
context += f"- Context: {notes}\n"
if plan:
context += f"- Recommended volunteers: {plan.get('recommended_volunteers')}\n"
context += f"- Estimated cleanup time: {plan.get('estimated_time_minutes')} minutes\n"
prompt = f"""Based on this trash detection data, write a professional report suitable for city authorities:
{context}
Create a clear, actionable report that:
1. Describes the situation factually
2. Emphasizes environmental/community impact
3. Provides specific cleanup recommendations
4. Has an appropriate professional tone
Format as an email that could be sent to city services."""
try:
report = llm_client.generate_text(
prompt,
system_prompt="You are a professional environmental reporter writing to city officials.",
max_tokens=500,
temperature=0.5
)
return report
except Exception as e:
print(f"LLM report generation failed: {e}")
# Fallback to template
return _generate_email_report(detections, severity, location, notes, plan)
|