File size: 4,939 Bytes
9519c42
 
333cd91
9519c42
0eb38a5
333cd91
0eb38a5
 
 
9519c42
 
333cd91
 
fb6b907
9519c42
 
 
 
 
fb6b907
9519c42
fb6b907
333cd91
f82692d
fb6b907
9519c42
 
 
 
 
fb6b907
9519c42
fb6b907
333cd91
 
fb6b907
9519c42
 
c68fd83
9519c42
 
 
fb6b907
9519c42
fb6b907
333cd91
0eb38a5
77122ee
 
 
 
 
0eb38a5
 
 
 
 
 
 
 
 
 
 
 
 
 
77122ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325d895
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
# Cell 1B: Inference Client

import gradio as gr
from huggingface_hub import InferenceClient
import logging

# Setup logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')

# Initialize Inference Client
client = InferenceClient(model="Addaci/mT5-small-experiment-13-checkpoint-2790")

def correct_htr(raw_htr_text):
    try:
        logging.info("Processing HTR correction with InferenceClient...")
        # Sending the input to the hosted model
        result = client.text_generation(f"correct this text: {raw_htr_text}")
        logging.debug(f"Generated output for HTR correction: {result}")
        return result['generated_text']  # Extracting the generated text from the response
    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 with InferenceClient...")
        # Sending the input to the hosted model
        result = client.text_generation(f"summarize the following legal text: {legal_text}")
        logging.debug(f"Generated summary: {result}")
        return result['generated_text']  # Extracting the generated text from the response
    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 with InferenceClient...")
        # Sending the input to the hosted model
        formatted_input = f"Answer the following question based on the provided context:\n\nQuestion: {question}\n\nContext: {legal_text}"
        result = client.text_generation(formatted_input)
        logging.debug(f"Generated answer: {result}")
        return result['generated_text']  # Extracting the generated text from the response
    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.")

    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()