File size: 17,247 Bytes
60af46a
 
45c6bff
0ce6a8e
45c6bff
2747a7e
45c6bff
2747a7e
45c6bff
 
8a0b2d3
45c6bff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2747a7e
45c6bff
 
 
 
2747a7e
45c6bff
 
 
 
 
 
2747a7e
45c6bff
2747a7e
45c6bff
 
2747a7e
 
45c6bff
2747a7e
45c6bff
2747a7e
45c6bff
2747a7e
 
45c6bff
 
2747a7e
45c6bff
d52b0bc
45c6bff
 
60af46a
45c6bff
 
60af46a
45c6bff
d52b0bc
45c6bff
 
 
 
d52b0bc
45c6bff
 
 
 
 
 
 
 
 
 
818ceaa
45c6bff
48e2057
45c6bff
 
 
48e2057
45c6bff
 
 
 
 
58fb878
 
 
 
45c6bff
 
 
 
 
 
 
 
 
 
 
0ce6a8e
45c6bff
 
 
 
 
 
 
 
d52b0bc
45c6bff
 
 
 
 
 
 
 
 
 
 
 
 
60af46a
45c6bff
 
d52b0bc
45c6bff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d52b0bc
45c6bff
0ce6a8e
45c6bff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ce6a8e
 
45c6bff
0ce6a8e
 
 
58fb878
 
 
45c6bff
58fb878
 
0ce6a8e
60af46a
45c6bff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df71242
45c6bff
58fb878
45c6bff
df71242
 
 
 
 
45c6bff
df71242
 
 
 
 
 
45c6bff
 
df2f901
45c6bff
 
 
0ce6a8e
2eed108
dd13db1
58fb878
60af46a
9a20fbe
 
2eed108
9a20fbe
45c6bff
9a20fbe
 
 
 
 
 
45c6bff
 
9a20fbe
 
df71242
df2f901
 
 
 
 
 
 
45c6bff
 
 
 
 
 
 
 
9a20fbe
45c6bff
9a20fbe
45c6bff
9a20fbe
 
 
45c6bff
9a20fbe
 
45c6bff
 
 
 
 
df71242
 
 
 
 
 
 
 
 
9a20fbe
 
dd13db1
0ce6a8e
9a20fbe
 
 
 
45c6bff
 
 
9a20fbe
 
 
45c6bff
 
df71242
45c6bff
 
 
 
 
 
df71242
45c6bff
dd13db1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45c6bff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d52b0bc
45c6bff
 
 
 
 
 
 
 
 
 
 
 
71a3b13
45c6bff
 
 
dd13db1
2747a7e
45c6bff
 
 
dd13db1
45c6bff
2747a7e
df2f901
 
 
 
 
 
dd13db1
df71242
 
 
 
 
 
 
df2f901
45c6bff
df2f901
 
 
 
 
 
df71242
 
 
 
 
 
45c6bff
 
 
 
 
0ce6a8e
45c6bff
 
 
 
 
 
 
 
 
 
71a3b13
45c6bff
 
 
60af46a
0ce6a8e
45c6bff
 
 
0ce6a8e
45c6bff
 
 
 
71a3b13
45c6bff
 
 
 
 
 
 
71a3b13
45c6bff
 
 
60af46a
0ce6a8e
45c6bff
 
 
 
 
60af46a
df2f901
60af46a
0032784
d52b0bc
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
from threading import Thread
import pdfplumber
from docx import Document
import pandas as pd
import io
from typing import List, Tuple, Optional
import json

# Memory Agent Class
class MemoryAgent:
    def __init__(self):
        self.memory_store = []
        self.documents = {}
    
    def add(self, text: str, source: str = "conversation"):
        """Add text to memory with source tracking"""
        self.memory_store.append({
            "content": text,
            "source": source
        })
    
    def add_document(self, filename: str, content: str):
        """Store document content"""
        self.documents[filename] = content
        # Store a summary in memory, but keep full content in documents dict
        summary = content[:1000] + "..." if len(content) > 1000 else content
        self.add(f"Document '{filename}' uploaded (Length: {len(content)} chars). Preview:\n{summary}", source=f"document:{filename}")
    
    def get_context(self, query: Optional[str] = None, max_items: int = 10) -> str:
        """Retrieve relevant context from memory"""
        if not self.memory_store:
            return ""
        
        # Return recent memory items
        recent_items = self.memory_store[-max_items:]
        context_parts = []
        
        for item in recent_items:
            if item["source"].startswith("document:"):
                context_parts.append(f"[From {item['source'].split(':')[1]}]\n{item['content']}")
            else:
                context_parts.append(item["content"])
        
        return "\n\n".join(context_parts)
    
    def clear(self):
        """Clear all memory"""
        self.memory_store = []
        self.documents = {}

