In [1]:
##Step 1: Import Libraries
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC # Corrected from SVMClassifier
import random

SVC.png

In [2]:
##Step 2: Create Training Data (Legal Topics)

"""We train AI with example legal questions"""
texts = [
    # Greetings
    "Hi",
    "Hello",
    "Good morning",

    # Bail
    "What is bail?",
    "How to apply for bail?",
    "Types of bail in India?",
    "Anticipatory bail meaning",

    # FIR
    "What is FIR?",
    "How to file FIR?",
    "Can police refuse FIR?",
    "Online FIR process",

    # IPC
    "What is IPC?",
    "IPC section 420 meaning",
    "Punishment under IPC 302",
    "Explain IPC 498A",

    # Goodbye
    "Bye",
    "Thank you"
]

labels = [
    "greet","greet","greet",
    "bail","bail","bail","bail",
    "fir","fir","fir","fir",
    "ipc","ipc","ipc","ipc",
    "bye","bye"
]
In [3]:
###tep 3: Convert Text to Numbers (TF-IDF)
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(texts)
In [4]:
##Step 4: Train Model
from sklearn.svm import SVC # Corrected import for Support Vector Classifier
##model = LogisticRegression()
model = SVC()
model.fit(X, labels)
Out[4]:
SVC()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.
In [5]:
###Step 5: Create Legal Knowledge Base (Responses)
responses = {
    "greet": ["Hello! I am your Legal Assistant.",
              "Hi! Ask me about Bail, FIR or IPC."],

    "bail": ["Bail is temporary release of an accused person.",
             "Types of bail: Regular bail, Anticipatory bail, Interim bail."],

    "fir": ["FIR stands for First Information Report.",
            "Police must register FIR for cognizable offenses."],

    "ipc": ["IPC stands for Indian Penal Code.",
            "IPC Section 302 deals with murder.", "IPC Section 420 deals with cheating."],

    "bye": ["Goodbye! Stay informed.",
            "Thank you for using Legal Assistant."]
}
In [6]:
##Step 6: Chat Loop
while True:
    user = input("You: ")

    if user.lower() == "exit":
        print("Legal AI: Goodbye!")
        break

    test = vectorizer.transform([user])
    intent = model.predict(test)[0]

    print("Legal AI:", random.choice(responses[intent]))

    """ EXPECTED OUTPUT

    You: IPC
Legal AI: IPC stands for Indian Penal Code.
You: What is IPC 420?
Legal AI: IPC Section 420 deals with cheating.
You: exit
Legal AI: Goodbye!

"""
You: ipc
Legal AI: IPC stands for Indian Penal Code.
You: rti
Legal AI: Types of bail: Regular bail, Anticipatory bail, Interim bail.
You: fir
Legal AI: FIR stands for First Information Report.
You: bail
Legal AI: Bail is temporary release of an accused person.
You: what is ipc 420
Legal AI: IPC Section 420 deals with cheating.
You: exit
Legal AI: Goodbye!
In [ ]: