mallamace commited on
Commit
f91ef9f
·
verified ·
1 Parent(s): 004eeb8

Upload agentic_legal_analyzer.py

Browse files
Files changed (1) hide show
  1. agentic_legal_analyzer.py +50 -0
agentic_legal_analyzer.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # agentic_legal_analyzer.py
2
+ import re
3
+ import requests
4
+
5
+ # === AGENT 1: CLAUSE CLEANER ===
6
+ def clean_and_split_clauses(raw_text):
7
+ # Split on newlines that look like numbered clauses (e.g., 1., 2., 3.)
8
+ raw_chunks = re.split(r"\n+\d+\.?\s+", raw_text)
9
+ cleaned = []
10
+ for chunk in raw_chunks:
11
+ clause = chunk.strip()
12
+ if len(clause.split()) > 4 and not clause.lower() in ["com", "back to home"]:
13
+ cleaned.append(clause)
14
+ return cleaned
15
+
16
+ # === AGENT 2: RISK ESCALATOR ===
17
+ def escalate_risks(analysis_results):
18
+ escalated = []
19
+ for entry in analysis_results:
20
+ try:
21
+ if entry.get("risk_score") and entry["risk_score"] >= 7:
22
+ response = requests.post("https://myoat.com/api/claude", json={
23
+ "prompt": f"Explain in plain English why this clause may be risky: {entry['clause']}"
24
+ })
25
+ entry["explanation"] = response.json().get("text", "")
26
+ escalated.append(entry)
27
+ except Exception:
28
+ entry["explanation"] = "Unable to analyze risk explanation."
29
+ return escalated
30
+
31
+ # === MAIN CHAIN (Example use with raw input) ===
32
+ if __name__ == "__main__":
33
+ raw_input = open("contract.txt").read() # Assume user uploads this
34
+ clauses = clean_and_split_clauses(raw_input)
35
+
36
+ analysis = []
37
+ for clause in clauses:
38
+ response = requests.post("https://myoat.com/api/claude", json={"prompt": f"Summarize and risk score: {clause}"})
39
+ try:
40
+ result = response.json()
41
+ result["clause"] = clause
42
+ analysis.append(result)
43
+ except:
44
+ analysis.append({"clause": clause, "summary": "", "risk_score": None})
45
+
46
+ escalations = escalate_risks(analysis)
47
+
48
+ print("\n\n=== Escalated Clauses ===")
49
+ for item in escalations:
50
+ print(f"Clause: {item['clause']}\nRisk: {item['risk_score']}\nWhy: {item['explanation']}\n")