Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 4 |
+
|
| 5 |
+
# Load the model and tokenizer (make sure your model is correctly loaded here)
|
| 6 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 7 |
+
|
| 8 |
+
# Specify the model path
|
| 9 |
+
model_name = "ipc_refined_approach_model" # Replace with your actual model path or name
|
| 10 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
| 11 |
+
model = model.to(device)
|
| 12 |
+
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 14 |
+
|
| 15 |
+
# Define your legal sections
|
| 16 |
+
sections = ['465', '467', '395', '332','353'] # Example sections, modify as per your actual list
|
| 17 |
+
|
| 18 |
+
# Streamlit UI setup
|
| 19 |
+
st.title("Legal Case Section Prediction")
|
| 20 |
+
|
| 21 |
+
# Get input text from user
|
| 22 |
+
st.subheader("Enter the legal text to predict the sections it belongs to:")
|
| 23 |
+
input_text = st.text_area("Input Text", height=250)
|
| 24 |
+
|
| 25 |
+
# Prediction function
|
| 26 |
+
def predict_text(text):
|
| 27 |
+
# Tokenize and encode input text
|
| 28 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
|
| 29 |
+
|
| 30 |
+
# Move inputs to the same device as the model
|
| 31 |
+
inputs = {key: value.to(device) for key, value in inputs.items()}
|
| 32 |
+
|
| 33 |
+
# Perform inference
|
| 34 |
+
model.eval()
|
| 35 |
+
with torch.no_grad():
|
| 36 |
+
outputs = model(**inputs)
|
| 37 |
+
logits = outputs.logits # Ensure logits are accessed correctly
|
| 38 |
+
|
| 39 |
+
# Apply sigmoid to get probabilities
|
| 40 |
+
probs = torch.sigmoid(logits).detach().cpu().numpy() # Move to CPU for processing
|
| 41 |
+
|
| 42 |
+
# Convert probabilities to binary predictions (threshold 0.5)
|
| 43 |
+
predictions = {section: int(prob > 0.5) for section, prob in zip(sections, probs[0])}
|
| 44 |
+
|
| 45 |
+
# Return the sections the case belongs to
|
| 46 |
+
sections_belongs_to = [section for section, pred in predictions.items() if pred == 1]
|
| 47 |
+
return sections_belongs_to
|
| 48 |
+
|
| 49 |
+
# Show results if input text is provided
|
| 50 |
+
if input_text:
|
| 51 |
+
st.subheader("Prediction Results")
|
| 52 |
+
|
| 53 |
+
# Get predictions for the input text
|
| 54 |
+
predicted_sections = predict_text(input_text)
|
| 55 |
+
|
| 56 |
+
# Show predictions
|
| 57 |
+
if predicted_sections:
|
| 58 |
+
st.write(f"This case belongs to Section(s): {', '.join(predicted_sections)}")
|
| 59 |
+
else:
|
| 60 |
+
st.write("This case does not belong to any known section.")
|
| 61 |
+
else:
|
| 62 |
+
st.write("Please enter some text to predict the sections.")
|