File size: 10,007 Bytes
5c0a82c
0f93e9d
5c0a82c
0f93e9d
cf6c44b
0f93e9d
8f0f58a
 
 
 
 
 
5c0a82c
80e6314
 
 
 
cf6c44b
 
 
 
5c0a82c
9949f77
0f93e9d
 
 
9949f77
0f93e9d
 
9949f77
0f93e9d
 
9949f77
0f93e9d
9949f77
0f93e9d
9949f77
0f93e9d
 
 
 
9949f77
0f93e9d
5c0a82c
 
 
0f93e9d
 
5c0a82c
 
 
 
 
9949f77
0f93e9d
 
 
 
9949f77
0f93e9d
 
9949f77
90dc8fa
0f93e9d
9949f77
0f93e9d
9949f77
90dc8fa
 
 
 
 
0f93e9d
9949f77
5c0a82c
0f93e9d
9949f77
5c0a82c
847ac0b
3ea5339
 
e486f13
 
 
 
 
 
 
9949f77
847ac0b
 
 
 
e486f13
 
 
 
 
9949f77
 
847ac0b
 
 
80e6314
 
 
847ac0b
80e6314
847ac0b
80e6314
847ac0b
80e6314
 
 
e486f13
3ea5339
9949f77
 
3ea5339
0f93e9d
9949f77
0f93e9d
 
 
 
9949f77
5c0a82c
c05e788
9949f77
c05e788
 
 
 
 
 
9949f77
c05e788
 
9949f77
c05e788
 
9949f77
0f93e9d
8f0f58a
 
 
 
 
0f93e9d
 
8f0f58a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f93e9d
8f0f58a
 
 
 
 
 
 
 
 
 
0f93e9d
8f0f58a
 
 
 
0f93e9d
8f0f58a
 
44051d3
8f0f58a
 
 
 
 
 
 
 
 
 
44051d3
8f0f58a
 
 
 
 
 
 
 
 
 
 
 
 
 
5c0a82c
9949f77
5c0a82c
9949f77
c05e788
9949f77
0f93e9d
cf6c44b
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
import gradio as gr
import os
from huggingface_hub import InferenceClient
from config.constants import DEFAULT_SYSTEM_MESSAGE
from config.settings import DEFAULT_MODEL, HF_TOKEN
from src.knowledge_base.vector_store import create_vector_store, load_vector_store
from web.training_interface import (
    get_models_df,
    generate_chat_analysis,
    register_model_action,
    start_finetune_action
)

if not HF_TOKEN:
    raise ValueError("HUGGINGFACE_TOKEN not found in environment variables")

# Initialize HF client with token
client = InferenceClient(
    DEFAULT_MODEL,
    token=HF_TOKEN
)

# State for storing context
context_store = {}

def get_context(message, conversation_id):
    """Get context from knowledge base"""
    vector_store = load_vector_store()
    if vector_store is None:
        return "Knowledge base not found. Please create it first."
    
    try:
        # Extract context
        context_docs = vector_store.similarity_search(message, k=3)
        context_text = "\n\n".join([f"From {doc.metadata.get('source', 'unknown')}: {doc.page_content}" for doc in context_docs])
        
        # Save context for this conversation
        context_store[conversation_id] = context_text
        
        return context_text
    except Exception as e:
        print(f"Error getting context: {str(e)}")
        return ""

def respond(
    message,
    history,
    conversation_id,
    system_message,
    max_tokens,
    temperature,
    top_p,
):
    # Create ID for new conversation
    if not conversation_id:
        import uuid
        conversation_id = str(uuid.uuid4())
    
    # Get context from knowledge base
    context = get_context(message, conversation_id)
    
    # Convert history from Gradio format to OpenAI format
    messages = [{"role": "system", "content": system_message}]
    if context:
        messages[0]["content"] += f"\n\nContext for response:\n{context}"
    
    # Convert history to OpenAI format
    for user_msg, assistant_msg in history:
        messages.extend([
            {"role": "user", "content": user_msg},
            {"role": "assistant", "content": assistant_msg}
        ])
    
    # Add current user message
    messages.append({"role": "user", "content": message})
    
    # Send API request and stream response
    response = ""
    is_complete = False
    
    try:
        for chunk in client.chat_completion(
            messages,
            max_tokens=max_tokens,
            stream=True,
            temperature=temperature,
            top_p=top_p,
        ):
            # Check for finish_reason in chunk
            if hasattr(chunk.choices[0], 'finish_reason') and chunk.choices[0].finish_reason is not None:
                is_complete = True
                break
                
            token = chunk.choices[0].delta.content
            if token:
                response += token
                yield [(message, response)], conversation_id

        # Save history if response is complete
        if is_complete or response:  # add response check as fallback
            messages.append({"role": "assistant", "content": response})
            try:
                from src.knowledge_base.dataset import DatasetManager
                from config.settings import HF_TOKEN
                
                dataset = DatasetManager(token=HF_TOKEN)  # Explicitly pass the token
                success, msg = dataset.save_chat_history(conversation_id, messages)
                print(f"Chat history save attempt: {success}, Message: {msg}")  # Add debug log
                if not success:
                    print(f"Failed to save chat history: {msg}")
            except Exception as e:
                import traceback
                print(f"Exception while saving chat history: {str(e)}")
                print(traceback.format_exc())  # Print full traceback for debugging
            
    except Exception as e:
        print(f"Error generating response: {str(e)}")
        yield [(message, "An error occurred while generating the response.")], conversation_id

