File size: 7,797 Bytes
5bf4b6c
 
 
 
4241674
 
5bf4b6c
 
 
 
 
 
 
 
 
 
4241674
 
 
 
 
 
 
 
 
5bf4b6c
 
4241674
5bf4b6c
4241674
 
 
 
5bf4b6c
4241674
 
5bf4b6c
4241674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bf4b6c
4241674
 
 
 
 
 
 
 
 
 
 
 
 
5bf4b6c
 
 
 
 
 
 
 
4241674
 
5bf4b6c
4241674
 
 
 
 
 
 
5bf4b6c
 
 
4241674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bf4b6c
 
 
4241674
 
 
 
5bf4b6c
 
 
4241674
 
5bf4b6c
 
 
4241674
5bf4b6c
4241674
 
5bf4b6c
 
 
 
4241674
 
5bf4b6c
4241674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bf4b6c
 
 
 
 
4241674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bf4b6c
 
 
4241674
 
5bf4b6c
4241674
 
 
 
 
 
 
 
 
 
 
 
5bf4b6c
 
 
 
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
import gradio as gr
from datasets import load_dataset
import pandas as pd
import random
from sentence_transformers import SentenceTransformer, util
import torch

# Load the Indian Law dataset
print("Loading Indian Law Dataset...")
ds = load_dataset("viber1/indian-law-dataset")

# Convert to pandas for easier manipulation
df = pd.DataFrame(ds['train'])
print(f"Dataset loaded successfully with {len(df)} entries")
print(f"Dataset columns: {df.columns.tolist()}")

# Load semantic search model
print("Loading sentence-transformers model for semantic search...")
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')

# Precompute embeddings for the dataset
print("Computing embeddings for dataset...")
df['combined_text'] = df.apply(lambda row: ' '.join([str(val) for val in row.values if pd.notna(val) and isinstance(val, str)]), axis=1)
corpus_embeddings = model.encode(df['combined_text'].tolist(), convert_to_tensor=True, show_progress_bar=True)
print("Embeddings computed successfully!")

def search_legal_info(question):
    """Search the dataset for relevant legal information using semantic search"""
    if not question or len(question.strip()) == 0:
        return "⚠️ Please enter a legal question to search."
    
    # Encode the query
    query_embedding = model.encode(question, convert_to_tensor=True)
    
    # Compute cosine similarity scores
    cos_scores = util.cos_sim(query_embedding, corpus_embeddings)[0]
    
    # Get top 5 results
    top_results = torch.topk(cos_scores, k=min(5, len(df)))
    
    # Format the response with best match first
    response = "# πŸ” Search Results\n\n"
    
    for i, (score, idx) in enumerate(zip(top_results.values, top_results.indices)):
        result = df.iloc[idx.item()].to_dict()
        similarity = score.item()
        
        # Skip low relevance results
        if similarity < 0.2:
            continue
            
        response += f"## πŸ“Œ Result {i+1} (Relevance: {similarity*100:.1f}%)\n\n"
        
        for key, value in result.items():
            if key == 'combined_text':  # Skip internal field
                continue
            if value and isinstance(value, str) and len(value.strip()) > 0:
                # Clean and format the text
                display_value = value.strip()
                if len(display_value) > 800:
                    display_value = display_value[:800] + "..."
                response += f"**{key.replace('_', ' ').title()}:**\n\n{display_value}\n\n"
        
        response += "---\n\n"
    
    if "Result 1" not in response:
        return "❌ No relevant information found in the dataset. Please try rephrasing your question or use different keywords."
    
    return response

def get_random_sample():
    """Get a random entry from the dataset"""
    random_idx = random.randint(0, len(df) - 1)
    sample = df.iloc[random_idx]
    
    response = "# πŸ“ Random Legal Information\n\n"
    
    for key, value in sample.items():
        if key == 'combined_text':  # Skip internal field
            continue
        if value and isinstance(value, str) and len(value.strip()) > 0:
            display_value = value.strip()
            if len(display_value) > 800:
                display_value = display_value[:800] + "..."
            response += f"**{key.replace('_', ' ').title()}:**\n\n{display_value}\n\n"
    
    return response