# Document Processing Functions
def extract_text_from_pdf(file_path: str) -> str:
    """Extract text from PDF using pdfplumber"""
    text_parts = []
    try:
        with pdfplumber.open(file_path) as pdf:
            for page in pdf.pages:
                text = page.extract_text()
                if text:
                    text_parts.append(text)
        return "\n\n".join(text_parts)
    except Exception as e:
        return f"Error extracting PDF: {str(e)}"

def extract_text_from_docx(file_path: str) -> str:
    """Extract text from DOCX"""
    try:
        doc = Document(file_path)
        return "\n\n".join([para.text for para in doc.paragraphs if para.text.strip()])
    except Exception as e:
        return f"Error extracting DOCX: {str(e)}"

def extract_text_from_txt(file_path: str) -> str:
    """Extract text from TXT file"""
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            return f.read()
    except Exception as e:
        return f"Error extracting TXT: {str(e)}"

def extract_text_from_csv(file_path: str) -> str:
    """Extract text from CSV"""
    try:
        df = pd.read_csv(file_path)
        return df.to_string()
    except Exception as e:
        return f"Error extracting CSV: {str(e)}"

def process_uploaded_file(file_path: str) -> Tuple[str, str]:
    """Process uploaded file and extract text"""
    filename = file_path.split('/')[-1]
    extension = filename.split('.')[-1].lower()
    
    if extension == 'pdf':
        content = extract_text_from_pdf(file_path)
    elif extension == 'docx':
        content = extract_text_from_docx(file_path)
    elif extension == 'txt':
        content = extract_text_from_txt(file_path)
    elif extension == 'csv':
        content = extract_text_from_csv(file_path)
    else:
        content = f"Unsupported file type: {extension}"
    
    return filename, content

# Global model cache
model_cache = {}
tokenizer_cache = {}

def load_model(model_name: str):
    """Load model and tokenizer with caching"""
    if model_name not in model_cache:
        try:
            tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
            # Set padding token if not exists
            if tokenizer.pad_token is None:
                tokenizer.pad_token = tokenizer.eos_token
            
            model = AutoModelForCausalLM.from_pretrained(
                model_name,
                device_map="auto",
                torch_dtype=torch.bfloat16,
                trust_remote_code=True,
                low_cpu_mem_usage=True
            )
            model_cache[model_name] = model
            tokenizer_cache[model_name] = tokenizer
        except Exception as e:
            raise Exception(f"Error loading model: {str(e)}")
    
    return model_cache[model_name], tokenizer_cache[model_name]

def format_chat_history(history: List[List[str]]) -> str:
    """Format chat history for context"""
    formatted = []
    for user_msg, assistant_msg in history:
        if user_msg:
            formatted.append(f"User: {user_msg}")
        if assistant_msg:
            formatted.append(f"Assistant: {assistant_msg}")
    return "\n".join(formatted)

