Views
No views yet
1{
2 "per_device_train_batch_size": 1,
3 "gradient_accumulation_steps": 32,
4 "num_train_epochs": 3,
5 "learning_rate": 2e-4,
6 "bf16": true,
7 "max_seq_length": 10500,
8 "evaluation_strategy": "steps",
9 "eval_steps": 500,
10 "save_steps": 500
11}1{
2 "case_information": {
3 "case_number": "حالة رقم ١٢٣٤",
4 "date_of_ruling": "٢٠٢٣/٠١/١٥",
5 "court": "المحكمة العليا",
6 "main_case_topic": "نزاع تجاري",
7 "parties_involved": "شركة أ ضد شركة ب"
8 },
9 "persons_involved": [
10 {
11 "name": "محمد أحمد",
12 "role": "المدعي"
13 }
14 ],
15 "background_of_the_case": {
16 "overview": "نزاع تجاري حول عقد توريد..."
17 }
18 // Additional fields omitted for brevity
19}1from transformers import AutoTokenizer, AutoModelForCausalLM
2import torch
3
4model_id = "ahmadsakor/Llama3.2-3B-Instruct-Legal-Summarization"
5
6# Set the device map based on GPU availability
7device_map = "cuda" if torch.cuda.is_available() else "cpu"
8dtype = torch.bfloat16 if torch.cuda.is_available() else torch.float32
9
10# Load model and tokenizer
11tokenizer = AutoTokenizer.from_pretrained(model_id)
12
13tokenizer.pad_token = tokenizer.eos_token # Use EOS token as padding token
14tokenizer.padding_side = 'left' # Left padding for batch alignment
15
16
17# Load the model with the appropriate dtype and device mapping
18model = AutoModelForCausalLM.from_pretrained(
19 model_id,
20 torch_dtype=dtype,
21 device_map=device_map
22)
23
24# Create the text generation pipeline
25pipe = pipeline(
26 "text-generation",
27 model=model,
28 tokenizer=tokenizer,
29 torch_dtype=dtype,
30 device_map=device_map
31)
32
33# System prompt for the AI assistant's task
34system_prompt = """
35You are a legal assistant AI that summarizes legal cases in JSON format following a specific template.
36Please ensure all outputs are structured and all keys are in English while the values are in Arabic.
37Be concise, informative, and follow the template strictly.
38"""
39
40# Template prompt for the legal text summary
41template_prompt="""
42###
43Legal Text Summary Template
441. Case Information
45
46Case Number: [Insert case number]
47Date of Ruling: [Insert date of ruling]
48Court: [Insert court name]
49Main Case Topic: [Mention the main topic of the case]
50Parties Involved: [Insert names of parties]
51
522. Persons involved including their:
53 [List the Persons in the text including their roles in a structured format (Name, Role)]
54
553. Background of the Case
56
57Overview: [Briefly describe the nature of the case and context]
58List of Relevant Dates with corresponding events in Arabic (Date, Event).
59
60
614. Key Issues
62
63[List the main legal issues or disputes in the case]
64
655. Arguments Presented
66
67Claimant’s Arguments:
68[Summarize the arguments made by the claimant]
69Defendant’s Arguments:
70[Summarize the arguments made by the defendant]
71
726. Court's Findings
73
74Evidence Reviewed: [Mention the evidence the court relied on]
75Rulings Made: [Summarize the rulings made by the court]
76Legal Principles Applied: [List any relevant legal principles or statutes cited]
77
787. Outcome
79
80Final Decision: [Describe the court's final decision]
81Implications: [Discuss any implications of the ruling]
82
838. Additional Notes
84
85[Any additional observations or relevant information that should be noted]
86#####
87Example of output json format:
88{
89 "case_information": {
90 "case_number": "",
91 "date_of_ruling": "",
92 "court": "",
93 "main_case_topic": "",
94 "parties_involved": ""
95 },
96 "persons_involved": [
97 {
98 "name": "",
99 "role": ""
100 }
101 ],
102 "background_of_the_case": {
103 "overview": "",
104 "relevant_dates": [
105 {
106 "date": "",
107 "event": ""
108 }
109 ]
110 },
111 "key_issues": [
112 ],
113 "arguments_presented": {
114 "claimants_arguments": "",
115 "defendants_arguments": ""
116 },
117 "courts_findings": {
118 "evidence_reviewed": "",
119 "rulings_made": "",
120 "legal_principles_applied": [
121 ]
122 },
123 "outcome": {
124 "final_decision": "",
125 "implications": ""
126 },
127 "additional_notes": {
128 "observations": ""
129 }
130}
131###
132Input:\n
133"""
134
135full_text = "قرار محكمة النقض رقم 1530 بتاريخ 17 نوفمبر 2022 في القضية الجنحية رقم 20201162213 استئناف - عدم أداء القسط الجزافي - أثره. لم يظهر من وثائق الملف ما يفيد أداء القسط الجزافي أثناء المرحلة الاستئنافية، فإن المحكمة لما رتبت على ذلك عدم قبول الاستئناف تكون قد طبقت القانون تطبيقاً سليماً وأن ما أثير بهذا الخصوص يبقى غير مؤسس. رفض الطلب باسم الطاعن وفق تصريح أفضى به بواسطة الأستاذ (ه.ب) بتاريخ 17. تم استئناف القضية المرفوعة أمام المحكمة الابتدائية بتازة والرامي إلى نقض القرار الصادر عن غرفة الاستئناف، في القضية ذات العدد 7 القاضي بإلغاء الحكم الابتدائي بشأن تعويض مدني قدره 4500 درهم بعد مؤاخذة المطلوبين في النقض (ع-ر١) و(م.1) من أجل جنحة انتزاع عقار بمنزاله الغير والحكم على كل واحد منهم بشهر واحد حبسا موقوف التنفيذ وغرامة نافذة قدرها 500 درهم. إن محكمة النقض، بعد أن تلا المستشار السيد المحفوظ سندالي التقرير المكلف به في القضية، وبعد الإنصات إلى المحامي العام السيد محمد جعبة في مستنتجاته، وبعد المداولة طبقاً للقانون. نظراً للمذكرة المدلى بها من لدن الطاعن بواسطة الأستاذ (ع.) المحامي بمدينة مكناس والمقبول للترافع أمام محكمة النقض والمستوفية للشروط الشكلية المتطلبة قانوناً. في شأن وسيلتي النقض مجتمعتين المستدل بها على النقض والمتخذتين في مجموعها من حرق مقتضيات الفقرة السابعة من المادة 365 من قانون المسطرة الجنائية ونقصان التعليل المنزلي معللاً، حيث انعدامه ذلك أن المحكمة مصدرة القرار المطعون فيه حالفت مقتضيات المادة أعلاه عندما لم تعمل على استدعاء الشاهد (ل.ط) المستمع إليه الوحيد ابتدائياً بعد صرف باقي الشهود الحاضرين من القاعة، والذين لم يتم الاستماع إليهم بدون توضيح السبب في ذلك. كما أن القرار موضوع النقض عندما قضى بعدم قبول استئناف العارض بعلة عدم أداء القسط الجزافي، فإنه استند على علة مخالفة للقانون بحيث إن العارض أدى القسط الجزافي أمامها. كما أن المحكمة باقتصارها على تبني النقاش الذي راج أمام محكمة البداية دون إتمامها بأي إجراء من إجراءات التحقيق للتأكد من صحة المعطيات المستقاة ابتدائياً والتي مكنت من بلوغ النتيجة التي وصلت إليها. علماً أن التقاضي هو على مستوى درجتين قانونيتين، كما أن المحكمة عندما اعتبرت تصريح الشاهد (ل.ط) بمثابة إقرار بمفهوم المخالفة على عدم ثبوت الحيازة علماً أنها لم تستمع إليه أمامها حتى تتمكن من بسط رقابتها على تصريحاته والبحث في باقي أوجه التصرف والاستغلال التي دفع بها العارض والتأكد والتدقيق في الحيازة المادية المطلوبة، وهو ما يجعلها مخالفة لمقتضيات الفصل 570 من القانون الجنائي، ما يكون معه القرار في مجمله على غير أساس وعرضة للنقض والإبطال. لكن حيث من جهة أولى فإنه ينفي في الملف ما يفيد أداء القسط الجنائي أثناء المرحلة الاستئنافية، فإن المحكمة لما رتبت على عدم قبول الاستئناف، تكون قد طبقت القانون تطبيقاً سليماً وأن ما أثير بهذا الخصوص يبقى غير مقبول. المحكمة النقض لصالحها برفض الطلب ورد مبلغ الضمانة لمودعه بعد استخلاص المصاريف القضائية. وبه صدر القرار وتلي في الجلسة العلنية المنعقدة بالتاريخ المذكور أعلاه بقاعة الجلسات العادية بمحكمة النقض الكائنة بشارع النخيل حي الرياض بالرباط، وكانت الهيئة الحاكمة متركبة من السادة: عبد الحكيم إدريسي قيطون رئيساً والمستشارين: المحفوظ سندالي مقرراً والمصطفى بارز ومحمد الغزاوي وفتيحة غزال، وبحضور المحامي العام السيد محمد جعبة الذي كان يمثل النيابة العامة وعاونه كاتبة الضبط السيدة سعاد عزيزي."
136user_part = template_prompt + '\n' + full_text
137prompt = (
138 f"<|start_header_id|>system<|end_header_id|>\n{system_prompt}\n"
139 f"<|start_header_id|>user<|end_header_id|>\n{user_part}\n"
140 f"<|start_header_id|>assistant<|end_header_id|>\n"
141)
142
143# Generate the summary using the pipeline
144generated_outputs = pipe(
145 prompt,
146 max_new_tokens=1500, # Limit the number of tokens in the output
147 num_return_sequences=1, # Return a single sequence
148 pad_token_id=pipe.tokenizer.eos_token_id,
149 padding=True,
150 return_full_text=False,
151)
152
153print (generated_outputs[0]["generated_text"])