Spaces:
Sleeping
Sleeping
| import os | |
| import chromadb | |
| from chromadb.utils import embedding_functions | |
| # Configuration | |
| CHROMA_PATH = "data/chroma_db" | |
| COLLECTION_NAME = "nyaya_legal_docs" | |
| def get_vector_collection(): | |
| client = chromadb.PersistentClient(path=CHROMA_PATH) | |
| embedding_func = embedding_functions.DefaultEmbeddingFunction() | |
| return client.get_or_create_collection( | |
| name=COLLECTION_NAME, | |
| embedding_function=embedding_func | |
| ) | |
| def search_legal_documents(query: str): | |
| """ | |
| Search across the indexed legal documents (Gazettes, Circulars, etc.) using semantic search. | |
| Args: | |
| query: The natural language query to search for in local documents. | |
| """ | |
| try: | |
| collection = get_vector_collection() | |
| results = collection.query( | |
| query_texts=[query], | |
| n_results=3 | |
| ) | |
| if results['documents']: | |
| return "\n\n".join(results['documents'][0]) | |
| return "No relevant local documents found." | |
| except Exception as e: | |
| return f"Error searching local documents: {e}" | |
| import os | |
| import requests | |
| def search_indian_kanoon(query: str): | |
| """ | |
| Search for legal cases, acts, and statutes on Indian Kanoon (Online). | |
| """ | |
| token = os.getenv("INDIANKANOON_API_KEY") | |
| if not token: | |
| return f"Search result placeholder for: {query}. (API Token missing in .env. Using internal model knowledge.)" | |
| try: | |
| headers = { | |
| "Authorization": f"Token {token}", | |
| "Accept": "application/json" | |
| } | |
| params = {"formInput": query, "pagenum": 0} | |
| # Searching for the query - Indian Kanoon requires POST with params in the URL | |
| response = requests.post( | |
| f"https://api.indiankanoon.org/search/", | |
| headers=headers, | |
| params=params | |
| ) | |
| data = response.json() | |
| if "docs" in data: | |
| results = data["docs"][:3] | |
| summary = "Found these cases on Indian Kanoon:\n" | |
| for res in results: | |
| summary += f"- {res['title']} (Source: {res.get('docsource', 'Unknown')}, ID: {res['tid']})\n" | |
| return summary | |
| return "No results found on Indian Kanoon." | |
| except Exception as e: | |
| return f"Error connecting to Indian Kanoon: {e}" | |
| def get_act_details(act_name: str, section: str = None): | |
| """ | |
| Retrieve details of a specific Indian Act or Section. | |
| Args: | |
| act_name: Name of the Act (e.g., 'IPC', 'DPDPA'). | |
| section: Specific section number (optional). | |
| """ | |
| return f"Details for {act_name} Section {section if section else 'All'}. (Data retrieval pending)" | |