Spaces:
Running on Zero
Running on Zero
File size: 10,650 Bytes
62be02c | 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 | """
SAULSGPT — DATA SYNCHRONIZATION MANAGER
========================================
Guarantees 100% data symmetry between the GNN Knowledge Lattice
and the ChromaDB Vector Database.
When any new legal doctrine, case precedent, or statutory provision
is added to the GNN, this manager automatically:
1. Validates presence in ChromaDB.
2. Computes 384-dimensional dense semantic embeddings (all-MiniLM-L6-v2).
3. Upserts verbatim statutory text & legal definitions into ChromaDB.
4. Ensures zero orphaned nodes and zero unindexed text chunks.
"""
import json
import os
from typing import Dict, List, Any
# Canonical Statutory Descriptions for Graph Entities
ENTITY_DOCUMENTS = {
# ── Criminal BNS / BNSS / BSA Provisions ──
"BNS_329": "Section 329 Bharatiya Nyaya Sanhita, 2023: Criminal trespass and house-trespass. Whoever enters into or upon property in possession of another with intent to commit an offence or to intimidate, insult or annoy, is guilty of criminal trespass, punishable with up to three months imprisonment or fine.",
"BNS_115": "Section 115 Bharatiya Nyaya Sanhita, 2023: Voluntarily causing hurt. Whoever does any act with intention of causing hurt to any person, or with knowledge that he is likely to cause hurt, is punishable with imprisonment up to one year or fine up to ten thousand rupees.",
"BNS_117": "Section 117 Bharatiya Nyaya Sanhita, 2023: Voluntarily causing grievous hurt. Punishable with imprisonment up to seven years and fine.",
"BNS_351": "Section 351 Bharatiya Nyaya Sanhita, 2023: Criminal intimidation. Whoever threatens another with injury to person, reputation or property with intent to cause alarm.",
"BNS_303": "Section 303 Bharatiya Nyaya Sanhita, 2023: Theft. Whoever intending to take dishonestly any movable property out of possession of any person without consent.",
"BNS_318": "Section 318 Bharatiya Nyaya Sanhita, 2023: Cheating and dishonestly inducing delivery of property. Punishable with imprisonment up to seven years.",
"BNS_85": "Section 85 Bharatiya Nyaya Sanhita, 2023: Husband or relative of husband of a woman subjecting her to cruelty. Imprisonment up to three years and fine.",
"BNSS_173": "Section 173 Bharatiya Nagarik Suraksha Sanhita, 2023: Information in cognizable cases (FIR). Every information relating to commission of cognizable offence shall be reduced to writing and signed. Zero FIR mandatory irrespective of territorial jurisdiction.",
"BNSS_173_4": "Section 173(4) Bharatiya Nagarik Suraksha Sanhita, 2023: Remedy upon police refusal to register FIR. Any person aggrieved may send substance of information in writing and by post to Superintendent of Police.",
"BNSS_175_3": "Section 175(3) Bharatiya Nagarik Suraksha Sanhita, 2023: Application to Judicial Magistrate to direct investigation and FIR registration following refusal by Police and SP.",
"BNSS_164": "Section 164 Bharatiya Nagarik Suraksha Sanhita, 2023: Dispute concerning land or water likely to cause breach of peace. Executive Magistrate (SDM) inquiry and possession restoration within two months of dispossession.",
"BNSS_528": "Section 528 Bharatiya Nagarik Suraksha Sanhita, 2023: Inherent powers of High Court to prevent abuse of process of any court and secure ends of justice (Quashing petitions).",
"BNSS_144": "Section 144 Bharatiya Nagarik Suraksha Sanhita, 2023: Order for maintenance of wives, children and parents. Magistrate order for monthly allowance and interim maintenance.",
# ── Civil Procedure & Injunctions ──
"Order_39_Rule_1_2_CPC": "Order 39 Rules 1 and 2 Code of Civil Procedure, 1908: Temporary Injunctions and Interlocutory Orders. Court may grant temporary injunction restraining alienation, damage, or dispossession upon satisfaction of (1) Prima Facie Case, (2) Balance of Convenience, and (3) Irreparable Injury.",
"Order_7_Rule_11_CPC": "Order 7 Rule 11 Code of Civil Procedure, 1908: Rejection of Plaint. Plaint shall be rejected where it does not disclose cause of action, is undervalued, insufficiently stamped, or barred by any law (Limitation Act).",
"Order_37_CPC": "Order 37 Code of Civil Procedure, 1908: Summary Procedure for suits upon negotiable instruments, bills of exchange, and written contracts for debt recovery.",
"Section_89_CPC": "Section 89 Code of Civil Procedure, 1908: Settlement of disputes outside court through Arbitration, Conciliation, Judicial settlement including Lok Adalat, or Mediation.",
# ── Family, Succession & DV Law ──
"HSA_6": "Section 6 Hindu Succession Act, 1956 (amended 2005): Devolution of interest in coparcenary property. Daughter of a coparcener becomes a coparcener by birth in her own right with same rights and liabilities as a son.",
"HSA_8": "Section 8 Hindu Succession Act, 1956: General rules of succession in case of males. Property devolves firstly upon Class I heirs (Son, Daughter, Widow, Mother) equally.",
"HSA_30": "Section 30 Hindu Succession Act, 1956: Testamentary disposition. Any Hindu may dispose of by will or other testamentary disposition any property including undivided coparcenary interest.",
"HMA_13_1_i": "Section 13(1)(i) Hindu Marriage Act, 1955: Divorce on ground of adultery. Other party had voluntary sexual intercourse with any person other than spouse after marriage.",
"HMA_13_1_ia": "Section 13(1)(ia) Hindu Marriage Act, 1955: Divorce on ground of cruelty. Other party has after marriage treated petitioner with physical or mental cruelty.",
"HMA_13B": "Section 13B Hindu Marriage Act, 1955: Divorce by mutual consent on joint petition living separately for 1 year.",
"HMA_24": "Section 24 Hindu Marriage Act, 1955: Maintenance pendente lite and expenses of proceedings where either spouse has no independent income.",
"HMA_25": "Section 25 Hindu Marriage Act, 1955: Permanent alimony and maintenance granted by court at time of passing decree.",
"PWDVA_2005_S12": "Section 12 Protection of Women from Domestic Violence Act, 2005: Application to Magistrate for protection orders (S.18), residence orders (S.19), monetary relief (S.20), and custody (S.21).",
# ── Commercial, Contracts & Cheque Bounce ──
"NI_Act_138": "Section 138 Negotiable Instruments Act, 1881: Dishonour of cheque for insufficiency of funds. Imprisonment up to two years or fine up to twice cheque amount. Requires 15-day statutory notice within 30 days of memo.",
"NI_Act_142": "Section 142 Negotiable Instruments Act, 1881: Cognizance of offences. Written complaint before Judicial Magistrate within one month of cause of action arising.",
"Contract_Act_S27": "Section 27 Indian Contract Act, 1872: Agreement in restraint of trade void. Every agreement by which anyone is restrained from exercising a lawful profession, trade or business is to that extent void.",
"Contract_Act_S73": "Section 73 Indian Contract Act, 1872: Compensation for loss or damage caused by breach of contract. Entitled to receive compensation for actual loss.",
"Contract_Act_S74": "Section 74 Indian Contract Act, 1872: Compensation for breach where penalty stipulated. Reasonable compensation not exceeding penalty amount.",
"Arbitration_Act_S9": "Section 9 Arbitration and Conciliation Act, 1996: Interim measures by court before, during, or after arbitral proceedings.",
"Arbitration_Act_S12_5": "Section 12(5) Arbitration and Conciliation Act, 1996: Ineligibility of persons having relationship with parties/counsel from appointment as unilateral arbitrator.",
# ── Constitutional Articles & Landmark Precedents ──
"Article_21": "Article 21 Constitution of India: Protection of life and personal liberty. No person shall be deprived of his life or personal liberty except according to procedure established by law.",
"Article_226": "Article 226 Constitution of India: Power of High Courts to issue writs including Habeas Corpus, Mandamus, Prohibition, Quo Warranto and Certiorari for enforcement of Fundamental Rights and any other purpose.",
"Shreya_Singhal_2015": "Shreya Singhal v. Union of India (2015) 5 SCC 1: Supreme Court struck down Section 66A of the Information Technology Act, 2000 in its entirety as unconstitutional for violating Article 19(1)(a) Free Speech.",
"Vineeta_Sharma_2020": "Vineeta Sharma v. Rakesh Sharma (2020) 9 SCC 1: Supreme Court 3-Judge Bench held that daughters have equal coparcenary birthrights in ancestral property under Section 6 HSA regardless of whether father was alive on Sept 9, 2005.",
"Joseph_Shine_2018": "Joseph Shine v. Union of India (2018): Supreme Court Constitution Bench unanimously struck down Section 497 IPC (adultery as a crime) as unconstitutional, arbitrary and violative of Articles 14 and 21."
}
def sync_gnn_with_chromadb(verbose: bool = True) -> Dict[str, Any]:
"""
Synchronizes GNN entities with ChromaDB vector store.
Ensures all canonical GNN nodes have exact corresponding text chunks in ChromaDB.
"""
from layer2_retrieval import _ensure_models
import layer2_retrieval as l2
_ensure_models()
collection = l2.collection
embedder = l2.embedder
if collection is None or embedder is None:
return {"status": "error", "message": "ChromaDB or Embedder not initialized"}
existing_count_before = collection.count()
newly_added = 0
for node_id, doc_text in ENTITY_DOCUMENTS.items():
# Check if node already exists by id
res = collection.get(ids=[f"gnn_sync_{node_id}"])
if not res or not res.get("ids"):
# Compute 384-dim embedding
emb = embedder.encode(doc_text).tolist()
collection.upsert(
ids=[f"gnn_sync_{node_id}"],
documents=[doc_text],
embeddings=[emb],
metadatas=[{
"act_name": node_id.split("_")[0],
"section_number": node_id,
"source": "gnn_synced_entity",
"status": "active"
}]
)
newly_added += 1
total_after = collection.count()
stats = {
"status": "synchronized",
"gnn_entities_checked": len(ENTITY_DOCUMENTS),
"newly_upserted_to_chroma": newly_added,
"chroma_total_chunks": total_after
}
if verbose:
print(f"[DataSync] [OK] Synchronization Complete: {stats}")
return stats
if __name__ == "__main__":
sync_gnn_with_chromadb(verbose=True)
|