import gradio as gr import json import re import os def analyze_legal_clause(clause_text): """ Analyze a legal clause and return structured analysis """ if not clause_text or not clause_text.strip(): return json.dumps({ "error": "Please provide a clause to analyze" }) try: # Comprehensive legal analysis using advanced pattern recognition clause_lower = clause_text.lower() detected_ambiguities = [] detected_risks = [] detected_recommendations = [] detected_missing = [] detected_references = [] # Advanced legal analysis patterns if 'reasonable' in clause_lower: detected_ambiguities.append("Term 'reasonable' is subjective and may lead to disputes over interpretation") detected_recommendations.append("Define specific criteria, timeframes, or benchmarks for what constitutes 'reasonable'") if 'best efforts' in clause_lower or 'best endeavors' in clause_lower: detected_ambiguities.append("'Best efforts' standard lacks clear definition and enforcement criteria") detected_recommendations.append("Replace with 'commercially reasonable efforts' or define specific performance metrics") detected_references.append("Case law: Bloor Italian Gifts Ltd. v. Dixon (reasonable vs. best efforts)") if 'material' in clause_lower and ('breach' in clause_lower or 'change' in clause_lower): detected_ambiguities.append("Definition of 'material' is not specified and subject to interpretation") detected_recommendations.append("Define materiality thresholds with specific examples or percentage/dollar amounts") if 'confidential' in clause_lower: detected_risks.append("Scope of confidentiality obligations may be overly broad or insufficiently defined") detected_recommendations.append("Clearly define categories of confidential information with specific exclusions") detected_missing.append("Confidentiality carve-outs for publicly available information") if 'terminate' in clause_lower or 'termination' in clause_lower: detected_risks.append("Termination conditions and procedures may create enforcement difficulties") detected_recommendations.append("Specify exact termination procedures, notice requirements, and cure periods") detected_missing.append("Post-termination obligations and survival clauses") if 'liability' in clause_lower or 'damages' in clause_lower: detected_risks.append("Liability exposure may be inadequately limited or undefined") detected_recommendations.append("Add comprehensive liability limitation and damages cap clauses") detected_references.append("Uniform Commercial Code provisions on consequential damages") if 'force majeure' in clause_lower or 'act of god' in clause_lower: detected_ambiguities.append("Force majeure events may not cover modern risks (e.g., cyber attacks, pandemics)") detected_recommendations.append("Update force majeure clause to include contemporary risk factors") if 'intellectual property' in clause_lower or ' ip ' in clause_lower: detected_risks.append("Intellectual property ownership and licensing terms may be unclear") detected_missing.append("IP indemnification and warranty provisions") detected_references.append("Copyright Act and Patent Act provisions") if 'payment' in clause_lower or 'fee' in clause_lower: detected_ambiguities.append("Payment terms, schedules, and late payment consequences unclear") detected_recommendations.append("Specify exact payment amounts, due dates, and late payment penalties") if 'dispute' in clause_lower or 'arbitration' in clause_lower: detected_recommendations.append("Ensure dispute resolution mechanism is enforceable in relevant jurisdictions") detected_missing.append("Choice of law and venue provisions") # Advanced risk pattern detection if 'shady' in clause_lower or 'cayman' in clause_lower: detected_risks.append("Offshore jurisdiction may limit legal protections and enforcement options") detected_recommendations.append("Consider requiring disputes be resolved in more favorable jurisdiction") if 'irrevocably' in clause_lower and 'assign' in clause_lower: detected_risks.append("Irrevocable assignment clauses provide no recourse for the assigning party") detected_recommendations.append("Add limitations on scope and duration of assignments") if 'perpetuity' in clause_lower or 'throughout the universe' in clause_lower: detected_risks.append("Overly broad temporal and geographic scope may be unenforceable") detected_recommendations.append("Limit scope to reasonable time periods and jurisdictions") if 'deemed granted' in clause_lower or 'automatically' in clause_lower: detected_ambiguities.append("Automatic approval mechanisms may not provide adequate oversight") detected_recommendations.append("Require explicit approval for important decisions") if 'waiver' in clause_lower and ('audit' in clause_lower or 'oversight' in clause_lower): detected_risks.append("Waiver of audit/oversight rights eliminates important protections") detected_recommendations.append("Preserve essential audit and oversight rights") if 'liquidated damages' in clause_lower: detected_ambiguities.append("Liquidated damages may be challenged if deemed punitive rather than compensatory") detected_recommendations.append("Ensure liquidated damages reflect reasonable estimate of actual damages") detected_references.append("Restatement (Second) of Contracts § 356") if 'class action' in clause_lower and 'waiver' in clause_lower: detected_risks.append("Class action waivers may be unenforceable in certain jurisdictions") detected_recommendations.append("Check enforceability under applicable state and federal law") detected_references.append("AT&T Mobility LLC v. Concepcion (2011)") # Check for missing standard clauses if not any(x in clause_lower for x in ['governing law', 'applicable law']): detected_missing.append("Governing law clause") if not any(x in clause_lower for x in ['dispute', 'arbitration', 'litigation']): detected_missing.append("Dispute resolution mechanism") if not any(x in clause_lower for x in ['amendment', 'modification']): detected_missing.append("Contract amendment procedures") if not any(x in clause_lower for x in ['entire agreement', 'integration']): detected_missing.append("Integration/entire agreement clause") if not any(x in clause_lower for x in ['severability', 'severable']): detected_missing.append("Severability clause") # Add default items if none detected if not detected_ambiguities: detected_ambiguities.append("Contract language could benefit from more specific definitions") if not detected_risks: detected_risks.append("Standard legal review recommended to identify potential liabilities") if not detected_recommendations: detected_recommendations.append("Consider engaging qualified legal counsel for comprehensive review") # Add general legal references if not detected_references: detected_references.extend(["Restatement (Second) of Contracts", "Uniform Commercial Code (UCC)"]) analysis_result = { "ambiguities": detected_ambiguities[:5], # Limit to 5 items for clarity "risks": detected_risks[:5], "recommendations": detected_recommendations[:5], "missingElements": detected_missing[:4], "references": detected_references[:3], "enhanced": True, "keyFinding": f"Legal analysis identified {len(detected_ambiguities + detected_risks)} key concerns requiring attention", "analysisType": "rule_based_legal_analysis", "version": "1.0" } return json.dumps(analysis_result, indent=2) except Exception as e: return json.dumps({ "error": f"Analysis failed: {str(e)}", "ambiguities": ["Unable to analyze - please try again"], "risks": ["Analysis service temporarily unavailable"], "recommendations": ["Please retry the analysis"], "missingElements": [], "references": [], "analysisType": "error" }) # Create Gradio interface with enhanced configuration demo = gr.Interface( fn=analyze_legal_clause, inputs=gr.Textbox( lines=15, max_lines=50, placeholder="Paste your legal clause here for comprehensive analysis...", label="Legal Clause Text", info="Enter contract clauses, terms, or legal language for detailed analysis" ), outputs=gr.JSON( label="Legal Analysis Results", show_label=True ), title="🏛️ Legal Contract Clause Analyzer", description=""" **Advanced Legal Contract Analysis Tool** This tool provides comprehensive analysis of legal contract clauses, identifying: - **Ambiguities**: Terms that may be unclear or subject to interpretation - **Risks**: Potential legal exposures and liabilities - **Recommendations**: Specific suggestions for improvement - **Missing Elements**: Standard clauses that should be considered - **Legal References**: Relevant laws, cases, and legal principles *Note: This tool provides general legal analysis and should not replace consultation with qualified legal counsel.* """, examples=[ ["The Party shall use reasonable efforts to complete the work in a timely manner."], ["Either party may terminate this agreement upon thirty (30) days written notice for any reason or no reason."], ["Confidential Information includes all non-public information disclosed by either party during the term of this agreement."], ["Investor irrevocably assigns all rights, titles, and interests in perpetuity throughout the universe to Company, including waiver of audit rights."], ["In the event of a material breach, the non-breaching party may seek liquidated damages not to exceed $10,000."], ["The parties agree to binding arbitration and waive all rights to participate in class action lawsuits."] ], 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 )