veerukhannan commited on
Commit
7e1e0a5
·
verified ·
1 Parent(s): 7540753

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -42
app.py CHANGED
@@ -17,7 +17,6 @@ client = OpenAI(
17
  )
18
 
19
  print("Starting Nyaya-Mitra initialization...")
20
- print(f"Current working directory: {os.getcwd()}")
21
 
22
  # Initialize document processor
23
  try:
@@ -52,16 +51,16 @@ def get_mistral_response(query: str, context: str, max_retries: int = 3) -> str:
52
  return cached_response
53
 
54
  system_prompt = """You are Nyaya-Mitra (न्याय-मित्र), a helpful legal assistant for the Indian justice system.
55
- Your role is to assist the public in understanding and navigating the new criminal laws (BNS, BNSS, and BSA).
56
- When providing information:
57
- 1. Be precise and cite specific sections when possible
58
- 2. Explain legal concepts in simple terms
59
- 3. Suggest relevant documents or next steps
60
- 4. For case-specific queries, provide general guidance while noting you cannot provide legal advice
61
- 5. If asked about document drafting, explain the required documents and their purpose
62
 
63
- Base your responses on the provided context and maintain a professional yet approachable tone.
64
- Always format your responses in a clear, readable manner using markdown."""
 
 
 
 
 
 
65
 
66
  for attempt in range(max_retries):
67
  try:
@@ -79,17 +78,17 @@ def get_mistral_response(query: str, context: str, max_retries: int = 3) -> str:
79
  except Exception as e:
80
  if "rate limit" in str(e).lower():
81
  if attempt < max_retries - 1:
82
- wait_time = (attempt + 1) * 2 # Exponential backoff
83
  time.sleep(wait_time)
84
  continue
85
  return f"""I apologize, but I'm currently experiencing high demand. Please try:
86
  1. Waiting a few moments before asking another question
87
- 2. Rephrasing your question
88
- 3. Breaking down complex questions into simpler ones
89
 
90
  Error details: {str(e)}"""
91
 
92
- return "I apologize, but I'm currently unable to process your request due to high demand. Please try again in a few moments."
93
 
94
  def format_sources(metadatas: List[Dict]) -> str:
95
  """Format source information for display"""
@@ -107,10 +106,7 @@ def chat_interface(message: str, history: List[List[str]]) -> str:
107
  try:
108
  if doc_processor is None:
109
  return """I apologize, but I'm having trouble accessing the legal documents.
110
- This might be due to initialization issues. Please try:
111
- 1. Refreshing the page
112
- 2. Checking if the PDF files are properly uploaded
113
- 3. Waiting a few moments and trying again"""
114
 
115
  # Search for relevant context
116
  results = doc_processor.search_documents(message)
@@ -128,9 +124,12 @@ def chat_interface(message: str, history: List[List[str]]) -> str:
128
 
129
  return formatted_response
130
  except Exception as e:
131
- return f"""I apologize, but I encountered an error: {str(e)}.
132
- Please try again or rephrase your question. If the problem persists,
133
- try refreshing the page."""
 
 
 
134
 
135
  # Create Gradio interface
