Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions uc-0a/agents.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
# agents.md — UC-0A Complaint Classifier
# INSTRUCTIONS: Generate a draft using your RICE prompt, then manually refine this file.
# Delete these comments before committing.

role: >
[FILL IN: Who is this agent? What is its operational boundary?]
You are an expert citizen complaint classifier. Your operational boundary is strictly processing rows of civic complaint data, analyzing their descriptions, and assigning appropriate structured metadata for department routing and triage prioritization.

intent: >
[FILL IN: What does a correct output look like — make it verifiable]
A correct output must strictly classify each complaint row with an exact allowed category, priority level, a one-sentence reason citing specific words from the description, and an optional review flag. The output must not contain hallucinated values or unvalidated schemas.

context: >
[FILL IN: What information is the agent allowed to use? State exclusions explicitly.]
You act upon the provided citizen complaint text data. You must evaluate this text against predefined schema rules. Do not use outside knowledge to infer severity unstated in the text.

enforcement:
- "[FILL IN: Specific testable rule 1 — e.g. Category must be exactly one of: Pothole, Flooding, ...]"
- "[FILL IN: Specific testable rule 2 — e.g. Priority must be Urgent if description contains: injury, child, school, ...]"
- "[FILL IN: Specific testable rule 3 — e.g. Every output row must include a reason field citing specific words from the description]"
- "[FILL IN: Refusal condition — e.g. If category cannot be determined from description alone, output category: Other and flag: NEEDS_REVIEW]"
- "The category must be exactly one of: Pothole, Flooding, Streetlight, Waste, Noise, Road Damage, Heritage Damage, Heat Hazard, Drain Blockage, Other."
- "The priority must be exactly one of: Urgent, Standard, Low."
- "The priority MUST be Urgent if the description contains any of these exact severity keywords: injury, child, school, hospital, ambulance, fire, hazard, fell, collapse."
- "The reason field must be exactly one sentence and must cite specific words from the description."
- "The flag must be NEEDS_REVIEW if the category is genuinely ambiguous; otherwise leave it blank."
132 changes: 117 additions & 15 deletions uc-0a/classifier.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,137 @@
"""
UC-0A — Complaint Classifier
Starter file. Build this using the RICE agents.md skills.md → CRAFT workflow.
Implementation powered by the RICE strategy defined in agents.md and skills.md.
"""
import argparse
import csv
import json
import os

def classify_complaint(row: dict) -> dict:
try:
from openai import OpenAI
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", ""))
HAS_LLM = True
except ImportError:
HAS_LLM = False

def _process_single_row(row: dict) -> dict:
"""
Internal helper to execute the agents.md RICE prompt classification on a single row.
"""
Classify a single complaint row.
Returns: dict with keys: complaint_id, category, priority, reason, flag
description = row.get("description", "")

TODO: Build this using your AI tool guided by your agents.md and skills.md.
Your RICE enforcement rules must be reflected in this function's behaviour.
system_prompt = """role: >
You are an expert citizen complaint classifier. Your operational boundary is strictly processing rows of civic complaint data, analyzing their descriptions, and assigning appropriate structured metadata for department routing and triage prioritization.

intent: >
A correct output must strictly classify each complaint row with an exact allowed category, priority level, a one-sentence reason citing specific words from the description, and an optional review flag. The output must not contain hallucinated values or unvalidated schemas.

context: >
You act upon the provided citizen complaint text data. You must evaluate this text against predefined schema rules. Do not use outside knowledge to infer severity unstated in the text.

enforcement:
- "The category must be exactly one of: Pothole, Flooding, Streetlight, Waste, Noise, Road Damage, Heritage Damage, Heat Hazard, Drain Blockage, Other."
- "The priority must be exactly one of: Urgent, Standard, Low."
- "The priority MUST be Urgent if the description contains any of these exact severity keywords: injury, child, school, hospital, ambulance, fire, hazard, fell, collapse."
- "The reason field must be exactly one sentence and must cite specific words from the description."
- "The flag must be NEEDS_REVIEW if the category is genuinely ambiguous; otherwise leave it blank."

Output your result purely as a JSON object with keys: "category", "priority", "reason", "flag".
"""
raise NotImplementedError("Build this using your AI tool + RICE prompt")

