|
|
|
|
|
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 = [ |
|
"يلتزم", "الزام" , "يتعهد", "يحق", "لا يحق", "شرط جزائي", |
|
"فسخ العقد", "إنهاء", "تعويض", "غرامة", "مدة العقد", |
|
"بمبلغ إجمالي", "دفعة أولى", "دفعة ثانية", "العربون", |
|
"طرف أول", "طرف ثاني", "قيمة العقد", "التزامات", "سداد", |
|
"دفعات", "ينكل ", "ضمان", "مخالفة", "إخلال", "قوة قاهرة" |
|
] |
|
|
|
|
|
self.analysis_prompt = """ |
|
تحليل العقد القانوني: |
|
|
|
1. معلومات أساسية: |
|
- تاريخ العقد: {date} |
|
- الأطراف المتعاقدة: {parties} |
|
- موضوع العقد: {subject} |
|
|
|
2. المخاطر المحتملة: |
|
{risks} |
|
|
|
3. العناصر المفقودة أو غير الواضحة: |
|
{missing_elements} |
|
|
|
4. توصيات قانونية: |
|
{recommendations} |
|
""" |
|
|
|
def extract_contract_info(self, text): |
|
"""استخراج المعلومات الأساسية من العقد""" |
|
info = { |
|
"date": "غير محدد", |
|
"parties": [], |
|
"subject": "غير محدد" |
|
} |
|
|
|
|
|
date_indicators = ["بتاريخ", "في يوم", "الموافق"] |
|
for indicator in date_indicators: |
|
if indicator in text: |
|
|
|
start_idx = text.find(indicator) |
|
end_idx = text.find("\n", start_idx) |
|
if end_idx == -1: |
|
end_idx = text.find(".", start_idx) |
|
if end_idx != -1: |
|
info["date"] = text[start_idx:end_idx].strip() |
|
|
|
|
|
party_indicators = ["طرف أول", "طرف ثاني", "الطرف الأول", "الطرف الثاني", "الفريق الأول", "الفريق الثاني"] |
|
for indicator in party_indicators: |
|
if indicator in text: |
|
start_idx = text.find(indicator) |
|
end_idx = text.find("\n", start_idx) |
|
if end_idx == -1: |
|
end_idx = text.find(".", start_idx) |
|
if end_idx != -1: |
|
info["parties"].append(text[start_idx:end_idx].strip()) |
|
|
|
|
|
|
|
if info["subject"] == "غير محدد": |
|
|
|
first_sentences = text.split('\n')[:3] |
|
for sentence in first_sentences: |
|
if any(word in sentence.lower() for word in ["اتفاق", "عقد", "تعاقد"]): |
|
info["subject"] = sentence.strip() |
|
break |
|
|
|
return info |
|
|
|
def analyze_contract(self, contract_text): |
|
try: |
|
|
|
contract_info = self.extract_contract_info(contract_text) |
|
|
|
|
|
sentences = contract_text.split('.') |
|
|
|
results = { |
|
"important_clauses": [], |
|
"risks": [], |
|
"missing_elements": [], |
|
"recommendations": [] |
|
} |
|
|
|
|
|
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["risks"].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) |
|
results["recommendations"].append(f"يجب إضافة {element} بشكل واضح في العقد") |
|
|
|
|
|
formatted_results = self.analysis_prompt.format( |
|
date=contract_info["date"], |
|
parties="\n".join(contract_info["parties"]) or "غير محدد", |
|
subject=contract_info["subject"], |
|
|
|
risks="\n".join([f"• {risk}" for risk in results["risks"]]) or "لا توجد مخاطر واضحة", |
|
missing_elements="\n".join([f"• {element}" for element in results["missing_elements"]]) or "لا توجد عناصر مفقودة", |
|
recommendations="\n".join([f"• {rec}" for rec in results["recommendations"]]) or "لا توجد توصيات إضافية" |
|
) |
|
|
|
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=30, |
|
rtl=True, |
|
), |
|
outputs=gr.Textbox( |
|
label="نتائج التحليل", |
|
lines=30, |
|
rtl=True, |
|
), |
|
title="محلل العقود القانونية ", |
|
description=""" |
|
قم بإدخال نص العقد القانوني للحصول على تحليل شامل يتضمن: |
|
• المعلومات الأساسية للعقد |
|
• المخاطر المحتملة |
|
• العناصر المفقودة |
|
• التوصيات القانونية |
|
""", |
|
theme=gr.themes.Soft( |
|
primary_hue="blue", |
|
secondary_hue="blue", |
|
neutral_hue="blue", |
|
|
|
), |
|
css=""" |
|
.gradio-container { |
|
direction: rtl !important; |
|
text-align: right !important; |
|
} |
|
.output-markdown { |
|
direction: rtl !important; |
|
text-align: right !important; |
|
} |
|
.input-markdown { |
|
direction: rtl !important; |
|
text-align: right !important; |
|
} |
|
label { |
|
text-align: right !important; |
|
} |
|
.prose { |
|
direction: rtl !important; |
|
text-align: right !important; |
|
} |
|
""" |
|
) |
|
|
|
|
|
iface.launch(share=True, debug=True) |
|
|
|
|