E-slam commited on
Commit
4db0522
·
verified ·
1 Parent(s): 5206cc1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +289 -289
app.py CHANGED
@@ -1,289 +1,289 @@
1
- import gradio as gr
2
- import pandas as pd
3
- from openai import AzureOpenAI
4
- import faiss
5
- import numpy as np
6
- import json
7
- import time
8
- import re
9
- import tiktoken
10
- import os
11
- from IPython.display import HTML
12
-
13
- def arabic_print(text, colour="blue"):
14
- """
15
- Displays Arabic text with proper RTL and right alignment in Jupyter Notebook.
16
-
17
- Parameters:
18
- text (str): The Arabic text to display.
19
- colour (str): The color of the text. Default is "blue".
20
- """
21
- text=text.replace("\n","<br>")
22
- html_content = f"""
23
- <div style="direction: rtl; text-align: right; font-size: 16px; line-height: 1.5; color: {colour};">
24
- {text}
25
- </div>
26
- """
27
- return HTML(html_content)
28
-
29
- # Example usage
30
- my_arabic_text = """
31
- باسم صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس دولة الإمارات العربية المتحدة / حاكم إمارة أبو ظبي
32
- بالجلسة المنعقدة بـ محكمة ابوظبي العمالية-ابتدائي بتاريخ 2 جمادى الآخرة 1441 هـ الموافق 27/01/2020 م
33
- برئاسة القاضي: إبراهيم ناصر الاحبابي وعضوية القاضي: مرتضى الصديق الحسن وعضوية القاضي: خليفة سليم
34
- """
35
-
36
- # Display the text using the function
37
- arabic_print(my_arabic_text, colour="green")
38
-
39
- from openai import AzureOpenAI
40
-
41
- AZURE_OPENAI_PREVIEW_API_VERSION = os.getenv("AZURE_OPENAI_PREVIEW_API_VERSION")
42
- AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
43
- AZURE_OPENAI_KEY = os.getenv("AZURE_OPENAI_KEY")
44
-
45
- client = AzureOpenAI(
46
- azure_endpoint = AZURE_OPENAI_ENDPOINT,
47
- api_key=AZURE_OPENAI_KEY,
48
- api_version= AZURE_OPENAI_PREVIEW_API_VERSION
49
- )
50
-
51
- def call_gpt_azure_message(message_text):
52
- completion = client.chat.completions.create(
53
- #model="GPT4Turbo",
54
- model="gpt-4o",
55
- messages = message_text,
56
- temperature=0.0,
57
- max_tokens=1000,
58
- top_p=0.95,
59
- frequency_penalty=0,
60
- presence_penalty=0,
61
- stop=None,
62
- )
63
- return completion.choices[0].message.content
64
-
65
-
66
- def call_gpt_azure_message_stream(message_text):
67
- completion = client.chat.completions.create(
68
- #model="GPT4Turbo",
69
- model="gpt-4o",
70
- messages = message_text,
71
- temperature=0.0,
72
- max_tokens=2000,
73
- top_p=0.95,
74
- frequency_penalty=0,
75
- presence_penalty=0,
76
- stop=None,
77
- stream=True
78
- )
79
- return completion
80
-
81
-
82
- def call_gpt_azure(SYS_PROMPT,USER_PROMPT,MODEL="gpt-4o"):
83
- message_text=[
84
- {
85
- "role": "system",
86
- "content": SYS_PROMPT
87
- },
88
- {
89
- "role": "user",
90
- "content": USER_PROMPT
91
- },
92
-
93
- ]
94
-
95
- completion = client.chat.completions.create(
96
- model=MODEL,
97
- messages = message_text,
98
- temperature=0.0,
99
- max_tokens=1000,
100
- top_p=0.95,
101
- frequency_penalty=0,
102
- presence_penalty=0,
103
- stop=None
104
- )
105
- return completion.choices[0].message.content
106
-
107
-
108
-
109
- # This is the main embedding function , that takes as input text and generate 1536 floats using ada3_small
110
- def generate_embeddings(text, model="ada3_small"): # model = "deployment_name"
111
- return client.embeddings.create(input = [text], model=model).data[0].embedding
112
-
113
-
114
- import tiktoken
115
- enc = tiktoken.get_encoding("o200k_base")
116
- assert enc.decode(enc.encode("hello world")) == "hello world"
117
-
118
- # To get the tokeniser corresponding to a specific model in the OpenAI API:
119
- enc = tiktoken.encoding_for_model("gpt-4o")
120
-
121
- import tiktoken
122
-
123
- def count_tokens_ada3(text, model_name="text-embedding-3-small"):
124
- # Automatically get the correct encoding for the given model
125
- encoding = tiktoken.encoding_for_model(model_name)
126
- # Encode the text and count the tokens
127
- return len(encoding.encode(text))
128
-
129
-
130
- def Get_nearest_cases_Json(case,K):
131
- vquery=np.array(generate_embeddings(case))
132
- vquery=vquery.reshape(1,-1)
133
-
134
- D, I = index_law.search(vquery, K) # search
135
- cxt_cases=""
136
- cxt_list=[]
137
- Locs=I[0]
138
- for L in Locs:
139
- cxt_cases+=str(json_cases[L])
140
-
141
-
142
- return cxt_cases
143
-
144
-
145
-
146
- def count_tokens(text):
147
- return len(enc.encode(text))
148
-
149
- vec_embs=np.load("Data/All_cases_embedded.npy")
150
-
151
- index_law=faiss.IndexFlatIP(vec_embs.shape[1])
152
- index_law.add(vec_embs)
153
-
154
- #case=Emb_text_list[10]
155
-
156
-
157
-
158
- # File path
159
- output_file = 'Data/KSA_Legal_cases.json'
160
-
161
- # Read the JSON file
162
- with open(output_file, 'r', encoding='utf-8') as file:
163
- json_cases = json.load(file)
164
-
165
-
166
-
167
- def GPT_AI_Judge_Json(case , cxt):
168
- SYS_PROMPT="""
169
-
170
-
171
- **System Role**:
172
- You are an **Arabic Legal Judge Assistant**, specialized in analyzing legal cases and extracting insights from related legal precedences.
173
-
174
-
175
- ### **Input Details**:
176
- You will be given:
177
- 1. A **legal case** (primary input).
178
- 2. A **context** containing multiple legal precedences in json format.
179
-
180
-
181
-
182
- ### **Your Tasks**:
183
- 1. **Analyze the Input Case**:
184
- - Focus on the **description of the case** (توصيف القضية) and its key aspects.
185
-
186
- 2. **Identify Relevant legal Precedences**:
187
- - Search the provided context for precedences only **closely related** to the input case.
188
-
189
- 3. **Create a Comparative Analysis**:
190
- - Present a **contrastive table** comparing the relevant precedences with columns containing metadata in context
191
- 4. **Discussion of Key Points**:
192
- - Highlight **commonalities and differences** between the input case and the relevant precedences.
193
-
194
- 5. **Suggest a Ruling Decision**:
195
- - Provide a **recommendation** for the Judge, based on the rulings of the similar precedences.
196
-
197
- ---
198
-
199
-
200
-
201
-
202
- ### **If No Relevant Precedences**:
203
- - Clearly state that no related precedences were identified from the context.
204
- - Apologize and note that a ruling recommendation cannot be provided.
205
-
206
- ---
207
-
208
- ### **Response Format**:
209
- 1. **Comparative Table**:
210
- - compare relevant precedences in a table.
211
-
212
- 2. **RTL Formatting**:
213
- - Use **right-to-left (RTL)** direction and **right alignment**.
214
- - Ensure all headers, lists, and paragraphs include `dir="rtl"` and `text-align: right`.
215
-
216
- 3. **Clear Structure**:
217
- - Provide a well-organized response for proper Arabic rendering.
218
-
219
- ---
220
-
221
- """
222
-
223
-
224
-
225
- User_Prompt=f"Input Legal Case {case} \n Legal precedences context : {cxt}"
226
- message_text=[
227
- {
228
- "role": "system",
229
- "content": SYS_PROMPT
230
- },
231
- {
232
- "role": "user",
233
- "content": User_Prompt
234
- },
235
-
236
- ]
237
- completion = client.chat.completions.create(
238
- #model="gpt-35-turbo-16k",
239
- model="gpt-4o",
240
- messages = message_text,
241
- temperature=0.0,
242
- max_tokens=3500,
243
- top_p=0.95,
244
- frequency_penalty=0,
245
- presence_penalty=0,
246
- stop=None
247
- )
248
- return completion.choices[0].message.content
249
-
250
-
251
- import gradio as gr
252
-
253
- # Define the processing function
254
- def gpt_judge(case,history):
255
- cxt=Get_nearest_cases_Json(case,5)
256
- print("Tokens ==>",count_tokens(cxt))
257
- # Example: Generate a markdown response
258
- response = GPT_AI_Judge_Json(case,cxt)
259
- # Save the response to a Markdown file
260
- # Convert Markdown to HTML
261
- return response
262
- welcome_message="اذكر احداث ووقائع وملابسات القضية وسأقوم بتحليلها و اقتراح الحكم بناءا عى سوابق قضائية مشابهة "
263
-
264
- chatbot=gr.Chatbot(value=[(None,welcome_message)],height=800,rtl=True)
265
-
266
- tit_html='\n<div style="text-align: right;">\n<p>اذكر الوقائع الخاصة بالقضية وتوصيفها للحصول على الاستشارة القانونية المناسبة.</p>\n</div>\n'
267
- tit_img = """
268
- <div style="text-align: right;">
269
- <img src="https://i.postimg.cc/rytvLcdm/ksa-leg.png" alt="Stars Logo" width="200" height="200">
270
- </div>
271
- """
272
- with gr.Blocks() as demo:
273
-
274
-
275
- gr.ChatInterface(
276
- gpt_judge,
277
- chatbot=chatbot,
278
- title=tit_img,
279
- description=tit_html,
280
- theme="soft",
281
- retry_btn=None,
282
- undo_btn="Delete Previous",
283
- clear_btn="Clear"
284
- )
285
- #btn = gr.Button("توعية قانونية")
286
- #btn.click(fn=greet)
287
-
288
-
289
- demo.launch()
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ from openai import AzureOpenAI
4
+ import faiss
5
+ import numpy as np
6
+ import json
7
+ import time
8
+ import re
9
+ import tiktoken
10
+ import os
11
+ from IPython.display import HTML
12
+
13
+ def arabic_print(text, colour="blue"):
14
+ """
15
+ Displays Arabic text with proper RTL and right alignment in Jupyter Notebook.
16
+
17
+ Parameters:
18
+ text (str): The Arabic text to display.
19
+ colour (str): The color of the text. Default is "blue".
20
+ """
21
+ text=text.replace("\n","<br>")
22
+ html_content = f"""
23
+ <div style="direction: rtl; text-align: right; font-size: 16px; line-height: 1.5; color: {colour};">
24
+ {text}
25
+ </div>
26
+ """
27
+ return HTML(html_content)
28
+
29
+ # Example usage
30
+ my_arabic_text = """
31
+ باسم صاحب السمو الشيخ خليفة بن زايد آل نهيان رئيس دولة الإمارات العربية المتحدة / حاكم إمارة أبو ظبي
32
+ بالجلسة المنعقدة بـ محكمة ابوظبي العمالية-ابتدائي بتاريخ 2 جمادى الآخرة 1441 هـ الموافق 27/01/2020 م
33
+ برئاسة القاضي: إبراهيم ناصر الاحبابي وعضوية القاضي: مرتضى الصديق الحسن وعضوية القاضي: خليفة سليم
34
+ """
35
+
36
+ # Display the text using the function
37
+ arabic_print(my_arabic_text, colour="green")
38
+
39
+ from openai import AzureOpenAI
40
+
41
+ AZURE_OPENAI_PREVIEW_API_VERSION = os.getenv("AZURE_OPENAI_PREVIEW_API_VERSION")
42
+ AZURE_OPENAI_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
43
+ AZURE_OPENAI_KEY = os.getenv("AZURE_OPENAI_KEY")
44
+
45
+ client = AzureOpenAI(
46
+ azure_endpoint = AZURE_OPENAI_ENDPOINT,
47
+ api_key=AZURE_OPENAI_KEY,
48
+ api_version= AZURE_OPENAI_PREVIEW_API_VERSION
49
+ )
50
+
51
+ def call_gpt_azure_message(message_text):
52
+ completion = client.chat.completions.create(
53
+ #model="GPT4Turbo",
54
+ model="gpt-4o",
55
+ messages = message_text,
56
+ temperature=0.0,
57
+ max_tokens=1000,
58
+ top_p=0.95,
59
+ frequency_penalty=0,
60
+ presence_penalty=0,
61
+ stop=None,
62
+ )
63
+ return completion.choices[0].message.content
64
+
65
+
66
+ def call_gpt_azure_message_stream(message_text):
67
+ completion = client.chat.completions.create(
68
+ #model="GPT4Turbo",
69
+ model="gpt-4o",
70
+ messages = message_text,
71
+ temperature=0.0,
72
+ max_tokens=2000,
73
+ top_p=0.95,
74
+ frequency_penalty=0,
75
+ presence_penalty=0,
76
+ stop=None,
77
+ stream=True
78
+ )
79
+ return completion
80
+
81
+
82
+ def call_gpt_azure(SYS_PROMPT,USER_PROMPT,MODEL="gpt-4o"):
83
+ message_text=[
84
+ {
85
+ "role": "system",
86
+ "content": SYS_PROMPT
87
+ },
88
+ {
89
+ "role": "user",
90
+ "content": USER_PROMPT
91
+ },
92
+
93
+ ]
94
+
95
+ completion = client.chat.completions.create(
96
+ model=MODEL,
97
+ messages = message_text,
98
+ temperature=0.0,
99
+ max_tokens=1000,
100
+ top_p=0.95,
101
+ frequency_penalty=0,
102
+ presence_penalty=0,
103
+ stop=None
104
+ )
105
+ return completion.choices[0].message.content
106
+
107
+
108
+
109
+ # This is the main embedding function , that takes as input text and generate 1536 floats using ada3_small
110
+ def generate_embeddings(text, model="ada3_small"): # model = "deployment_name"
111
+ return client.embeddings.create(input = [text], model=model).data[0].embedding
112
+
113
+
114
+ import tiktoken
115
+ enc = tiktoken.get_encoding("o200k_base")
116
+ assert enc.decode(enc.encode("hello world")) == "hello world"
117
+
118
+ # To get the tokeniser corresponding to a specific model in the OpenAI API:
119
+ enc = tiktoken.encoding_for_model("gpt-4o")
120
+
121
+ import tiktoken
122
+
123
+ def count_tokens_ada3(text, model_name="text-embedding-3-small"):
124
+ # Automatically get the correct encoding for the given model
125
+ encoding = tiktoken.encoding_for_model(model_name)
126
+ # Encode the text and count the tokens
127
+ return len(encoding.encode(text))
128
+
129
+
130
+ def Get_nearest_cases_Json(case,K):
131
+ vquery=np.array(generate_embeddings(case))
132
+ vquery=vquery.reshape(1,-1)
133
+
134
+ D, I = index_law.search(vquery, K) # search
135
+ cxt_cases=""
136
+ cxt_list=[]
137
+ Locs=I[0]
138
+ for L in Locs:
139
+ cxt_cases+=str(json_cases[L])
140
+
141
+
142
+ return cxt_cases
143
+
144
+
145
+
146
+ def count_tokens(text):
147
+ return len(enc.encode(text))
148
+
149
+ vec_embs=np.load("data/All_cases_embedded.npy")
150
+
151
+ index_law=faiss.IndexFlatIP(vec_embs.shape[1])
152
+ index_law.add(vec_embs)
153
+
154
+ #case=Emb_text_list[10]
155
+
156
+
157
+
158
+ # File path
159
+ output_file = 'data/KSA_Legal_cases.json'
160
+
161
+ # Read the JSON file
162
+ with open(output_file, 'r', encoding='utf-8') as file:
163
+ json_cases = json.load(file)
164
+
165
+
166
+
167
+ def GPT_AI_Judge_Json(case , cxt):
168
+ SYS_PROMPT="""
169
+
170
+
171
+ **System Role**:
172
+ You are an **Arabic Legal Judge Assistant**, specialized in analyzing legal cases and extracting insights from related legal precedences.
173
+
174
+
175
+ ### **Input Details**:
176
+ You will be given:
177
+ 1. A **legal case** (primary input).
178
+ 2. A **context** containing multiple legal precedences in json format.
179
+
180
+
181
+
182
+ ### **Your Tasks**:
183
+ 1. **Analyze the Input Case**:
184
+ - Focus on the **description of the case** (توصيف القضية) and its key aspects.
185
+
186
+ 2. **Identify Relevant legal Precedences**:
187
+ - Search the provided context for precedences only **closely related** to the input case.
188
+
189
+ 3. **Create a Comparative Analysis**:
190
+ - Present a **contrastive table** comparing the relevant precedences with columns containing metadata in context
191
+ 4. **Discussion of Key Points**:
192
+ - Highlight **commonalities and differences** between the input case and the relevant precedences.
193
+
194
+ 5. **Suggest a Ruling Decision**:
195
+ - Provide a **recommendation** for the Judge, based on the rulings of the similar precedences.
196
+
197
+ ---
198
+
199
+
200
+
201
+
202
+ ### **If No Relevant Precedences**:
203
+ - Clearly state that no related precedences were identified from the context.
204
+ - Apologize and note that a ruling recommendation cannot be provided.
205
+
206
+ ---
207
+
208
+ ### **Response Format**:
209
+ 1. **Comparative Table**:
210
+ - compare relevant precedences in a table.
211
+
212
+ 2. **RTL Formatting**:
213
+ - Use **right-to-left (RTL)** direction and **right alignment**.
214
+ - Ensure all headers, lists, and paragraphs include `dir="rtl"` and `text-align: right`.
215
+
216
+ 3. **Clear Structure**:
217
+ - Provide a well-organized response for proper Arabic rendering.
218
+
219
+ ---
220
+
221
+ """
222
+
223
+
224
+
225
+ User_Prompt=f"Input Legal Case {case} \n Legal precedences context : {cxt}"
226
+ message_text=[
227
+ {
228
+ "role": "system",
229
+ "content": SYS_PROMPT
230
+ },
231
+ {
232
+ "role": "user",
233
+ "content": User_Prompt
234
+ },
235
+
236
+ ]
237
+ completion = client.chat.completions.create(
238
+ #model="gpt-35-turbo-16k",
239
+ model="gpt-4o",
240
+ messages = message_text,
241
+ temperature=0.0,
242
+ max_tokens=3500,
243
+ top_p=0.95,
244
+ frequency_penalty=0,
245
+ presence_penalty=0,
246
+ stop=None
247
+ )
248
+ return completion.choices[0].message.content
249
+
250
+
251
+ import gradio as gr
252
+
253
+ # Define the processing function
254
+ def gpt_judge(case,history):
255
+ cxt=Get_nearest_cases_Json(case,5)
256
+ print("Tokens ==>",count_tokens(cxt))
257
+ # Example: Generate a markdown response
258
+ response = GPT_AI_Judge_Json(case,cxt)
259
+ # Save the response to a Markdown file
260
+ # Convert Markdown to HTML
261
+ return response
262
+ welcome_message="اذكر احداث ووقائع وملابسات القضية وسأقوم بتحليلها و اقتراح الحكم بناءا عى سوابق قضائية مشابهة "
263
+
264
+ chatbot=gr.Chatbot(value=[(None,welcome_message)],height=800,rtl=True)
265
+
266
+ tit_html='\n<div style="text-align: right;">\n<p>اذكر الوقائع الخاصة بالقضية وتوصيفها للحصول على الاستشارة القانونية المناسبة.</p>\n</div>\n'
267
+ tit_img = """
268
+ <div style="text-align: right;">
269
+ <img src="https://i.postimg.cc/rytvLcdm/ksa-leg.png" alt="Stars Logo" width="200" height="200">
270
+ </div>
271
+ """
272
+ with gr.Blocks() as demo:
273
+
274
+
275
+ gr.ChatInterface(
276
+ gpt_judge,
277
+ chatbot=chatbot,
278
+ title=tit_img,
279
+ description=tit_html,
280
+ theme="soft",
281
+ retry_btn=None,
282
+ undo_btn="Delete Previous",
283
+ clear_btn="Clear"
284
+ )
285
+ #btn = gr.Button("توعية قانونية")
286
+ #btn.click(fn=greet)
287
+
288
+
289
+ demo.launch()