File size: 17,783 Bytes
18617ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import gradio as gr
import json
import re
import time
from typing import Dict, List, Any

def analyze_legal_clause_enhanced(clause_text: str) -> str:
    """
    Enhanced legal clause analysis with comprehensive pattern matching,
    risk assessment, and plain English explanations
    """
    if not clause_text or not clause_text.strip():
        return json.dumps({
            "error": "Please provide a clause to analyze"
        })
    
    try:
        clause_lower = clause_text.lower()
        
        # Enhanced pattern matching
        ambiguities = []
        risks = []
        risk_scores = {}
        
        # Ambiguous terms with enhanced detection
        ambiguous_patterns = {
            'reasonable': {
                'risk': 2, 
                'desc': 'Subjective standard that may lead to disputes',
                'plain': 'The word "reasonable" means different things to different people',
                'recommendation': 'Define specific criteria, timeframes, or benchmarks for what constitutes "reasonable"'
            },
            'material': {
                'risk': 2, 
                'desc': 'Undefined materiality threshold creates interpretation risk',
                'plain': 'What counts as "material" should be clearly defined with numbers or examples',
                'recommendation': 'Specify dollar amounts, percentages, or concrete examples of materiality'
            },
            'best efforts': {
                'risk': 3, 
                'desc': 'Highest standard of performance with unclear boundaries',
                'plain': '"Best efforts" could mean unlimited obligation - very risky for you',
                'recommendation': 'Replace with "commercially reasonable efforts" with defined performance metrics'
            },
            'timely': {
                'risk': 2, 
                'desc': 'Vague timeframe creates potential scheduling disputes',
                'plain': 'Always use specific dates instead of vague terms like "timely"',
                'recommendation': 'Specify exact deadlines, timeframes, and delivery dates'
            },
            'professional manner': {
                'risk': 1, 
                'desc': 'Subjective performance standard without clear definition',
                'plain': 'Describe exactly what "professional" means in this specific context',
                'recommendation': 'Define specific quality standards or industry benchmarks for professional performance'
            }
        }
        
        for term, details in ambiguous_patterns.items():
            if term in clause_lower:
                risk_level = ['LOW', 'MEDIUM', 'HIGH', 'CRITICAL'][min(details['risk'], 3)]
                ambiguities.append({
                    'issue': f'Ambiguous term: "{term}"',
                    'description': details['desc'],
                    'plainEnglish': details['plain'],
                    'recommendation': details['recommendation'],
                    'riskLevel': risk_level
                })
                risk_scores[term] = details['risk']
        
        # High-risk patterns with comprehensive detection
        high_risk_patterns = [
            {
                'pattern': r'unlimited liability|any and all damages|no limitation.*liability|liable.*all.*damages',
                'risk': 4,
                'name': 'Unlimited liability exposure',
                'desc': 'Exposes party to potentially catastrophic financial risk',
                'plain': 'This could bankrupt you - always limit your liability exposure',
                'recommendation': 'Add liability caps and exclude consequential damages'
            },
            {
                'pattern': r'irrevocably.*assign|irrevocable.*assignment',
                'risk': 3,
                'name': 'Irrevocable assignment',
                'desc': 'Permanent transfer of rights with no recourse or reversal option',
                'plain': 'Once you sign this, you can never get these rights back',
                'recommendation': 'Add termination conditions and scope limitations to assignments'
            },
            {
                'pattern': r'perpetuity|throughout.*universe|forever',
                'risk': 3,
                'name': 'Unlimited duration',
                'desc': 'Unlimited time duration may be legally unenforceable',
                'plain': 'Forever is too long - courts may not enforce overly broad terms',
                'recommendation': 'Limit scope to reasonable time periods and geographic areas'
            },
            {
                'pattern': r'cayman.*law|cayman islands',
                'risk': 3,
                'name': 'Offshore jurisdiction',
                'desc': 'Offshore jurisdiction may limit legal protections and enforcement',
                'plain': 'Resolving disputes offshore may be difficult, expensive, and risky',
                'recommendation': 'Consider requiring disputes be resolved in more favorable jurisdiction'
            },
            {
                'pattern': r'class action.*waiver|waiving.*class action',
                'risk': 2,
                'name': 'Class action waiver',
                'desc': 'Prevents joining group lawsuits against the other party',
                'plain': 'You cannot join with others to sue - this may limit your legal options',
                'recommendation': 'Verify enforceability under applicable state and federal law'
            },
            {
                'pattern': r'waiver.*audit|waiving.*audit',
                'risk': 3,
                'name': 'Audit rights waiver',
                'desc': 'Eliminates oversight and verification rights',
                'plain': 'You are giving up the right to check if they are following the rules',
                'recommendation': 'Preserve essential audit and oversight rights'
            },
            {
                'pattern': r'terminate.*immediately.*without|immediate.*termination.*without',
                'risk': 2,
                'name': 'Immediate termination without notice',
                'desc': 'Allows abrupt contract termination without warning or cure period',
                'plain': 'The other party can end this deal instantly without warning you',
                'recommendation': 'Add reasonable notice requirements and cure periods'
            },
            {
                'pattern': r'indemnify.*harmless|hold.*harmless',
                'risk': 3,
                'name': 'Broad indemnification',
                'desc': 'May require defending against claims beyond your control',
                'plain': 'You might have to pay for problems you did not cause',
                'recommendation': 'Limit indemnification to specific scenarios and exclude gross negligence'
            }
        ]
        
        for pattern_info in high_risk_patterns:
            if re.search(pattern_info['pattern'], clause_lower):
                risk_level = ['LOW', 'MEDIUM', 'HIGH', 'CRITICAL'][min(pattern_info['risk'], 3)]
                risks.append({
                    'issue': f'High-risk clause: {pattern_info["name"]}',
                    'description': pattern_info['desc'],
                    'plainEnglish': pattern_info['plain'],
                    'recommendation': pattern_info['recommendation'],
                    'riskLevel': risk_level
                })
                risk_scores[pattern_info['name']] = pattern_info['risk']
        
        # Contract type detection with specialized analysis
        contract_type = 'General Contract'
        if any(term in clause_lower for term in ['employment', 'employee', 'employer', 'salary', 'benefits']):
            contract_type = 'Employment Agreement'
        elif any(term in clause_lower for term in ['service', 'contractor', 'deliverable', 'statement of work']):
            contract_type = 'Service Agreement'
        elif any(term in clause_lower for term in ['confidential', 'non-disclosure', 'proprietary']):
            contract_type = 'Non-Disclosure Agreement'
        elif any(term in clause_lower for term in ['license', 'intellectual property', 'software', 'patent']):
            contract_type = 'License Agreement'
        elif any(term in clause_lower for term in ['purchase', 'sale', 'goods', 'product']):
            contract_type = 'Purchase Agreement'
        
        # Calculate overall severity based on risk scores
        if risk_scores:
            max_risk = max(risk_scores.values())
            avg_risk = sum(risk_scores.values()) / len(risk_scores)
            
            if max_risk >= 4 or avg_risk >= 3:
                severity = 'CRITICAL'
            elif max_risk >= 3 or avg_risk >= 2.5:
                severity = 'HIGH'
            elif max_risk >= 2 or avg_risk >= 1.5:
                severity = 'MEDIUM'
            else:
                severity = 'LOW'
        else:
            severity = 'LOW'
        
        total_issues = len(ambiguities) + len(risks)
        
        # Generate comprehensive key findings
        key_findings = []
        if total_issues == 0:
            key_findings.append('No major legal issues detected in this clause')
        elif total_issues == 1:
            key_findings.append('Found 1 legal concern requiring attention')
        else:
            key_findings.append(f'Found {total_issues} legal concerns requiring attention')
        
        if severity in ['HIGH', 'CRITICAL']:
            key_findings.append(f'โš ๏ธ {severity} RISK: Immediate legal review strongly recommended')
        
        if contract_type != 'General Contract':
            key_findings.append(f'Detected as {contract_type} - specialized analysis applied')
        
        # Missing standard protections check
        missing_protections = []
        if not any(term in clause_lower for term in ['governing law', 'applicable law']):
            missing_protections.append({
                'element': 'Governing Law Clause',
                'description': 'Specifies which jurisdiction\'s laws apply to the contract',
                'recommendation': 'Add governing law clause to clarify legal jurisdiction',
                'plainEnglish': 'You need to specify which state or country\'s laws apply'
            })
        
        if not any(term in clause_lower for term in ['dispute', 'arbitration', 'litigation']):
            missing_protections.append({
                'element': 'Dispute Resolution Mechanism',
                'description': 'Defines how conflicts will be resolved',
                'recommendation': 'Add clear dispute resolution procedures',
                'plainEnglish': 'You need a plan for handling disagreements'
            })
        
        if 'liability' in clause_lower and not any(term in clause_lower for term in ['limit', 'cap', 'exclude']):
            missing_protections.append({
                'element': 'Liability Limitations',
                'description': 'Protects against excessive financial exposure',
                'recommendation': 'Add liability caps and damage exclusions',
                'plainEnglish': 'You should limit how much you could owe if something goes wrong'
            })
        
        # Legal references based on content
        legal_references = ['Restatement (Second) of Contracts']
        
        if contract_type == 'Employment Agreement':
            legal_references.extend(['Fair Labor Standards Act (FLSA)', 'National Labor Relations Act'])
        elif 'intellectual property' in clause_lower:
            legal_references.extend(['Copyright Act of 1976', 'Patent Act (35 U.S.C.)'])
        elif 'arbitration' in clause_lower:
            legal_references.append('Federal Arbitration Act (FAA)')
        elif any(term in clause_lower for term in ['sale', 'goods', 'purchase']):
            legal_references.append('Uniform Commercial Code (UCC)')
        
        # Format comprehensive analysis results
        formatted_analysis = {
            "summary": {
                "contractType": contract_type,
                "overallSeverity": severity,
                "totalIssues": total_issues,
                "keyFindings": key_findings
            },
            "detailedAnalysis": {
                "ambiguities": ambiguities,
                "risks": risks,
                "missingProtections": missing_protections[:3]  # Limit to top 3
            },
            "recommendations": {
                "immediate": [item['recommendation'] for item in ambiguities + risks if item.get('riskLevel') in ['HIGH', 'CRITICAL']],
                "general": [item['recommendation'] for item in ambiguities + risks if item.get('riskLevel') in ['LOW', 'MEDIUM']],
                "missingElements": [item['recommendation'] for item in missing_protections]
            },
            "plainEnglishExplanation": {
                "whatThisMeans": [item['plainEnglish'] for item in ambiguities + risks],
                "whyItMatters": "Legal language can hide important risks and obligations. This analysis helps you understand what you're agreeing to in simple terms.",
                "nextSteps": "Consider consulting with a qualified attorney for complex agreements, high-value transactions, or when critical risks are identified."
            },
            "legalReferences": legal_references[:4],  # Limit to most relevant
            "metadata": {
                "analysisVersion": "2.0-enhanced-production",
                "analysisDate": time.strftime("%Y-%m-%d %H:%M:%S"),
                "disclaimer": "This analysis is for informational purposes only and does not constitute legal advice. Consult qualified legal counsel for specific legal guidance."
            }
        }
        
        return json.dumps(formatted_analysis, indent=2)
        
    except Exception as e:
        return json.dumps({
            "error": f"Analysis failed: {str(e)}",
            "summary": {"overallSeverity": "UNKNOWN"},
            "recommendations": {"immediate": ["Please retry the analysis or consult legal counsel"]},
            "metadata": {"analysisVersion": "2.0-enhanced-error"}
        })