def build_kb():
    """Function to create knowledge base"""
    try:
        success, message = create_vector_store()
        return message
    except Exception as e:
        return f"Error creating knowledge base: {str(e)}"

def load_vector_store():
    """Load knowledge base from dataset"""
    try:
        from src.knowledge_base.dataset import DatasetManager
        dataset = DatasetManager()
        success, store = dataset.download_vector_store()
        if success:
            return store
        print(f"Error loading knowledge base: {store}")
        return None
    except Exception as e:
        print(f"Error loading knowledge base: {str(e)}")
        return None

# Create interface
with gr.Blocks() as demo:
    with gr.Tabs():
        with gr.Tab("Chat"):
            gr.Markdown("# ⚖️ Status Law Assistant")
            
            conversation_id = gr.State(None)
            
            with gr.Row():
                with gr.Column(scale=3):
                    chatbot = gr.Chatbot(
                        label="Chat",
                        bubble_full_width=False,
                        avatar_images=["user.png", "assistant.png"]  # optional
                    )
                    
                    with gr.Row():
                        msg = gr.Textbox(
                            label="Your question",
                            placeholder="Enter your question...",
                            scale=4
                        )
                        submit_btn = gr.Button("Send", variant="primary")
                
                with gr.Column(scale=1):
                    gr.Markdown("### Knowledge Base Management")
                    build_kb_btn = gr.Button("Create/Update Knowledge Base", variant="primary")
                    kb_status = gr.Textbox(label="Knowledge Base Status", interactive=False)
                    
                    gr.Markdown("### Generation Settings")
                    max_tokens = gr.Slider(
                        minimum=1, 
                        maximum=2048, 
                        value=512, 
                        step=1, 
                        label="Maximum Response Length",
                        info="Limits the number of tokens in response. More tokens = longer response"
                    )
                    temperature = gr.Slider(
                        minimum=0.1, 
                        maximum=2.0, 
                        value=0.7, 
                        step=0.1, 
                        label="Temperature",
                        info="Controls creativity. Lower value = more predictable responses"
                    )
                    top_p = gr.Slider(
                        minimum=0.1, 
                        maximum=1.0, 
                        value=0.95, 
                        step=0.05, 
                        label="Top-p",
                        info="Controls diversity. Lower value = more focused responses"
                    )
                    
                    clear_btn = gr.Button("Clear Chat History")

            def respond_and_clear(
                message,
                history,
                conversation_id,
                max_tokens,
                temperature,
                top_p,
            ):
                # Use existing respond function
                response_generator = respond(
                    message,
                    history,
                    conversation_id,
                    DEFAULT_SYSTEM_MESSAGE,
                    max_tokens,
                    temperature,
                    top_p,
                )
                
                # Return result and empty string to clear input field
                for response in response_generator:
                    yield response[0], response[1], ""  # chatbot, conversation_id, empty string for msg

            # Event handlers
            msg.submit(
                respond_and_clear,
                [msg, chatbot, conversation_id, max_tokens, temperature, top_p],
                [chatbot, conversation_id, msg]  # Add msg to output parameters
            )
            submit_btn.click(
                respond_and_clear,
                [msg, chatbot, conversation_id, max_tokens, temperature, top_p],
                [chatbot, conversation_id, msg]  # Add msg to output parameters
            )
            build_kb_btn.click(build_kb, None, kb_status)
            clear_btn.click(lambda: ([], None), None, [chatbot, conversation_id])

        with gr.Tab("Model Training"):
            gr.Markdown("### Model Training Interface")
            
            with gr.Row():
                with gr.Column():
                    epochs = gr.Slider(minimum=1, maximum=10, value=3, step=1, label="Number of Epochs")
                    batch_size = gr.Slider(minimum=1, maximum=32, value=4, step=1, label="Batch Size")
                    learning_rate = gr.Slider(minimum=1e-6, maximum=1e-3, value=2e-4, label="Learning Rate")
                    train_btn = gr.Button("Start Training", variant="primary")
                    training_output = gr.Textbox(label="Training Status", interactive=False)

                with gr.Column():
                    analysis_btn = gr.Button("Generate Chat Analysis")
                    analysis_output = gr.Markdown()
            
            train_btn.click(
                start_finetune_action,
                inputs=[epochs, batch_size, learning_rate],
                outputs=[training_output]
            )
            analysis_btn.click(
                generate_chat_analysis,
                inputs=[],
                outputs=[analysis_output]
            )

# Launch application
if __name__ == "__main__":
    # Check knowledge base availability in dataset
    if not load_vector_store():
        print("Knowledge base not found. Please create it through the interface.")
    
    demo.launch()