michaelmc1618 commited on
Commit
4f4bbd6
·
verified ·
1 Parent(s): 4194261

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -226
app.py CHANGED
@@ -12,182 +12,37 @@ from datasets import load_dataset
12
  import time
13
  import fitz # PyMuPDF
14
 
15
- dir_ = Path(__file__).parent
16
-
17
  dataset = load_dataset("ibunescu/qa_legal_dataset_train")
18
 
19
- # Load the BERT model and tokenizer
20
- tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
21
- model = AutoModelForMaskedLM.from_pretrained("google-bert/bert-base-uncased")
22
-
23
- # Create the fill-mask pipeline
24
- pipe = pipeline("fill-mask", model=model, tokenizer=tokenizer)
25
-
26
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
27
-
28
- def respond(
29
- message,
30
- history: list[tuple[str, str]],
31
- system_message,
32
- max_tokens,
33
- temperature,
34
- top_p,
35
- ):
36
- messages = [{"role": "system", "content": system_message}]
37
-
38
- for val in history:
39
- if val[0]:
40
- messages.append({"role": "user", "content": val[0]})
41
- if val[1]:
42
- messages.append({"role": "assistant", "content": val[1]})
43
-
44
- messages.append({"role": "user", "content": message})
45
-
46
- try:
47
- response = ""
48
- for message in client.chat_completion(
49
- messages,
50
- max_tokens=max_tokens,
51
- stream=True,
52
- temperature=temperature,
53
- top_p=top_p,
54
- ):
55
- token = message.choices[0].delta.content
56
- if token is not None:
57
- response += token
58
- yield response, history + [(message, response)]
59
- except Exception as e:
60
- print(f"Error during chat completion: {e}")
61
- yield "An error occurred during the chat completion.", history
62
-
63
- def generate_case_outcome(prosecutor_response, defense_response):
64
- prompt = f"Prosecutor's arguments: {prosecutor_response}\n\nDefense's arguments: {defense_response}\n\nProvide details on who won the case and why. Provide reasons for your decision and provide a link to the source of the case."
65
- evaluation = ""
66
- try:
67
- for message in client.chat_completion(
68
- [{"role": "system", "content": "You are a legal expert evaluating the details of the case presented by the prosecution and the defense."},
69
- {"role": "user", "content": prompt}],
70
- max_tokens=512,
71
- stream=True,
72
- temperature=0.6,
73
- top_p=0.95,
74
- ):
75
- token = message.choices[0].delta.content
76
- if token is not None:
77
- evaluation += token
78
- except Exception as e:
79
- print(f"Error during case outcome generation: {e}")
80
- return "An error occurred during the case outcome generation."
81
  return evaluation
82
 
83
- def determine_outcome(outcome):
84
- prosecutor_count = outcome.split().count("Prosecutor")
85
- defense_count = outcome.split().count("Defense")
86
- if prosecutor_count > defense_count:
87
- return "Prosecutor Wins"
88
- elif defense_count > prosecutor_count:
89
- return "Defense Wins"
90
- else:
91
- return "No clear winner"
92
 
