Addaci commited on
Commit
93d2618
1 Parent(s): 6c8ecd4
Files changed (1) hide show
  1. app.py +23 -71
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
  import logging
4
 
5
- # Setup logging (optional, but helpful for debugging)
6
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
7
 
8
  # Load the Flan-T5 Small model and tokenizer
@@ -12,81 +12,54 @@ model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
12
 
13
  def correct_htr(raw_htr_text, max_new_tokens, temperature):
14
  try:
15
- if not raw_htr_text:
16
- raise ValueError("Input text cannot be empty.")
17
-
18
- logging.info("Processing HTR correction with Flan-T5 Small...")
19
  prompt = f"Correct this text: {raw_htr_text}"
20
  inputs = tokenizer(prompt, return_tensors="pt")
21
- max_length = min(max_new_tokens, len(inputs['input_ids'][0]) + max_new_tokens)
22
- outputs = model.generate(**inputs, max_length=max_length, temperature=temperature)
23
  corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
- logging.debug(f"Generated output for HTR correction: {corrected_text}")
25
  return corrected_text
26
- except ValueError as ve:
27
- logging.warning(f"Validation error: {ve}")
28
- return str(ve)
29
  except Exception as e:
30
  logging.error(f"Error in HTR correction: {e}", exc_info=True)
31
- return "An error occurred while processing the text."
32
 
33
  def summarize_text(legal_text, max_new_tokens, temperature):
34
  try:
35
- if not legal_text:
36
- raise ValueError("Input text cannot be empty.")
37
-
38
- logging.info("Processing summarization with Flan-T5 Small...")
39
  prompt = f"Summarize the following legal text: {legal_text}"
40
  inputs = tokenizer(prompt, return_tensors="pt")
41
- max_length = min(max_new_tokens, len(inputs['input_ids'][0]) + max_new_tokens)
42
- outputs = model.generate(**inputs, max_length=max_length, temperature=temperature)
43
  summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
44
- logging.debug(f"Generated summary: {summary}")
45
  return summary
46
- except ValueError as ve:
47
- logging.warning(f"Validation error: {ve}")
48
- return str(ve)
49
  except Exception as e:
50
  logging.error(f"Error in summarization: {e}", exc_info=True)
51
- return "An error occurred while summarizing the text."
52
 
53
  def answer_question(legal_text, question, max_new_tokens, temperature):
54
  try:
55
- if not legal_text or not question:
56
- raise ValueError("Both legal text and question must be provided.")
57
-
58
- logging.info("Processing question-answering with Flan-T5 Small...")
59
  prompt = f"Answer the following question based on the provided context:\n\nQuestion: {question}\n\nContext: {legal_text}"
60
  inputs = tokenizer(prompt, return_tensors="pt")
61
- max_length = min(max_new_tokens, len(inputs['input_ids'][0]) + max_new_tokens)
62
- outputs = model.generate(**inputs, max_length=max_length, temperature=temperature)
63
  answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
64
- logging.debug(f"Generated answer: {answer}")
65
  return answer
66
- except ValueError as ve:
67
- logging.warning(f"Validation error: {ve}")
68
- return str(ve)
69
  except Exception as e:
70
  logging.error(f"Error in question-answering: {e}", exc_info=True)
71
- return "An error occurred while answering the question."
72
-
73
- def clear_fields():
74
- return "", "", ""
75
 
76
  # Create the Gradio Blocks interface
77
- with gr.Blocks(css=".block .input-slider { color: blue !important }") as demo:
78
  gr.Markdown("# Flan-T5 Small Legal Assistant")
79
  gr.Markdown("Use this tool to correct raw HTR, summarize legal texts, or answer questions about legal cases (powered by Flan-T5 Small).")
80
-
81
  with gr.Row():
82
  gr.HTML('''
83
  <div style="display: flex; gap: 10px;">
84
- <div style="border: 2px solid black; padding: 10px; display: inline-block;">
85
  <a href="http://www.marinelives.org/wiki/Tools:_Admiralty_court_legal_glossary" target="_blank">
86
  <button style="font-weight:bold;">Admiralty Court Legal Glossary</button>
87
  </a>
88
  </div>
89
- <div style="border: 2px solid black; padding: 10px; display: inline-block;">
90
  <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">
91
  <button style="font-weight:bold;">HCA 13/70 Ground Truth (1654-55)</button>
92
  </a>
@@ -94,38 +67,27 @@ with gr.Blocks(css=".block .input-slider { color: blue !important }") as demo:
94
  </div>
95
  ''')
96
 
 
97
  with gr.Tab("Correct HTR"):
