Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,12 +3,14 @@ os.system('pip install transformers')
|
|
3 |
os.system('pip install datasets')
|
4 |
os.system('pip install gradio')
|
5 |
os.system('pip install minijinja')
|
|
|
6 |
|
7 |
import gradio as gr
|
8 |
from huggingface_hub import InferenceClient
|
9 |
from transformers import pipeline
|
10 |
from datasets import load_dataset
|
11 |
import time
|
|
|
12 |
|
13 |
dataset = load_dataset("ibunescu/qa_legal_dataset_train")
|
14 |
|
@@ -65,7 +67,6 @@ def generate_case_outcome(prosecutor_response, defense_response):
|
|
65 |
return evaluation
|
66 |
|
67 |
def score_argument_from_outcome(outcome, argument):
|
68 |
-
# Simplified scoring based on keywords in the outcome
|
69 |
if "Prosecutor" in outcome:
|
70 |
prosecutor_score = outcome.count("Prosecutor") * 2
|
71 |
if "won" in outcome and "Prosecutor" in outcome:
|
@@ -167,7 +168,6 @@ footer {
|
|
167 |
}
|
168 |
"""
|
169 |
|
170 |
-
# Function to facilitate the conversation between the two chatbots
|
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]
|
@@ -191,8 +191,35 @@ def chat_between_bots(system_message1, system_message2, max_tokens, temperature,
|
|
191 |
|
192 |
return response1, response2, history1, history2, shared_history, outcome, prosecutor_score_color, defense_score_color
|
193 |
|
194 |
-
def
|
195 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
196 |
|
197 |
def add_message(history, message):
|
198 |
for x in message["files"]:
|
@@ -202,21 +229,42 @@ def add_message(history, message):
|
|
202 |
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
203 |
|
204 |
def bot(history):
|
205 |
-
|
206 |
-
|
207 |
-
for
|
208 |
-
|
209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
210 |
yield history
|
211 |
|
212 |
def print_like_dislike(x: gr.LikeData):
|
213 |
print(x.index, x.value, x.liked)
|
214 |
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
with gr.Blocks(css=custom_css) as demo:
|
216 |
history1 = gr.State([])
|
217 |
history2 = gr.State([])
|
218 |
shared_history = gr.State([])
|
219 |
pdf_files = gr.State([])
|
|
|
220 |
|
221 |
with gr.Tab("Argument Evaluation"):
|
222 |
message = gr.Textbox(label="Case to Argue")
|
@@ -239,15 +287,25 @@ with gr.Blocks(css=custom_css) as demo:
|
|
239 |
|
240 |
shared_argument = gr.Textbox(label="Case Outcome", interactive=True)
|
241 |
submit_btn = gr.Button("Argue")
|
|
|
|
|
242 |
|
243 |
-
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,
|
|
|
|
|
244 |
|
245 |
with gr.Tab("PDF Management"):
|
246 |
pdf_upload = gr.File(label="Upload Case Files (PDF)", file_types=[".pdf"])
|
247 |
pdf_gallery = gr.Gallery(label="PDF Gallery")
|
|
|
|
|
|
|
248 |
pdf_upload_btn = gr.Button("Update PDF Gallery")
|
249 |
-
|
250 |
-
|
|
|
|
|
|
|
251 |
|
252 |
with gr.Tab("Chatbot"):
|
253 |
chatbot = gr.Chatbot(
|
|
|
3 |
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 time
|
13 |
+
import fitz # PyMuPDF
|
14 |
|
15 |
dataset = load_dataset("ibunescu/qa_legal_dataset_train")
|
16 |
|
|
|
67 |
return evaluation
|
68 |
|
69 |
def score_argument_from_outcome(outcome, argument):
|
|
|
70 |
if "Prosecutor" in outcome:
|
71 |
prosecutor_score = outcome.count("Prosecutor") * 2
|
72 |
if "won" in outcome and "Prosecutor" in outcome:
|
|
|
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]
|
|
|
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 = ""
|
196 |
+
doc = fitz.open(pdf_file)
|
197 |
+
for page in doc:
|
198 |
+
text += page.get_text()
|
199 |
+
return text
|
200 |
+
|
201 |
+
def ask_about_pdf(pdf_text, question):
|
202 |
+
prompt = f"PDF Content: {pdf_text}\n\nQuestion: {question}\n\nAnswer:"
|
203 |
+
response = ""
|
204 |
+
for message in client.chat_completion(
|
205 |
+
[{"role": "system", "content": "You are a legal expert answering questions based on the PDF content provided."},
|
206 |
+
{"role": "user", "content": prompt}],
|
207 |
+
max_tokens=512,
|
208 |
+
stream=True,
|
209 |
+
temperature=0.6,
|
210 |
+
top_p=0.95,
|
211 |
+
):
|
212 |
+
token = message.choices[0].delta.content
|
213 |
+
if token is not None:
|
214 |
+
response += token
|
215 |
+
return response
|
216 |
+
|
217 |
+
def update_pdf_gallery_and_extract_text(pdf_files):
|
218 |
+
if len(pdf_files) > 0:
|
219 |
+
pdf_text = extract_text_from_pdf(pdf_files[0].name)
|
220 |
+
else:
|
221 |
+
pdf_text = ""
|
222 |
+
return pdf_files, pdf_text
|
223 |
|
224 |
def add_message(history, message):
|
225 |
for x in message["files"]:
|
|
|
229 |
return history, gr.MultimodalTextbox(value=None, interactive=False)
|
230 |
|
231 |
def bot(history):
|
232 |
+
system_message = "You are a helpful assistant."
|
233 |
+
messages = [{"role": "system", "content": system_message}]
|
234 |
+
for val in history:
|
235 |
+
if val[0]:
|
236 |
+
messages.append({"role": "user", "content": val[0]})
|
237 |
+
if val[1]:
|
238 |
+
messages.append({"role": "assistant", "content": val[1]})
|
239 |
+
response = ""
|
240 |
+
for message in client.chat_completion(
|
241 |
+
messages,
|
242 |
+
max_tokens=150,
|
243 |
+
stream=True,
|
244 |
+
temperature=0.6,
|
245 |
+
top_p=0.95,
|
246 |
+
):
|
247 |
+
token = message.choices[0].delta.content
|
248 |
+
if token is not None:
|
249 |
+
response += token
|
250 |
+
history[-1][1] = response
|
251 |
yield history
|
252 |
|
253 |
def print_like_dislike(x: gr.LikeData):
|
254 |
print(x.index, x.value, x.liked)
|
255 |
|
256 |
+
def reset_conversation():
|
257 |
+
return [], [], "", "", ""
|
258 |
+
|
259 |
+
def save_conversation(history1, history2, shared_history):
|
260 |
+
return history1, history2, shared_history
|
261 |
+
|
262 |
with gr.Blocks(css=custom_css) as demo:
|
263 |
history1 = gr.State([])
|
264 |
history2 = gr.State([])
|
265 |
shared_history = gr.State([])
|
266 |
pdf_files = gr.State([])
|
267 |
+
pdf_text = gr.State("")
|
268 |
|
269 |
with gr.Tab("Argument Evaluation"):
|
270 |
message = gr.Textbox(label="Case to Argue")
|
|
|
287 |
|
288 |
shared_argument = gr.Textbox(label="Case Outcome", interactive=True)
|
289 |
submit_btn = gr.Button("Argue")
|
290 |
+
clear_btn = gr.Button("Clear and Reset")
|
291 |
+
save_btn = gr.Button("Save Conversation")
|
292 |
|
293 |
+
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])
|
294 |
+
clear_btn.click(reset_conversation, outputs=[history1, history2, shared_history, prosecutor_response, defense_response, shared_argument])
|
295 |
+
save_btn.click(save_conversation, inputs=[history1, history2, shared_history], outputs=[history1, history2, shared_history])
|
296 |
|
297 |
with gr.Tab("PDF Management"):
|
298 |
pdf_upload = gr.File(label="Upload Case Files (PDF)", file_types=[".pdf"])
|
299 |
pdf_gallery = gr.Gallery(label="PDF Gallery")
|
300 |
+
pdf_view = gr.Textbox(label="PDF Content", interactive=False, elem_classes=["scroll-box"])
|
301 |
+
pdf_question = gr.Textbox(label="Ask a Question about the PDF")
|
302 |
+
pdf_answer = gr.Textbox(label="Answer", interactive=False, elem_classes=["scroll-box"])
|
303 |
pdf_upload_btn = gr.Button("Update PDF Gallery")
|
304 |
+
pdf_ask_btn = gr.Button("Ask")
|
305 |
+
|
306 |
+
pdf_upload_btn.click(update_pdf_gallery_and_extract_text, inputs=[pdf_upload], outputs=[pdf_gallery, pdf_text])
|
307 |
+
pdf_text.change(fn=lambda x: x, inputs=pdf_text, outputs=pdf_view)
|
308 |
+
pdf_ask_btn.click(ask_about_pdf, inputs=[pdf_text, pdf_question], outputs=pdf_answer)
|
309 |
|
310 |
with gr.Tab("Chatbot"):
|
311 |
chatbot = gr.Chatbot(
|