Addaci commited on
Commit
333cd91
1 Parent(s): 46d85f4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
+
4
+ # Load your fine-tuned mT5 model
5
+ model_name = "MarineLives/mT5-small-experiment-13" # Replace with your model name or path
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
+
9
+ def correct_htr(raw_htr_text):
10
+ # Tokenize the input text
11
+ inputs = tokenizer(raw_htr_text, return_tensors="pt")
12
+
13
+ # Generate corrected text
14
+ outputs = model.generate(**inputs)
15
+ corrected_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
16
+
17
+ return corrected_text
18
+
19
+ def summarize_text(legal_text):
20
+ # Tokenize the input text with summarization prompt
21
+ inputs = tokenizer("summarize: " + legal_text, return_tensors="pt")
22
+
23
+ # Generate summary
24
+ outputs = model.generate(**inputs)
25
+ summary = tokenizer.decode(outputs[0], skip_special_tokens=True)
26
+
27
+ return summary
28
+
29
+ def answer_question(legal_text, question):
30
+ # Combine context and question
31
+ inputs = tokenizer(f"question: {question} context: {legal_text}", return_tensors="pt")
32
+
33
+ # Generate answer
34
+ outputs = model.generate(**inputs)
35
+ answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
36
+
37
+ return answer
38
+
39
+ # Create the Gradio interface
40
+ iface = gr.Interface(
41
+ fn=[correct_htr, summarize_text, answer_question],
42
+ inputs=[
43
+ gr.Textbox(lines=5, placeholder="Enter raw HTR text here..."),
44
+ gr.Textbox(lines=10, placeholder="Enter legal text to summarize..."),
45
+ [gr.Textbox(lines=10, placeholder="Enter legal text..."),
46
+ gr.Textbox(lines=2, placeholder="Enter your question...")]
47
+ ],
48
+ outputs=[
49
+ gr.Textbox(lines=5, placeholder="Corrected HTR text"),
50
+ gr.Textbox(lines=5, placeholder="Summary of legal text"),
51
+ gr.Textbox(lines=5, placeholder="Answer to your question")
52
+ ],
53
+ title="mT5 Legal Assistant",
54
+ description="Use this tool to correct raw HTR, summarize legal texts, or answer questions about legal cases."
55
+ )
56
+
57
+ iface.launch()