def handle_feedback(question, feedback_type):
    """Handle user feedback"""
    return f"βœ… Thank you for your {feedback_type}! Your input helps us improve the system."

# Example questions
EXAMPLE_QUESTIONS = [
    "Can a plaint be amended after it has been filed in a civil case in India?",
    "What are the provisions for bail under Indian law?",
    "What are the rights of an accused person in India?",
    "How can property rights be transferred in India?",
    "What is the procedure for filing a divorce petition?",
    "What are the provisions related to consumer protection?",
    "What are the penalties for copyright infringement in India?",
]

# Create Gradio interface with improved UI
with gr.Blocks(title="Indian Law Q&A Assistant", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # πŸ›οΈ Indian Law Q&A Assistant
    
    ### ⚠️ IMPORTANT DISCLAIMER
    **This application is for informational purposes only and does NOT constitute legal advice.**
    The information provided is based on a dataset and should not be relied upon for legal decisions.
    Always consult with a qualified legal professional for specific legal matters.
    
    ---
    
    Welcome to the Indian Law Question-Answer Assistant powered by semantic search technology
    and the `viber1/indian-law-dataset`. Ask questions and get relevant legal information instantly!
    """)
    
    with gr.Row():
        with gr.Column(scale=2):
            question_input = gr.Textbox(
                label="πŸ’¬ Your Legal Question",
                placeholder="Type your legal question here...",
                lines=3
            )
            
            with gr.Row():
                search_btn = gr.Button("πŸ” Search", variant="primary", size="lg")
                random_btn = gr.Button("🎲 Random Sample", size="lg")
            
            gr.Markdown("### πŸ“‹ Example Questions (Click to use):")
            
            with gr.Row():
                example_btns = []
                for example in EXAMPLE_QUESTIONS[:4]:
                    btn = gr.Button(example, size="sm")
                    example_btns.append(btn)
            
            with gr.Row():
                for example in EXAMPLE_QUESTIONS[4:]:
                    btn = gr.Button(example, size="sm")
                    example_btns.append(btn)
    
    output_box = gr.Markdown(label="πŸ“„ Response", value="Enter a question above and click Search to begin.")
    
    with gr.Row():
        gr.Markdown("""
        ### πŸ“’ Feedback
        Found this helpful? Have suggestions? Click below:
        """)
    
    with gr.Row():
        helpful_btn = gr.Button("πŸ‘ Helpful", size="sm")
        report_btn = gr.Button("πŸ“ Report Issue", size="sm")
    
    feedback_output = gr.Markdown(visible=False)
    
    # Button actions
    search_btn.click(fn=search_legal_info, inputs=question_input, outputs=output_box)
    random_btn.click(fn=get_random_sample, inputs=None, outputs=output_box)
    
    # Example button actions
    for i, btn in enumerate(example_btns):
        btn.click(
            fn=lambda ex=EXAMPLE_QUESTIONS[i]: ex,
            inputs=None,
            outputs=question_input
        )
    
    # Feedback actions
    helpful_btn.click(
        fn=lambda q: handle_feedback(q, "positive feedback"),
        inputs=question_input,
        outputs=feedback_output
    ).then(lambda: gr.update(visible=True), outputs=feedback_output)
    
    report_btn.click(
        fn=lambda q: handle_feedback(q, "report"),
        inputs=question_input,
        outputs=feedback_output
    ).then(lambda: gr.update(visible=True), outputs=feedback_output)
    
    gr.Markdown("""
    ---
    
    ### πŸ“Š Dataset Information
    - **Source**: viber1/indian-law-dataset on Hugging Face
    - **Total Entries**: """ + str(len(df)) + """
    - **Search Method**: Semantic search using sentence-transformers
    - **Model**: sentence-transformers/all-MiniLM-L6-v2
    
    ### πŸ”§ Features
    - βœ… Semantic search for better relevance
    - βœ… Results ranked by similarity score
    - βœ… Clean, readable Markdown formatting
    - βœ… Example questions for quick start
    - βœ… Random exploration of dataset
    - βœ… User feedback mechanism
    
    *Built with ❀️ using Gradio, Hugging Face Datasets, and Sentence Transformers*
    """)

if __name__ == "__main__":
    demo.launch()