93
- # Custom CSS for white background and black text for input and output boxes
94
- custom_css = """
95
- body {
96
- background-color: #ffffff;
97
- color: #000000;
98
- font-family: Arial, sans-serif;
99
- }
100
- .gradio-container {
101
- max-width: 1000px;
102
- margin: 0 auto;
103
- padding: 20px;
104
- background-color: #ffffff;
105
- border: 1px solid #e0e0e0;
106
- border-radius: 8px;
107
- box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
108
- }
109
- .gr-button {
110
- background-color: #ffffff !important;
111
- border-color: #ffffff !important;
112
- color: #000000 !important;
113
- margin: 5px;
114
- }
115
- .gr-button:hover {
116
- background-color: #ffffff !important;
117
- border-color: #004085 !important;
118
- }
119
- .gr-input, .gr-textbox, .gr-slider, .gr-markdown, .gr-chatbox {
120
- border-radius: 4px;
121
- border: 1px solid #ced4da;
122
- background-color: #ffffff !important;
123
- color: #000000 !important;
124
- }
125
- .gr-input:focus, .gr-textbox:focus, .gr-slider:focus {
126
- border-color: #ffffff;
127
- outline: 0;
128
- box-shadow: 0 0 0 0.2rem rgba(255, 255, 255, 1.0);
129
- }
130
- #flagging-button {
131
- display: none;
132
- }
133
- footer {
134
- display: none;
135
- }
136
- .chatbox .chat-container .chat-message {
137
- background-color: #ffffff !important;
138
- color: #000000 !important;
139
- }
140
- .chatbox .chat-container .chat-message-input {
141
- background-color: #ffffff !important;
142
- color: #000000 !important;
143
- }
144
- .gr-markdown {
145
- background-color: #ffffff !important;
146
- color: #000000 !important;
147
- }
148
- .gr-markdown h1, .gr-markdown h2, .gr-markdown h3, .gr-markdown h4, .gr-markdown h5, .gr-markdown h6, .gr-markdown p, .gr-markdown ul, .gr-markdown ol, .gr-markdown li {
149
- color: #000000 !important;
150
- }
151
- .score-box {
152
- width: 60px;
153
- height: 60px;
154
- display: flex;
155
- align-items: center;
156
- justify-content: center;
157
- font-size: 12px;
158
- font-weight: bold;
159
- color: black;
160
- margin: 5px;
161
- }
162
- .scroll-box {
163
- max-height: 200px;
164
- overflow-y: scroll;
165
- border: 1px solid #ced4da;
166
- padding: 10px;
167
- border-radius: 4px;
168
  }
169
  """
170
 
 
171
  def chat_between_bots(system_message1, system_message2, max_tokens, temperature, top_p, history1, history2, shared_history, message):
172
  response1, history1 = list(respond(message, history1, system_message1, max_tokens, temperature, top_p))[-1]
173
  response2, history2 = list(respond(message, history2, system_message2, max_tokens, temperature, top_p))[-1]
174
- shared_history.append(f"Prosecutor: {response1}")
175
- shared_history.append(f"Defense Attorney: {response2}")
176
-
177
- max_length = max(len(response1), len(response2))
178
- response1 = response1[:max_length]
179
- response2 = response2[:max_length]
180
 
181
- outcome = generate_case_outcome(response1, response2)
182
- winner = determine_outcome(outcome)
183
-
184
- return response1, response2, history1, history2, shared_history, outcome
 
 
 
 
185
 
186
- def get_top_5_cases():
187
- prompt = "List 5 random high-profile legal cases that have received significant media attention and are currently ongoing. Just a list of case names and numbers."
188
  response = ""
