File size: 4,614 Bytes
9830db9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

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)

# تكوين واجهة gradio
iface = gr.Interface(
    fn=analyze_text,
    inputs=gr.Textbox(
        placeholder="أدخل نص العقد هنا...",
        label="نص العقد",
        lines=10
    ),
    outputs=gr.Textbox(
        label="نتائج التحليل",
        lines=20
    ),
    title="محلل العقود القانونية",
    description="قم بإدخال نص العقد القانوني للحصول على تحليل شامل للبنود والمخاطر المحتملة والعناصر المفقودة",
    theme="huggingface",
    #layout="vertical"
)

# تشغيل الواجهة
iface.launch(share=True, debug=True)