if HAS_LLM and os.environ.get("OPENAI_API_KEY"):
try:
response = client.chat.completions.create(
model="gpt-4o-mini",
response_format={"type": "json_object"},
temperature=0.0,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": f"Classify this citizen complaint:\\n\\nDescription: {description}"}
]
)
return json.loads(response.choices[0].message.content)
except Exception as e:
pass # Fallback to rule-based engine on API error

# Fallback to rule-based mapping
desc_lower = description.lower()

category = "Other"
flag = "NEEDS_REVIEW"
if "pothole" in desc_lower: category, flag = "Pothole", ""
elif "flood" in desc_lower or "water" in desc_lower: category, flag = "Flooding", ""
elif "light" in desc_lower: category, flag = "Streetlight", ""
elif "waste" in desc_lower or "garbage" in desc_lower: category, flag = "Waste", ""
elif "noise" in desc_lower or "loud" in desc_lower: category, flag = "Noise", ""
elif "road" in desc_lower and "damage" in desc_lower: category, flag = "Road Damage", ""
elif "drain" in desc_lower or "block" in desc_lower: category, flag = "Drain Blockage", ""

urgent_keywords = ["injury", "child", "school", "hospital", "ambulance", "fire", "hazard", "fell", "collapse"]
has_urgent = any(kw in desc_lower for kw in urgent_keywords)
priority = "Urgent" if has_urgent else "Standard"

reason_words = [kw for kw in urgent_keywords if kw in desc_lower]
if reason_words:
reason = f"The description contains '{reason_words[0]}'."
else:
reason = f"The description pertains to {category.lower()}."

return {
"category": category,
"priority": priority,
"reason": reason,
"flag": flag
}


def classify_complaint(input_path: str, output_path: str):
"""
Classify logic applied directly taking CSV files as requested in skills.md
"""
batch_classify(input_path, output_path)


def batch_classify(input_path: str, output_path: str):
"""
Read input CSV, classify each row, write results CSV.

TODO: Build this using your AI tool.
Must: flag nulls, not crash on bad rows, produce output even if some rows fail.
Read input CSV, classify each row, write results CSV robustly (skills.md)
"""
raise NotImplementedError("Build this using your AI tool + RICE prompt")
with open(input_path, 'r', encoding='utf-8') as fin:
reader = csv.DictReader(fin)
rows = list(reader)

fieldnames = list(reader.fieldnames or [])
for new_field in ['category', 'priority', 'reason', 'flag']:
if new_field not in fieldnames:
fieldnames.append(new_field)

results = []
# Process rows gracefully as dictated by skills.md error_handling
for i, row in enumerate(rows):
try:
classified_data = _process_single_row(row)
row.update(classified_data)
except Exception as e:
print(f"Error classifying row {i+1}: {e}")
row.update({
'category': 'Other',
'priority': 'Standard',
'reason': 'Extraction error during processing.',
'flag': 'NEEDS_REVIEW'
})
results.append(row)

with open(output_path, 'w', encoding='utf-8', newline='') as fout:
writer = csv.DictWriter(fout, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(results)


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="UC-0A Complaint Classifier")
parser.add_argument("--input", required=True, help="Path to test_[city].csv")
parser.add_argument("--input", required=True, help="Path to input test CSV")
parser.add_argument("--output", required=True, help="Path to write results CSV")
args = parser.parse_args()
batch_classify(args.input, args.output)
print(f"Done. Results written to {args.output}")