189
  for message in client.chat_completion(
190
- [{"role": "system", "content": "You are a legal research expert, able to provide information about high-profile legal cases."},
191
  {"role": "user", "content": prompt}],
192
  max_tokens=512,
193
  stream=True,
@@ -199,86 +54,79 @@ def get_top_5_cases():
199
  response += token
200
  return response
201
 
 
 
 
 
 
 
 
202
  def add_message(history, message):
203
  for x in message["files"]:
204
- history.append(((x,), None))
205
- if message["text"] is not None:
206
- history.append((message["text"], None))
207
- return history, gr.MultimodalTextbox(value=None, interactive=True)
208
 
209
- def print_like_dislike(x: gr.LikeData):
210
- print(x.index, x.value, x.liked)
211
-
212
- def reset_conversation():
213
- return [], [], "", "", ""
214
-
215
- def save_conversation(history1, history2, shared_history):
216
- return history1, history2, shared_history
217
-
218
- def ask_about_case_outcome(shared_history, question):
219
- prompt = f"Case Outcome: {shared_history}\n\nQuestion: {question}\n\nAnswer:"
220
  response = ""
221
  for message in client.chat_completion(
222
- [{"role": "system", "content": "Give the real case details of the case being argued, from a verifed source."},
223
- {"role": "user", "content": prompt}],
224
- max_tokens=512,
225
  stream=True,
226
- temperature=0.7,
227
  top_p=0.95,
228
  ):
229
  token = message.choices[0].delta.content
230
  if token is not None:
231
  response += token
232
- return response
 
 
 
 
 
 
 
 
 
 
233
 
234
  with gr.Blocks(css=custom_css) as demo:
235
  history1 = gr.State([])
236
  history2 = gr.State([])
237
  shared_history = gr.State([])
238
- top_10_cases = gr.State("")
239
-
 
240
  with gr.Tab("Argument Evaluation"):
241
- with gr.Row():
242
- with gr.Column(scale=1):
243
- top_5_btn = gr.Button("Give me the top 5 cases")
244
- top_5_output = gr.Textbox(label="Top 5 Cases", interactive=False, elem_classes=["scroll-box"])
245
- top_5_btn.click(get_top_5_cases, outputs=top_5_output)
246
- with gr.Column(scale=2):
247
- message = gr.Textbox(label="Case to Argue")
248
- system_message1 = gr.State("You are an expert Prosecutor. Give your best arguments for the case on behalf of the prosecution.")
249
- system_message2 = gr.State("You are an expert Defense Attorney. Give your best arguments for the case on behalf of the Defense.")
250
- max_tokens = gr.State(512)
251
- temperature = gr.State(0.5)
252
- top_p = gr.State(0.95)
253
-
254
- with gr.Row():
255
- with gr.Column(scale=4):
256
- prosecutor_response = gr.Textbox(label="Prosecutor's Response", interactive=True, elem_classes=["scroll-box"])
257
- with gr.Column(scale=1):
258
- prosecutor_score_color = gr.HTML()
259
-
260
- with gr.Column(scale=4):
261
- defense_response = gr.Textbox(label="Defense Attorney's Response", interactive=True, elem_classes=["scroll-box"])
262
- with gr.Column(scale=1):
263
- defense_score_color = gr.HTML()
264
-
265
- outcome = gr.Textbox(label="Outcome", interactive=True, elem_classes=["scroll-box"])
266
-
267
- with gr.Row():
268
- submit_btn = gr.Button("Argue")
269
- clear_btn = gr.Button("Clear and Reset")
270
- save_btn = gr.Button("Save Conversation")
271
-
272
- submit_btn.click(chat_between_bots, inputs=[system_message1, system_message2, max_tokens, temperature, top_p, history1, history2, shared_history, message], outputs=[prosecutor_response, defense_response, history1, history2, shared_history, outcome])
273
- clear_btn.click(reset_conversation, outputs=[history1, history2, shared_history, prosecutor_response, defense_response, outcome])
274
- save_btn.click(save_conversation, inputs=[history1, history2, shared_history], outputs=[history1, history2, shared_history])
275
 
276
- with gr.Tab("Case Outcome Chat"):
277
- case_question = gr.Textbox(label="Ask a Question about the Case Outcome")
278
- case_answer = gr.Textbox(label="Answer", interactive=False, elem_classes=["scroll-box"])
279
- ask_case_btn = gr.Button("Ask")
280
-
281
- ask_case_btn.click(ask_about_case_outcome, inputs=[shared_history, case_question], outputs=case_answer)
282
-
283
- demo.queue()
284
- demo.launch()
 
 
 
 
 
 
 
12
  import time
13
  import fitz # PyMuPDF
14
 
 
 
15
  dataset = load_dataset("ibunescu/qa_legal_dataset_train")
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  return evaluation
18
 
19
+ def score_argument_from_outcome(outcome, argument):
 
 
 
 
 
 
 
 
20
 
21
+ if "Prosecutor" in outcome:
22
+ prosecutor_score = outcome.count("Prosecutor") * 2
23
+ if "won" in outcome and "Prosecutor" in outcome:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  }
25
  """
26
 
27
+
28
  def chat_between_bots(system_message1, system_message2, max_tokens, temperature, top_p, history1, history2, shared_history, message):
29
  response1, history1 = list(respond(message, history1, system_message1, max_tokens, temperature, top_p))[-1]
30
  response2, history2 = list(respond(message, history2, system_message2, max_tokens, temperature, top_p))[-1]
 
 
 
 
 
 
31
 
32
+ return response1, response2, history1, history2, shared_history, outcome, prosecutor_score_color, defense_score_color
33
+
34
+ def extract_text_from_pdf(pdf_file):
35
+ text = ""
36
+ doc = fitz.open(pdf_file)
37
+ for page in doc:
38
+ text += page.get_text()
39
+ return text
40
 
41
+ def ask_about_pdf(pdf_text, question):
42
+ prompt = f"PDF Content: {pdf_text}\n\nQuestion: {question}\n\nAnswer:"
43
  response = ""
44
  for message in client.chat_completion(
45
+ [{"role": "system", "content": "You are a legal expert answering questions based on the PDF content provided."},
46
  {"role": "user", "content": prompt}],
47
  max_tokens=512,
48
  stream=True,
 
54
  response += token
55
  return response
56
 
57
+ def update_pdf_gallery_and_extract_text(pdf_files):
58
+ if len(pdf_files) > 0:
59
+ pdf_text = extract_text_from_pdf(pdf_files[0].name)
60
+ else:
61
+ pdf_text = ""
62
+ return pdf_files, pdf_text
63
+
64
  def add_message(history, message):
65
  for x in message["files"]:
66
+ return history, gr.MultimodalTextbox(value=None, interactive=False)
 
 
 
67
 
68
+ def bot(history):
69
+ system_message = "You are a helpful assistant."
70
+ messages = [{"role": "system", "content": system_message}]
71
+ for val in history:
72
+ if val[0]:
73
+ messages.append({"role": "user", "content": val[0]})
74
+ if val[1]:
75
+ messages.append({"role": "assistant", "content": val[1]})
 
 
 
76
  response = ""
77
  for message in client.chat_completion(
78
+ messages,
79
+ max_tokens=150,
 
80
  stream=True,
81
+ temperature=0.6,
82
  top_p=0.95,
83
  ):
84
  token = message.choices[0].delta.content
85
  if token is not None:
86
  response += token
87
+ history[-1][1] = response
88
+ yield history
89
+
90
+ def print_like_dislike(x: gr.LikeData):
91
+ print(x.index, x.value, x.liked)
92
+
93
+ def reset_conversation():
94
+ return [], [], "", "", ""
95
+
96
+ def save_conversation(history1, history2, shared_history):
97
+ return history1, history2, shared_history
98
 
99
  with gr.Blocks(css=custom_css) as demo:
100
  history1 = gr.State([])
101
  history2 = gr.State([])
102
  shared_history = gr.State([])
103
+ pdf_files = gr.State([])
104
+ pdf_text = gr.State("")
105
+
106
  with gr.Tab("Argument Evaluation"):
107
+ message = gr.Textbox(label="Case to Argue")
108
+
109
+ shared_argument = gr.Textbox(label="Case Outcome", interactive=True)
110
+ submit_btn = gr.Button("Argue")
111
+ clear_btn = gr.Button("Clear and Reset")
112
+ save_btn = gr.Button("Save Conversation")
113
+
114
+ submit_btn.click(chat_between_bots, inputs=[system_message1, system_message2, max_tokens, temperature, top_p, history1, history2, shared_history, message], outputs=[prosecutor_response, defense_response, history1, history2, shared_argument, prosecutor_score_color, defense_score_color])
115
+ clear_btn.click(reset_conversation, outputs=[history1, history2, shared_history, prosecutor_response, defense_response, shared_argument])
116
+ save_btn.click(save_conversation, inputs=[history1, history2, shared_history], outputs=[history1, history2, shared_history])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
+ with gr.Tab("PDF Management"):
119
+ pdf_upload = gr.File(label="Upload Case Files (PDF)", file_types=[".pdf"])
120
+ pdf_gallery = gr.Gallery(label="PDF Gallery")
121
+ pdf_view = gr.Textbox(label="PDF Content", interactive=False, elem_classes=["scroll-box"])
122
+ pdf_question = gr.Textbox(label="Ask a Question about the PDF")
123
+ pdf_answer = gr.Textbox(label="Answer", interactive=False, elem_classes=["scroll-box"])
124
+ pdf_upload_btn = gr.Button("Update PDF Gallery")
125
+ pdf_ask_btn = gr.Button("Ask")
126
+
127
+ pdf_upload_btn.click(update_pdf_gallery_and_extract_text, inputs=[pdf_upload], outputs=[pdf_gallery, pdf_text])
128
+ pdf_text.change(fn=lambda x: x, inputs=pdf_text, outputs=pdf_view)
129
+ pdf_ask_btn.click(ask_about_pdf, inputs=[pdf_text, pdf_question], outputs=pdf_answer)
130
+
131
+ with gr.Tab("Chatbot"):
132
+ chatbot = gr.Chatbot(