Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,7 @@ class ContractAnalyzer:
|
|
11 |
self.model = AutoModel.from_pretrained("aubmindlab/bert-large-arabertv2")
|
12 |
print("تم تحميل النموذج بنجاح!")
|
13 |
|
|
|
14 |
self.legal_keywords = [
|
15 |
"يلتزم", "يتعهد", "يحق", "لا يحق", "شرط جزائي",
|
16 |
"فسخ العقد", "إنهاء", "تعويض", "غرامة", "مدة العقد",
|
@@ -18,38 +19,113 @@ class ContractAnalyzer:
|
|
18 |
"دفعات", "ضمان", "مخالفة", "إخلال", "قوة قاهرة"
|
19 |
]
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
def analyze_contract(self, contract_text):
|
22 |
try:
|
|
|
|
|
|
|
|
|
23 |
sentences = contract_text.split('.')
|
24 |
-
|
25 |
results = {
|
26 |
-
"potential_issues": [],
|
27 |
"important_clauses": [],
|
28 |
-
"
|
|
|
|
|
|
|
|
|
29 |
}
|
30 |
-
|
31 |
# تحليل كل جملة
|
32 |
for sentence in sentences:
|
33 |
if len(sentence.strip()) < 5:
|
34 |
continue
|
35 |
-
|
36 |
# تحويل النص إلى تمثيل رقمي
|
37 |
inputs = self.tokenizer(sentence, return_tensors="pt", padding=True, truncation=True)
|
38 |
outputs = self.model(**inputs)
|
39 |
-
|
40 |
-
#
|
41 |
for keyword in self.legal_keywords:
|
42 |
if keyword in sentence:
|
43 |
results["important_clauses"].append({
|
44 |
"text": sentence.strip(),
|
45 |
"keyword": keyword
|
46 |
})
|
47 |
-
|
48 |
-
# تحليل
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
risk_words = ["مخالفة", "خرق", "نزاع", "خلاف", "إخلال", "فسخ"]
|
50 |
if any(word in sentence.lower() for word in risk_words):
|
51 |
-
results["
|
52 |
-
|
53 |
# التحقق من العناصر المفقودة
|
54 |
required_elements = [
|
55 |
"مدة العقد", "قيمة العقد", "التزامات الطرفين",
|
@@ -58,28 +134,21 @@ class ContractAnalyzer:
|
|
58 |
for element in required_elements:
|
59 |
if not any(element in s for s in sentences):
|
60 |
results["missing_elements"].append(element)
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
# العناصر المفقودة
|
78 |
-
formatted_results += "\n❌ العناصر المفقودة:\n"
|
79 |
-
formatted_results += "================\n"
|
80 |
-
for element in results["missing_elements"]:
|
81 |
-
formatted_results += f"• {element}\n"
|
82 |
-
|
83 |
return formatted_results
|
84 |
|
85 |
except Exception as e:
|
@@ -88,7 +157,7 @@ class ContractAnalyzer:
|
|
88 |
# إنشاء كائن المحلل
|
89 |
analyzer = ContractAnalyzer()
|
90 |
|
91 |
-
#
|
92 |
def analyze_text(text):
|
93 |
return analyzer.analyze_contract(text)
|
94 |
|
@@ -104,11 +173,18 @@ iface = gr.Interface(
|
|
104 |
label="نتائج التحليل",
|
105 |
lines=20
|
106 |
),
|
107 |
-
title="محلل العقود القانونية",
|
108 |
-
description="
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
)
|
112 |
|
113 |
# تشغيل الواجهة
|
114 |
-
iface.launch(share=True, debug=True)
|
|
|
11 |
self.model = AutoModel.from_pretrained("aubmindlab/bert-large-arabertv2")
|
12 |
print("تم تحميل النموذج بنجاح!")
|
13 |
|
14 |
+
# الكلمات المفتاحية القانونية
|
15 |
self.legal_keywords = [
|
16 |
"يلتزم", "يتعهد", "يحق", "لا يحق", "شرط جزائي",
|
17 |
"فسخ العقد", "إنهاء", "تعويض", "غرامة", "مدة العقد",
|
|
|
19 |
"دفعات", "ضمان", "مخالفة", "إخلال", "قوة قاهرة"
|
20 |
]
|
21 |
|
22 |
+
# قالب التحليل (Prompt)
|
23 |
+
self.analysis_prompt = """
|
24 |
+
تحليل العقد القانوني:
|
25 |
+
|
26 |
+
1. معلومات أساسية:
|
27 |
+
- تاريخ العقد: {date}
|
28 |
+
- الأطراف المتعاقدة: {parties}
|
29 |
+
- موضوع العقد: {subject}
|
30 |
+
|
31 |
+
2. تحليل البنود الرئيسية:
|
32 |
+
{clauses_analysis}
|
33 |
+
|
34 |
+
3. الالتزامات:
|
35 |
+
أ. التزامات الطرف الأول:
|
36 |
+
{party1_obligations}
|
37 |
+
|
38 |
+
ب. التزامات الطرف الثاني:
|
39 |
+
{party2_obligations}
|
40 |
+
|
41 |
+
4. المخاطر المحتملة:
|
42 |
+
{risks}
|
43 |
+
|
44 |
+
5. العناصر المفقودة أو غير الواضحة:
|
45 |
+
{missing_elements}
|
46 |
+
|
47 |
+
6. توصيات قانونية:
|
48 |
+
{recommendations}
|
49 |
+
"""
|
50 |
+
|
51 |
+
def extract_contract_info(self, text):
|
52 |
+
"""استخراج المعلومات الأساسية من العقد"""
|
53 |
+
info = {
|
54 |
+
"date": "غير محدد",
|
55 |
+
"parties": [],
|
56 |
+
"subject": "غير محدد"
|
57 |
+
}
|
58 |
+
|
59 |
+
# البحث عن التاريخ
|
60 |
+
date_indicators = ["بتاريخ", "في يوم", "الموافق"]
|
61 |
+
for indicator in date_indicators:
|
62 |
+
if indicator in text:
|
63 |
+
# استخراج التاريخ باستخدام تعبير نمطي بسيط
|
64 |
+
start_idx = text.find(indicator)
|
65 |
+
end_idx = text.find("\n", start_idx)
|
66 |
+
if end_idx == -1:
|
67 |
+
end_idx = text.find(".", start_idx)
|
68 |
+
if end_idx != -1:
|
69 |
+
info["date"] = text[start_idx:end_idx].strip()
|
70 |
+
|
71 |
+
# البحث عن الأطراف
|
72 |
+
party_indicators = ["طرف أول", "طرف ثاني", "الطرف الأول", "الطرف الثاني"]
|
73 |
+
for indicator in party_indicators:
|
74 |
+
if indicator in text:
|
75 |
+
start_idx = text.find(indicator)
|
76 |
+
end_idx = text.find("\n", start_idx)
|
77 |
+
if end_idx == -1:
|
78 |
+
end_idx = text.find(".", start_idx)
|
79 |
+
if end_idx != -1:
|
80 |
+
info["parties"].append(text[start_idx:end_idx].strip())
|
81 |
+
|
82 |
+
return info
|
83 |
+
|
84 |
def analyze_contract(self, contract_text):
|
85 |
try:
|
86 |
+
# استخراج المعلومات الأساسية
|
87 |
+
contract_info = self.extract_contract_info(contract_text)
|
88 |
+
|
89 |
+
# تحليل البنود والمخاطر
|
90 |
sentences = contract_text.split('.')
|
91 |
+
|
92 |
results = {
|
|
|
93 |
"important_clauses": [],
|
94 |
+
"party1_obligations": [],
|
95 |
+
"party2_obligations": [],
|
96 |
+
"risks": [],
|
97 |
+
"missing_elements": [],
|
98 |
+
"recommendations": []
|
99 |
}
|
100 |
+
|
101 |
# تحليل كل جملة
|
102 |
for sentence in sentences:
|
103 |
if len(sentence.strip()) < 5:
|
104 |
continue
|
105 |
+
|
106 |
# تحويل النص إلى تمثيل رقمي
|
107 |
inputs = self.tokenizer(sentence, return_tensors="pt", padding=True, truncation=True)
|
108 |
outputs = self.model(**inputs)
|
109 |
+
|
110 |
+
# تحليل البنود المهمة
|
111 |
for keyword in self.legal_keywords:
|
112 |
if keyword in sentence:
|
113 |
results["important_clauses"].append({
|
114 |
"text": sentence.strip(),
|
115 |
"keyword": keyword
|
116 |
})
|
117 |
+
|
118 |
+
# تحليل الالتزامات
|
119 |
+
if "طرف أول" in sentence or "الطرف الأول" in sentence:
|
120 |
+
results["party1_obligations"].append(sentence.strip())
|
121 |
+
elif "طرف ثاني" in sentence or "الطرف الثاني" in sentence:
|
122 |
+
results["party2_obligations"].append(sentence.strip())
|
123 |
+
|
124 |
+
# تحليل المخاطر
|
125 |
risk_words = ["مخالفة", "خرق", "نزاع", "خلاف", "إخلال", "فسخ"]
|
126 |
if any(word in sentence.lower() for word in risk_words):
|
127 |
+
results["risks"].append(sentence.strip())
|
128 |
+
|
129 |
# التحقق من العناصر المفقودة
|
130 |
required_elements = [
|
131 |
"مدة العقد", "قيمة العقد", "التزامات الطرفين",
|
|
|
134 |
for element in required_elements:
|
135 |
if not any(element in s for s in sentences):
|
136 |
results["missing_elements"].append(element)
|
137 |
+
results["recommendations"].append(f"يجب إضافة {element} بشكل واضح في العقد")
|
138 |
+
|
139 |
+
# تنسيق النتائج باستخدام القالب
|
140 |
+
formatted_results = self.analysis_prompt.format(
|
141 |
+
date=contract_info["date"],
|
142 |
+
parties="\n".join(contract_info["parties"]) or "غير محدد",
|
143 |
+
subject=contract_info["subject"],
|
144 |
+
clauses_analysis="\n".join([f"• {clause['keyword']}: {clause['text']}" for clause in results["important_clauses"]]),
|
145 |
+
party1_obligations="\n".join([f"• {ob}" for ob in results["party1_obligations"]]) or "غير محدد",
|
146 |
+
party2_obligations="\n".join([f"• {ob}" for ob in results["party2_obligations"]]) or "غير محدد",
|
147 |
+
risks="\n".join([f"• {risk}" for risk in results["risks"]]) or "لا توجد مخاطر واضحة",
|
148 |
+
missing_elements="\n".join([f"• {element}" for element in results["missing_elements"]]) or "لا توجد عناصر مفقودة",
|
149 |
+
recommendations="\n".join([f"• {rec}" for rec in results["recommendations"]]) or "لا توجد توصيات إضافية"
|
150 |
+
)
|
151 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
return formatted_results
|
153 |
|
154 |
except Exception as e:
|
|
|
157 |
# إنشاء كائن المحلل
|
158 |
analyzer = ContractAnalyzer()
|
159 |
|
160 |
+
# دالة التحليل لواجهة gradio
|
161 |
def analyze_text(text):
|
162 |
return analyzer.analyze_contract(text)
|
163 |
|
|
|
173 |
label="نتائج التحليل",
|
174 |
lines=20
|
175 |
),
|
176 |
+
title="محلل العقود القانونية المتقدم",
|
177 |
+
description="""
|
178 |
+
قم بإدخال نص العقد القانوني للحصول على تحليل شامل يتضمن:
|
179 |
+
• المعلومات الأساسية للعقد
|
180 |
+
• تحليل البنود الرئيسية
|
181 |
+
• التزامات كل طرف
|
182 |
+
• المخاطر المحتملة
|
183 |
+
• العناصر المفقودة
|
184 |
+
• التوصيات القانونية
|
185 |
+
""",
|
186 |
+
theme="huggingface"
|
187 |
)
|
188 |
|
189 |
# تشغيل الواجهة
|
190 |
+
iface.launch(share=True, debug=True)
|