VortexNode commited on
Commit
81720b7
·
verified ·
1 Parent(s): 63fcb27

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +66 -0
main.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import gradio as gr
3
+
4
+ # Load pre-trained pipelines
5
+ try:
6
+ summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
7
+ ner = pipeline("ner", model="Davlan/bert-base-multilingual-cased-ner-hrl", aggregation_strategy="simple")
8
+ except Exception as e:
9
+ summarizer = None
10
+ ner = None
11
+ print("Error loading models:", e)
12
+
13
+ # Nigerian law reference (basic keyword-to-punishment mapping)
14
+ crime_punishment_map = {
15
+ "theft": {"law": "Criminal Code Act, Section 390", "punishment": "Up to 3 years imprisonment"},
16
+ "armed robbery": {"law": "Robbery and Firearms Act, Section 1", "punishment": "Death penalty or life imprisonment"},
17
+ "internet fraud": {"law": "Cybercrime Act, 2015", "punishment": "Minimum of 7 years imprisonment"},
18
+ "rape": {"law": "Criminal Law of Lagos State, Section 260", "punishment": "Life imprisonment"},
19
+ "murder": {"law": "Criminal Code Act, Section 319", "punishment": "Death penalty"},
20
+ "assault": {"law": "Criminal Code Act, Section 351", "punishment": "1 year imprisonment"}
21
+ }
22
+
23
+ def classify_crime(text):
24
+ text = text.lower()
25
+ for crime in crime_punishment_map:
26
+ if crime in text:
27
+ return crime, crime_punishment_map[crime]
28
+ return "unknown", {
29
+ "law": "N/A",
30
+ "punishment": "No specific punishment found. Manual review required."
31
+ }
32
+
33
+ # Main analysis function with full error handling
34
+ def analyze_text(text):
35
+ try:
36
+ if not text.strip():
37
+ return "No text provided.", [], {"Crime Type": "N/A", "Applicable Law": "N/A", "Recommended Punishment": "N/A"}
38
+
39
+ summary = summarizer(text, max_length=80, min_length=30, do_sample=False)[0].get("summary_text", "Summary failed.")
40
+ entities = ner(text)
41
+ crime_type, law_info = classify_crime(text)
42
+
43
+ return summary, entities, {
44
+ "Crime Type": crime_type.title() if crime_type != "unknown" else "Unknown",
45
+ "Applicable Law": law_info["law"],
46
+ "Recommended Punishment": law_info["punishment"]
47
+ }
48
+ except Exception as e:
49
+ return f"⚠️ An error occurred: {str(e)}", [], {
50
+ "Crime Type": "Error",
51
+ "Applicable Law": "Error",
52
+ "Recommended Punishment": "Error"
53
+ }
54
+
55
+ # Launch app
56
+ gr.Interface(
57
+ fn=analyze_text,
58
+ inputs=gr.Textbox(lines=12, label="Paste Criminal Case Text"),
59
+ outputs=[
60
+ gr.Textbox(label="Summary"),
61
+ gr.JSON(label="Extracted Entities"),
62
+ gr.JSON(label="Legal Analysis / Recommended Punishment")
63
+ ],
64
+ title="JusticeAI - Legal Case Analyzer",
65
+ description="Paste any criminal case report. This AI will summarize it, extract important entities, and recommend the legal punishment based on Nigerian law."
66
+ ).launch()