Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -12,8 +12,6 @@ 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
|
@@ -187,25 +185,6 @@ def chat_between_bots(system_message1, system_message2, max_tokens, temperature,
|
|
187 |
|
188 |
return response1, response2, history1, history2, shared_history, outcome
|
189 |
|
190 |
-
def extract_text_from_pdf(pdf_file):
|
191 |
-
text = ""
|
192 |
-
doc = fitz.open(pdf_file)
|
193 |
-
for page in doc:
|
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):
|
203 |
-
if len(pdf_files) > 0:
|
204 |
-
pdf_text = extract_text_from_pdf(pdf_files[0].name)
|
205 |
-
else:
|
206 |
-
pdf_text = ""
|
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 = ""
|
@@ -229,11 +208,6 @@ def add_message(history, message):
|
|
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)
|
239 |
|
@@ -259,17 +233,10 @@ def ask_about_case_outcome(shared_history, question):
|
|
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([])
|
270 |
shared_history = gr.State([])
|
271 |
-
pdf_files = gr.State([])
|
272 |
-
pdf_text = gr.State("")
|
273 |
top_10_cases = gr.State("")
|
274 |
|
275 |
with gr.Tab("Argument Evaluation"):
|
@@ -308,34 +275,6 @@ with gr.Blocks(css=custom_css) as demo:
|
|
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"):
|
312 |
-
pdf_upload = gr.File(label="Upload Case Files (PDF)", file_types=[".pdf"])
|
313 |
-
pdf_gallery = gr.Gallery(label="PDF Gallery")
|
314 |
-
pdf_view = gr.Textbox(label="PDF Content", interactive=False, elem_classes=["scroll-box"])
|
315 |
-
pdf_question = gr.Textbox(label="Ask a Question about the PDF")
|
316 |
-
pdf_answer = gr.Textbox(label="Answer", interactive=False, elem_classes=["scroll-box"])
|
317 |
-
pdf_upload_btn = gr.Button("Update PDF Gallery")
|
318 |
-
pdf_ask_btn = gr.Button("Ask")
|
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(
|
326 |
-
[],
|
327 |
-
elem_id="chatbot",
|
328 |
-
bubble_full_width=False
|
329 |
-
)
|
330 |
-
|
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)
|
338 |
-
|
339 |
with gr.Tab("Case Outcome Chat"):
|
340 |
case_question = gr.Textbox(label="Ask a Question about the Case Outcome")
|
341 |
case_answer = gr.Textbox(label="Answer", interactive=False, elem_classes=["scroll-box"])
|
|
|
12 |
from transformers import pipeline, AutoTokenizer, AutoModelForMaskedLM
|
13 |
from datasets import load_dataset
|
14 |
import fitz # PyMuPDF
|
|
|
|
|
15 |
from pathlib import Path
|
16 |
|
17 |
dir_ = Path(__file__).parent
|
|
|
185 |
|
186 |
return response1, response2, history1, history2, shared_history, outcome
|
187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
def get_top_10_cases():
|
189 |
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."
|
190 |
response = ""
|
|
|
208 |
history.append((message["text"], None))
|
209 |
return history, gr.MultimodalTextbox(value=None, interactive=True)
|
210 |
|
|
|
|
|
|
|
|
|
|
|
211 |
def print_like_dislike(x: gr.LikeData):
|
212 |
print(x.index, x.value, x.liked)
|
213 |
|
|
|
233 |
response += token
|
234 |
return response
|
235 |
|
|
|
|
|
|
|
|
|
|
|
236 |
with gr.Blocks(css=custom_css) as demo:
|
237 |
history1 = gr.State([])
|
238 |
history2 = gr.State([])
|
239 |
shared_history = gr.State([])
|
|
|
|
|
240 |
top_10_cases = gr.State("")
|
241 |
|
242 |
with gr.Tab("Argument Evaluation"):
|
|
|
275 |
clear_btn.click(reset_conversation, outputs=[history1, history2, shared_history, prosecutor_response, defense_response, outcome])
|
276 |
save_btn.click(save_conversation, inputs=[history1, history2, shared_history], outputs=[history1, history2, shared_history])
|
277 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
278 |
with gr.Tab("Case Outcome Chat"):
|
279 |
case_question = gr.Textbox(label="Ask a Question about the Case Outcome")
|
280 |
case_answer = gr.Textbox(label="Answer", interactive=False, elem_classes=["scroll-box"])
|