Addaci commited on
Commit
8c25be1
1 Parent(s): 1b938f5

Reapplying missing title and buttons and clickable URLs

Browse files
Files changed (1) hide show
  1. app.py +62 -53
app.py CHANGED
@@ -1,70 +1,79 @@
1
  import gradio as gr
2
- from transformers import pipeline, T5Tokenizer, T5ForConditionalGeneration
3
 
4
  # Load model and tokenizer
5
  model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-small")
6
  tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-small")
7
 
8
- # Summarize Legal Text function
9
- def summarize_legal_text(input_text, max_new_tokens, temperature):
10
- input_ids = tokenizer(input_text, return_tensors="pt").input_ids
11
- summary_ids = model.generate(input_ids, max_new_tokens=max_new_tokens, temperature=temperature)
12
- return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
 
 
 
 
 
13
 
14
- # Correct HTR function
15
- def correct_htr_text(input_text, max_new_tokens, temperature):
16
- input_ids = tokenizer(input_text, return_tensors="pt").input_ids
17
- output_ids = model.generate(input_ids, max_new_tokens=max_new_tokens, temperature=temperature)
18
- return tokenizer.decode(output_ids[0], skip_special_tokens=True)
 
 
 
 
 
 
19
 
20
- # Answer Legal Question function
21
  def answer_legal_question(context, question, max_new_tokens, temperature):
22
- input_text = f"Answer the following question based on the context: {question}\nContext: {context}"
23
- input_ids = tokenizer(input_text, return_tensors="pt").input_ids
24
- output_ids = model.generate(input_ids, max_new_tokens=max_new_tokens, temperature=temperature)
25
- return tokenizer.decode(output_ids[0], skip_special_tokens=True)
 
 
 
 
 
26
 
27
- # Gradio Interface
28
  with gr.Blocks() as demo:
 
 
 
 
 
 
29
 
30
- with gr.Tab("Summarize Legal Text"):
31
- summarize_input = gr.Textbox(label="Input Text", placeholder="Enter legal text here...", lines=10)
32
- summarize_output = gr.Textbox(label="Summarized Text", lines=10)
33
- max_new_tokens_summarize = gr.Slider(10, 512, value=256, step=1, label="Max New Tokens")
34
- temperature_summarize = gr.Slider(0.1, 1, value=0.5, step=0.1, label="Temperature")
35
- summarize_button = gr.Button("Summarize Text")
36
-
37
- summarize_button.click(
38
- summarize_legal_text,
39
- inputs=[summarize_input, max_new_tokens_summarize, temperature_summarize],
40
- outputs=summarize_output,
41
- )
42
-
43
- with gr.Tab("Correct Raw HTR Text"):
44
- htr_input = gr.Textbox(label="Input HTR Text", placeholder="Enter HTR text here...", lines=5)
45
- htr_output = gr.Textbox(label="Corrected HTR Text", lines=5)
46
- max_new_tokens_htr = gr.Slider(10, 512, value=128, step=1, label="Max New Tokens")
47
- temperature_htr = gr.Slider(0.1, 1, value=0.7, step=0.1, label="Temperature")
48
- htr_button = gr.Button("Correct HTR")
49
 
50
- htr_button.click(
51
- correct_htr_text,
52
- inputs=[htr_input, max_new_tokens_htr, temperature_htr],
53
- outputs=htr_output,
54
- )
 
 
55
 
56
  with gr.Tab("Answer Legal Question"):
57
- question_input_context = gr.Textbox(label="Context Text", placeholder="Enter legal context...", lines=10)
58
- question_input = gr.Textbox(label="Enter your question", placeholder="Enter your question here...", lines=2)
59
- question_output = gr.Textbox(label="Answer", lines=5)
60
- max_new_tokens_question = gr.Slider(10, 512, value=128, step=1, label="Max New Tokens")
61
- temperature_question = gr.Slider(0.1, 1, value=0.7, step=0.1, label="Temperature")
62
- question_button = gr.Button("Get Answer")
63
-
64
- question_button.click(
65
- answer_legal_question,
66
- inputs=[question_input_context, question_input, max_new_tokens_question, temperature_question],
67
- outputs=question_output,
68
- )
69
 
 
70
  demo.launch()
 
1
  import gradio as gr
2
+ from transformers import T5ForConditionalGeneration, T5Tokenizer
3
 
4
  # Load model and tokenizer
5
  model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-small")
6
  tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-small")
