Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from openai import OpenAI
|
4 |
+
from add_embeddings import LegalDocumentProcessor
|
5 |
+
import json
|
6 |
+
from typing import Dict, List
|
7 |
+
import markdown
|
8 |
+
|
9 |
+
# Initialize clients and models
|
10 |
+
client = OpenAI(
|
11 |
+
api_key=os.environ.get("MISTRAL_API_KEY"),
|
12 |
+
base_url="https://api.mistral.ai/v1"
|
13 |
+
)
|
14 |
+
|
15 |
+
# Initialize document processor
|
16 |
+
doc_processor = LegalDocumentProcessor()
|
17 |
+
|
18 |
+
def get_mistral_response(query: str, context: str) -> str:
|
19 |
+
"""Get response from Mistral AI"""
|
20 |
+
system_prompt = """You are a helpful legal assistant for the Indian justice system.
|
21 |
+
Your role is to assist the public in understanding and navigating the new criminal laws (BNS, BNSS, and BSA).
|
22 |
+
When providing information:
|
23 |
+
1. Be precise and cite specific sections when possible
|
24 |
+
2. Explain legal concepts in simple terms
|
25 |
+
3. Suggest relevant documents or next steps
|
26 |
+
4. For case-specific queries, provide general guidance while noting you cannot provide legal advice
|
27 |
+
5. If asked about document drafting, explain the required documents and their purpose
|
28 |
+
|
29 |
+
Base your responses on the provided context and maintain a professional yet approachable tone."""
|
30 |
+
|
31 |
+
try:
|
32 |
+
response = client.chat.completions.create(
|
33 |
+
model="mistral-medium",
|
34 |
+
messages=[
|
35 |
+
{"role": "system", "content": system_prompt},
|
36 |
+
{"role": "user", "content": f"Context: {context}\n\nQuestion: {query}"}
|
37 |
+
]
|
38 |
+
)
|
39 |
+
return response.choices[0].message.content
|
40 |
+
except Exception as e:
|
41 |
+
return f"Error: {str(e)}"
|
42 |
+
|
43 |
+
def format_sources(metadatas: List[Dict]) -> str:
|
44 |
+
"""Format source information for display"""
|
45 |
+
sources = []
|
46 |
+
for metadata in metadatas:
|
47 |
+
law_code = metadata["law_code"]
|
48 |
+
source = metadata["source"]
|
49 |
+
sources.append(f"- {law_code} ({source})")
|
50 |
+
return "\n".join(sources)
|
51 |
+
|
52 |
+
def chat_interface(message: str, history: List[List[str]]) -> str:
|
53 |
+
"""Main chat interface function"""
|
54 |
+
# Search for relevant context
|
55 |
+
results = doc_processor.search_documents(message)
|
56 |
+
context = "\n".join(results["documents"])
|
57 |
+
|
58 |
+
# Get response from Mistral
|
59 |
+
response = get_mistral_response(message, context)
|
60 |
+
|
61 |
+
# Add source information
|
62 |
+
sources = format_sources(results["metadatas"])
|
63 |
+
full_response = f"{response}\n\n**Sources:**\n{sources}"
|
64 |
+
|
65 |
+
# Format response with markdown
|
66 |
+
formatted_response = markdown.markdown(full_response)
|
67 |
+
|
68 |
+
return formatted_response
|
69 |
+
|
70 |
+
# Initialize ChromaDB with legal documents
|
71 |
+
doc_processor.process_and_store_documents()
|
72 |
+
|
73 |
+
# Create Gradio interface
|
74 |
+
iface = gr.ChatInterface(
|
75 |
+
fn=chat_interface,
|
76 |
+
title="Indian Legal Assistant",
|
77 |
+
description="""Welcome to the Indian Legal Assistant! I can help you understand:
|
78 |
+
- Bharatiya Nyaya Sanhita (BNS)
|
79 |
+
- Bharatiya Nagarik Suraksha Sanhita (BNSS)
|
80 |
+
- Bharatiya Sakshya Adhiniyam (BSA)
|
81 |
+
|
82 |
+
I can:
|
83 |
+
1. Explain specific sections and provisions
|
84 |
+
2. Help understand your legal rights
|
85 |
+
3. Suggest relevant documents for your case
|
86 |
+
4. Guide you through legal procedures
|
87 |
+
5. Assist with document requirements
|
88 |
+
|
89 |
+
Ask me any questions about these new criminal laws.""",
|
90 |
+
theme="soft",
|
91 |
+
examples=[
|
92 |
+
"What are the key changes in the new criminal laws?",
|
93 |
+
"How does BNS handle digital crimes?",
|
94 |
+
"What documents do I need to file an FIR under BNSS?",
|
95 |
+
"How is electronic evidence handled under BSA?",
|
96 |
+
"What are the provisions for mob lynching in BNS?"
|
97 |
+
]
|
98 |
+
)
|
99 |
+
|
100 |
+
# Launch the interface
|
101 |
+
if __name__ == "__main__":
|
102 |
+
iface.launch()
|