""" đŸ”Ŧ AI-Powered Forensic Triage & Postmortem Intelligence System """ import gradio as gr import pandas as pd import numpy as np import sys import os from datetime import datetime from typing import Dict import warnings warnings.filterwarnings("ignore") sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from modules.autopsy_analyzer import AutopsyAnalyzer from modules.tod_estimator import TimeOfDeathEstimator from modules.digital_evidence import DigitalEvidenceCorrelator from modules.risk_scorer import CaseRiskScorer from modules.timeline_builder import TimelineBuilder autopsy_analyzer = AutopsyAnalyzer() tod_estimator = TimeOfDeathEstimator() evidence_correlator = DigitalEvidenceCorrelator() risk_scorer = CaseRiskScorer() timeline_builder = TimelineBuilder() FORENSIC_CSS = """ .gradio-container { max-width: 1400px !important; } .disclaimer-box { background: #1c1c1c; border: 1px solid #f85149; border-radius: 8px; padding: 12px; margin: 10px 0; font-size: 0.85em; color: #ffa657; } .header-box { text-align: center; padding: 20px; background: linear-gradient(135deg, #0d1117 0%, #161b22 100%); border-radius: 12px; margin-bottom: 20px; border: 1px solid #30363d; } """ # ═══ Demo Data ═══ DEMO_REPORT = """AUTOPSY REPORT - CASE #2024-MH-0847 DECEDENT: John Doe (unidentified male) AGE: Approximately 35-45 years DATE OF EXAMINATION: March 15, 2024, 09:00 AM DATE BODY FOUND: March 14, 2024, 06:30 PM LOCATION: Abandoned warehouse, Industrial District, Block 7 EXTERNAL EXAMINATION: The body is that of a well-nourished adult male, approximately 5'10" tall, weighing approximately 78 kg. Rigor mortis is fully developed in all extremities. Lividity is fixed and posterior, consistent with supine position. No signs of decomposition noted. Body temperature (rectal) measured at 28.5°C at time of discovery. Ambient temperature at scene: 18°C. INJURIES: 1. Blunt force trauma to the right temporal region, measuring 4.5 x 3.2 cm, with underlying subdural hematoma. 2. Defensive wounds on both forearms - three linear abrasions (2-4 cm each). 3. Contusion on the left shoulder, 6 x 4 cm, consistent with forceful impact. 4. Petechial hemorrhages noted in conjunctivae bilaterally. 5. Ligature mark around the neck, 0.5 cm width, horizontal orientation with slight upward angle posteriorly. INTERNAL EXAMINATION: - Subdural hematoma (right hemisphere, approximately 80ml) - Cerebral edema noted - Hyoid bone intact - Lungs show mild congestion - Heart: 340g, no significant atherosclerosis - Liver: mild fatty changes - Stomach contents: partially digested meal (rice, vegetables), approximately 200ml TOXICOLOGY: - Blood alcohol: 0.04 g/dL - Benzodiazepines: detected (trace levels - diazepam) - No illicit substances detected CAUSE OF DEATH: Combination of blunt force head trauma with subdural hematoma and asphyxia due to ligature compression of neck. MANNER OF DEATH: Homicide ADDITIONAL NOTES: - Skin under fingernails collected for DNA analysis - Foreign fibers recovered from ligature site (synthetic, blue) - Time of death estimated between 12-18 hours prior to discovery based on postmortem changes""" DEMO_EVIDENCE = """timestamp,source,event_type,location_lat,location_lon,details 2024-03-14 00:30,CCTV-Cam7,vehicle_detected,19.0760,72.8777,White sedan entering industrial area 2024-03-14 01:15,Mobile-Tower,cell_ping,19.0755,72.8780,Victim phone connected to tower ID-4421 2024-03-14 01:45,CCTV-Cam12,person_detected,19.0762,72.8775,Two individuals walking toward warehouse 2024-03-14 02:00,Mobile-Tower,cell_ping,19.0762,72.8774,Victim phone - last active ping 2024-03-14 02:15,CCTV-Cam12,person_detected,19.0762,72.8775,Single individual leaving warehouse rapidly 2024-03-14 02:20,CCTV-Cam7,vehicle_detected,19.0760,72.8777,White sedan exiting industrial area at high speed 2024-03-14 02:30,Mobile-Tower,cell_disconnect,19.0762,72.8774,Victim phone disconnected 2024-03-14 06:00,CCTV-Cam7,person_detected,19.0758,72.8779,Security guard patrol - routine 2024-03-14 18:30,Emergency,call_received,19.0762,72.8775,Body discovered by security guard""" # ═══ Handler Functions ═══ def analyze_autopsy_report(report_file, report_text, state): try: if report_file is not None: text = autopsy_analyzer.extract_text_from_file(report_file) elif report_text and report_text.strip(): text = report_text.strip() else: return ([("Please upload a file or paste report text.", None)], pd.DataFrame(columns=["Entity", "Category", "Confidence", "Context"]), "## âš ī¸ No input provided", state) results = autopsy_analyzer.analyze(text) state = state or {} state["report_text"] = text state["entities"] = results["entities"] state["report_summary"] = results["summary"] state["timeline_events"] = state.get("timeline_events", []) + results.get("time_events", []) return (results["highlighted_text"], pd.DataFrame(results["entities_table"]), results["summary_markdown"], state) except Exception as e: return [("Error.", None)], pd.DataFrame(), f"## ❌ Error\n{e}", state def calculate_tod(t_rectal, t_ambient, body_weight, corrective_str, rigor, lividity, decomp, humidity, wind, state): try: corrective = float(corrective_str.split(" - ")[0]) results = tod_estimator.estimate(t_rectal=t_rectal, t_ambient=t_ambient, body_weight=body_weight, corrective_factor=corrective, rigor=rigor, lividity=lividity, decomp=decomp, humidity=humidity, wind_speed=wind) cooling_plot = tod_estimator.plot_cooling_curve(t_rectal, t_ambient, body_weight, corrective) state = state or {} state["tod_estimate"] = results["summary"] state["timeline_events"] = state.get("timeline_events", []) + [{ "event": f"TOD: ~{results['summary'].get('estimated_pmi_hours', '?')}h before discovery", "category": "TOD Window", "source": "Henssge Model", "timestamp": datetime.now().isoformat() }] return results["summary"], cooling_plot, results["detail_markdown"], state except Exception as e: return {"error": str(e)}, None, f"## ❌ Error\n{e}", state def correlate_evidence(evidence_file, manual_entries, state): try: state = state or {} if evidence_file is not None: results = evidence_correlator.analyze_from_file(evidence_file) elif manual_entries and manual_entries.strip(): results = evidence_correlator.analyze_from_text(manual_entries) else: return pd.DataFrame(), None, "## âš ī¸ No data provided", state state["digital_evidence"] = results["evidence_records"] state["timeline_events"] = state.get("timeline_events", []) + results.get("timeline_events", []) state["correlations"] = results.get("correlations", []) return results["evidence_table"], results["correlation_plot"], results["analysis_markdown"], state except Exception as e: return pd.DataFrame(), None, f"## ❌ Error\n{e}", state def compute_risk(state): try: state = state or {} if not any(k in state for k in ["entities", "tod_estimate", "digital_evidence"]): return (0, {"LOW": 1.0}, None, "## âš ī¸ Insufficient Data\nAnalyze evidence in other tabs first.", state) results = risk_scorer.compute_risk(state) state["risk_score"] = results["risk_score"] state["anomalies"] = results.get("anomalies", []) return (results["risk_score"], results["risk_classification"], results["anomaly_plot"], results["explanation_markdown"], state) except Exception as e: return 0, {"ERROR": 1.0}, None, f"## ❌ Error\n{e}", state def build_timeline(state): try: state = state or {} if not state.get("timeline_events"): return None, pd.DataFrame(), "## âš ī¸ No timeline data" results = timeline_builder.build(state) return results["timeline_plot"], results["timeline_table"], results["summary_markdown"] except Exception as e: return None, pd.DataFrame(), f"## ❌ Error\n{e}" # ═══ Build App ═══ def create_app(): with gr.Blocks(title="đŸ”Ŧ Forensic Triage & Postmortem Intelligence System", theme=gr.themes.Soft(primary_hue="red", secondary_hue="blue", neutral_hue="slate"), css=FORENSIC_CSS) as demo: case_state = gr.State({}) gr.HTML("""