def generate_response(
    message: str,
    history: List[List[str]],
    model_name: str,
    temperature: float,
    max_tokens: int,
    system_prompt: str,
    memory_agent: MemoryAgent
):
    """Generate streaming response from SaulLM model"""
    try:
        # Load model
        model, tokenizer = load_model(model_name)
        
        # Build context from memory
        memory_context = memory_agent.get_context(max_items=15)
        
        # Build full prompt with enhanced structure
        context_parts = [system_prompt]
        
        # Add uploaded documents context if available
        if memory_agent.documents:
            context_parts.append(f"\n### UPLOADED DOCUMENTS:")
            context_parts.append("You have access to the following documents that the user uploaded:")
            for doc_name, doc_content in memory_agent.documents.items():
                # Include more document content for better context
                content_preview = doc_content[:5000] if len(doc_content) > 5000 else doc_content
                context_parts.append(f"\n**Document: {doc_name}**\n{content_preview}")
                if len(doc_content) > 5000:
                    context_parts.append(f"[Document continues... Total length: {len(doc_content)} characters]")
            context_parts.append("\nWhen the user asks about documents, refer to the specific content above and provide detailed, accurate answers based on what you can see in the uploaded documents.")
        
        # Add general memory/context
        if memory_context and not memory_agent.documents:
            context_parts.append(f"\n### Conversation Context:\n{memory_context}")
        
        # Add chat history
        if history:
            recent_history = history[-4:]  # Last 4 exchanges to leave room for documents
            context_parts.append(f"\n### Recent Conversation:\n{format_chat_history(recent_history)}")
        
        context_parts.append(f"\n### Current User Question:\n{message}\n\n### Your Response (provide a detailed, professional answer):")
        
        full_prompt = "\n".join(context_parts)
        
        # Tokenize
        inputs = tokenizer(full_prompt, return_tensors="pt", truncation=True, max_length=4096)
        inputs = {k: v.to(model.device) for k, v in inputs.items()}
        
        # Setup streamer
        streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
        
        # Generation parameters
        generation_kwargs = dict(
            inputs,
            streamer=streamer,
            max_new_tokens=max_tokens,
            temperature=temperature,
            do_sample=temperature > 0,
            top_p=0.95,
            repetition_penalty=1.1
        )
        
        # Start generation in thread
        thread = Thread(target=model.generate, kwargs=generation_kwargs)
        thread.start()
        
        # Stream response
        partial_response = ""
        for new_text in streamer:
            partial_response += new_text
            # Clean up special tokens and formatting
            cleaned_response = partial_response.replace("[/INST]", "").replace("[INST]", "").replace("</s>", "").replace("<s>", "").strip()
            yield cleaned_response
        
        # Add to memory (use cleaned version)
        memory_agent.add(f"User: {message}\nAssistant: {cleaned_response}")
        
    except Exception as e:
        yield f"Error generating response: {str(e)}"

def handle_file_upload(files, memory_agent: MemoryAgent):
    """Handle multiple file uploads"""
    if not files:
        return "No files uploaded", []
    
    uploaded_docs = []
    for file in files:
        filename, content = process_uploaded_file(file.name)
        memory_agent.add_document(filename, content)
        uploaded_docs.append(filename)
    
    doc_list = "\n".join([f"✓ {doc}" for doc in uploaded_docs])
    return f"Uploaded {len(uploaded_docs)} file(s):\n{doc_list}", uploaded_docs

# Default system prompt
DEFAULT_SYSTEM_PROMPT = """You are SaulLM, a helpful and knowledgeable AI assistant.

Answer questions directly and professionally across all domains including law, finance, securitization, technology, science, and general knowledge.

When documents are uploaded:
- Read the document content carefully
- Answer questions based on the specific information in the documents
- Reference relevant parts of the document when answering
- Be precise and accurate with document details

Response style:
- Provide direct answers without unnecessary preambles
- Don't say "as an AI" or similar phrases
- Be clear, concise, and professional
- Give complete, helpful responses
- Use your expertise to provide accurate information"""

