michaelmc1618 commited on
Commit
af62734
·
verified ·
1 Parent(s): c0d434a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -70
app.py CHANGED
@@ -4,17 +4,28 @@ os.system('pip install datasets')
4
  os.system('pip install gradio')
5
  os.system('pip install minijinja')
6
  os.system('pip install PyMuPDF')
 
 
7
 
8
  import gradio as gr
9
  from huggingface_hub import InferenceClient
10
- from transformers import pipeline
11
  from datasets import load_dataset
12
  import fitz # PyMuPDF
 
 
 
 
 
13
 
14
  dataset = load_dataset("ibunescu/qa_legal_dataset_train")
15
 
16
- # Use a pipeline as a high-level helper
17
- pipe = pipeline("fill-mask", model="nlpaueb/legal-bert-base-uncased")
 
 
 
 
18
 
19
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
20
 
@@ -36,36 +47,44 @@ def respond(
36
 
37
  messages.append({"role": "user", "content": message})
38
 
39
- response = ""
40
- for message in client.chat_completion(
41
- messages,
42
- max_tokens=max_tokens,
43
- stream=True,
44
- temperature=temperature,
45
- top_p=top_p,
46
- ):
47
- token = message.choices[0].delta.content
48
- if token is not None:
49
- response += token
50
- yield response, history + [(message, response)]
 
 
 
 
51
 
52
  def generate_case_outcome(prosecutor_response, defense_response):
53
  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."
54
  evaluation = ""
55
- for message in client.chat_completion(
56
- [{"role": "system", "content": "You are a legal expert evaluating the details of the case presented by the prosecution and the defense."},
57
- {"role": "user", "content": prompt}],
58
- max_tokens=512,
59
- stream=True,
60
- temperature=0.6,
61
- top_p=0.95,
62
- ):
63
- token = message.choices[0].delta.content
64
- if token is not None:
65
- evaluation += token
 
 
 
 
66
  return evaluation
67
 
68
- def determine_winner(outcome):
69
  prosecutor_count = outcome.split().count("Prosecutor")
70
  defense_count = outcome.split().count("Defense")
71
  if prosecutor_count > defense_count:
@@ -164,9 +183,9 @@ def chat_between_bots(system_message1, system_message2, max_tokens, temperature,
164
  response2 = response2[:max_length]
165
 
166
  outcome = generate_case_outcome(response1, response2)
167
- winner = determine_winner(outcome)
168
 
169
- return response1, response2, history1, history2, shared_history, outcome, winner
170
 
171
  def extract_text_from_pdf(pdf_file):
172
  text = ""
@@ -175,20 +194,9 @@ def extract_text_from_pdf(pdf_file):
175
  text += page.get_text()
176
  return text
177
 
178
- def ask_about_pdf(pdf_text, question):
179
- prompt = f"PDF Content: {pdf_text}\n\nQuestion: {question}\n\nAnswer:"
180
- response = ""
181
- for message in client.chat_completion(
182
- [{"role": "system", "content": "You are a legal expert answering questions based on the PDF content provided."},
183
- {"role": "user", "content": prompt}],
184
- max_tokens=512,
185
- stream=True,
186
- temperature=0.6,
187
- top_p=0.95,
188
- ):
189
- token = message.choices[0].delta.content
190
- if token is not None:
191
- response += token
192
  return response
193
 
194
  def update_pdf_gallery_and_extract_text(pdf_files):
@@ -199,7 +207,7 @@ def update_pdf_gallery_and_extract_text(pdf_files):
199
  return pdf_files, pdf_text
200
 
201
  def get_top_10_cases():
202
- 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. Show different cases on each click."
203
  response = ""
204
  for message in client.chat_completion(
205
  [{"role": "system", "content": "You are a legal research expert, able to provide information about high-profile legal cases."},
@@ -221,27 +229,10 @@ def add_message(history, message):
221
  history.append((message["text"], None))
222
  return history, gr.MultimodalTextbox(value=None, interactive=True)
223
 
224
- def bot(history):
225
  system_message = "You are a helpful assistant."
226
- messages = [{"role": "system", "content": system_message}]
227
- for val in history:
228
- if val[0]:
229
- messages.append({"role": "user", "content": val[0]})
230
- if val[1]:
231
- messages.append({"role": "assistant", "content": val[1]})
232
- response = ""
233
- for message in client.chat_completion(
234
- messages,
235
- max_tokens=150,
236
- stream=True,
237
- temperature=0.6,
238
- top_p=0.95,
239
- ):
240
- token = message.choices[0].delta.content
241
- if token is not None:
242
- response += token
243
- history[-1][1] = response
244
- yield history
245
 
246
  def print_like_dislike(x: gr.LikeData):
247
  print(x.index, x.value, x.liked)
@@ -268,6 +259,11 @@ def ask_about_case_outcome(shared_history, question):
268
  response += token
269
  return response
270
 
 
 
 
 
 
271
  with gr.Blocks(css=custom_css) as demo:
272
  history1 = gr.State([])
273
  history2 = gr.State([])
@@ -301,16 +297,15 @@ with gr.Blocks(css=custom_css) as demo:
301
  with gr.Column(scale=1):
302
  defense_score_color = gr.HTML()
303
 
304
- shared_argument = gr.Textbox(label="Case Outcome", interactive=True, elem_classes=["scroll-box"])
305
- winner = gr.Textbox(label="Winner", interactive=False, elem_classes=["scroll-box"])
306
 
307
  with gr.Row():
308
  submit_btn = gr.Button("Argue")
309
  clear_btn = gr.Button("Clear and Reset")
310
  save_btn = gr.Button("Save Conversation")
311
 
312
- 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, winner])
313
- clear_btn.click(reset_conversation, outputs=[history1, history2, shared_history, prosecutor_response, defense_response, shared_argument, winner])
314
  save_btn.click(save_conversation, inputs=[history1, history2, shared_history], outputs=[history1, history2, shared_history])
315
 
316
  with gr.Tab("PDF Management"):
@@ -324,7 +319,7 @@ with gr.Blocks(css=custom_css) as demo:
324
 
325
  pdf_upload_btn.click(update_pdf_gallery_and_extract_text, inputs=[pdf_upload], outputs=[pdf_gallery, pdf_text])
326
  pdf_text.change(fn=lambda x: x, inputs=pdf_text, outputs=pdf_view)
327
- pdf_ask_btn.click(ask_about_pdf, inputs=[pdf_text, pdf_question], outputs=pdf_answer)
328
 
329
  with gr.Tab("Chatbot"):
330
  chatbot = gr.Chatbot(
@@ -336,7 +331,7 @@ with gr.Blocks(css=custom_css) as demo:
336
  chat_input = gr.MultimodalTextbox(interactive=True, file_types=["image"], placeholder="Enter message or upload file...", show_label=False)
337
 
338
  chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
339
- bot_msg = chat_msg.then(bot, chatbot, chatbot, api_name="bot_response")
340
  bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
341
 
342
  chatbot.like(print_like_dislike, None, None)
 
4
  os.system('pip install gradio')
5
  os.system('pip install minijinja')
6
  os.system('pip install PyMuPDF')
7
+ os.system('pip install pdf2image')
8
+ os.system('pip install gradio_pdf')
9
 
10
  import gradio as gr
11
  from huggingface_hub import InferenceClient
12
+ from transformers import pipeline, AutoTokenizer, AutoModelForMaskedLM
13
  from datasets import load_dataset
14
  import fitz # PyMuPDF
15
+ from pdf2image import convert_from_path
16
+ from gradio_pdf import PDF
17
+ from pathlib import Path
18
+
19
+ dir_ = Path(__file__).parent
20
 
21
  dataset = load_dataset("ibunescu/qa_legal_dataset_train")
22
 
23
+ # Load the BERT model and tokenizer
24
+ tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased")
25
+ model = AutoModelForMaskedLM.from_pretrained("google-bert/bert-base-uncased")
26
+
27
+ # Create the fill-mask pipeline
28
+ pipe = pipeline("fill-mask", model=model, tokenizer=tokenizer)
29
 
30
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
31
 
 
47
 
48
  messages.append({"role": "user", "content": message})
49
 
50
+ try:
51
+ response = ""
52
+ for message in client.chat_completion(
53
+ messages,
54
+ max_tokens=max_tokens,
55
+ stream=True,
56
+ temperature=temperature,
57
+ top_p=top_p,
58
+ ):
59
+ token = message.choices[0].delta.content
60
+ if token is not None:
61
+ response += token
62
+ yield response, history + [(message, response)]
63
+ except Exception as e:
64
+ print(f"Error during chat completion: {e}")
65
+ yield "An error occurred during the chat completion.", history
66
 
67
  def generate_case_outcome(prosecutor_response, defense_response):
68
  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."
69
  evaluation = ""
70
+ try:
71
+ for message in client.chat_completion(
72
+ [{"role": "system", "content": "You are a legal expert evaluating the details of the case presented by the prosecution and the defense."},
73
+ {"role": "user", "content": prompt}],
74
+ max_tokens=512,
75
+ stream=True,
76
+ temperature=0.6,
77
+ top_p=0.95,
78
+ ):
79
+ token = message.choices[0].delta.content
80
+ if token is not None:
81
+ evaluation += token
82
+ except Exception as e:
83
+ print(f"Error during case outcome generation: {e}")
84
+ return "An error occurred during the case outcome generation."
85
  return evaluation
86
 
87
+ def determine_outcome(outcome):
88
  prosecutor_count = outcome.split().count("Prosecutor")
89
  defense_count = outcome.split().count("Defense")
90
  if prosecutor_count > defense_count:
 
183
  response2 = response2[:max_length]
184
 
185
  outcome = generate_case_outcome(response1, response2)
186
+ winner = determine_outcome(outcome)
187
 
188
+ return response1, response2, history1, history2, shared_history, outcome
189
 
190
  def extract_text_from_pdf(pdf_file):
191
  text = ""
 
194
  text += page.get_text()
195
  return text
196
 
197
+ def ask_about_pdf(pdf_text, question, history):
198
+ system_message = "You are a legal expert answering questions based on the PDF content provided."
199
+ response = list(respond(question, history, system_message, max_tokens=512, temperature=0.6, top_p=0.95))[-1][0]
 
 
 
 
 
 
 
 
 
 
 
200
  return response
201
 
202
  def update_pdf_gallery_and_extract_text(pdf_files):
 
207
  return pdf_files, pdf_text
208
 
209
  def get_top_10_cases():
210
+ prompt = "List 10 high-profile legal cases that have received significant media attention and are currently ongoing. Just a list of case names and numbers."
211
  response = ""
212
  for message in client.chat_completion(
213
  [{"role": "system", "content": "You are a legal research expert, able to provide information about high-profile legal cases."},
 
229
  history.append((message["text"], None))
230
  return history, gr.MultimodalTextbox(value=None, interactive=True)
231
 
232
+ def bot(history, message):
233
  system_message = "You are a helpful assistant."
234
+ response = list(respond(message, history, system_message, max_tokens=150, temperature=0.6, top_p=0.95))[-1][0]
235
+ return response, history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
 
237
  def print_like_dislike(x: gr.LikeData):
238
  print(x.index, x.value, x.liked)
 
259
  response += token
260
  return response
261
 
262
+ def qa(question: str, doc: str) -> str:
263
+ img = convert_from_path(doc)[0]
264
+ output = pipe(img, question)
265
+ return sorted(output, key=lambda x: x["score"], reverse=True)[0]['answer']
266
+
267
  with gr.Blocks(css=custom_css) as demo:
268
  history1 = gr.State([])
269
  history2 = gr.State([])
 
297
  with gr.Column(scale=1):
298
  defense_score_color = gr.HTML()
299
 
300
+ outcome = gr.Textbox(label="Outcome", interactive=False, elem_classes=["scroll-box"])
 
301
 
302
  with gr.Row():
303
  submit_btn = gr.Button("Argue")
304
  clear_btn = gr.Button("Clear and Reset")
305
  save_btn = gr.Button("Save Conversation")
306
 
307
+ 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])
308
+ clear_btn.click(reset_conversation, outputs=[history1, history2, shared_history, prosecutor_response, defense_response, outcome])
309
  save_btn.click(save_conversation, inputs=[history1, history2, shared_history], outputs=[history1, history2, shared_history])
310
 
311
  with gr.Tab("PDF Management"):
 
319
 
320
  pdf_upload_btn.click(update_pdf_gallery_and_extract_text, inputs=[pdf_upload], outputs=[pdf_gallery, pdf_text])
321
  pdf_text.change(fn=lambda x: x, inputs=pdf_text, outputs=pdf_view)
322
+ pdf_ask_btn.click(qa, inputs=[pdf_question, pdf_text], outputs=pdf_answer)
323
 
324
  with gr.Tab("Chatbot"):
325
  chatbot = gr.Chatbot(
 
331
  chat_input = gr.MultimodalTextbox(interactive=True, file_types=["image"], placeholder="Enter message or upload file...", show_label=False)
332
 
333
  chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot, chat_input])
334
+ bot_msg = chat_msg.then(bot, inputs=[history1, chat_input], outputs=[chatbot, history1])
335
  bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
336
 
337
  chatbot.like(print_like_dislike, None, None)