7
 
8
+ # Function for Correct Raw HTR
9
+ def correct_htr(text, max_new_tokens, temperature):
10
+ inputs = tokenizer(text, return_tensors="pt")
11
+ outputs = model.generate(
12
+ **inputs,
13
+ max_new_tokens=max_new_tokens,
14
+ temperature=temperature,
15
+ do_sample=True
16
+ )
17
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
18
 
19
+ # Function for Summarize Legal Text
20
+ def summarize_legal_text(text, max_new_tokens, temperature):
21
+ prompt = "summarize: " + text
22
+ inputs = tokenizer(prompt, return_tensors="pt")
23
+ outputs = model.generate(
24
+ **inputs,
25
+ max_new_tokens=max_new_tokens,
26
+ temperature=temperature,
27
+ do_sample=True
28
+ )
29
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
30
 
31
+ # Function for Answer Legal Question
32
  def answer_legal_question(context, question, max_new_tokens, temperature):
33
+ prompt = f"question: {question} context: {context}"
34
+ inputs = tokenizer(prompt, return_tensors="pt")
35
+ outputs = model.generate(
36
+ **inputs,
37
+ max_new_tokens=max_new_tokens,
38
+ temperature=temperature,
39
+ do_sample=True
40
+ )
41
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
42
 
43
+ # Gradio Interface Setup
44
  with gr.Blocks() as demo:
45
+ # Title and clickable buttons with URLs
46
+ gr.Markdown("# Flan-T5 Legal Assistant")
47
+
48
+ with gr.Row():
49
+ gr.Markdown('[Admiralty Court Legal Glossary](http://www.marinelives.org/wiki/Tools:_Admiralty_court_legal_glossary)')
50
+ gr.Markdown('[HCA 13/70 Ground Truth](https://github.com/Addaci/HCA/blob/main/HCA_13_70_Full_Volume_Processed_Text_EDITED_Ver.1.2_18062024.txt)')
51
 
52
+ # Tabs for different functionalities
53
+ with gr.Tab("Correct Raw HTR"):
54
+ text_input_htr = gr.Textbox(label="Textbox", placeholder="Enter text to correct")
55
+ text_output_htr = gr.Textbox(label="Textbox", placeholder="Corrected text will appear here")
56
+ max_new_tokens_htr = gr.Slider(10, 512, value=128, label="Max New Tokens")
57
+ temperature_htr = gr.Slider(0.1, 1.0, value=0.7, label="Temperature")
58
+ gr.Button("Correct HTR").click(correct_htr, inputs=[text_input_htr, max_new_tokens_htr, temperature_htr], outputs=text_output_htr)
59
+ gr.Button("Clear").click(lambda: "", None, text_input_htr)
 
 
 
 
 
 
 
 
 
 
 
60
 
61
+ with gr.Tab("Summarize Legal Text"):
62
+ text_input_summarize = gr.Textbox(label="Textbox", placeholder="Enter legal text to summarize")
63
+ text_output_summarize = gr.Textbox(label="Textbox", placeholder="Summary will appear here")
64
+ max_new_tokens_summarize = gr.Slider(10, 512, value=256, label="Max New Tokens")
65
+ temperature_summarize = gr.Slider(0.1, 1.0, value=0.5, label="Temperature")
66
+ gr.Button("Summarize Text").click(summarize_legal_text, inputs=[text_input_summarize, max_new_tokens_summarize, temperature_summarize], outputs=text_output_summarize)
67
+ gr.Button("Clear").click(lambda: "", None, text_input_summarize)
68
 
69
  with gr.Tab("Answer Legal Question"):
70
+ context_input = gr.Textbox(label="Textbox", placeholder="Enter legal text for context")
71
+ question_input = gr.Textbox(label="Textbox", placeholder="Enter your question")
72
+ answer_output = gr.Textbox(label="Textbox", placeholder="Answer will appear here")
73
+ max_new_tokens_answer = gr.Slider(10, 512, value=128, label="Max New Tokens")
74
+ temperature_answer = gr.Slider(0.1, 1.0, value=0.7, label="Temperature")
75
+ gr.Button("Get Answer").click(answer_legal_question, inputs=[context_input, question_input, max_new_tokens_answer, temperature_answer], outputs=answer_output)
76
+ gr.Button("Clear").click(lambda: "", None, [context_input, question_input])
 
 
 
 
 
77
 
78
+ # Launch the demo
79
  demo.launch()