# Build Gradio Interface
with gr.Blocks(title="SaulLM Chat", theme=gr.themes.Soft()) as demo:
    # State variables
    memory_state = gr.State(lambda: MemoryAgent())
    uploaded_docs_state = gr.State([])
    
    gr.Markdown("# SaulLM Chat")

    
    with gr.Row():
        # Left Sidebar
        with gr.Column(scale=1, min_width=300):
            gr.Markdown("### Configuration")
            
            model_dropdown = gr.Dropdown(
                choices=[
                    "Equall/Saul-7B-Instruct-v1",
                    "Equall/SaulLM-54B-Instruct",
                    "Equall/SaulLM-141B-Instruct"
                ],
                value="Equall/Saul-7B-Instruct-v1",
                label="Model Selection",
                interactive=True
            )
            
            load_model_btn = gr.Button("Load Model", variant="primary")
            model_load_status = gr.Textbox(
                label="Model Status",
                value="No model loaded yet",
                interactive=False,
                lines=2
            )
            
            temperature_slider = gr.Slider(
                minimum=0.0,
                maximum=1.0,
                value=0.7,
                step=0.1,
                label="Temperature",
                info="Higher = more creative"
            )
            
            max_tokens_slider = gr.Slider(
                minimum=128,
                maximum=2048,
                value=1024,
                step=128,
                label="Max Tokens",
                info="Maximum response length"
            )
            
            system_prompt_box = gr.Textbox(
                value=DEFAULT_SYSTEM_PROMPT,
                label="System Prompt",
                lines=10,
                max_lines=15,
                info="Define the assistant's behavior and expertise"
            )
            
            update_prompt_btn = gr.Button("Update System Prompt", variant="secondary")
            prompt_update_status = gr.Textbox(
                label="Prompt Status",
                value="Using default system prompt",
                interactive=False,
                lines=1
            )
            
            clear_btn = gr.Button("🗑️ Clear Chat & Memory", variant="stop")
        
        # Main Chat Area
        with gr.Column(scale=3):
            chatbot = gr.Chatbot(
                label="Chat",
                height=600,
                bubble_full_width=False,
                show_copy_button=True
            )
            
            with gr.Row():
                msg_input = gr.Textbox(
                    label="Message",
                    placeholder="Ask me...",
                    lines=2,
                    scale=9
                )
                send_btn = gr.Button("Send", variant="primary", scale=1)
            
            with gr.Row():
                regenerate_btn = gr.Button("Regenerate Response")
                clear_input_btn = gr.Button("Clear Input")
            
            gr.Markdown("### Document Upload")
            file_upload = gr.File(
                label="Upload Documents (PDF, DOCX, TXT, CSV)",
                file_count="multiple",
                file_types=[".pdf", ".docx", ".txt", ".csv"]
            )
            
            with gr.Row():
                upload_status = gr.Textbox(
                    label="Upload Status",
                    interactive=False,
                    lines=2,
                    scale=1
                )
                uploaded_docs_display = gr.Textbox(
                    label="Uploaded Documents",
                    interactive=False,
                    lines=2,
                    scale=1
                )
    
    # Event Handlers
    def user_submit(message, history):
        """Handle user message submission"""
        return "", history + [[message, None]]
    
    def bot_response(history, model_name, temperature, max_tokens, system_prompt, memory_agent):
        """Generate bot response"""
        if not history or history[-1][1] is not None:
            return history
        
        user_message = history[-1][0]
        history[-1][1] = ""
        
        for partial_response in generate_response(
            user_message,
            history[:-1],
            model_name,
            temperature,
            max_tokens,
            system_prompt,
            memory_agent
        ):
            history[-1][1] = partial_response
            yield history
    
    def regenerate_last(history, model_name, temperature, max_tokens, system_prompt, memory_agent):
        """Regenerate the last response"""
        if not history:
            return history
        
        # Remove last assistant response
        if history[-1][1]:
            history[-1][1] = None
        
        # Generate new response
        for updated_history in bot_response(history, model_name, temperature, max_tokens, system_prompt, memory_agent):
            yield updated_history
    
    def clear_chat(memory_agent):
        """Clear chat and memory"""
        memory_agent.clear()
        return [], "", "Chat and memory cleared"
    
    def update_uploaded_docs_display(docs):
        """Update the uploaded documents display"""
        if not docs:
            return ""
        return "\n".join([f"✓ {doc}" for doc in docs])
    
    def load_model_handler(model_name):
        """Handle model loading button click"""
        try:
            load_model(model_name)
            return f"✓ Model '{model_name}' loaded successfully!"
        except Exception as e:
            return f" Error loading model: {str(e)}"
    
    def update_system_prompt_handler(prompt):
        """Handle system prompt update"""
        if prompt.strip():
            return "✓ System prompt updated successfully!"
        else:
            return "System prompt cannot be empty"
    
    # Wire up events
    load_model_btn.click(
        load_model_handler,
        [model_dropdown],
        model_load_status
    )
    
    update_prompt_btn.click(
        update_system_prompt_handler,
        [system_prompt_box],
        prompt_update_status
    )
    
    msg_input.submit(
        user_submit,
        [msg_input, chatbot],
        [msg_input, chatbot],
        queue=False
    ).then(
        bot_response,
        [chatbot, model_dropdown, temperature_slider, max_tokens_slider, system_prompt_box, memory_state],
        chatbot
    )
    
    send_btn.click(
        user_submit,
        [msg_input, chatbot],
        [msg_input, chatbot],
        queue=False
    ).then(
        bot_response,
        [chatbot, model_dropdown, temperature_slider, max_tokens_slider, system_prompt_box, memory_state],
        chatbot
    )
    
    regenerate_btn.click(
        regenerate_last,
        [chatbot, model_dropdown, temperature_slider, max_tokens_slider, system_prompt_box, memory_state],
        chatbot
    )
    
    clear_input_btn.click(
        lambda: "",
        None,
        msg_input
    )
    
    file_upload.change(
        handle_file_upload,
        [file_upload, memory_state],
        [upload_status, uploaded_docs_state]
    ).then(
        update_uploaded_docs_display,
        [uploaded_docs_state],
        uploaded_docs_display
    )
    
    clear_btn.click(
        clear_chat,
        [memory_state],
        [chatbot, uploaded_docs_display, upload_status]
    )

# Launch
if __name__ == "__main__":
    demo.queue(max_size=20)
    demo.launch()