smrithifs commited on
Commit
c9e8004
·
verified ·
1 Parent(s): 6a8e906

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -17
app.py CHANGED
@@ -1,25 +1,43 @@
1
  import gradio as gr
2
- from transformers import AutoTokenizer, AutoModelForMaskedLM, pipeline
3
 
4
- model_id = "law-ai/InLegalBERT"
5
- tokenizer = AutoTokenizer.from_pretrained(model_id)
6
- model = AutoModelForMaskedLM.from_pretrained(model_id)
7
 
8
- nlp = pipeline("fill-mask", model=model, tokenizer=tokenizer)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- def explain_law(text):
11
  try:
12
- results = nlp(text)
13
- return results[0]['sequence']
14
  except Exception as e:
15
- return f"Error: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- demo = gr.Interface(
18
- fn=explain_law,
19
- inputs=gr.Textbox(label="Enter Legal Sentence with [MASK] for completion"),
20
- outputs=gr.Textbox(label="Legal Output"),
21
- title="InLegalBERT Legal Engine",
22
- allow_flagging="never"
23
- )
24
 
25
- demo.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
3
 
4
+ # Load the InLegalBERT model and tokenizer for Question Answering
5
+ qa_pipeline = pipeline("question-answering", model="law-ai/InLegalBERT", tokenizer="law-ai/InLegalBERT")
 
6
 
7
+ # Example context to help model reason (you can replace this with a full Bare Act or user-provided text)
8
+ default_context = """
9
+ Section 420 of the Indian Penal Code deals with cheating and dishonestly inducing delivery of property.
10
+ Whoever cheats and thereby dishonestly induces the person deceived to deliver any property to any person,
11
+ or to make, alter or destroy the whole or any part of a valuable security, shall be punished with imprisonment
12
+ of up to 7 years and shall also be liable to fine.
13
+ """
14
+
15
+ # Function to generate legal answer
16
+ def get_legal_answer(user_question, context_text):
17
+ if not user_question.strip():
18
+ return "Please enter a valid legal question.", ""
19
+
20
+ # Use default context if not provided
21
+ context = context_text if context_text.strip() else default_context
22
 
 
23
  try:
24
+ result = qa_pipeline(question=user_question, context=context)
25
+ return result["answer"], context
26
  except Exception as e:
27
+ return f"Error: {str(e)}", context
28
+
29
+ # Gradio UI
30
+ with gr.Blocks() as app:
31
+ gr.Markdown("## 🧑‍⚖️ InLegalBERT Legal Engine - Ask Legal Questions")
32
+
33
+ with gr.Row():
34
+ question_input = gr.Textbox(label="Enter Legal Question", placeholder="e.g., What is Section 420?")
35
+ context_input = gr.Textbox(label="Context (Optional)", placeholder="Leave empty to use default legal context")
36
+
37
+ output = gr.Textbox(label="Legal Output")
38
+
39
+ submit_btn = gr.Button("Submit")
40
 
41
+ submit_btn.click(fn=get_legal_answer, inputs=[question_input, context_input], outputs=output)
 
 
 
 
 
 
42
 
43
+ app.launch()