--- title: Indian Legal AI Assistant emoji: 🏛️ colorFrom: blue colorTo: indigo sdk: gradio sdk_version: 4.44.1 python_version: "3.10" app_file: app.py pinned: false --- # 🏛️ Indian Legal AI Assistant An AI-powered Indian legal information assistant built for Hugging Face Spaces. This Space combines: - a local GGUF language model running through `llama-cpp-python` - integrated India Code lookup from https://www.indiacode.nic.in/ - HTML text extraction - PDF text extraction - OCR fallback for scanned PDFs - streamed responses for faster display - recent chat history for follow-up questions --- ## What this app does The assistant answers questions about Indian laws, Acts, sections, rules, legal procedures, and related follow-up questions. For each user question, the app automatically searches and reads relevant material from **India Code**, then uses that retrieved context to generate a detailed answer. The user does not need to run a separate search step. --- ## Primary legal source The integrated web lookup is designed to use India Code as the primary legal source: https://www.indiacode.nic.in/ India Code is used to retrieve legal material such as Acts, sections, rules, regulations, notifications, orders, ordinances, subordinate legislation, and related legal documents where available. --- ## Model used This Space uses the following Hugging Face model repository: ```text invincibleambuj/Ambuj-Tripathi-Indian-Legal-Llama-GGUF ```` The specific GGUF file used by the app is: ```text llama-3.2-1b-instruct.Q4_K_M.gguf ``` Model runtime: ```text llama-cpp-python ``` The app is configured for CPU-only inference. --- ## Key features ### India Code integrated lookup The app searches and reads content from India Code inside the response flow. It checks India Code pages such as: * homepage navigation * Central Acts browse pages * State Acts links discovered from India Code navigation * Repealed Acts pages * Spent Acts pages * India Code document links * India Code PDF links The retrieved India Code context is injected into the model prompt before generation. --- ### PDF support The app supports legal documents in PDF format. It first tries normal embedded-text extraction using `pypdf`. If the PDF appears scanned or contains too little extractable text, the app falls back to OCR using: * `PyMuPDF` * `pytesseract` * `Tesseract OCR` OCR is intentionally limited by default to keep the app usable on CPU Spaces. --- ### Follow-up questions The app keeps recent chat history so users can ask follow-up questions. For example: ```text User: Explain the Right to Information Act. User: What are the penalties under it? User: Who can file an appeal? ``` The assistant uses recent conversation context while also checking India Code again for the current question. --- ### Streaming responses Responses are streamed token-by-token, so users can start reading while the model is still generating. This improves perceived speed, especially on CPU-only Spaces. --- ## Example questions You can ask questions such as: ```text What is the current status of Section 377 under Indian law? ``` ```text Explain the Bharatiya Nyaya Sanhita in detail. ``` ```text What are the grounds for divorce under the Hindu Marriage Act? ``` ```text Find the latest India Code position on the Right to Information Act. ``` ```text What does the Indian Contract Act say about agreements without consideration? ``` ```text Explain the limitation period for filing a civil suit. ``` --- ## Files required This Space needs the following files: ```text app.py requirements.txt packages.txt README.md ``` --- ## requirements.txt ```txt gradio==4.44.1 gradio-client==1.3.0 huggingface-hub==0.34.3 llama-cpp-python==0.2.90 pydantic==2.10.6 fastapi==0.115.2 starlette==0.40.0 requests>=2.31.0 beautifulsoup4>=4.12.0 pypdf>=4.2.0 pymupdf>=1.24.0 pytesseract>=0.3.10 pillow>=10.0.0 ``` --- ## packages.txt ```txt tesseract-ocr tesseract-ocr-eng ``` --- ## Recommended README YAML header The top of this README should contain: ```yaml --- title: Indian Legal AI Assistant emoji: 🏛️ colorFrom: blue colorTo: indigo sdk: gradio sdk_version: 4.44.1 python_version: "3.10" app_file: app.py pinned: false --- ``` Important: keep `"3.10"` in quotes. Without quotes, YAML can interpret `3.10` as `3.1`. --- ## CPU configuration The app is configured for CPU-only inference. Default settings in `app.py`: ```text N_CTX=4096 N_THREADS=2 N_THREADS_BATCH=2 N_BATCH=512 MAX_TOKENS=1200 MAX_CONTEXT_DOCS=6 MAX_TEXT_PER_DOC=3000 MAX_OCR_PAGES=3 ``` These defaults aim to balance: * detailed responses * reasonable CPU performance * context-window safety * PDF/OCR capability --- ## Performance notes The first user query may be slower because the app may need to: 1. download the GGUF model, 2. load the model into memory, 3. search India Code, 4. read HTML or PDF documents, 5. run OCR if a PDF is scanned. After the model is loaded, later responses are usually faster unless the Space restarts. OCR can be slower on CPU because scanned PDFs must be rendered page-by-page and passed through Tesseract. --- ## Persistent storage recommendation For faster restarts, attach persistent storage to the Hugging Face Space and set the model cache path to persistent storage. For example, in `app.py`: ```python MODEL_CACHE_DIR = os.getenv("MODEL_CACHE_DIR", "/data/models") ``` Only use `/data/models` after persistent storage has been attached to the Space. --- ## Limitations This app is designed to be robust, but there are practical limits: * local CPU inference is slower than GPU inference * OCR is slower than normal PDF text extraction * scanned PDFs may contain OCR recognition errors * the local model has a finite context window * very long user inputs or very large legal documents must be trimmed before generation * India Code pages or documents may occasionally be unavailable or difficult to parse * the assistant should not be treated as a lawyer The app includes prompt trimming to prevent context-window crashes. --- ## Legal disclaimer This assistant provides general legal information only. It does not provide formal legal advice. It does not create a lawyer-client relationship. For specific legal disputes, filings, rights, liabilities, criminal matters, contracts, property matters, family law issues, or court proceedings, users should consult a qualified legal professional. --- ## Developer Developed by [Rohan R](https://rotsl.github.io). ``` ```