Muthukumarank's picture
Add app.py
e36bc3e verified
"""
๐Ÿ”ฌ 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("""<div class="header-box">
<h1>๐Ÿ”ฌ AI-Powered Forensic Triage & Postmortem Intelligence System</h1>
<p style="color: #8b949e; font-size: 1.1em;">Intelligent Investigative Support โ€ข Evidence Analysis โ€ข Digital Correlation โ€ข Risk Assessment</p>
</div>""")
gr.HTML("""<div class="disclaimer-box">
โš ๏ธ <strong>DISCLAIMER:</strong> This is strictly an investigative assistance platform โ€” NOT a replacement for forensic experts or legal authorities. All outputs support human decision-making only.
</div>""")
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('<div class="disclaimer-box">โ„น๏ธ Requires data from other tabs for comprehensive assessment.</div>')
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("""<div style="text-align: center; padding: 20px; color: #8b949e; font-size: 0.85em; border-top: 1px solid #30363d; margin-top: 20px;">
๐Ÿ”ฌ Forensic Triage Intelligence System v1.0 โ€ข Investigative Assistance Only โ€ข Built with ๐Ÿค— Hugging Face & Gradio
</div>""")
return demo
if __name__ == "__main__":
demo = create_app()
demo.queue().launch(server_name="0.0.0.0", server_port=7860)