File size: 7,942 Bytes
4db0522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import gradio as gr
import pandas as pd
from openai import AzureOpenAI
import faiss
import numpy as np
import json
import time
import re
import tiktoken
import os
from IPython.display import HTML

def arabic_print(text, colour="blue"):
    """
    Displays Arabic text with proper RTL and right alignment in Jupyter Notebook.
    
    Parameters:
    text (str): The Arabic text to display.
    colour (str): The color of the text. Default is "blue".
    """
    text=text.replace("\n","<br>")
    html_content = f"""
    <div style="direction: rtl; text-align: right; font-size: 16px; line-height: 1.5; color: {colour};">
        {text}
    </div>
    """
    return HTML(html_content)

# Example usage
my_arabic_text = """
باسم صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس دولة الإمارات العربية المتحدة / حاكم إمارة أبو ظبي
بالجلسة المنعقدة بـ محكمة ابوظبي العمالية-ابتدائي بتاريخ 2 جمادى الآخرة 1441 هـ الموافق 27/01/2020 م
برئاسة القاضي: إبراهيم ناصر الاحبابي وعضوية القاضي: مرتضى الصديق الحسن وعضوية القاضي: خليفة سليم
"""

# Display the text using the function
arabic_print(my_arabic_text, colour="green")

from openai import AzureOpenAI

AZURE_OPENAI_PREVIEW_API_VERSION = os.getenv("AZURE_OPENAI_PREVIEW_API_VERSION")
AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
AZURE_OPENAI_KEY = os.getenv("AZURE_OPENAI_KEY")

client = AzureOpenAI(
  azure_endpoint = AZURE_OPENAI_ENDPOINT, 
  api_key=AZURE_OPENAI_KEY,  
  api_version= AZURE_OPENAI_PREVIEW_API_VERSION
)

def call_gpt_azure_message(message_text):
    completion = client.chat.completions.create(
    #model="GPT4Turbo",
    model="gpt-4o",      
    messages = message_text,
    temperature=0.0,
    max_tokens=1000,
    top_p=0.95,
    frequency_penalty=0,
    presence_penalty=0,
    stop=None,
    )
    return completion.choices[0].message.content


def call_gpt_azure_message_stream(message_text):
    completion = client.chat.completions.create(
    #model="GPT4Turbo",
    model="gpt-4o",      
    messages = message_text,
    temperature=0.0,
    max_tokens=2000,
    top_p=0.95,
    frequency_penalty=0,
    presence_penalty=0,
    stop=None,
    stream=True
    )
    return completion


def call_gpt_azure(SYS_PROMPT,USER_PROMPT,MODEL="gpt-4o"):
    message_text=[
      {
        "role": "system",
        "content": SYS_PROMPT
      },
      {
        "role": "user",
        "content": USER_PROMPT
      },

    ]
      
    completion = client.chat.completions.create(
    model=MODEL,   
    messages = message_text,
    temperature=0.0,
    max_tokens=1000,
    top_p=0.95,
    frequency_penalty=0,
    presence_penalty=0,
    stop=None
    )
    return completion.choices[0].message.content



# This is the main embedding function , that takes as input text and generate 1536 floats using ada3_small
def generate_embeddings(text, model="ada3_small"): # model = "deployment_name"
    return client.embeddings.create(input = [text], model=model).data[0].embedding    


import tiktoken
enc = tiktoken.get_encoding("o200k_base")
assert enc.decode(enc.encode("hello world")) == "hello world"

# To get the tokeniser corresponding to a specific model in the OpenAI API:
enc = tiktoken.encoding_for_model("gpt-4o")

import tiktoken

def count_tokens_ada3(text, model_name="text-embedding-3-small"):
    # Automatically get the correct encoding for the given model
    encoding = tiktoken.encoding_for_model(model_name)
    # Encode the text and count the tokens
    return len(encoding.encode(text))


def Get_nearest_cases_Json(case,K):
    vquery=np.array(generate_embeddings(case))
    vquery=vquery.reshape(1,-1)
    
    D, I = index_law.search(vquery, K)  # search
    cxt_cases=""
    cxt_list=[]
    Locs=I[0]
    for L in Locs:
        cxt_cases+=str(json_cases[L])
        
        
    return cxt_cases
    


