File size: 10,340 Bytes
6c643b6
 
 
4c62205
6c643b6
4c62205
6c643b6
 
 
4c62205
6c643b6
 
 
4c62205
6c643b6
4c62205
6c643b6
 
 
 
4c62205
6c643b6
 
 
 
4c62205
 
6c643b6
 
 
 
 
 
 
4c62205
6c643b6
4c62205
6c643b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36d3a2e
6c643b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36d3a2e
6c643b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c62205
 
 
6c643b6
4c62205
 
 
 
6c643b6
 
 
 
 
 
 
 
 
 
f2ad939
 
 
 
6c643b6
 
 
 
 
 
 
 
 
 
 
 
 
4c62205
6c643b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from chromadb_semantic_search_for_dataset import search_cases, chat_with_cases

# Global variables to store current context
current_cases_context = ""
current_user_case = ""

def search_and_store_cases(query):
    """Search for cases and store context for chat"""
    global current_cases_context, current_user_case
    if not query.strip():
        return "कृपया केस खोज्नका लागि केही लेख्नुहोस्।", ""
    
    formatted_results, context, user_case = search_cases(query)
    current_cases_context = context
    current_user_case = user_case
    return formatted_results, ""

def chat_function(message, history):
    """Main chat function that uses current case context"""
    global current_cases_context, current_user_case
    
    if not message.strip():
        return history, ""
    
    # Generate response using current case context and user's case
    bot_response = chat_with_cases(message, current_cases_context, current_user_case)
    
    # Add to chat history
    history.append([message, bot_response])
    return history, ""

def clear_context():
    """Clear the stored case context"""
    global current_cases_context, current_user_case
    current_cases_context = ""
    current_user_case = ""
    return "केस कन्टेक्स्ट सफा गरियो। अब तपाईं सामान्य च्याट गर्न सक्नुहुन्छ।", [], ""

# Custom CSS
css = """
.gradio-container {
    max-width: 1200px !important;
    margin: auto !important;
}
.main-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 20px;
    border-radius: 10px;
    text-align: center;
    margin-bottom: 20px;
}
.section-header {
    background: #f8f9fa;
    padding: 15px;
    border-radius: 8px;
    border-left: 4px solid #007bff;
    margin: 10px 0;
}
.chat-container {
    border: 2px solid #e9ecef;
    border-radius: 10px;
    padding: 10px;
}
.search-container {
    border: 2px solid #e9ecef;
    border-radius: 10px;
    padding: 10px;
    background: #f8f9fa;
}
"""

