Spaces:
Running
Running
Download decision_policy.py from CoderInfinity/Lexora: direct link, hf CLI and curl.
- Browser
- Download file 10.5 kB
-
https://huggingface.co/spaces/CoderInfinity/Lexora/resolve/main/decision_policy.py
- Command line
-
hf download hf://spaces/CoderInfinity/Lexora/decision_policy.py
-
curl -L -o decision_policy.py https://huggingface.co/spaces/CoderInfinity/Lexora/resolve/main/decision_policy.py
10.5 kB
| """ | |
| Situation classifier β decide self-help, Abunzi mediation, or licensed advocate. | |
| Classification uses the citizen question (not the memorandum boilerplate). | |
| The canned footer always mentions advocates/courts, so mixing the answer | |
| into keyword matching falsely routes almost every query to NEEDS_LAWYER. | |
| v2 fixes | |
| -------- | |
| 1. WORD-BOUNDARY MATCHING (was: substring `in` checks) | |
| "sue" is a substring of "issue"; "penal" is a substring of "penalty"; | |
| "fence" is a substring of "defence". Under the old substring matching, | |
| ordinary questions like "I have an issue with my land title" or "what's | |
| the penalty fee for late registration" silently matched LAWYER_SIGNALS | |
| and got routed to NEEDS_LAWYER for no real reason. All keyword checks | |
| now match whole words only. | |
| 2. REAL URGENCY PROPAGATION (was: `"HIGH" if urgency == "HIGH" else "HIGH"`) | |
| Both branches of that ternary returned "HIGH" β _urgency()'s actual | |
| output was discarded, so every LAWYER_SIGNALS match got flagged urgency | |
| HIGH regardless of how mild the question actually was. Fixed to use the | |
| computed urgency, capped at MEDIUM minimum (a lawyer-signal match is | |
| never truly LOW urgency, but it isn't automatically HIGH either). | |
| 3. SIMPLE-LOOKUP BYPASS NARROWED (was: `is_simple_lookup` skipped BOTH the | |
| fidelity check and the confidence-band check entirely) | |
| `is_simple_lookup` is a pure phrasing heuristic from chat.py β it knows | |
| nothing about whether retrieval actually found anything relevant. Under | |
| the old logic, a simply-phrased question with near-zero citation | |
| fidelity (retrieval essentially failed) still fell through to | |
| SELF_RESOLVABLE by default, producing a confident-sounding answer on a | |
| question the system had no real grounding for. The fidelity check can | |
| no longer be fully bypassed β a simple phrasing only relaxes the | |
| threshold, it doesn't remove the floor. | |
| """ | |
| from __future__ import annotations | |
| import re | |
| from typing import Dict, Optional | |
| SITUATION_SELF = "SELF_RESOLVABLE" | |
| SITUATION_MEDIATION = "NEEDS_MEDIATION" | |
| SITUATION_LAWYER = "NEEDS_LAWYER" | |
| LAWYER_SIGNALS = [ | |
| "court", "tribunal", "inkiko", "litigation", "sue", "lawsuit", | |
| "criminal", "penal", "arrest", "arrested", "warrant", "appeal", | |
| "eviction", "evict", "forcefully", "illegally seized", "fraud", | |
| "forgery", "deadline", "court order", "injunction", "prosecution", | |
| "charges", "summons", "bailliff", "bailiff", "forced sale", | |
| "title stolen", "forged title", | |
| ] | |
| MEDIATION_SIGNALS = [ | |
| "neighbor", "neighbour", "boundary", "fence", "encroach", | |
| "family land", "abunzi", "cell mediation", "sector mediation", | |
| "informal agreement", "disagree", "dispute between", | |
| "izungura", "ubutaka bw'umuryango", "family dispute", | |
| ] | |
| SELF_HELP_SIGNALS = [ | |
| "how do i register", "how to register", "what documents", | |
| "what is", "define", "how long", "fees", "cost of", | |
| "where do i", "procedure to register", "steps to", | |
| ] | |
| # Fidelity floor for a "simple lookup" phrasing β lower than the normal | |
| # 0.50 floor, but not zero. A simple-sounding question with fidelity below | |
| # even this relaxed bar still means retrieval effectively failed, and that | |
| # must still escalate rather than silently fall through to SELF_RESOLVABLE. | |
| SIMPLE_LOOKUP_FIDELITY_FLOOR = 0.35 | |
| def _match_words(text: str, phrases: list) -> bool: | |
| """ | |
| Whole-word / whole-phrase matching. Using \\b word boundaries instead | |
| of a plain `in` substring check avoids false positives like | |
| "issue" matching "sue", or "penalty" matching "penal". | |
| Phrases containing spaces (e.g. "court order") still match correctly | |
| because \\b anchors on the first and last word of the phrase, not | |
| every internal character. | |
| """ | |
| for phrase in phrases: | |
| pattern = r"\b" + re.escape(phrase) + r"\b" | |
| if re.search(pattern, text): | |
| return True | |
| return False | |
| def _urgency(question: str) -> str: | |
| q = question.lower() | |
| if _match_words(q, ["urgent", "court", "deadline", "eviction", "criminal", "arrested", "tomorrow", "summons"]): | |
| return "HIGH" | |
| if _match_words(q, ["dispute", "conflict", "refuses", "denied"]): | |
| return "MEDIUM" | |
| return "NORMAL" | |
| def decide_situation( | |
| question: str, | |
| category: str, | |
| confidence_band: str, | |
| citation_fidelity: float, | |
| is_simple_lookup: bool = False, | |
| ) -> Dict: | |
| q = question.lower() | |
| urgency = _urgency(question) | |
| if _match_words(q, LAWYER_SIGNALS): | |
| return { | |
| "situation": SITUATION_LAWYER, | |
| # A lawyer-signal match is always at least MEDIUM urgency, and | |
| # HIGH when _urgency() independently agrees β but no longer | |
| # hardcoded to HIGH regardless of what _urgency() found. | |
| "urgency": "HIGH" if urgency == "HIGH" else "MEDIUM", | |
| "rationale": ( | |
| "This matter involves courts, enforcement, crime, fraud, or a hard deadline. " | |
| "A licensed Rwanda Bar Association advocate should handle strategy and filings." | |
| ), | |
| "citizen_guidance": ( | |
| "Use the matched advocates below. Share the briefing note so they can start from " | |
| "the Gazette provisions already identified, then confirm the facts with you." | |
| ), | |
| "how_to_work_with_lawyer": [ | |
| "Send the briefing note and your IDs/title documents before the first call.", | |
| "Ask the advocate which forum applies (Abunzi, primary court, or land office) and why.", | |
| "Agree the scope: advice only, drafting, or representation β and the fee before work starts.", | |
| "Do not sign transfers, settlements, or court papers until the advocate has reviewed them.", | |
| ], | |
| } | |
| # A simple-sounding question relaxes the fidelity floor but never | |
| # removes it β see SIMPLE_LOOKUP_FIDELITY_FLOOR docstring above. | |
| fidelity_floor = SIMPLE_LOOKUP_FIDELITY_FLOOR if is_simple_lookup else 0.50 | |
| if citation_fidelity < fidelity_floor: | |
| return { | |
| "situation": SITUATION_LAWYER, | |
| "urgency": "MEDIUM", | |
| "rationale": ( | |
| "Retrieved Gazette provisions are not a close enough match to answer safely. " | |
| "A licensed advocate should check the current statute and your documents." | |
| ), | |
| "citizen_guidance": ( | |
| "Treat the answer as a starting map only. A matched advocate can confirm whether " | |
| "another law or a later amendment applies to your facts." | |
| ), | |
| "how_to_work_with_lawyer": [ | |
| "Explain the facts in date order and bring originals of every document named in the chat.", | |
| "Ask them to confirm or correct the citations the system used.", | |
| "Request a written next-step list with offices and deadlines.", | |
| ], | |
| } | |
| if _match_words(q, MEDIATION_SIGNALS) or ( | |
| # NOTE: "family" is deliberately excluded from this category list. | |
| # Formal family matters (e.g. divorce) go through court in Rwanda, | |
| # not Abunzi, so an informal family conflict only reaches mediation | |
| # here if it hits a MEDIATION_SIGNALS phrase directly (e.g. "family | |
| # dispute"). Confirm this matches actual legal-process guidance | |
| # before changing it β it was not something this fix altered. | |
| category in ("land", "succession", "dispute") and confidence_band == "MEDIUM" | |
| and not _match_words(q, SELF_HELP_SIGNALS) | |
| ): | |
| return { | |
| "situation": SITUATION_MEDIATION, | |
| "urgency": urgency, | |
| "rationale": ( | |
| "This looks like a neighbourhood, family-land, or low-value dispute that Rwandan " | |
| "procedure usually routes through Abunzi before court." | |
| ), | |
| "citizen_guidance": ( | |
| "Start at cell/sector Abunzi with the dispute summary. If mediation fails or the " | |
| "other party ignores it, then use the lawyer match for representation." | |
| ), | |
| "how_to_work_with_lawyer": [ | |
| "If Abunzi does not resolve it, take the mediation record to a land/succession advocate.", | |
| "Ask the advocate whether the claim still needs Abunzi exhaustion before court.", | |
| ], | |
| } | |
| if confidence_band == "LOW" and not is_simple_lookup: | |
| return { | |
| "situation": SITUATION_LAWYER, | |
| "urgency": "MEDIUM", | |
| "rationale": "Confidence is too low for a self-serve determination on this fact pattern.", | |
| "citizen_guidance": "A matched advocate should verify the applicable law before you act.", | |
| "how_to_work_with_lawyer": [ | |
| "Share the chat memorandum and ask which facts would change the answer.", | |
| ], | |
| } | |
| return { | |
| "situation": SITUATION_SELF, | |
| "urgency": "NORMAL", | |
| "rationale": ( | |
| "The question can be answered from retrieved Gazette provisions with practical next steps " | |
| "the citizen can take at a sector or land office." | |
| ), | |
| "citizen_guidance": ( | |
| "Follow the next steps with your documents. Consult an advocate if an office refuses " | |
| "service, a deadline appears, or another party starts a formal complaint." | |
| ), | |
| "how_to_work_with_lawyer": [], | |
| } | |
| def lawyer_match_criteria( | |
| question: str, | |
| category: str, | |
| language: str, | |
| district: Optional[str], | |
| ) -> Dict: | |
| specialty_map = { | |
| "land": "Land law", | |
| "succession": "Succession / inheritance", | |
| "family": "Family law", | |
| "dispute": "Land law", | |
| "general": "General practice", | |
| } | |
| min_years = 5 if _urgency(question) == "HIGH" else 0 | |
| return { | |
| "specialty": specialty_map.get(category, "General practice"), | |
| "district": district, | |
| "language": language, | |
| "min_years_experience": min_years, | |
| "prefer_accepting_clients": True, | |
| # Feeds lawyer_matcher.py's bio/CV keyword-overlap scoring β lets a | |
| # lawyer's bio text match specific facts in the citizen's question, | |
| # not just the coarse specialty label. | |
| "case_description": question, | |
| } |