Spaces:
Build error
Build error
Reapplying missing title and buttons and clickable URLs
Browse files
app.py
CHANGED
@@ -1,70 +1,79 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import
|
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 |
-
#
|
9 |
-
def
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
#
|
15 |
-
def
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
# Answer Legal Question
|
21 |
def answer_legal_question(context, question, max_new_tokens, temperature):
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
# Gradio Interface
|
28 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
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 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
)
|
|
|
|
|
55 |
|
56 |
with gr.Tab("Answer Legal Question"):
|
57 |
-
|
58 |
-
question_input = gr.Textbox(label="
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
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()
|