98
  gr.Markdown("### Correct Raw HTR Text")
99
  raw_htr_input = gr.Textbox(lines=5, placeholder="Enter raw HTR text here...")
100
  corrected_output = gr.Textbox(lines=5, placeholder="Corrected HTR text")
101
  correct_button = gr.Button("Correct HTR")
102
  clear_button = gr.Button("Clear")
103
- correct_max_new_tokens = gr.Slider(minimum=10, maximum=512, value=128, step=1, label="Max New Tokens")
104
- correct_temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")
105
-
106
- correct_button.click(correct_htr, inputs=[raw_htr_input, correct_max_new_tokens, correct_temperature], outputs=corrected_output)
107
- clear_button.click(clear_fields, outputs=[raw_htr_input, corrected_output])
108
-
109
- gr.Markdown("### Set Parameters")
110
- correct_max_new_tokens.render()
111
- correct_temperature.render()
112
 
 
113
  with gr.Tab("Summarize Legal Text"):
114
  gr.Markdown("### Summarize Legal Text")
115
  legal_text_input = gr.Textbox(lines=10, placeholder="Enter legal text to summarize...")
116
  summary_output = gr.Textbox(lines=5, placeholder="Summary of legal text")
117
  summarize_button = gr.Button("Summarize Text")
118
  clear_button = gr.Button("Clear")
119
- summarize_max_new_tokens = gr.Slider(minimum=10, maximum=1024, value=256, step=1, label="Max New Tokens")
120
- summarize_temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.5, step=0.1, label="Temperature")
121
-
122
- summarize_button.click(summarize_text, inputs=[legal_text_input, summarize_max_new_tokens, summarize_temperature], outputs=summary_output)
123
- clear_button.click(clear_fields, outputs=[legal_text_input, summary_output])
124
-
125
- gr.Markdown("### Set Parameters")
126
- summarize_max_new_tokens.render()
127
- summarize_temperature.render()
128
 
 
129
  with gr.Tab("Answer Legal Question"):
130
  gr.Markdown("### Answer a Question Based on Legal Text")
131
  legal_text_input_q = gr.Textbox(lines=10, placeholder="Enter legal text...")
@@ -133,18 +95,8 @@ with gr.Blocks(css=".block .input-slider { color: blue !important }") as demo:
133
  answer_output = gr.Textbox(lines=5, placeholder="Answer to your question")
134
  answer_button = gr.Button("Get Answer")
135
  clear_button = gr.Button("Clear")
136
- answer_max_new_tokens = gr.Slider(minimum=10, maximum=512, value=150, step=1, label="Max New Tokens")
137
- answer_temperature = gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.1, label="Temperature")
138
-
139
- answer_button.click(answer_question, inputs=[legal_text_input_q, question_input, answer_max_new_tokens, answer_temperature], outputs=answer_output)
140
- clear_button.click(clear_fields, outputs=[legal_text_input_q, question_input, answer_output])
141
-
142
- gr.Markdown("### Set Parameters")
143
- answer_max_new_tokens.render()
144
- answer_temperature.render()
145
-
146
- # Model warm-up (optional, but useful for performance)
147
- model.generate(**tokenizer("Warm-up", return_tensors="pt"), max_length=10)
148
 
149
  # Launch the Gradio interface
150
  if __name__ == "__main__":
 
2
  from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
  import logging
4
 
5
+ # Setup logging
6
  logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
7
 
8
  # Load the Flan-T5 Small model and tokenizer
 
12
 
13
  def correct_htr(raw_htr_text, max_new_tokens, temperature):
14
  try:
15
+ logging.info("Processing HTR correction...")
 
 
 
16
  prompt = f"Correct this text: {raw_htr_text}"
17
  inputs = tokenizer(prompt, return_tensors="pt")
18
+ outputs = model.generate(**inputs, max_length=min(max_new_tokens, len(inputs['input_ids'][0]) + max_new_tokens), temperature=temperature)
 
19
  corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
20
  return corrected_text
 
 
 
21
  except Exception as e:
22
  logging.error(f"Error in HTR correction: {e}", exc_info=True)
23
+ return str(e)
24
 
25
  def summarize_text(legal_text, max_new_tokens, temperature):
26
  try:
27
+ logging.info("Processing summarization...")
 
 
 
28
  prompt = f"Summarize the following legal text: {legal_text}"
29
  inputs = tokenizer(prompt, return_tensors="pt")
30
+ outputs = model.generate(**inputs, max_length=min(max_new_tokens, len(inputs['input_ids'][0]) + max_new_tokens), temperature=temperature)
 