def count_tokens(text):
    return len(enc.encode(text))

vec_embs=np.load("data/All_cases_embedded.npy")

index_law=faiss.IndexFlatIP(vec_embs.shape[1])
index_law.add(vec_embs)

#case=Emb_text_list[10]



# File path
output_file = 'data/KSA_Legal_cases.json'

# Read the JSON file
with open(output_file, 'r', encoding='utf-8') as file:
    json_cases = json.load(file)



def GPT_AI_Judge_Json(case , cxt):
    SYS_PROMPT="""
    

**System Role**:  
You are an **Arabic Legal Judge Assistant**, specialized in analyzing legal cases and extracting insights from related legal precedences.


### **Input Details**:  
You will be given:
1. A **legal case** (primary input).  
2. A **context** containing multiple legal precedences in json format.



### **Your Tasks**:  
1. **Analyze the Input Case**:  
   - Focus on the **description of the case** (توصيف القضية) and its key aspects.  

2. **Identify Relevant legal Precedences**:  
   - Search the provided context for precedences only **closely related** to the input case.  

3. **Create a Comparative Analysis**:  
   - Present a **contrastive table** comparing the relevant precedences with columns containing metadata in context
4. **Discussion of Key Points**:  
   - Highlight **commonalities and differences** between the input case and the relevant precedences.  

5. **Suggest a Ruling Decision**:  
   - Provide a **recommendation** for the Judge, based on the rulings of the similar precedences.  

---




### **If No Relevant Precedences**:  
- Clearly state that no related precedences were identified from the context.  
- Apologize and note that a ruling recommendation cannot be provided.  

---

### **Response Format**:  
1. **Comparative Table**:  
   - compare  relevant precedences in a table.  

2. **RTL Formatting**:  
   - Use  **right-to-left (RTL)** direction and **right alignment**.  
   - Ensure all headers, lists, and paragraphs include `dir="rtl"` and `text-align: right`.  

3. **Clear Structure**:  
   - Provide a well-organized response for proper Arabic rendering.

---

"""


    
    User_Prompt=f"Input Legal Case {case} \n Legal precedences context : {cxt}"
    message_text=[
      {
        "role": "system",
        "content": SYS_PROMPT
      },
      {
        "role": "user",
        "content": User_Prompt
      },

    ]
    completion = client.chat.completions.create(
    #model="gpt-35-turbo-16k",  
    model="gpt-4o",   
    messages = message_text,
    temperature=0.0,
    max_tokens=3500,
    top_p=0.95,
    frequency_penalty=0,
    presence_penalty=0,
    stop=None
    )
    return completion.choices[0].message.content


import gradio as gr

# Define the processing function
def gpt_judge(case,history):
    cxt=Get_nearest_cases_Json(case,5)
    print("Tokens ==>",count_tokens(cxt))
    # Example: Generate a markdown response
    response = GPT_AI_Judge_Json(case,cxt)
    # Save the response to a Markdown file
    # Convert Markdown to HTML
    return response
welcome_message="اذكر احداث ووقائع وملابسات القضية وسأقوم بتحليلها و اقتراح الحكم بناءا عى سوابق قضائية مشابهة "

chatbot=gr.Chatbot(value=[(None,welcome_message)],height=800,rtl=True)

tit_html='\n<div style="text-align: right;">\n<p>اذكر الوقائع الخاصة بالقضية وتوصيفها للحصول على الاستشارة القانونية المناسبة.</p>\n</div>\n'
tit_img = """
<div style="text-align: right;">
<img src="https://i.postimg.cc/rytvLcdm/ksa-leg.png" alt="Stars Logo" width="200" height="200">
</div>
"""
with gr.Blocks() as demo:
    
    
    gr.ChatInterface(
        gpt_judge,
        chatbot=chatbot,
        title=tit_img,
        description=tit_html,
        theme="soft",
    )
    #btn = gr.Button("توعية قانونية")
    #btn.click(fn=greet)
    

demo.launch()