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|
| 3 |
+
import os
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
+
DB_FILE = "cases.db"
|
| 8 |
+
|
| 9 |
+
# ------------------ DB Setup ------------------
|
| 10 |
+
def init_db():
|
| 11 |
+
conn = sqlite3.connect(DB_FILE)
|
| 12 |
+
c = conn.cursor()
|
| 13 |
+
c.execute("""
|
| 14 |
+
CREATE TABLE IF NOT EXISTS cases (
|
| 15 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 16 |
+
citizen_name TEXT,
|
| 17 |
+
fine_amount REAL,
|
| 18 |
+
description TEXT,
|
| 19 |
+
police_evidence TEXT,
|
| 20 |
+
citizen_appeal TEXT,
|
| 21 |
+
citizen_evidence TEXT,
|
| 22 |
+
status TEXT,
|
| 23 |
+
created_at TEXT
|
| 24 |
+
)
|
| 25 |
+
""")
|
| 26 |
+
conn.commit()
|
| 27 |
+
conn.close()
|
| 28 |
+
|
| 29 |
+
init_db()
|
| 30 |
+
|
| 31 |
+
# ------------------ Police Functions ------------------
|
| 32 |
+
def police_create_case(citizen_name, fine_amount, description, police_evidence):
|
| 33 |
+
conn = sqlite3.connect(DB_FILE)
|
| 34 |
+
c = conn.cursor()
|
| 35 |
+
c.execute("""
|
| 36 |
+
INSERT INTO cases (citizen_name, fine_amount, description, police_evidence, status, created_at)
|
| 37 |
+
VALUES (?, ?, ?, ?, ?, ?)
|
| 38 |
+
""", (citizen_name, fine_amount, description, police_evidence.name if police_evidence else None,
|
| 39 |
+
"Pending Payment", datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
|
| 40 |
+
conn.commit()
|
| 41 |
+
case_id = c.lastrowid
|
| 42 |
+
conn.close()
|
| 43 |
+
return f"✅ Case created with ID: {case_id}"
|
| 44 |
+
|
| 45 |
+
# ------------------ Citizen Functions ------------------
|
| 46 |
+
def citizen_view_case(case_id):
|
| 47 |
+
conn = sqlite3.connect(DB_FILE)
|
| 48 |
+
c = conn.cursor()
|
| 49 |
+
c.execute("SELECT * FROM cases WHERE id=?", (case_id,))
|
| 50 |
+
row = c.fetchone()
|
| 51 |
+
conn.close()
|
| 52 |
+
if not row:
|
| 53 |
+
return "❌ Case not found"
|
| 54 |
+
return pd.DataFrame([row], columns=["ID", "Citizen", "Fine", "Description", "Police Evidence",
|
| 55 |
+
"Citizen Appeal", "Citizen Evidence", "Status", "Created At"])
|
| 56 |
+
|
| 57 |
+
def citizen_pay_fine(case_id):
|
| 58 |
+
conn = sqlite3.connect(DB_FILE)
|
| 59 |
+
c = conn.cursor()
|
| 60 |
+
c.execute("UPDATE cases SET status=? WHERE id=?", ("Fine Paid", case_id))
|
| 61 |
+
conn.commit()
|
| 62 |
+
conn.close()
|
| 63 |
+
return f"💰 Fine for case {case_id} marked as paid."
|
| 64 |
+
|
| 65 |
+
def citizen_appeal(case_id, appeal_text, evidence_file):
|
| 66 |
+
conn = sqlite3.connect(DB_FILE)
|
| 67 |
+
c = conn.cursor()
|
| 68 |
+
c.execute("UPDATE cases SET citizen_appeal=?, citizen_evidence=?, status=? WHERE id=?",
|
| 69 |
+
(appeal_text, evidence_file.name if evidence_file else None, "Appealed", case_id))
|
| 70 |
+
conn.commit()
|
| 71 |
+
conn.close()
|
| 72 |
+
return f"📤 Appeal submitted for case {case_id}"
|
| 73 |
+
|
| 74 |
+
# ------------------ Magistrate Functions ------------------
|
| 75 |
+
def magistrate_dashboard():
|
| 76 |
+
conn = sqlite3.connect(DB_FILE)
|
| 77 |
+
c = conn.cursor()
|
| 78 |
+
c.execute("SELECT * FROM cases WHERE status='Appealed'")
|
| 79 |
+
rows = c.fetchall()
|
| 80 |
+
conn.close()
|
| 81 |
+
if not rows:
|
| 82 |
+
return "No appealed cases."
|
| 83 |
+
return pd.DataFrame(rows, columns=["ID", "Citizen", "Fine", "Description", "Police Evidence",
|
| 84 |
+
"Citizen Appeal", "Citizen Evidence", "Status", "Created At"])
|
| 85 |
+
|
| 86 |
+
def magistrate_decide(case_id, decision):
|
| 87 |
+
new_status = {
|
| 88 |
+
"Approve Appeal": "Appeal Approved",
|
| 89 |
+
"Reject Appeal": "Appeal Rejected",
|
| 90 |
+
"Escalate Hearing": "Hearing Scheduled"
|
| 91 |
+
}[decision]
|
| 92 |
+
|
| 93 |
+
conn = sqlite3.connect(DB_FILE)
|
| 94 |
+
c = conn.cursor()
|
| 95 |
+
c.execute("UPDATE cases SET status=? WHERE id=?", (new_status, case_id))
|
| 96 |
+
conn.commit()
|
| 97 |
+
conn.close()
|
| 98 |
+
return f"⚖️ Case {case_id} updated: {new_status}"
|
| 99 |
+
|
| 100 |
+
# ------------------ Gradio UI ------------------
|
| 101 |
+
with gr.Blocks() as demo:
|
| 102 |
+
gr.Markdown("# 🚦 Smart Traffic Fine & Case Management MVP")
|
| 103 |
+
|
| 104 |
+
with gr.Tab("Citizen Portal"):
|
| 105 |
+
case_id_input = gr.Number(label="Enter Case ID", precision=0)
|
| 106 |
+
view_btn = gr.Button("View Case")
|
| 107 |
+
case_output = gr.Dataframe()
|
| 108 |
+
view_btn.click(citizen_view_case, inputs=case_id_input, outputs=case_output)
|
| 109 |
+
|
| 110 |
+
with gr.Row():
|
| 111 |
+
pay_btn = gr.Button("Pay Fine")
|
| 112 |
+
appeal_text = gr.Textbox(label="Appeal Text")
|
| 113 |
+
appeal_file = gr.File(label="Upload Appeal Evidence", type="file")
|
| 114 |
+
appeal_btn = gr.Button("Submit Appeal")
|
| 115 |
+
|
| 116 |
+
pay_result = gr.Textbox(label="Result")
|
| 117 |
+
appeal_result = gr.Textbox(label="Appeal Result")
|
| 118 |
+
|
| 119 |
+
pay_btn.click(citizen_pay_fine, inputs=case_id_input, outputs=pay_result)
|
| 120 |
+
appeal_btn.click(citizen_appeal, inputs=[case_id_input, appeal_text, appeal_file], outputs=appeal_result)
|
| 121 |
+
|
| 122 |
+
with gr.Tab("Traffic Police Portal"):
|
| 123 |
+
citizen_name = gr.Textbox(label="Citizen Name")
|
| 124 |
+
fine_amount = gr.Number(label="Fine Amount")
|
| 125 |
+
description = gr.Textbox(label="Description")
|
| 126 |
+
police_file = gr.File(label="Upload Evidence", type="file")
|
| 127 |
+
create_case_btn = gr.Button("Create Case")
|
| 128 |
+
police_result = gr.Textbox(label="Result")
|
| 129 |
+
create_case_btn.click(police_create_case, inputs=[citizen_name, fine_amount, description, police_file], outputs=police_result)
|
| 130 |
+
|
| 131 |
+
with gr.Tab("Magistrate Portal"):
|
| 132 |
+
dashboard_btn = gr.Button("Refresh Appeals Dashboard")
|
| 133 |
+
dashboard_output = gr.Dataframe()
|
| 134 |
+
dashboard_btn.click(magistrate_dashboard, outputs=dashboard_output)
|
| 135 |
+
|
| 136 |
+
case_id_mag = gr.Number(label="Case ID", precision=0)
|
| 137 |
+
decision = gr.Dropdown(["Approve Appeal", "Reject Appeal", "Escalate Hearing"], label="Decision")
|
| 138 |
+
decide_btn = gr.Button("Submit Decision")
|
| 139 |
+
decision_result = gr.Textbox(label="Decision Result")
|
| 140 |
+
decide_btn.click(magistrate_decide, inputs=[case_id_mag, decision], outputs=decision_result)
|
| 141 |
|
|
|
|
| 142 |
demo.launch()
|
| 143 |
+
|