# Create the main interface
with gr.Blocks(css=css, title="नेपाली कानूनी सहायक - Legal RAG System") as demo:
    
    # Header
    gr.HTML("""
    <div class="main-header">
        <h1>⚖️ नेपाली कानूनी सहायक</h1>
        <h2>Nepali Legal Assistant with RAG</h2>
        <p><strong>आफ्नो मुद्दा खोज्नुहोस् र कानूनी सहायताका लागि च्याट गर्नुहोस्</strong></p>
        <p><em>Search for your case and chat for legal assistance</em></p>
    </div>
    """)
    
    with gr.Row():
        # Left Column - Case Search
        with gr.Column(scale=1):
            gr.HTML('<div class="section-header"><h3 style="color: #333;">🔍 केस खोज्नुहोस् (Case Search)</h3></div>')
            
            with gr.Group():
                search_input = gr.Textbox(
                    label="आफ्नो मुद्दाको विवरण लेख्नुहोस्",
                    placeholder="जस्तै: सम्पत्ति विवाद, हत्या मुद्दा, आर्थिक कसूर...",
                    lines=3
                )
                
                with gr.Row():
                    search_btn = gr.Button("🔍 खोज्नुहोस्", variant="primary", scale=2)
                    clear_btn = gr.Button("🗑️ सफा गर्नुहोस्", variant="secondary", scale=1)
                
                search_results = gr.Textbox(
                    label="समान मुद्दाहरू (Similar Cases)",
                    lines=12,
                    max_lines=15,
                    interactive=False
                )
                
                gr.Examples(
                    examples=[
                        ["जमीन जग्गाको विवाद"],
                        ["घरेलु हिंसा"],
                        ["ऋण असुली"],
                        ["हत्या मुद्दा"],
                        ["आर्थिक ठगी"],
                        ["चोरी मुद्दा"],
                        ["बलात्कार मुद्दा"],
                        ["भ्रष्टाचार मुद्दा"]
                    ],
                    inputs=search_input,
                    label="उदाहरणहरू (Examples)"
                )
        
        # Right Column - Chat Interface  
        with gr.Column(scale=1):
            gr.HTML('<div class="section-header"><h3 style="color: #333;">💬 कानूनी सहायता च्याट (Legal Chat)</h3></div>')
            
            with gr.Group():
                chatbot_ui = gr.Chatbot(
                    label="च्याट",
                    height=400,
                    show_copy_button=True,
                    bubble_full_width=False
                )
                
                with gr.Row():
                    msg_input = gr.Textbox(
                        placeholder="आफ्नो प्रश्न सोध्नुहोस्...",
                        show_label=False,
                        scale=4,
                        lines=1
                    )
                    send_btn = gr.Button("📤 पठाउनुहोस्", scale=1, variant="primary")
                
                with gr.Row():
                    clear_chat_btn = gr.Button("🗑️ च्याट सफा गर्नुहोस्", variant="secondary", scale=1)
                    clear_context_btn = gr.Button("🔄 कन्टेक्स्ट रिसेट गर्नुहोस्", variant="secondary", scale=1)
                
                context_status = gr.Textbox(
                    label="स्थिति (Status)",
                    value="सामान्य च्याट मोड - कुनै केस कन्टेक्स्ट छैन",
                    interactive=False,
                    lines=1
                )
                
                gr.Examples(
                    examples=[
                        ["मेरो केसमा के फैसला हुन सक्छ?"],
                        ["यी केसहरूसँग मेरो मुद्दाको समानता के छ?"],
                        ["मेरो केसको भिन्नता के छ?"],
                        ["यस्ता मुद्दामा कति समय लाग्छ?"],
                        ["के प्रमाणहरू चाहिन्छ?"],
                        ["मेरो केसमा जित्ने सम्भावना कति छ?"],
                        ["यी फैसलाहरूबाट के सिक्न सकिन्छ?"],
                        ["अदालती प्रक्रिया कस्तो हुन्छ?"]
                    ],
                    inputs=msg_input,
                    label="च्याट उदाहरणहरू (Chat Examples)"
                )
    
    # Information Section
    gr.HTML("""
    <div style="background: #e8f4fd; padding: 20px; border-radius: 10px; margin: 20px 0;">
        <h3 style="color: #0066cc;">📋 कसरी प्रयोग गर्ने (How to Use)</h3>
        <ol style="color: #333;">
            <li style="color: #333;"><strong style="color: #333;">केस खोज्नुहोस्:</strong> बाँया भागमा आफ्नो मुद्दाको विवरण लेखेर खोज्नुहोस्</li>
            <li style="color: #333;"><strong style="color: #333;">परिणाम हेर्नुहोस्:</strong> ५ वटा समान मुद्दाहरू देखाउनेछ</li>
            <li style="color: #333;"><strong style="color: #333;">च्याट गर्नुहोस्:</strong> दाँया भागमा ती मुद्दाहरूको बारेमा प्रश्न सोध्नुहोस्</li>
            <li style="color: #333;"><strong style="color: #333;">सामान्य च्याट:</strong> केस नखोजीकन पनि कानूनी प्रश्न सोध्न सक्नुहुन्छ</li>
        </ol>
        <p style="color: #d32f2f; font-weight: bold;">
            ⚠️ चेतावनी: यो केवल जानकारीमूलक सहायता हो। वास्तविक कानूनी सल्लाहका लागि योग्य वकिलसँग सल्लाह लिनुहोस्।
        </p>
    </div>
    """)
    
    # Event handlers
    search_btn.click(
        fn=search_and_store_cases,
        inputs=[search_input],
        outputs=[search_results, context_status]
    ).then(
        fn=lambda: "तपाईंको केस र समान केसहरूको कन्टेक्स्ट लोड भयो। अब तुलनाका लागि च्याट गर्न सक्नुहुन्छ।",
        outputs=[context_status]
    )
    
    clear_btn.click(
        fn=lambda: ("", ""),
        outputs=[search_input, search_results]
    )
    
    msg_input.submit(
        chat_function,
        inputs=[msg_input, chatbot_ui],
        outputs=[chatbot_ui, msg_input]
    )
    
    send_btn.click(
        chat_function,
        inputs=[msg_input, chatbot_ui],
        outputs=[chatbot_ui, msg_input]
    )
    
    clear_chat_btn.click(
        lambda: [],
        outputs=[chatbot_ui]
    )
    
    clear_context_btn.click(
        clear_context,
        outputs=[context_status, chatbot_ui, msg_input]
    )

if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )