Addaci's picture
Update app.py (sort out logging; separate button boxes)
0eb38a5 verified
raw
history blame
5.66 kB
import os
import gradio as gr
import logging
from transformers import MT5Tokenizer, MT5ForConditionalGeneration
# Setup logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
# Load your fine-tuned mT5 model
model_name = "Addaci/mT5-small-experiment-13-checkpoint-2790"
tokenizer = MT5Tokenizer.from_pretrained(model_name)
model = MT5ForConditionalGeneration.from_pretrained(model_name)
def correct_htr(raw_htr_text):
try:
logging.info("Processing HTR correction...")
inputs = tokenizer(raw_htr_text, return_tensors="pt", max_length=512, truncation=True)
logging.debug(f"Tokenized Inputs for HTR Correction: {inputs}")
outputs = model.generate(**inputs, max_length=128, num_beams=4, early_stopping=True)
logging.debug(f"Generated Output (Tokens) for HTR Correction: {outputs}")
corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
logging.debug(f"Decoded Output for HTR Correction: {corrected_text}")
return corrected_text
except Exception as e:
logging.error(f"Error in HTR Correction: {e}", exc_info=True)
return str(e)
def summarize_text(legal_text):
try:
logging.info("Processing summarization...")
inputs = tokenizer("summarize: " + legal_text, return_tensors="pt", max_length=512, truncation=True)
logging.debug(f"Tokenized Inputs for Summarization: {inputs}")
outputs = model.generate(**inputs, max_length=150, num_beams=4, early_stopping=True)
logging.debug(f"Generated Summary (Tokens): {outputs}")
summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
logging.debug(f"Decoded Summary: {summary}")
return summary
except Exception as e:
logging.error(f"Error in Summarization: {e}", exc_info=True)
return str(e)
def answer_question(legal_text, question):
try:
logging.info("Processing question-answering...")
formatted_input = f"question: {question} context: {legal_text}"
inputs = tokenizer(formatted_input, return_tensors="pt", max_length=512, truncation=True)
logging.debug(f"Tokenized Inputs for Question Answering: {inputs}")
outputs = model.generate(**inputs, max_length=150, num_beams=4, early_stopping=True)
logging.debug(f"Generated Answer (Tokens): {outputs}")
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
logging.debug(f"Decoded Answer: {answer}")
return answer
except Exception as e:
logging.error(f"Error in Question Answering: {e}", exc_info=True)
return str(e)
# Create the Gradio Blocks interface
with gr.Blocks() as demo:
gr.Markdown("# mT5 Legal Assistant")
gr.Markdown("Use this tool to correct raw HTR, summarize legal texts, or answer questions about legal cases.")
# Adding external link buttons with a box around each and bold text
with gr.Row():
gr.HTML('''
<div style="display: flex; gap: 10px;">
<div style="border: 2px solid black; padding: 10px; display: inline-block;">
<a href="http://www.marinelives.org/wiki/Tools:_Admiralty_court_legal_glossary" target="_blank">
<button style="font-weight:bold;">Admiralty Court Legal Glossary</button>
</a>
</div>
<div style="border: 2px solid black; padding: 10px; display: inline-block;">
<a href="https://raw.githubusercontent.com/Addaci/HCA/refs/heads/main/HCA_13_70_Full_Volume_Processed_Text_EDITED_Ver.1.2_18062024.txt" target="_blank">
<button style="font-weight:bold;">HCA 13/70 Ground Truth (1654-55)</button>
</a>
</div>
</div>
''')
with gr.Tab("Correct HTR"):
gr.Markdown("### Correct Raw HTR Text")
raw_htr_input = gr.Textbox(lines=5, placeholder="Enter raw HTR text here...")
corrected_output = gr.Textbox(lines=5, placeholder="Corrected HTR text")
correct_button = gr.Button("Correct HTR")
clear_button = gr.Button("Clear")
correct_button.click(correct_htr, inputs=raw_htr_input, outputs=corrected_output)
clear_button.click(lambda: ("", ""), outputs=[raw_htr_input, corrected_output])
with gr.Tab("Summarize Legal Text"):
gr.Markdown("### Summarize Legal Text")
legal_text_input = gr.Textbox(lines=10, placeholder="Enter legal text to summarize...")
summary_output = gr.Textbox(lines=5, placeholder="Summary of legal text")
summarize_button = gr.Button("Summarize Text")
clear_button = gr.Button("Clear")
summarize_button.click(summarize_text, inputs=legal_text_input, outputs=summary_output)
clear_button.click(lambda: ("", ""), outputs=[legal_text_input, summary_output])
with gr.Tab("Answer Legal Question"):
gr.Markdown("### Answer a Question Based on Legal Text")
legal_text_input_q = gr.Textbox(lines=10, placeholder="Enter legal text...")
question_input = gr.Textbox(lines=2, placeholder="Enter your question...")
answer_output = gr.Textbox(lines=5, placeholder="Answer to your question")
answer_button = gr.Button("Get Answer")
clear_button = gr.Button("Clear")
answer_button.click(answer_question, inputs=[legal_text_input_q, question_input], outputs=answer_output)
clear_button.click(lambda: ("", "", ""), outputs=[legal_text_input_q, question_input, answer_output])
# Launch the Gradio interface
demo.launch()