|
|
|
import gradio as gr |
|
from transformers import AutoTokenizer, AutoModel |
|
import torch |
|
import numpy as np |
|
|
|
class ContractAnalyzer: |
|
def __init__(self): |
|
print("جاري تحميل النموذج...") |
|
self.tokenizer = AutoTokenizer.from_pretrained("aubmindlab/bert-large-arabertv2") |
|
self.model = AutoModel.from_pretrained("aubmindlab/bert-large-arabertv2") |
|
print("تم تحميل النموذج بنجاح!") |
|
|
|
self.legal_keywords = [ |
|
"يلتزم", "يتعهد", "يحق", "لا يحق", "شرط جزائي", |
|
"فسخ العقد", "إنهاء", "تعويض", "غرامة", "مدة العقد", |
|
"طرف أول", "طرف ثاني", "قيمة العقد", "التزامات", "سداد", |
|
"دفعات", "ضمان", "مخالفة", "إخلال", "قوة قاهرة" |
|
] |
|
|
|
def analyze_contract(self, contract_text): |
|
try: |
|
sentences = contract_text.split('.') |
|
|
|
results = { |
|
"potential_issues": [], |
|
"important_clauses": [], |
|
"missing_elements": [] |
|
} |
|
|
|
|
|
for sentence in sentences: |
|
if len(sentence.strip()) < 5: |
|
continue |
|
|
|
|
|
inputs = self.tokenizer(sentence, return_tensors="pt", padding=True, truncation=True) |
|
outputs = self.model(**inputs) |
|
|
|
|
|
for keyword in self.legal_keywords: |
|
if keyword in sentence: |
|
results["important_clauses"].append({ |
|
"text": sentence.strip(), |
|
"keyword": keyword |
|
}) |
|
|
|
|
|
risk_words = ["مخالفة", "خرق", "نزاع", "خلاف", "إخلال", "فسخ"] |
|
if any(word in sentence.lower() for word in risk_words): |
|
results["potential_issues"].append(sentence.strip()) |
|
|
|
|
|
required_elements = [ |
|
"مدة العقد", "قيمة العقد", "التزامات الطرفين", |
|
"طريقة السداد", "الضمانات", "شروط الإنهاء" |
|
] |
|
for element in required_elements: |
|
if not any(element in s for s in sentences): |
|
results["missing_elements"].append(element) |
|
|
|
|
|
formatted_results = "نتائج تحليل العقد:\n\n" |
|
|
|
|
|
formatted_results += "🔍 البنود المهمة:\n" |
|
formatted_results += "================\n" |
|
for clause in results["important_clauses"]: |
|
formatted_results += f"• {clause['keyword']}: {clause['text']}\n" |
|
|
|
|
|
formatted_results += "\n⚠️ المشاكل المحتملة:\n" |
|
formatted_results += "================\n" |
|
for issue in results["potential_issues"]: |
|
formatted_results += f"• {issue}\n" |
|
|
|
|
|
formatted_results += "\n❌ العناصر المفقودة:\n" |
|
formatted_results += "================\n" |
|
for element in results["missing_elements"]: |
|
formatted_results += f"• {element}\n" |
|
|
|
return formatted_results |
|
|
|
except Exception as e: |
|
return f"حدث خطأ أثناء التحليل: {str(e)}" |
|
|
|
|
|
analyzer = ContractAnalyzer() |
|
|
|
|
|
def analyze_text(text): |
|
return analyzer.analyze_contract(text) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=analyze_text, |
|
inputs=gr.Textbox( |
|
placeholder="أدخل نص العقد هنا...", |
|
label="نص العقد", |
|
lines=10 |
|
), |
|
outputs=gr.Textbox( |
|
label="نتائج التحليل", |
|
lines=20 |
|
), |
|
title="محلل العقود القانونية", |
|
description="قم بإدخال نص العقد القانوني للحصول على تحليل شامل للبنود والمخاطر المحتملة والعناصر المفقودة", |
|
theme="huggingface", |
|
|
|
) |
|
|
|
|
|
iface.launch(share=True, debug=True) |