Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Install required libraries
|
| 2 |
+
!pip install sentence-transformers faiss-cpu gradio transformers
|
| 3 |
+
|
| 4 |
+
# Import libraries
|
| 5 |
+
from sentence_transformers import SentenceTransformer
|
| 6 |
+
from transformers import pipeline
|
| 7 |
+
import faiss
|
| 8 |
+
import numpy as np
|
| 9 |
+
import gradio as gr
|
| 10 |
+
|
| 11 |
+
# Step 1: Build Knowledge Base from Legal Texts
|
| 12 |
+
legal_texts = [
|
| 13 |
+
# Dowry and Marriage
|
| 14 |
+
"Is dowry illegal in India? -> Yes, under the Dowry Prohibition Act, 1961, giving, taking, or demanding dowry is illegal and punishable by up to 3 years of imprisonment and fines.",
|
| 15 |
+
"What are a woman's rights after marriage? -> A woman has rights to residence, maintenance, dignity, and protection against domestic violence under Indian law.",
|
| 16 |
+
|
| 17 |
+
# Divorce
|
| 18 |
+
"How can a woman file for divorce? -> A woman can file for divorce on grounds such as cruelty, adultery, desertion, conversion, mental disorder, and mutual consent under the Hindu Marriage Act, 1955.",
|
| 19 |
+
"Can a divorced woman claim maintenance? -> Yes, under Section 125 of the Criminal Procedure Code (CrPC), a divorced woman can claim maintenance if she cannot maintain herself.",
|
| 20 |
+
|
| 21 |
+
# Domestic Violence
|
| 22 |
+
"What protections does a woman have under the Domestic Violence Act? -> The Protection of Women from Domestic Violence Act, 2005 grants women the right to protection orders, residence, monetary relief, and child custody.",
|
| 23 |
+
"What is domestic violence under Indian law? -> Domestic violence includes physical, emotional, sexual, verbal, and economic abuse against a woman within the family.",
|
| 24 |
+
|
| 25 |
+
# Workplace Rights
|
| 26 |
+
"What rights do women have at the workplace? -> Under the POSH Act, 2013, women have the right to a safe working environment and can file complaints about sexual harassment at the workplace.",
|
| 27 |
+
"How can a woman report sexual harassment at work? -> She can file a complaint with the Internal Complaints Committee (ICC) established under the POSH Act, 2013.",
|
| 28 |
+
|
| 29 |
+
# Property and Inheritance
|
| 30 |
+
"Can daughters inherit ancestral property? -> Yes, under the Hindu Succession (Amendment) Act, 2005, daughters have equal rights to ancestral property as sons.",
|
| 31 |
+
"What happens to a woman's property after marriage? -> A woman's property remains solely hers after marriage unless she chooses to transfer or gift it voluntarily.",
|
| 32 |
+
|
| 33 |
+
# Maternity Benefits
|
| 34 |
+
"What are a woman's rights during pregnancy at work? -> Under the Maternity Benefit Act, 1961, a woman is entitled to 26 weeks of paid maternity leave for the first two children.",
|
| 35 |
+
"Is it legal to fire a woman during maternity leave? -> No, dismissing a woman during maternity leave is illegal under the Maternity Benefit Act.",
|
| 36 |
+
|
| 37 |
+
# Police and Legal Assistance
|
| 38 |
+
"How can a woman file an FIR for harassment? -> A woman can directly approach a police station to file an FIR. As per Supreme Court guidelines, no woman can be denied lodging an FIR.",
|
| 39 |
+
"Can a woman file an online police complaint? -> Yes, many Indian states offer online FIR and grievance redressal portals where women can file complaints remotely.",
|
| 40 |
+
|
| 41 |
+
# Sexual Offenses and Safety
|
| 42 |
+
"What are the laws against sexual assault in India? -> Sections 354, 354A, 354B, 354C, and 354D of IPC deal with outraging a woman's modesty, harassment, stalking, and voyeurism.",
|
| 43 |
+
"Is marital rape recognized in Indian law? -> As of now, marital rape is not fully criminalized under Indian law except under certain circumstances like child marriage or separation cases.",
|
| 44 |
+
|
| 45 |
+
# Child Marriage
|
| 46 |
+
"Is child marriage illegal in India? -> Yes, under the Prohibition of Child Marriage Act, 2006, marriage below 18 years (for girls) and 21 years (for boys) is illegal and punishable.",
|
| 47 |
+
|
| 48 |
+
# Human Trafficking and Forced Labour
|
| 49 |
+
"What protections exist against human trafficking? -> The Immoral Traffic (Prevention) Act, 1956 criminalizes trafficking of women and girls for prostitution.",
|
| 50 |
+
"Can women working as domestic helpers claim protection? -> Yes, domestic workers are entitled to protection under labor laws and state-level welfare boards.",
|
| 51 |
+
|
| 52 |
+
# Cyber Crimes
|
| 53 |
+
"What are a woman's rights against cyberbullying? -> A woman facing online abuse, threats, or revenge porn can report under the Information Technology Act, 2000 and IPC Section 66A, 67, 67A.",
|
| 54 |
+
"How can a woman report cybercrime? -> She can file a complaint at the nearest Cyber Crime Cell or use the online portal: cybercrime.gov.in.",
|
| 55 |
+
|
| 56 |
+
# Financial Rights
|
| 57 |
+
"Are women entitled to joint property after marriage? -> In case of divorce, women can claim maintenance and residential rights but automatic rights over husband's property depend on various factors.",
|
| 58 |
+
"Do women have banking rights without husband/family permission? -> Yes, adult women have the full legal right to operate independent bank accounts and investments.",
|
| 59 |
+
|
| 60 |
+
# Widow Rights
|
| 61 |
+
"What are a widow's rights to husband's property? -> A widow has a rightful share in her deceased husband's property under the Hindu Succession Act, 1956.",
|
| 62 |
+
|
| 63 |
+
# Child Custody
|
| 64 |
+
"What are a woman's rights regarding child custody after divorce? -> Custody decisions are based on the child's welfare. Generally, mothers are granted custody for minor children unless proven unfit.",
|
| 65 |
+
|
| 66 |
+
# Protection Against Acid Attacks
|
| 67 |
+
"What legal protections exist against acid attacks? -> Acid attacks are punishable under IPC Sections 326A and 326B with imprisonment up to 10 years or life, along with fines.",
|
| 68 |
+
"Can acid attack victims get compensation? -> Yes, acid attack victims are entitled to free medical treatment and compensation under Supreme Court rulings and state schemes.",
|
| 69 |
+
|
| 70 |
+
# Reproductive Rights
|
| 71 |
+
"Can a woman legally terminate a pregnancy? -> Under the Medical Termination of Pregnancy (MTP) Act, 1971, a woman can legally terminate a pregnancy up to 24 weeks under certain conditions.",
|
| 72 |
+
|
| 73 |
+
# Miscellaneous
|
| 74 |
+
"Do women have the right to equal pay? -> Yes, under the Equal Remuneration Act, 1976, women must be paid equally to men for the same work.",
|
| 75 |
+
"Can a woman demand workplace creche facilities? -> Under the Maternity Benefit (Amendment) Act, 2017, organizations with more than 50 employees must provide creche facilities."
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# Step 2: Encode Knowledge Base with SentenceTransformer
|
| 80 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 81 |
+
embeddings = model.encode(legal_texts)
|
| 82 |
+
|
| 83 |
+
# Step 3: Create FAISS Index for fast retrieval
|
| 84 |
+
dimension = embeddings.shape[1]
|
| 85 |
+
index = faiss.IndexFlatL2(dimension)
|
| 86 |
+
index.add(np.array(embeddings))
|
| 87 |
+
|
| 88 |
+
# Step 4: Load a Lightweight LLM for Answer Generation
|
| 89 |
+
generator = pipeline('text-generation', model='tiiuae/falcon-7b-instruct', max_new_tokens=100)
|
| 90 |
+
|
| 91 |
+
# Step 5: Retrieval + Generation Logic (RAG)
|
| 92 |
+
def empowerbot_answer(user_query):
|
| 93 |
+
# Encode user query
|
| 94 |
+
query_embedding = model.encode([user_query])
|
| 95 |
+
|
| 96 |
+
# Search FAISS index for the most relevant document
|
| 97 |
+
D, I = index.search(np.array(query_embedding), k=1) # Get top-1 match
|
| 98 |
+
matched_text = legal_texts[I[0][0]]
|
| 99 |
+
|
| 100 |
+
# Build prompt for LLM
|
| 101 |
+
prompt = f"You are an expert legal assistant for women. Based on the following law: {matched_text} \n\nAnswer the user's question: {user_query}"
|
| 102 |
+
|
| 103 |
+
# Generate answer using the LLM
|
| 104 |
+
output = generator(prompt)[0]['generated_text']
|
| 105 |
+
|
| 106 |
+
return output
|
| 107 |
+
|
| 108 |
+
# Step 6: Feedback Loop (User Ratings)
|
| 109 |
+
def feedback_loop(user_query, correct_answer):
|
| 110 |
+
# Store the user's query and the correct answer for manual feedback
|
| 111 |
+
with open('feedback_data.txt', 'a') as f:
|
| 112 |
+
f.write(f"Query: {user_query}\nCorrect Answer: {correct_answer}\n\n")
|
| 113 |
+
|
| 114 |
+
print("Feedback received, thank you for helping improve EmpowerBot!")
|
| 115 |
+
|
| 116 |
+
# Step 7: Gradio UI for Chatbot Interface
|
| 117 |
+
def chatbot_interface(user_input):
|
| 118 |
+
answer = empowerbot_answer(user_input)
|
| 119 |
+
return answer
|
| 120 |
+
|
| 121 |
+
# Feedback UI (for rating answers)
|
| 122 |
+
def rate_answer(user_input, correct_answer):
|
| 123 |
+
feedback_loop(user_input, correct_answer)
|
| 124 |
+
return "Thank you for your feedback!"
|
| 125 |
+
|
| 126 |
+
# Gradio Interface (Main Chatbot + Feedback)
|
| 127 |
+
iface = gr.Interface(fn=chatbot_interface,
|
| 128 |
+
inputs=gr.Textbox(label="Ask your question about women's legal rights"),
|
| 129 |
+
outputs=gr.Textbox(label="EmpowerBot's Answer"),
|
| 130 |
+
title="EmpowerBot - Women's Legal Assistant",
|
| 131 |
+
description="Ask EmpowerBot about women's rights and get reliable legal information.")
|
| 132 |
+
|
| 133 |
+
feedback_iface = gr.Interface(fn=rate_answer,
|
| 134 |
+
inputs=[gr.Textbox(label="Your Query"), gr.Textbox(label="Correct Answer")],
|
| 135 |
+
outputs="text",
|
| 136 |
+
title="Provide Feedback",
|
| 137 |
+
description="Help improve EmpowerBot by providing feedback on answers.")
|
| 138 |
+
|
| 139 |
+
# Launch both interfaces
|
| 140 |
+
iface.launch()#main chatbot interface
|
| 141 |
+
feedback_iface.launch(share=True) # Feedback interface for user ratings
|