đŸ”Ŧ AI-Powered Forensic Triage & Postmortem Intelligence System

Intelligent Investigative Support â€ĸ Evidence Analysis â€ĸ Digital Correlation â€ĸ Risk Assessment

""") gr.HTML("""
âš ī¸ DISCLAIMER: This is strictly an investigative assistance platform — NOT a replacement for forensic experts or legal authorities. All outputs support human decision-making only.
""") with gr.Tabs(): # ═══ TAB 1: Autopsy Report ═══ with gr.Tab("📄 Autopsy Report Analysis"): gr.Markdown("### AI-Based Autopsy Report Analysis\nExtract forensic entities, injury patterns, and key findings using NLP.") with gr.Row(): with gr.Column(scale=1): report_file = gr.File(label="📁 Upload Report (PDF/TXT)", file_types=[".pdf", ".txt"], type="filepath") report_text = gr.Textbox(label="📝 Or Paste Report Text", lines=12, placeholder="Paste autopsy report...") with gr.Row(): analyze_btn = gr.Button("🔍 Analyze Report", variant="primary", scale=2) demo_btn = gr.Button("📋 Load Demo", variant="secondary", scale=1) with gr.Column(scale=2): ner_output = gr.HighlightedText(label="đŸˇī¸ Extracted Entities", show_legend=True, combine_adjacent=True, color_map={"CAUSE_OF_DEATH": "#f85149", "INJURY": "#ff7b72", "TOXICOLOGY": "#ffa657", "TIME_INDICATOR": "#79c0ff", "ANATOMICAL": "#56d364", "MEDICAL_FINDING": "#d2a8ff", "MANNER_OF_DEATH": "#f47067", "DEMOGRAPHIC": "#e3b341", "LOCATION": "#a5d6ff", "EVIDENCE": "#7ee787"}) entities_table = gr.DataFrame(label="📊 Structured Entities") report_summary = gr.Markdown(label="📋 Summary") analyze_btn.click(fn=analyze_autopsy_report, inputs=[report_file, report_text, case_state], outputs=[ner_output, entities_table, report_summary, case_state]) demo_btn.click(fn=lambda: DEMO_REPORT, inputs=[], outputs=[report_text]) # ═══ TAB 2: TOD Estimation ═══ with gr.Tab("âąī¸ Time-of-Death Estimation"): gr.Markdown("### Post-Mortem Interval (PMI) Estimation\nHenssge nomogram + postmortem indicators.") with gr.Row(): with gr.Column(scale=1): gr.Markdown("#### đŸŒĄī¸ Temperature") t_rectal = gr.Slider(15, 37.2, value=28.5, step=0.1, label="Rectal Temp (°C)") t_ambient = gr.Slider(-10, 45, value=18, step=0.5, label="Ambient Temp (°C)") body_weight = gr.Slider(20, 180, value=78, step=1, label="Body Weight (kg)") corrective = gr.Dropdown(choices=["1.0 - Naked", "0.75 - Thin layers", "0.9 - Light clothing", "1.1 - Thicker layers", "1.2 - 2-3 layers", "1.3 - Heavy clothing", "0.5 - Moving water", "0.7 - Still water", "0.35 - Flowing water"], value="0.9 - Light clothing", label="Corrective Factor") gr.Markdown("#### đŸ”Ŧ Postmortem Signs") rigor = gr.Radio(["absent", "developing", "full", "resolving"], value="full", label="Rigor Mortis") lividity = gr.Radio(["absent", "developing", "present_movable", "fixed"], value="fixed", label="Lividity") decomp = gr.Radio(["absent", "early_discoloration", "bloating", "advanced"], value="absent", label="Decomposition") gr.Markdown("#### đŸŒ¤ī¸ Environment") humidity = gr.Slider(0, 100, value=65, step=5, label="Humidity (%)") wind = gr.Slider(0, 50, value=5, step=1, label="Wind Speed (km/h)") calc_btn = gr.Button("âąī¸ Calculate PMI", variant="primary") with gr.Column(scale=2): pmi_result = gr.JSON(label="📊 PMI Results") cooling_plot = gr.Plot(label="đŸŒĄī¸ Cooling Curve") tod_detail = gr.Markdown(label="📋 Detailed Analysis") calc_btn.click(fn=calculate_tod, inputs=[t_rectal, t_ambient, body_weight, corrective, rigor, lividity, decomp, humidity, wind, case_state], outputs=[pmi_result, cooling_plot, tod_detail, case_state]) # ═══ TAB 3: Digital Evidence ═══ with gr.Tab("📱 Digital Evidence Correlation"): gr.Markdown("### Digital Evidence Analysis\nCorrelate CCTV, mobile, and geolocation data.") with gr.Row(): with gr.Column(scale=1): evidence_file = gr.File(label="📁 Upload CSV", file_types=[".csv"], type="filepath") gr.Markdown("**CSV format:** `timestamp, source, event_type, location_lat, location_lon, details`") manual_evidence = gr.Textbox(label="📝 Or Enter CSV Data", lines=8, placeholder="timestamp,source,event_type,...") with gr.Row(): correlate_btn = gr.Button("🔗 Correlate", variant="primary", scale=2) demo_ev_btn = gr.Button("📋 Demo", variant="secondary", scale=1) with gr.Column(scale=2): evidence_table = gr.DataFrame(label="📊 Evidence Log") evidence_plot = gr.Plot(label="📍 Correlation Timeline") evidence_md = gr.Markdown(label="📋 Analysis") correlate_btn.click(fn=correlate_evidence, inputs=[evidence_file, manual_evidence, case_state], outputs=[evidence_table, evidence_plot, evidence_md, case_state]) demo_ev_btn.click(fn=lambda: DEMO_EVIDENCE, inputs=[], outputs=[manual_evidence]) # ═══ TAB 4: Risk Scoring ═══ with gr.Tab("âš ī¸ Risk & Anomaly Detection"): gr.Markdown("### Case Risk Scoring & Anomaly Detection\nMulti-factor risk assessment from all evidence.") gr.HTML('
â„šī¸ Requires data from other tabs for comprehensive assessment.
') score_btn = gr.Button("🧮 Compute Risk Score", variant="primary", size="lg") with gr.Row(): with gr.Column(scale=1): risk_score_out = gr.Number(label="Risk Score (0-100)", precision=1) risk_label_out = gr.Label(label="Classification") with gr.Column(scale=2): anomaly_plot_out = gr.Plot(label="📊 Risk Factors") risk_md_out = gr.Markdown(label="📋 Assessment Report") score_btn.click(fn=compute_risk, inputs=[case_state], outputs=[risk_score_out, risk_label_out, anomaly_plot_out, risk_md_out, case_state]) # ═══ TAB 5: Timeline ═══ with gr.Tab("📅 Investigation Timeline"): gr.Markdown("### Integrated Investigation Timeline\nAll evidence sources consolidated.") timeline_btn = gr.Button("📅 Build Timeline", variant="primary", size="lg") timeline_plot_out = gr.Plot(label="📅 Timeline") timeline_table_out = gr.DataFrame(label="📋 Events") timeline_md_out = gr.Markdown(label="📋 Summary") timeline_btn.click(fn=build_timeline, inputs=[case_state], outputs=[timeline_plot_out, timeline_table_out, timeline_md_out]) # ═══ TAB 6: About ═══ with gr.Tab("â„šī¸ About"): gr.Markdown(""" ## đŸ”Ŧ AI-Powered Forensic Triage & Postmortem Intelligence System ### Capabilities | Module | Description | |--------|-------------| | 📄 Autopsy NLP | Entity extraction from unstructured reports | | âąī¸ TOD Estimation | Henssge nomogram + postmortem indicators | | 📱 Digital Evidence | CCTV/mobile/geolocation correlation | | âš ī¸ Risk Scoring | Multi-factor assessment + anomaly detection | | 📅 Timeline | Integrated evidence visualization | ### Methodology - **Henssge (1988)**: Double-exponential cooling model for PMI - **Pattern NLP**: Forensic-domain regex entity extraction - **Risk Engine**: Weighted multi-factor scoring (violence, gaps, toxicology, patterns) - **Anomaly Detection**: Cross-factor inconsistency identification ### Ethics & Legal - âš–ī¸ Not a replacement for experts or legal authorities - 🔒 Data privacy is the responsibility of operating agencies - 🔍 All outputs include reasoning for transparency ### Future Scope - GLiNER-BioMed zero-shot entity extraction - Forensic database integration (AFIS, CODIS) - Multilingual report analysis - Real-time IoT sensor feeds - Federated learning for cross-agency collaboration --- *Version 1.0 — Demonstration of AI-assisted forensic investigation* """) gr.HTML("""
đŸ”Ŧ Forensic Triage Intelligence System v1.0 â€ĸ Investigative Assistance Only â€ĸ Built with 🤗 Hugging Face & Gradio
""") return demo if __name__ == "__main__": demo = create_app() demo.queue().launch(server_name="0.0.0.0", server_port=7860)