classify_complaint(args.input, args.output)
print(f"Done. Classified records written to {args.output}")
16 changes: 16 additions & 0 deletions uc-0a/results_ahmedabad.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
complaint_id,date_raised,city,ward,location,description,reported_by,days_open,category,priority,reason,flag
AM-202401,2024-04-24,Ahmedabad,Ward 13 – Sabarmati,Kankaria Lake promenade,Tarmac surface melting at 44°C. Footwear sticking. Park users unsafe.,Email,1,Other,Standard,The description pertains to other.,NEEDS_REVIEW
AM-202402,2024-04-23,Ahmedabad,Ward 12 – Maninagar,"Naroda Industrial Area, main gate",Metal bus shelter reaching dangerous temperatures. Commuters refusing to use.,Citizen Portal,4,Other,Standard,The description pertains to other.,NEEDS_REVIEW
AM-202405,2024-05-08,Ahmedabad,Ward 11 – Vastrapur,"Sabarmati Riverfront, North section",Dead trees with split branches. Fall risk to walkers. 3 trees affected.,WhatsApp Helpline,19,Other,Standard,The description pertains to other.,NEEDS_REVIEW
AM-202406,2024-04-21,Ahmedabad,Ward 15 – Chandkheda,"Chandkheda, Sector 22 park",Irrigation system broken. Grass dying in heatwave conditions.,Phone Helpline,18,Other,Standard,The description pertains to other.,NEEDS_REVIEW
AM-202407,2024-04-25,Ahmedabad,Ward 15 – Chandkheda,"Thaltej, residential park",Broken bench and upturned paving. Child injured last week.,Email,20,Other,Urgent,The description contains 'child'.,NEEDS_REVIEW
AM-202410,2024-05-02,Ahmedabad,Ward 12 – Maninagar,SG Highway near Prahladnagar,Pothole on main highway causing morning rush lane closure.,Ward Office Walk-in,4,Pothole,Standard,The description pertains to pothole.,
AM-202414,2024-04-20,Ahmedabad,Ward 14 – Odhav,"Jodhpur Village, off SG Highway",Residential colony unlit after 9pm. Wiring theft reported.,Citizen Portal,10,Other,Standard,The description pertains to other.,NEEDS_REVIEW
AM-202417,2024-05-08,Ahmedabad,Ward 13 – Sabarmati,"Manek Chowk, Old City",Night market waste not cleared before morning. Heritage area affected.,Councillor Referral,21,Waste,Standard,The description pertains to waste.,
AM-202421,2024-05-03,Ahmedabad,Ward 14 – Odhav,"CG Road, commercial zone",Club music audible at residential buildings at 2am.,Citizen Portal,18,Other,Standard,The description pertains to other.,NEEDS_REVIEW
AM-202424,2024-05-04,Ahmedabad,Ward 11 – Vastrapur,"Shahibaug, near zoo",Zoo approach road surface bubbling at 45°C. Visitor complaints.,Councillor Referral,19,Other,Standard,The description pertains to other.,NEEDS_REVIEW
AM-202429,2024-04-24,Ahmedabad,Ward 11 – Vastrapur,Ellis Bridge walking track,River walk surface temperature unbearable. Installed temperature reads 52°C.,Councillor Referral,3,Other,Standard,The description pertains to other.,NEEDS_REVIEW
AM-202431,2024-04-26,Ahmedabad,Ward 14 – Odhav,"Paldi, Ratanpol area",Old city road subsidence near ancient step well. Heritage concern.,Citizen Portal,18,Other,Standard,The description pertains to other.,NEEDS_REVIEW
AM-202435,2024-05-07,Ahmedabad,Ward 11 – Vastrapur,"New CG Road, Chandkheda",Black metal road dividers storing heat. Motorists reporting burns on contact.,WhatsApp Helpline,16,Other,Standard,The description pertains to other.,NEEDS_REVIEW
AM-202444,2024-05-15,Ahmedabad,Ward 12 – Maninagar,"Jivraj Park, commercial area",Restaurant waste bins overflowing on Sunday night. Health risk.,Councillor Referral,20,Waste,Standard,The description pertains to waste.,
AM-202445,2024-04-19,Ahmedabad,Ward 13 – Sabarmati,Ranip Bus Rapid Transit stop,BRT shelter roof glass broken. Users exposed to full sun.,Councillor Referral,9,Other,Standard,The description pertains to other.,NEEDS_REVIEW
16 changes: 16 additions & 0 deletions uc-0a/results_hyderabad.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
complaint_id,date_raised,city,ward,location,description,reported_by,days_open,category,priority,reason,flag
GH-202401,2024-07-01,Hyderabad,Ward 55 – Secunderabad,Tank Bund Road underpass,Underpass flooded after 1hr rain. Ambulance diverted. Lives at risk.,WhatsApp Helpline,13,Flooding,Urgent,The description contains 'ambulance'.,
GH-202402,2024-07-13,Hyderabad,Ward 55 – Secunderabad,Dilsukhnagar Monda Market area,Market area flooded. Traders suffering losses. Drain completely blocked.,Citizen Portal,20,Flooding,Standard,The description pertains to flooding.,
GH-202406,2024-07-26,Hyderabad,Ward 54 – Kukatpally,"Mehdipatnam, Rethibowli Road",Main stormwater drain 100% blocked with construction debris.,Phone Helpline,3,Flooding,Standard,The description pertains to flooding.,
GH-202407,2024-07-15,Hyderabad,Ward 55 – Secunderabad,"Santoshnagar, Saidabad",Drain blocked and mosquito breeding. Dengue concern.,WhatsApp Helpline,4,Drain Blockage,Standard,The description pertains to drain blockage.,
GH-202410,2024-07-23,Hyderabad,Ward 55 – Secunderabad,"Outer Ring Road service lane, Narsingi",Potholes causing vehicles to slow to 20kmph on fast road.,Councillor Referral,18,Pothole,Standard,The description pertains to pothole.,
GH-202411,2024-07-31,Hyderabad,Ward 55 – Secunderabad,"Tolichowki, Mount Pleasant Road",Pothole swallowed entire motorcycle wheel. Rider hospitalised.,Ward Office Walk-in,17,Pothole,Urgent,The description contains 'hospital'.,
GH-202412,2024-07-24,Hyderabad,Ward 53 – LB Nagar,"Nagole, Ramanthapur Road",School bus struggling to navigate 6 potholes in 200m stretch.,Citizen Portal,4,Pothole,Urgent,The description contains 'school'.,
GH-202417,2024-07-16,Hyderabad,Ward 53 – LB Nagar,"Charminar area, old city",Heritage zone garbage overflow. Tourist photographs showing piles of waste.,WhatsApp Helpline,11,Waste,Standard,The description pertains to waste.,
GH-202420,2024-07-10,Hyderabad,Ward 55 – Secunderabad,Hitech City co-working zone,Construction drilling from 5am daily near residential towers.,WhatsApp Helpline,12,Other,Standard,The description pertains to other.,NEEDS_REVIEW
GH-202422,2024-07-31,Hyderabad,Ward 52 – Madhapur,"Attapur, Shaikpet Road",Road collapsed partially. Crater 1m deep near residential gate.,Social Media,1,Other,Urgent,The description contains 'collapse'.,NEEDS_REVIEW
GH-202424,2024-07-19,Hyderabad,Ward 52 – Madhapur,Malkajgiri railway underpass,Underpass floods in light rain. Cars regularly abandoned.,Councillor Referral,5,Flooding,Standard,The description pertains to flooding.,
GH-202428,2024-07-24,Hyderabad,Ward 54 – Kukatpally,Tolichowki Friday Market area,Post-market waste not cleared. Area unusable by Sunday morning.,Ward Office Walk-in,8,Waste,Standard,The description pertains to waste.,
GH-202432,2024-07-19,Hyderabad,Ward 51 – Jubilee Hills,"Kukatpally, KPHB Phase 7",24hr supermarket delivery trucks idling with engines on.,Citizen Portal,2,Other,Standard,The description pertains to other.,NEEDS_REVIEW
GH-202448,2024-07-22,Hyderabad,Ward 52 – Madhapur,Kondapur main drain,Main drain blocked — entire locality at flooding risk this week.,Councillor Referral,8,Flooding,Standard,The description pertains to flooding.,
GH-202438,2024-07-06,Hyderabad,Ward 53 – LB Nagar,Hayathnagar residential colony,Colony surrounded by fields that channel rainwater through main road.,Social Media,11,Flooding,Standard,The description pertains to flooding.,
16 changes: 16 additions & 0 deletions uc-0a/results_kolkata.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
complaint_id,date_raised,city,ward,location,description,reported_by,days_open,category,priority,reason,flag
KM-202401,2024-06-19,Kolkata,Ward 27 – Salt Lake,"Park Street, Victoria area",Heritage lamp post knocked over by delivery vehicle. Not restored.,Email,17,Other,Standard,The description pertains to other.,NEEDS_REVIEW
KM-202402,2024-06-07,Kolkata,Ward 25 – Jadavpur,"College Street, Boi Para",Historic tram road cobblestones broken up by cable laying work.,Councillor Referral,16,Other,Standard,The description pertains to other.,NEEDS_REVIEW
KM-202405,2024-06-13,Kolkata,Ward 25 – Jadavpur,"Jorasanko, heritage zone",Wedding band playing near Tagore Museum at 11pm. Festival season ongoing.,Ward Office Walk-in,10,Other,Standard,The description pertains to other.,NEEDS_REVIEW
KM-202409,2024-06-05,Kolkata,Ward 26 – Behala,VIP Road near Dum Dum Airport,Airport access road full of potholes. Diplomatic complaint received.,Councillor Referral,7,Pothole,Standard,The description pertains to pothole.,
KM-202410,2024-06-21,Kolkata,Ward 26 – Behala,EM Bypass near Science City,Pothole causing tyre blowouts. Three incidents this week.,Email,1,Pothole,Standard,The description pertains to pothole.,
KM-202411,2024-06-23,Kolkata,Ward 26 – Behala,"Tollygunge, Sardar Shankar Road",Deep pothole filling with rainwater. Depth invisible. Accident risk.,Councillor Referral,12,Pothole,Standard,The description pertains to pothole.,
KM-202415,2024-06-28,Kolkata,Ward 26 – Behala,"Kasba, off bypass",New residential complex draining directly onto public road.,WhatsApp Helpline,13,Drain Blockage,Standard,The description pertains to drain blockage.,
KM-202418,2024-06-04,Kolkata,Ward 23 – Park Street,"New Market, Lindsay Street",Tourist zone waste overflowing. Foreign visitors photographing piles.,Ward Office Walk-in,15,Waste,Standard,The description pertains to waste.,
KM-202421,2024-06-08,Kolkata,Ward 23 – Park Street,Rashbehari Avenue,Footpath broken and sinking. Elderly pedestrian fell. Hospital visit.,Social Media,3,Other,Urgent,The description contains 'hospital'.,NEEDS_REVIEW
KM-202422,2024-06-06,Kolkata,Ward 24 – Shyambazar,"Howrah Bridge approach, Kolkata side",Road surface buckled near bridge. Structural concern raised.,Social Media,1,Other,Standard,The description pertains to other.,NEEDS_REVIEW
KM-202426,2024-06-17,Kolkata,Ward 24 – Shyambazar,"Bow Barracks, central Kolkata",Heritage residential building exterior defaced by billboard installation.,Phone Helpline,1,Other,Standard,The description pertains to other.,NEEDS_REVIEW
KM-202430,2024-06-08,Kolkata,Ward 23 – Park Street,"Park Street, restaurant zone",Road subsided near gas pipeline. Gas leak smell reported too.,Social Media,21,Other,Standard,The description pertains to other.,NEEDS_REVIEW
KM-202434,2024-06-15,Kolkata,Ward 23 – Park Street,"Marble Palace, Muktaram Babu Street",Street paving removed for utility work — heritage stone not replaced.,Email,14,Other,Standard,The description pertains to other.,NEEDS_REVIEW
KM-202436,2024-06-26,Kolkata,Ward 27 – Salt Lake,"New Alipore, residential colony",Entire colony substation tripped. Darkness for 3 nights.,Councillor Referral,17,Other,Standard,The description pertains to other.,NEEDS_REVIEW
KM-202438,2024-06-22,Kolkata,Ward 25 – Jadavpur,"Esplanade, near Metro",Street vendors using amplifiers illegally in heritage precinct.,Councillor Referral,19,Other,Standard,The description pertains to other.,NEEDS_REVIEW
Loading