136
  iface = gr.ChatInterface(
@@ -143,32 +142,23 @@ Your AI Legal Assistant for India's New Criminal Laws:
143
  - 📋 Bharatiya Nagarik Suraksha Sanhita (BNSS)
144
  - ⚖️ Bharatiya Sakshya Adhiniyam (BSA)
145
 
146
- I can help you:
147
- 1. 📖 Understand specific sections and provisions
148
- 2. Learn about your legal rights
149
- 3. 📝 Get guidance on required documents
150
- 4. 🔍 Navigate legal procedures
151
- 5. 💡 Get explanations in simple terms
152
-
153
- Ask me any questions about these new criminal laws!
154
-
155
- *Note: This is an AI assistant meant for information purposes only. For legal advice, please consult qualified legal professionals.*
156
 
157
- **Usage Tips:**
158
- - Ask one question at a time
159
- - Be specific in your questions
160
- - Wait a few moments between questions
161
- - Refresh the page if you encounter any issues""",
162
  theme="soft",
163
  examples=[
164
- "What are the key changes in the new criminal laws?",
165
- "How does BNS handle digital crimes?",
166
- "What documents do I need to file an FIR under BNSS?",
167
- "How is electronic evidence handled under BSA?",
168
- "What are the provisions for mob lynching in BNS?"
169
  ]
170
  )
171
 
172
  # Launch the interface
173
  if __name__ == "__main__":
174
- iface.launch()
 
17
  )
18
 
19
  print("Starting Nyaya-Mitra initialization...")
 
20
 
21
  # Initialize document processor
22
  try:
 
51
  return cached_response
52
 
53
  system_prompt = """You are Nyaya-Mitra (न्याय-मित्र), a helpful legal assistant for the Indian justice system.
54
+ Provide concise, accurate responses based on the context provided. Focus on the most relevant information.
 
 
 
 
 
 
55
 
56
+ Guidelines:
57
+ 1. Be precise and cite specific sections
58
+ 2. Explain concepts simply
59
+ 3. Suggest next steps if applicable
60
+ 4. Note that you cannot provide legal advice
61
+ 5. Keep responses focused and relevant
62
+
63
+ Format responses clearly using markdown."""
64
 
65
  for attempt in range(max_retries):
66
  try:
 
78
  except Exception as e:
79
  if "rate limit" in str(e).lower():
80
  if attempt < max_retries - 1:
81
+ wait_time = (attempt + 1) * 2
82
  time.sleep(wait_time)
83
  continue
84
  return f"""I apologize, but I'm currently experiencing high demand. Please try:
85
  1. Waiting a few moments before asking another question
86
+ 2. Making your question more specific
87
+ 3. Breaking down your question into smaller parts
88
 
89
  Error details: {str(e)}"""
90
 
91
+ return "I apologize, but I'm currently unable to process your request. Please try again in a few moments."
92
 
93
  def format_sources(metadatas: List[Dict]) -> str:
94
  """Format source information for display"""
 
106
  try:
107
  if doc_processor is None:
108
  return """I apologize, but I'm having trouble accessing the legal documents.
109
+ Please try refreshing the page or waiting a moment."""
 
 
 
110
 
111
  # Search for relevant context
112
  results = doc_processor.search_documents(message)
 
124
 
125
  return formatted_response
126
  except Exception as e:
127
+ return f"""I apologize, but I encountered an error. Please try:
128
+ 1. Making your question more specific
129
+ 2. Waiting a moment and trying again
130
+ 3. Refreshing the page
131
+
132
+ Error: {str(e)}"""
133
 
134
  # Create Gradio interface
135
  iface = gr.ChatInterface(
 
142
  - 📋 Bharatiya Nagarik Suraksha Sanhita (BNSS)
143
  - ⚖️ Bharatiya Sakshya Adhiniyam (BSA)
144
 
145
+ **Tips for Best Results:**
146
+ 1. Ask specific, focused questions
147
+ 2. One topic at a time
148
+ 3. Wait briefly between questions
149
+ 4. Use simple, clear language
 
 
 
 
 
150
 
151
+ *Note: This is an AI assistant for information only. For legal advice, please consult qualified legal professionals.*""",
 
 
 
 
152
  theme="soft",
153
  examples=[
154
+ "What are the main provisions for cybercrime in BNS?",
155
+ "How do I file a complaint under BNSS?",
156
+ "What is the process for electronic evidence under BSA?",
157
+ "What are the key changes in mob lynching laws?",
158
+ "How does BNS define organized crime?"
159
  ]
160
  )
161
 
162
  # Launch the interface
163
  if __name__ == "__main__":
164
+ iface.launch()