31
  summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
32
  return summary
 
 
 
33
  except Exception as e:
34
  logging.error(f"Error in summarization: {e}", exc_info=True)
35
+ return str(e)
36
 
37
  def answer_question(legal_text, question, max_new_tokens, temperature):
38
  try:
39
+ logging.info("Processing question-answering...")
 
 
 
40
  prompt = f"Answer the following question based on the provided context:\n\nQuestion: {question}\n\nContext: {legal_text}"
41
  inputs = tokenizer(prompt, return_tensors="pt")
42
+ outputs = model.generate(**inputs, max_length=min(max_new_tokens, len(inputs['input_ids'][0]) + max_new_tokens), temperature=temperature)
 
43
  answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
44
  return answer
 
 
 
45
  except Exception as e:
46
  logging.error(f"Error in question-answering: {e}", exc_info=True)
47
+ return str(e)
 
 
 
48
 
49
  # Create the Gradio Blocks interface
50
+ with gr.Blocks() as demo:
51
  gr.Markdown("# Flan-T5 Small Legal Assistant")
52
  gr.Markdown("Use this tool to correct raw HTR, summarize legal texts, or answer questions about legal cases (powered by Flan-T5 Small).")
53
+
54
  with gr.Row():
55
  gr.HTML('''
56
  <div style="display: flex; gap: 10px;">
57
+ <div style="border: 2px solid black; padding: 10px;">
58
  <a href="http://www.marinelives.org/wiki/Tools:_Admiralty_court_legal_glossary" target="_blank">
59
  <button style="font-weight:bold;">Admiralty Court Legal Glossary</button>
60
  </a>
61
  </div>
62
+ <div style="border: 2px solid black; padding: 10px;">
63
  <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">
64
  <button style="font-weight:bold;">HCA 13/70 Ground Truth (1654-55)</button>
65
  </a>
 
67
  </div>
68
  ''')
69
 
70
+ # Tab 1: Correct HTR
71
  with gr.Tab("Correct HTR"):
72
  gr.Markdown("### Correct Raw HTR Text")
73
  raw_htr_input = gr.Textbox(lines=5, placeholder="Enter raw HTR text here...")
74
  corrected_output = gr.Textbox(lines=5, placeholder="Corrected HTR text")
75
  correct_button = gr.Button("Correct HTR")
76
  clear_button = gr.Button("Clear")
77
+ correct_button.click(correct_htr, inputs=[raw_htr_input, gr.Slider(minimum=10, maximum=512, value=128, step=1, label="Max New Tokens"), gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Temperature")], outputs=corrected_output)
78
+ clear_button.click(lambda: ("", ""), outputs=[raw_htr_input, corrected_output])
 
 
 
 
 
 
 
79
 
80
+ # Tab 2: Summarize Legal Text
81
  with gr.Tab("Summarize Legal Text"):
82
  gr.Markdown("### Summarize Legal Text")
83
  legal_text_input = gr.Textbox(lines=10, placeholder="Enter legal text to summarize...")
84
  summary_output = gr.Textbox(lines=5, placeholder="Summary of legal text")
85
  summarize_button = gr.Button("Summarize Text")
86
  clear_button = gr.Button("Clear")
87
+ summarize_button.click(summarize_text, inputs=[legal_text_input, gr.Slider(minimum=10, maximum=512, value=256, step=1, label="Max New Tokens"), gr.Slider(minimum=0.1, maximum=1.0, value=0.5, step=0.1, label="Temperature")], outputs=summary_output)
88
+ clear_button.click(lambda: ("", ""), outputs=[legal_text_input, summary_output])
 
 
 
 
 
 
 
89
 
90
+ # Tab 3: Answer Legal Question
91
  with gr.Tab("Answer Legal Question"):
92
  gr.Markdown("### Answer a Question Based on Legal Text")
93
  legal_text_input_q = gr.Textbox(lines=10, placeholder="Enter legal text...")
 
95
  answer_output = gr.Textbox(lines=5, placeholder="Answer to your question")
96
  answer_button = gr.Button("Get Answer")
97
  clear_button = gr.Button("Clear")
98
+ answer_button.click(answer_question, inputs=[legal_text_input_q, question_input, gr.Slider(minimum=10, maximum=512, value=150, step=1, label="Max New Tokens"), gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.1, label="Temperature")], outputs=answer_output)
99
+ clear_button.click(lambda: ("", "", ""), outputs=[legal_text_input_q, question_input, answer_output])
 
 
 
 
 
 
 
 
 
 
100
 
101
  # Launch the Gradio interface
102
  if __name__ == "__main__":