# Create enhanced Gradio interface
demo = gr.Interface(
    fn=analyze_legal_clause_enhanced,
    inputs=gr.Textbox(
        lines=15,
        max_lines=50,
        placeholder="Paste your legal clause here for comprehensive AI-powered analysis...",
        label="Legal Clause Text",
        info="Enter contract clauses, terms, or legal language for detailed analysis with risk assessment and plain-English explanations"
    ),
    outputs=gr.JSON(
        label="Enhanced Legal Analysis Results",
        show_label=True
    ),
    title="๐Ÿ›๏ธ Enhanced Legal Contract Clause Analyzer",
    description="""
    **AI-Powered Legal Contract Analysis Tool - Enhanced Version 2.0**
    
    This enhanced tool provides comprehensive, professional-grade analysis of legal contract clauses, featuring:
    
    ๐Ÿ” **Comprehensive Analysis**:
    - **Risk Assessment**: Severity scoring (LOW/MEDIUM/HIGH/CRITICAL) with detailed risk evaluation
    - **Plain English**: Simple explanations of complex legal terms and their implications
    - **Contract Type Detection**: Specialized analysis based on agreement type (Employment, Service, NDA, etc.)
    - **Missing Protections**: Identification of important missing clauses and safeguards
    
    ๐Ÿ“Š **Enhanced Features**:
    - **Advanced Pattern Matching**: Detects subtle legal risks and problematic language
    - **Jurisdiction Analysis**: Identifies location-specific legal considerations  
    - **Prioritized Recommendations**: Immediate vs. general action items based on risk level
    - **Legal References**: Relevant laws, cases, and regulations for further research
    - **Professional Structure**: Organized analysis comparable to legal review
    
    โš–๏ธ **Quality Assurance**: Designed to provide thorough, digestible legal insights that help you understand contract risks and make informed decisions.
    
    *Important: This tool provides enhanced legal analysis but should not replace consultation with qualified legal counsel for important agreements or when significant risks are identified.*
    """,
    examples=[
        ["The Contractor shall use reasonable efforts to complete the work in a timely manner using professional standards."],
        ["Company shall be liable for any and all damages, losses, costs, and expenses arising from this agreement, including consequential damages, with no limitation on liability."],
        ["Employee hereby irrevocably assigns to Company all rights, title, and interest in any intellectual property created during employment, including ideas conceived outside work hours, for perpetuity throughout the universe."],
        ["Either party may terminate this agreement immediately without cause or notice. Payment obligations survive indefinitely."],
        ["Recipient agrees to maintain in confidence all information disclosed, including publicly available information, for 50 years."],
        ["All disputes shall be resolved through binding arbitration in the Cayman Islands under Cayman law, with each party waiving rights to class action lawsuits and audit rights."],
        ["Contractor agrees to indemnify and hold harmless Company from any and all claims, including those arising from Company's gross negligence."],
        ["This license grants worldwide, perpetual, irrevocable rights with automatic renewal and waiver of termination rights."]
    ],
    theme=gr.themes.Soft(),
    allow_flagging="never",
    analytics_enabled=False
)

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )