michaelmc1618 commited on
Commit
80942ac
·
verified ·
1 Parent(s): ef181e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -21
app.py CHANGED
@@ -11,15 +11,11 @@ from transformers import pipeline
11
  from datasets import load_dataset
12
  import fitz # PyMuPDF
13
 
14
- # Loading datasets
15
  dataset = load_dataset("ibunescu/qa_legal_dataset_train")
16
 
17
- # Different pipelines for different tasks
18
- qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
19
- summarization_pipeline = pipeline("summarization", model="facebook/bart-large-cnn")
20
- mask_filling_pipeline = pipeline("fill-mask", model="nlpaueb/legal-bert-base-uncased")
21
 
22
- # Inference client for chat completion
23
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
24
 
25
  def respond(
@@ -71,7 +67,7 @@ def generate_case_outcome(prosecutor_response, defense_response):
71
 
72
  def determine_winner(outcome):
73
  if "Prosecutor" in outcome and "Defense" in outcome:
74
- if outcome.count("Prosecutor") > outcome.count("Defense"):
75
  return "Prosecutor Wins"
76
  else:
77
  return "Defense Wins"
@@ -183,8 +179,20 @@ def extract_text_from_pdf(pdf_file):
183
  return text
184
 
185
  def ask_about_pdf(pdf_text, question):
186
- result = qa_pipeline(question=question, context=pdf_text)
187
- return result['answer']
 
 
 
 
 
 
 
 
 
 
 
 
188
 
189
  def update_pdf_gallery_and_extract_text(pdf_files):
190
  if len(pdf_files) > 0:
@@ -194,8 +202,20 @@ def update_pdf_gallery_and_extract_text(pdf_files):
194
  return pdf_files, pdf_text
195
 
196
  def get_top_10_cases():
197
- result = summarization_pipeline("Top 10 current legal cases in the country", max_length=150, min_length=50, do_sample=False)
198
- return result[0]['summary_text']
 
 
 
 
 
 
 
 
 
 
 
 
199
 
200
  def add_message(history, message):
201
  for x in message["files"]:
@@ -236,8 +256,20 @@ def save_conversation(history1, history2, shared_history):
236
  return history1, history2, shared_history
237
 
238
  def ask_about_case_outcome(shared_history, question):
239
- result = qa_pipeline(question=question, context=shared_history)
240
- return result['answer']
 
 
 
 
 
 
 
 
 
 
 
 
241
 
242
  with gr.Blocks(css=custom_css) as demo:
243
  history1 = gr.State([])
@@ -283,14 +315,6 @@ with gr.Blocks(css=custom_css) as demo:
283
  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])
284
  clear_btn.click(reset_conversation, outputs=[history1, history2, shared_history, prosecutor_response, defense_response, shared_argument, winner])
285
  save_btn.click(save_conversation, inputs=[history1, history2, shared_history], outputs=[history1, history2, shared_history])
286
-
287
- # Inner HTML for asking about the case outcome
288
- with gr.Row():
289
- case_question = gr.Textbox(label="Ask a Question about the Case Outcome")
290
- case_answer = gr.Textbox(label="Answer", interactive=False, elem_classes=["scroll-box"])
291
- ask_case_btn = gr.Button("Ask")
292
-
293
- ask_case_btn.click(ask_about_case_outcome, inputs=[shared_history, case_question], outputs=case_answer)
294
 
295
  with gr.Tab("PDF Management"):
296
  pdf_upload = gr.File(label="Upload Case Files (PDF)", file_types=[".pdf"])
@@ -319,6 +343,13 @@ with gr.Blocks(css=custom_css) as demo:
319
  bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
320
 
321
  chatbot.like(print_like_dislike, None, None)
 
 
 
 
 
 
 
322
 
323
  demo.queue()
324
  demo.launch()
 
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
 
21
  def respond(
 
67
 
68
  def determine_winner(outcome):
69
  if "Prosecutor" in outcome and "Defense" in outcome:
70
+ if "Prosecutor" in outcome.split().count("Prosecutor") > outcome.split().count("Defense"):
71
  return "Prosecutor Wins"
72
  else:
73
  return "Defense Wins"
 
179
  return text
180
 
181
  def ask_about_pdf(pdf_text, question):
182
+ prompt = f"PDF Content: {pdf_text}\n\nQuestion: {question}\n\nAnswer:"
183
+ response = ""
184
+ for message in client.chat_completion(
185
+ [{"role": "system", "content": "You are a legal expert answering questions based on the PDF content provided."},
186
+ {"role": "user", "content": prompt}],
187
+ max_tokens=512,
188
+ stream=True,
189
+ temperature=0.6,
190
+ top_p=0.95,
191
+ ):
192
+ token = message.choices[0].delta.content
193
+ if token is not None:
194
+ response += token
195
+ return response
196
 
197
  def update_pdf_gallery_and_extract_text(pdf_files):
198
  if len(pdf_files) > 0:
 
202
  return pdf_files, pdf_text
203
 
204
  def get_top_10_cases():
205
+ prompt = "Give me a list of the current top 10 cases in the country being discussed by the top lawyers in the country."
206
+ response = ""
207
+ for message in client.chat_completion(
208
+ [{"role": "system", "content": "You are a legal expert providing information about top legal cases."},
209
+ {"role": "user", "content": prompt}],
210
+ max_tokens=512,
211
+ stream=True,
212
+ temperature=0.6,
213
+ top_p=0.95,
214
+ ):
215
+ token = message.choices[0].delta.content
216
+ if token is not None:
217
+ response += token
218
+ return response
219
 
220
  def add_message(history, message):
221
  for x in message["files"]:
 
256
  return history1, history2, shared_history
257
 
258
  def ask_about_case_outcome(shared_history, question):
259
+ prompt = f"Case Outcome: {shared_history}\n\nQuestion: {question}\n\nAnswer:"
260
+ response = ""
261
+ for message in client.chat_completion(
262
+ [{"role": "system", "content": "You are a legal expert answering questions based on the case outcome provided."},
263
+ {"role": "user", "content": prompt}],
264
+ max_tokens=512,
265
+ stream=True,
266
+ temperature=0.6,
267
+ top_p=0.95,
268
+ ):
269
+ token = message.choices[0].delta.content
270
+ if token is not None:
271
+ response += token
272
+ return response
273
 
274
  with gr.Blocks(css=custom_css) as demo:
275
  history1 = gr.State([])
 
315
  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])
316
  clear_btn.click(reset_conversation, outputs=[history1, history2, shared_history, prosecutor_response, defense_response, shared_argument, winner])
317
  save_btn.click(save_conversation, inputs=[history1, history2, shared_history], outputs=[history1, history2, shared_history])
 
 
 
 
 
 
 
 
318
 
319
  with gr.Tab("PDF Management"):
320
  pdf_upload = gr.File(label="Upload Case Files (PDF)", file_types=[".pdf"])
 
343
  bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
344
 
345
  chatbot.like(print_like_dislike, None, None)
346
+
347
+ with gr.Tab("Case Outcome Chat"):
348
+ case_question = gr.Textbox(label="Ask a Question about the Case Outcome")
349
+ case_answer = gr.Textbox(label="Answer", interactive=False, elem_classes=["scroll-box"])
350
+ ask_case_btn = gr.Button("Ask")
351
+
352
+ ask_case_btn.click(ask_about_case_outcome, inputs=[shared_history, case_question], outputs=case_answer)
353
 
354
  demo.queue()
355
  demo.launch()