|
import streamlit as st |
|
import torch |
|
from transformers import AutoTokenizer, AutoModelForSequenceClassification |
|
|
|
|
|
model_name = "ipc_refined_approach_model" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForSequenceClassification.from_pretrained(model_name) |
|
|
|
|
|
sections = ['465', '395', '332', '353', '467'] |
|
|
|
|
|
with open("labels.txt", "w") as f: |
|
f.write("\n".join(sections)) |
|
|
|
|
|
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
model = model.to(device) |
|
|
|
|
|
def predict_text(text): |
|
|
|
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512) |
|
|
|
|
|
inputs = {key: value.to(device) for key, value in inputs.items()} |
|
|
|
|
|
model.eval() |
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
|
|
|
|
probs = torch.sigmoid(logits).detach().cpu().numpy() |
|
|
|
|
|
predictions = {section: int(prob > 0.5) for section, prob in zip(sections, probs[0])} |
|
|
|
|
|
sections_belongs_to = [section for section, pred in predictions.items() if pred == 1] |
|
if sections_belongs_to: |
|
st.write(f"This case belongs to Section(s): **{', '.join(sections_belongs_to)}**") |
|
else: |
|
st.write("This case does not belong to any known section.") |
|
|
|
return predictions |
|
with open("style.css") as f: |
|
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True) |
|
|
|
st.title("Legal Section Classification for FIR") |
|
st.write("Enter the text for case classification:") |
|
|
|
|
|
sample_text = st.text_area("Case Description", "") |
|
|
|
|
|
if st.button("Classify Case"): |
|
predictions = predict_text(sample_text) |
|
st.write(predictions) |
|
|