Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,6 @@ import gradio as gr
|
|
9 |
from huggingface_hub import InferenceClient
|
10 |
from transformers import pipeline
|
11 |
from datasets import load_dataset
|
12 |
-
import time
|
13 |
import fitz # PyMuPDF
|
14 |
|
15 |
dataset = load_dataset("ibunescu/qa_legal_dataset_train")
|
@@ -51,7 +50,7 @@ def respond(
|
|
51 |
yield response, history + [(message, response)]
|
52 |
|
53 |
def generate_case_outcome(prosecutor_response, defense_response):
|
54 |
-
prompt = f"Prosecutor's Argument: {prosecutor_response}\nDefense Attorney's Argument: {defense_response}\n\nEvaluate both arguments and determine who won the case. Provide reasons for your decision."
|
55 |
evaluation = ""
|
56 |
for message in client.chat_completion(
|
57 |
[{"role": "system", "content": "You are a legal expert evaluating the arguments presented by the prosecution and the defense."},
|
@@ -66,30 +65,18 @@ def generate_case_outcome(prosecutor_response, defense_response):
|
|
66 |
evaluation += token
|
67 |
return evaluation
|
68 |
|
69 |
-
def
|
70 |
-
if "Prosecutor" in outcome:
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
74 |
else:
|
75 |
-
|
76 |
-
|
77 |
-
if "Defense" in outcome:
|
78 |
-
defense_score = outcome.count("Defense") * 2
|
79 |
-
if "won" in outcome and "Defense" in outcome:
|
80 |
-
defense_score += 10
|
81 |
-
else:
|
82 |
-
defense_score = 0
|
83 |
-
|
84 |
-
return prosecutor_score if "Prosecutor" in argument else defense_score
|
85 |
-
|
86 |
-
def color_code(score):
|
87 |
-
if score > 50:
|
88 |
-
return "green"
|
89 |
-
elif score > 30:
|
90 |
-
return "yellow"
|
91 |
-
else:
|
92 |
-
return "red"
|
93 |
|
94 |
# Custom CSS for white background and black text for input and output boxes
|
95 |
custom_css = """
|
@@ -111,6 +98,7 @@ body {
|
|
111 |
background-color: #ffffff !important;
|
112 |
border-color: #ffffff !important;
|
113 |
color: #000000 !important;
|
|
|
114 |
}
|
115 |
.gr-button:hover {
|
116 |
background-color: #ffffff !important;
|
@@ -179,17 +167,9 @@ def chat_between_bots(system_message1, system_message2, max_tokens, temperature,
|
|
179 |
response2 = response2[:max_length]
|
180 |
|
181 |
outcome = generate_case_outcome(response1, response2)
|
|
|
182 |
|
183 |
-
|
184 |
-
score2 = score_argument_from_outcome(outcome, "Defense")
|
185 |
-
|
186 |
-
prosecutor_color = color_code(score1)
|
187 |
-
defense_color = color_code(score2)
|
188 |
-
|
189 |
-
prosecutor_score_color = f"<div class='score-box' style='background-color:{prosecutor_color};'>Score: {score1}</div>"
|
190 |
-
defense_score_color = f"<div class='score-box' style='background-color:{defense_color};'>Score: {score2}</div>"
|
191 |
-
|
192 |
-
return response1, response2, history1, history2, shared_history, outcome, prosecutor_score_color, defense_score_color
|
193 |
|
194 |
def extract_text_from_pdf(pdf_file):
|
195 |
text = ""
|
@@ -222,7 +202,7 @@ def update_pdf_gallery_and_extract_text(pdf_files):
|
|
222 |
return pdf_files, pdf_text
|
223 |
|
224 |
def get_top_10_cases():
|
225 |
-
prompt = "Give me a list of
|
226 |
response = ""
|
227 |
for message in client.chat_completion(
|
228 |
[{"role": "system", "content": "You are a legal expert providing information about top legal cases."},
|
@@ -275,6 +255,22 @@ def reset_conversation():
|
|
275 |
def save_conversation(history1, history2, shared_history):
|
276 |
return history1, history2, shared_history
|
277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
with gr.Blocks(css=custom_css) as demo:
|
279 |
history1 = gr.State([])
|
280 |
history2 = gr.State([])
|
@@ -308,13 +304,16 @@ with gr.Blocks(css=custom_css) as demo:
|
|
308 |
with gr.Column(scale=1):
|
309 |
defense_score_color = gr.HTML()
|
310 |
|
311 |
-
shared_argument = gr.Textbox(label="Case Outcome", interactive=True)
|
312 |
-
|
313 |
-
|
314 |
-
|
|
|
|
|
|
|
315 |
|
316 |
-
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,
|
317 |
-
clear_btn.click(reset_conversation, outputs=[history1, history2, shared_history, prosecutor_response, defense_response, shared_argument])
|
318 |
save_btn.click(save_conversation, inputs=[history1, history2, shared_history], outputs=[history1, history2, shared_history])
|
319 |
|
320 |
with gr.Tab("PDF Management"):
|
@@ -344,6 +343,13 @@ with gr.Blocks(css=custom_css) as demo:
|
|
344 |
bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
|
345 |
|
346 |
chatbot.like(print_like_dislike, None, None)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
347 |
|
348 |
demo.queue()
|
349 |
demo.launch()
|
|
|
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")
|
|
|
50 |
yield response, history + [(message, response)]
|
51 |
|
52 |
def generate_case_outcome(prosecutor_response, defense_response):
|
53 |
+
prompt = f"Prosecutor's Argument: {prosecutor_response}\nDefense Attorney's Argument: {defense_response}\n\nEvaluate both arguments, point out the strengths and weaknesses, and determine who won the case. Provide reasons for your decision."
|
54 |
evaluation = ""
|
55 |
for message in client.chat_completion(
|
56 |
[{"role": "system", "content": "You are a legal expert evaluating the arguments presented by the prosecution and the defense."},
|
|
|
65 |
evaluation += token
|
66 |
return evaluation
|
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"
|
74 |
+
elif "Prosecutor" in outcome:
|
75 |
+
return "Prosecutor Wins"
|
76 |
+
elif "Defense" in outcome:
|
77 |
+
return "Defense Wins"
|
78 |
else:
|
79 |
+
return "No clear winner"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
# Custom CSS for white background and black text for input and output boxes
|
82 |
custom_css = """
|
|
|
98 |
background-color: #ffffff !important;
|
99 |
border-color: #ffffff !important;
|
100 |
color: #000000 !important;
|
101 |
+
margin: 5px;
|
102 |
}
|
103 |
.gr-button:hover {
|
104 |
background-color: #ffffff !important;
|
|
|
167 |
response2 = response2[:max_length]
|
168 |
|
169 |
outcome = generate_case_outcome(response1, response2)
|
170 |
+
winner = determine_winner(outcome)
|
171 |
|
172 |
+
return response1, response2, history1, history2, shared_history, outcome, winner
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
|
174 |
def extract_text_from_pdf(pdf_file):
|
175 |
text = ""
|
|
|
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."},
|
|
|
255 |
def save_conversation(history1, history2, shared_history):
|
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([])
|
276 |
history2 = gr.State([])
|
|
|
304 |
with gr.Column(scale=1):
|
305 |
defense_score_color = gr.HTML()
|
306 |
|
307 |
+
shared_argument = gr.Textbox(label="Case Outcome", interactive=True, elem_classes=["scroll-box"])
|
308 |
+
winner = gr.Textbox(label="Winner", interactive=False, elem_classes=["scroll-box"])
|
309 |
+
|
310 |
+
with gr.Row():
|
311 |
+
submit_btn = gr.Button("Argue")
|
312 |
+
clear_btn = gr.Button("Clear and Reset")
|
313 |
+
save_btn = gr.Button("Save Conversation")
|
314 |
|
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"):
|
|
|
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()
|