LegalOSS181.8ktracked
Legal Research & Search · Retrieval & RAG · Legal AI & NLP

LawLens

Ranjith00005/LawLens

AI-powered legal research assistant providing legal advisory, case outcome prediction, and report generation from PDF documents using FastAPI, Streamlit, and Groq LLM.

LawLens ⚖️

LawLens is an AI-powered legal research and document analysis system that helps users analyze legal documents, explore case outcomes, and generate structured legal reports.

The system uses Retrieval-Augmented Generation (RAG) to retrieve relevant sections from uploaded PDF documents before sending them to an LLM. This helps keep the generated responses grounded in the content of the provided documents.


🚀 Features

📄 Legal Advisory

Upload one or more legal documents and ask a question about them.

The system:

  • Extracts text from uploaded PDFs
  • Splits the text into smaller chunks
  • Creates vector embeddings
  • Stores the embeddings using FAISS
  • Retrieves the most relevant document sections
  • Generates a legal analysis using an LLM

The response includes:

  1. Legal Analysis
  2. Available Options
  3. Recommendations
  4. Potential Risks

⚖️ Case Outcome Prediction

Analyze a legal document and receive an AI-generated assessment of the possible case outcome.

The system provides:

  1. Likelihood Assessment
  2. Key Factors Influencing the Outcome
  3. Potential Scenarios
  4. Recommendations for Improving the Chances of a Favorable Outcome

Note: The prediction is an AI-generated assessment based on the provided document and should not be considered a guaranteed legal outcome.


📑 Legal Report Generator

Generate a structured legal report from uploaded documents and a user query.

The generated report contains:

  1. Executive Summary
  2. Key Findings
  3. Legal Analysis
  4. Recommendations
  5. Conclusion

🧠 How It Works

LawLens follows a Retrieval-Augmented Generation (RAG) pipeline.

                 User
                  │
                  ▼
        Upload Legal PDF(s)
                  │
                  ▼
           PDF Text Extraction
               (PyPDF2)
                  │
                  ▼
            Text Chunking
       (RecursiveCharacterTextSplitter)
                  │
                  ▼
          Generate Embeddings
     (all-MiniLM-L6-v2)
                  │
                  ▼
            FAISS Vector DB
                  │
                  ▼
       Similarity Search (Top 6)
                  │
                  ▼
       Relevant Document Sections
                  │
                  ▼
          Groq LLM (GPT-OSS 120B)
                  │
                  ▼
        Generated Legal Response

🛠️ Tech Stack

Backend

  • Python
  • FastAPI
  • Uvicorn

AI / LLM

  • Groq API
  • GPT-OSS 120B
  • LangChain Groq

Retrieval-Augmented Generation

  • FAISS
  • Hugging Face Sentence Transformers
  • sentence-transformers/all-MiniLM-L6-v2
  • RecursiveCharacterTextSplitter

Document Processing

  • PyPDF2

API & Configuration

  • FastAPI File Uploads
  • CORS Middleware
  • Python-dotenv

📁 Project Structure

LawLens/
│
├── backend/
│   ├── main.py
│   ├── .env
│   └── requirements.txt
│
├── frontend/
│   └── ...
│
└── README.md

The exact folder structure may vary depending on your project setup.


⚙️ Installation

1. Clone the Repository

git clone https://github.com/Ranjith00005/LawLens.git
cd LawLens

2. Create a Virtual Environment

python -m venv venv

Activate it:

macOS / Linux

source venv/bin/activate

Windows

venv\Scripts\activate

3. Install Dependencies

pip install -r requirements.txt

If you haven't created requirements.txt, the main dependencies used by the backend are:

fastapi
uvicorn
langchain-groq
langchain-text-splitters
langchain-community
sentence-transformers
faiss-cpu
PyPDF2
python-dotenv
python-multipart

🔑 Environment Variables

Create a .env file in the backend directory:

GROQ_API_KEY=your_groq_api_key

Replace your_groq_api_key with your Groq API key.

Do not commit your .env file to GitHub.

Add it to .gitignore:

.env
venv/
__pycache__/

▶️ Running the Backend

Start the FastAPI server with:

