File size: 13,917 Bytes
4780a80 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
import gradio as gr
import asyncio
from datetime import datetime
from specter_legal_assistant.rag_manager import rag_manager
from specter_legal_assistant.config import settings
from specter_legal_assistant.utils import format_response_for_gradio
from pathlib import Path
import json
async def legal_query(query: str, language: str = "english") -> str:
"""Handle legal queries through RAG system"""
try:
if not query.strip():
return "Please enter a legal question."
# Get context from RAG system
context = rag_manager.get_relevant_context(query, k=3)
# Prepare prompt
prompt = f"""You are a helpful legal assistant providing clear, practical advice about Indian law. Provide a natural, empathetic response that directly addresses their specific situation.\n\nUser's Question: {query}\n\nLegal Information:\n{context}\n\nProvide a clear, practical response that explains their legal options and applicable laws in natural language."""
# Use Groq client for response generation
from specter_legal_assistant import groq_client
response = groq_client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[
{"role": "system", "content": "You are a helpful legal assistant that provides clear, accurate information about Indian law."},
{"role": "user", "content": prompt}
],
max_tokens=500,
temperature=0.7,
)
result = response.choices[0].message.content
return format_response_for_gradio(result)
except Exception as e:
return f"I apologize, but I encountered an error while processing your request. Please try again or rephrase your question."
def generate_fir(name: str, location: str, details: str) -> str:
"""Generate FIR document"""
try:
from specter_legal_assistant import generate_fir_pdf
filename = generate_fir_pdf(name, location, details)
file_path = Path("static") / filename
return str(file_path.absolute())
except Exception as e:
return f"Error generating FIR: {str(e)}"
def generate_rental_agreement(landlord_name: str, tenant_name: str, property_address: str,
rent_amount: float, security_deposit: float,
lease_start_date: str, lease_end_date: str,
terms_and_conditions: str) -> str:
"""Generate Rental Agreement document"""
try:
from specter_legal_assistant import RentalAgreementData, generate_rental_agreement
data = RentalAgreementData(
landlord_name=landlord_name,
tenant_name=tenant_name,
property_address=property_address,
rent_amount=rent_amount,
security_deposit=security_deposit,
lease_start_date=lease_start_date,
lease_end_date=lease_end_date,
terms_and_conditions=terms_and_conditions
)
filename = generate_rental_agreement(data)
file_path = Path("static") / filename
return str(file_path.absolute())
except Exception as e:
return f"Error generating Rental Agreement: {str(e)}"
def generate_consumer_complaint(complainant_name: str, complainant_address: str,
complainant_contact: str, company_name: str,
company_address: str, product_service_details: str,
complaint_details: str, desired_resolution: str) -> str:
"""Generate Consumer Complaint document"""
try:
from specter_legal_assistant import ConsumerComplaintData, generate_consumer_complaint
data = ConsumerComplaintData(
complainant_name=complainant_name,
complainant_address=complainant_address,
complainant_contact=complainant_contact,
company_name=company_name,
company_address=company_address,
product_service_details=product_service_details,
complaint_details=complaint_details,
desired_resolution=desired_resolution
)
filename = generate_consumer_complaint(data)
file_path = Path("static") / filename
return str(file_path.absolute())
except Exception as e:
return f"Error generating Consumer Complaint: {str(e)}"
def create_gradio_interface():
# Create the Gradio interface with improved design
with gr.Blocks(
title="Legal Assistant AI",
theme=gr.themes.Soft(),
css="""
.gradio-container {
max-width: 1200px !important;
margin: auto !important;
}
.main-header {
text-align: center;
padding: 20px;
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
color: white;
border-radius: 10px;
margin-bottom: 20px;
}
"""
) as interface:
# Header
gr.HTML("""
<div class="main-header">
<h1>ποΈ Legal Assistant AI</h1>
<p>Get instant legal advice and generate legal documents for Indian law</p>
</div>
""")
with gr.Tabs():
# Legal Query Tab
with gr.Tab("π€ Ask Legal Questions"):
with gr.Row():
with gr.Column(scale=2):
query_input = gr.Textbox(
label="Enter your legal question",
lines=4,
placeholder="Example: What are my rights if I'm arrested by the police?"
)
language_dropdown = gr.Dropdown(
choices=["english", "hindi"],
value="english",
label="Language"
)
query_button = gr.Button("Get Legal Advice", variant="primary", size="lg")
with gr.Column(scale=1):
gr.Markdown("""
### π‘ Tips for better responses:
- Be specific about your situation
- Mention relevant details (location, circumstances)
- Ask about specific laws or procedures
- Include any relevant dates or events
""")
query_output = gr.Textbox(
label="Legal Advice",
lines=8,
interactive=False,
show_copy_button=True
)
query_button.click(
fn=lambda q, l: asyncio.run(legal_query(q, l)),
inputs=[query_input, language_dropdown],
outputs=query_output
)
# FIR Generation Tab
with gr.Tab("π Generate FIR"):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("### First Information Report (FIR) Generator")
fir_name = gr.Textbox(
label="Your Full Name",
placeholder="Enter your complete name"
)
fir_location = gr.Textbox(
label="Location of Incident",
placeholder="Where did the incident occur?"
)
fir_details = gr.Textbox(
label="Incident Details",
lines=4,
placeholder="Describe what happened in detail..."
)
fir_button = gr.Button("Generate FIR Document", variant="primary")
with gr.Column(scale=1):
gr.Markdown("""
### π FIR Information:
- Used to report criminal offenses
- Filed at the nearest police station
- Required for legal proceedings
- Contains complainant and incident details
""")
fir_output = gr.File(label="Download Generated FIR", file_count="single")
fir_button.click(
fn=generate_fir,
inputs=[fir_name, fir_location, fir_details],
outputs=fir_output
)
# Rental Agreement Tab
with gr.Tab("π Rental Agreement"):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("### Rental Agreement Generator")
landlord_name = gr.Textbox(label="Landlord Name", placeholder="Enter landlord's full name")
tenant_name = gr.Textbox(label="Tenant Name", placeholder="Enter tenant's full name")
property_address = gr.Textbox(
label="Property Address",
lines=2,
placeholder="Complete address of the rental property"
)
rent_amount = gr.Number(label="Monthly Rent Amount (βΉ)", precision=2)
security_deposit = gr.Number(label="Security Deposit Amount (βΉ)", precision=2)
lease_start = gr.Textbox(label="Lease Start Date (DD-MM-YYYY)", placeholder="01-01-2024")
lease_end = gr.Textbox(label="Lease End Date (DD-MM-YYYY)", placeholder="31-12-2024")
terms = gr.Textbox(
label="Terms and Conditions",
lines=3,
placeholder="Additional terms and conditions..."
)
rental_button = gr.Button("Generate Rental Agreement", variant="primary")
with gr.Column(scale=1):
gr.Markdown("""
### π Rental Agreement Info:
- Legal contract between landlord and tenant
- Defines rent, deposit, and lease terms
- Protects both parties' rights
- Required for rental disputes
""")
rental_output = gr.File(label="Download Rental Agreement", file_count="single")
rental_button.click(
fn=generate_rental_agreement,
inputs=[landlord_name, tenant_name, property_address,
rent_amount, security_deposit, lease_start,
lease_end, terms],
outputs=rental_output
)
# Consumer Complaint Tab
with gr.Tab("π Consumer Complaint"):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("### Consumer Complaint Generator")
comp_name = gr.Textbox(label="Your Name", placeholder="Enter your full name")
comp_address = gr.Textbox(
label="Your Address",
lines=2,
placeholder="Your complete address"
)
comp_contact = gr.Textbox(label="Your Contact Number", placeholder="+91-XXXXXXXXXX")
company_name = gr.Textbox(label="Company Name", placeholder="Name of the company you're complaining against")
company_addr = gr.Textbox(
label="Company Address",
lines=2,
placeholder="Company's address"
)
product_details = gr.Textbox(label="Product/Service Details", placeholder="What product or service are you complaining about?")
complaint_details = gr.Textbox(
label="Complaint Details",
lines=3,
placeholder="Describe your complaint in detail..."
)
resolution = gr.Textbox(
label="Desired Resolution",
lines=2,
placeholder="What resolution do you want?"
)
complaint_button = gr.Button("Generate Consumer Complaint", variant="primary")
with gr.Column(scale=1):
gr.Markdown("""
### π‘οΈ Consumer Rights:
- Right to safety and quality
- Right to information
- Right to choose
- Right to redressal
- Right to be heard
""")
complaint_output = gr.File(label="Download Consumer Complaint", file_count="single")
complaint_button.click(
fn=generate_consumer_complaint,
inputs=[comp_name, comp_address, comp_contact,
company_name, company_addr, product_details,
complaint_details, resolution],
outputs=complaint_output
)
return interface
if __name__ == "__main__":
interface = create_gradio_interface()
interface.launch(
server_name=settings.GRADIO_SERVER_NAME,
server_port=settings.GRADIO_SERVER_PORT,
share=settings.GRADIO_SHARE
)
|