Spaces:
Sleeping
Sleeping
File size: 5,088 Bytes
fb37d48 36d7e68 0e133d3 d625ced 4194261 36d7e68 4194261 0e133d3 af62734 36d7e68 8b780e6 a33e195 36d7e68 4f4bbd6 36d7e68 4f4bbd6 d205403 36d7e68 4f4bbd6 36d7e68 4f4bbd6 ab05725 4f4bbd6 80942ac 4f4bbd6 80942ac 018293e 4f4bbd6 7039cd5 4f4bbd6 7039cd5 4f4bbd6 80942ac 4f4bbd6 80942ac 4f4bbd6 80942ac 4f4bbd6 a5673c7 36d7e68 4f4bbd6 d205403 4f4bbd6 36d7e68 4f4bbd6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
import os
os.system('pip install transformers')
os.system('pip install datasets')
os.system('pip install gradio')
os.system('pip install minijinja')
os.system('pip install PyMuPDF')
import gradio as gr
from huggingface_hub import InferenceClient
from transformers import pipeline
from datasets import load_dataset
import time
import fitz # PyMuPDF
dataset = load_dataset("ibunescu/qa_legal_dataset_train")
return evaluation
def score_argument_from_outcome(outcome, argument):
if "Prosecutor" in outcome:
prosecutor_score = outcome.count("Prosecutor") * 2
if "won" in outcome and "Prosecutor" in outcome:
}
"""
def chat_between_bots(system_message1, system_message2, max_tokens, temperature, top_p, history1, history2, shared_history, message):
response1, history1 = list(respond(message, history1, system_message1, max_tokens, temperature, top_p))[-1]
response2, history2 = list(respond(message, history2, system_message2, max_tokens, temperature, top_p))[-1]
return response1, response2, history1, history2, shared_history, outcome, prosecutor_score_color, defense_score_color
def extract_text_from_pdf(pdf_file):
text = ""
doc = fitz.open(pdf_file)
for page in doc:
text += page.get_text()
return text
def ask_about_pdf(pdf_text, question):
prompt = f"PDF Content: {pdf_text}\n\nQuestion: {question}\n\nAnswer:"
response = ""
for message in client.chat_completion(
[{"role": "system", "content": "You are a legal expert answering questions based on the PDF content provided."},
{"role": "user", "content": prompt}],
max_tokens=512,
stream=True,
temperature=0.6,
top_p=0.95,
):
token = message.choices[0].delta.content
if token is not None:
response += token
return response
def update_pdf_gallery_and_extract_text(pdf_files):
if len(pdf_files) > 0:
pdf_text = extract_text_from_pdf(pdf_files[0].name)
else:
pdf_text = ""
return pdf_files, pdf_text
def add_message(history, message):
for x in message["files"]:
return history, gr.MultimodalTextbox(value=None, interactive=False)
def bot(history):
system_message = "You are a helpful assistant."
messages = [{"role": "system", "content": system_message}]
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
response = ""
for message in client.chat_completion(
messages,
max_tokens=150,
stream=True,
temperature=0.6,
top_p=0.95,
):
token = message.choices[0].delta.content
if token is not None:
response += token
history[-1][1] = response
yield history
def print_like_dislike(x: gr.LikeData):
print(x.index, x.value, x.liked)
def reset_conversation():
return [], [], "", "", ""
def save_conversation(history1, history2, shared_history):
return history1, history2, shared_history
with gr.Blocks(css=custom_css) as demo:
history1 = gr.State([])
history2 = gr.State([])
shared_history = gr.State([])
pdf_files = gr.State([])
pdf_text = gr.State("")
with gr.Tab("Argument Evaluation"):
message = gr.Textbox(label="Case to Argue")
shared_argument = gr.Textbox(label="Case Outcome", interactive=True)
submit_btn = gr.Button("Argue")
clear_btn = gr.Button("Clear and Reset")
save_btn = gr.Button("Save Conversation")
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])
clear_btn.click(reset_conversation, outputs=[history1, history2, shared_history, prosecutor_response, defense_response, shared_argument])
save_btn.click(save_conversation, inputs=[history1, history2, shared_history], outputs=[history1, history2, shared_history])
with gr.Tab("PDF Management"):
pdf_upload = gr.File(label="Upload Case Files (PDF)", file_types=[".pdf"])
pdf_gallery = gr.Gallery(label="PDF Gallery")
pdf_view = gr.Textbox(label="PDF Content", interactive=False, elem_classes=["scroll-box"])
pdf_question = gr.Textbox(label="Ask a Question about the PDF")
pdf_answer = gr.Textbox(label="Answer", interactive=False, elem_classes=["scroll-box"])
pdf_upload_btn = gr.Button("Update PDF Gallery")
pdf_ask_btn = gr.Button("Ask")
pdf_upload_btn.click(update_pdf_gallery_and_extract_text, inputs=[pdf_upload], outputs=[pdf_gallery, pdf_text])
pdf_text.change(fn=lambda x: x, inputs=pdf_text, outputs=pdf_view)
pdf_ask_btn.click(ask_about_pdf, inputs=[pdf_text, pdf_question], outputs=pdf_answer)
with gr.Tab("Chatbot"):
chatbot = gr.Chatbot( |