uvicorn main:app --reload

Or run the Python file directly:

python main.py

The API will be available at:

http://localhost:8000

FastAPI automatically provides interactive API documentation at:

http://localhost:8000/docs

🔌 API Endpoints

1. Legal Advisory

POST /legal-advisory/

Input

  • query — User's legal question
  • files — One or more PDF documents

Example

Query:
"What are the possible legal remedies available to the plaintiff?"

Response

{
  "result": "Legal analysis..."
}

2. Case Outcome Prediction

POST /case-outcome-prediction/

Input

  • query — Case-related question
  • files — Legal PDF documents

Response

{
  "prediction": "Likelihood assessment..."
}

3. Legal Report Generator

POST /report-generator/

Input

  • query — Report requirements
  • files — Legal PDF documents

Response

{
  "report": "Generated legal report..."
}

🔍 RAG Implementation

LawLens uses Retrieval-Augmented Generation instead of directly passing the entire document to the language model.

Step 1 — PDF Extraction

Uploaded PDFs are processed using PyPDF2.

pdf_reader = PyPDF2.PdfReader(io.BytesIO(content))

Text is extracted from each page.


Step 2 — Text Chunking

The extracted text is divided into smaller sections.

CHUNK_SIZE = 700
CHUNK_OVERLAP = 150

A 150-character overlap helps preserve context between neighboring chunks.


Step 3 — Embeddings

Each text chunk is converted into a vector representation using:

sentence-transformers/all-MiniLM-L6-v2

Step 4 — FAISS Vector Database

The embeddings are stored in a FAISS vector database.

vector_db = FAISS.from_texts(chunks, embedding_model)

Step 5 — Similarity Search

When the user asks a question, LawLens searches the vector database for the most relevant sections.

RETRIEVAL_TOP_K = 6

The top six relevant chunks are retrieved.


Step 6 — LLM Generation

The retrieved content and user query are provided to the Groq-hosted LLM.

chat = ChatGroq(
    model="openai/gpt-oss-120b",
    api_key=groq_api_key
)

The model then generates the final response based on the retrieved document content.


📌 Configuration

The main RAG parameters can be adjusted in the backend:

CHUNK_SIZE = 700
CHUNK_OVERLAP = 150
RETRIEVAL_TOP_K = 6

Chunk Size

Controls the amount of text contained in each document chunk.

Chunk Overlap

Maintains some repeated context between neighboring chunks.

Retrieval Top K

Controls how many relevant chunks are passed to the LLM.


🛡️ Error Handling

LawLens handles several document-processing issues, including:

  • Empty user queries
  • PDFs with no extractable text
  • Image-based PDFs
  • AI processing errors

For example, if a PDF contains no extractable text, the API returns an error indicating that OCR or a text-based PDF may be required.


🔐 Security Considerations

For production deployment, additional security measures should be implemented.

Recommended improvements include:

  • Restricting CORS origins
  • Validating uploaded file types
  • Limiting PDF file sizes
  • Adding authentication and authorization
  • Protecting API keys
  • Adding rate limiting
  • Sanitizing user input
  • Using secure production deployment settings

🔮 Future Improvements

Potential future enhancements include:

  • OCR support for scanned legal documents
  • Persistent vector database storage
  • Citation and source highlighting
  • Legal case-law database integration
  • Multi-document comparison
  • Document summarization
  • User authentication
  • Conversation history
  • Improved legal-domain prompting
  • Cloud deployment
  • Streaming AI responses

⚠️ Disclaimer

LawLens is an AI-assisted legal research and document analysis tool.

It is intended for educational and research purposes and does not replace advice from a qualified legal professional. AI-generated analysis and predictions may contain errors and should be independently verified.


👨‍💻 Author

Ranjith Rameshbabu

GitHub: https://github.com/Ranjith00005/LawLens


⭐ Project Highlights

  • AI-powered legal document analysis
  • Retrieval-Augmented Generation (RAG)
  • Semantic document search
  • FAISS vector similarity search
  • Hugging Face embeddings
  • Groq LLM integration
  • REST API using FastAPI
  • Multi-PDF document processing
  • Automated legal report generation
  • Case outcome assessment