File size: 17,495 Bytes
4e933f3
be1c5c4
a407706
 
 
be1c5c4
a407706
 
02d23e2
 
be1c5c4
a407706
 
 
be1c5c4
a407706
 
 
 
 
 
02d23e2
a407706
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02d23e2
 
 
a407706
02d23e2
 
 
 
 
 
 
 
 
 
 
a407706
 
02d23e2
 
 
 
 
 
 
 
 
 
 
 
a407706
 
 
4135159
a407706
4135159
 
 
a407706
 
 
 
 
 
 
 
 
 
02d23e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a407706
 
 
 
 
 
 
 
4135159
a407706
 
 
 
 
 
 
 
 
 
 
 
 
 
02d23e2
 
a407706
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02d23e2
 
 
 
a407706
 
 
 
 
02d23e2
a407706
 
 
 
 
 
 
02d23e2
a407706
 
 
 
 
 
 
 
 
 
02d23e2
 
a407706
 
 
 
 
 
 
02d23e2
 
 
 
 
 
 
 
 
 
 
a407706
 
02d23e2
 
a407706
 
02d23e2
 
 
a407706
be1c5c4
 
02d23e2
a407706
02d23e2
a407706
 
 
02d23e2
a407706
 
02d23e2
 
 
 
 
a407706
02d23e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a407706
02d23e2
a407706
 
 
 
be1c5c4
 
a407706
 
 
 
 
 
 
 
 
 
 
 
 
 
f3d6f52
 
02d23e2
f3d6f52
 
 
 
 
 
 
 
 
 
02d23e2
 
 
 
 
 
 
 
 
a407706
 
 
02d23e2
 
 
a407706
 
02d23e2
 
a407706
02d23e2
 
 
 
 
 
 
 
 
a407706
02d23e2
f3d6f52
 
 
02d23e2
a407706
f3d6f52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a407706
 
f3d6f52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a407706
f3d6f52
 
 
 
 
 
 
 
 
 
 
 
 
a407706
f3d6f52
a407706
f3d6f52
02d23e2
 
4e933f3
 
 
02d23e2
 
a407706
 
 
 
 
 
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
import gradio as gr
import json
import numpy as np
from transformers import pipeline, AutoTokenizer, AutoModel
import torch
import os
from typing import List, Dict, Any
import time
import requests
import re

# Configure device
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")

class RAGtimBot:
    def __init__(self):
        self.embedder = None
        self.knowledge_base = []
        self.embeddings = []
        self.initialize_models()
        self.load_markdown_knowledge_base()
        
    def initialize_models(self):
        """Initialize the embedding model"""
        try:
            print("Loading embedding model...")
            self.embedder = pipeline(
                'feature-extraction', 
                'sentence-transformers/all-MiniLM-L6-v2',
                device=0 if device == "cuda" else -1
            )
            print("βœ… Embedding model loaded successfully")
        except Exception as e:
            print(f"❌ Error loading embedding model: {e}")
            raise e
    
    def load_markdown_knowledge_base(self):
        """Load knowledge base from markdown files"""
        print("Loading knowledge base from markdown files...")
        
        # Reset knowledge base
        self.knowledge_base = []
        
        # Load all markdown files
        markdown_files = [
            'about.md',
            'research_details.md', 
            'publications_detailed.md',
            'skills_expertise.md',
            'experience_detailed.md',
            'statistics.md'
        ]
        
        for filename in markdown_files:
            try:
                if os.path.exists(filename):
                    with open(filename, 'r', encoding='utf-8') as f:
                        content = f.read()
                    self.process_markdown_file(content, filename)
                    print(f"βœ… Loaded {filename}")
                else:
                    print(f"⚠️ File not found: {filename}")
            except Exception as e:
                print(f"❌ Error loading {filename}: {e}")
        
        # Generate embeddings for knowledge base
        print("Generating embeddings for knowledge base...")
        self.embeddings = []
        for i, doc in enumerate(self.knowledge_base):
            try:
                # Truncate content to avoid token limit issues
                content = doc["content"][:500]  # Limit to 500 characters
                embedding = self.embedder(content, return_tensors="pt")
                # Convert to numpy and flatten
                embedding_np = embedding[0].mean(dim=0).detach().cpu().numpy()
                self.embeddings.append(embedding_np)
            except Exception as e:
                print(f"Error generating embedding for doc {doc['id']}: {e}")
                # Fallback to zero embedding
                self.embeddings.append(np.zeros(384))
        
        print(f"βœ… Knowledge base loaded with {len(self.knowledge_base)} documents")
    
    def process_markdown_file(self, content: str, filename: str):
        """Process a markdown file and extract sections"""
        # Determine file type and priority
        file_type_map = {
            'about.md': ('about', 10),
            'research_details.md': ('research', 9),
            'publications_detailed.md': ('publications', 8),
            'skills_expertise.md': ('skills', 7),
            'experience_detailed.md': ('experience', 8),
            'statistics.md': ('statistics', 9)
        }
        
        file_type, priority = file_type_map.get(filename, ('general', 5))
        
        # Split content into sections
        sections = self.split_markdown_into_sections(content)
        
        for section in sections:
            if len(section['content'].strip()) > 100:  # Only process substantial content
                doc = {
                    "id": f"{filename}_{section['title']}_{len(self.knowledge_base)}",
                    "content": section['content'],
                    "metadata": {
                        "type": file_type,
                        "priority": priority,
                        "section": section['title'],
                        "source": filename
                    }
                }
                self.knowledge_base.append(doc)
    
    def split_markdown_into_sections(self, content: str) -> List[Dict[str, str]]:
        """Split markdown content into sections based on headers"""
        sections = []
        lines = content.split('\n')
        current_section = {'title': 'Introduction', 'content': ''}
        
        for line in lines:
            # Check if line is a header
            if line.startswith('#'):
                # Save previous section if it has content
                if current_section['content'].strip():
                    sections.append(current_section.copy())
                
                # Start new section
                header_level = len(line) - len(line.lstrip('#'))
                title = line.lstrip('#').strip()
                current_section = {
                    'title': title,
                    'content': line + '\n'
                }
            else:
                current_section['content'] += line + '\n'
        
        # Add the last section
        if current_section['content'].strip():
            sections.append(current_section)
        
        return sections
    
    def cosine_similarity(self, a, b):
        """Calculate cosine similarity between two vectors"""
        return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
    
    def search_knowledge_base(self, query: str, top_k: int = 5) -> List[Dict]:
        """Search the knowledge base using semantic similarity"""
        try:
            # Generate query embedding
            query_embedding = self.embedder(query[:500], return_tensors="pt")  # Truncate query too
            query_vector = query_embedding[0].mean(dim=0).detach().cpu().numpy()
            
            # Calculate similarities
            similarities = []
            for i, doc_embedding in enumerate(self.embeddings):
                similarity = self.cosine_similarity(query_vector, doc_embedding)
                similarities.append({
                    "id": self.knowledge_base[i]["id"],
                    "content": self.knowledge_base[i]["content"],
                    "metadata": self.knowledge_base[i]["metadata"],
                    "score": float(similarity),
                    "index": i
                })
            
            # Sort by similarity and priority
            similarities.sort(key=lambda x: (x["score"], x["metadata"]["priority"]), reverse=True)
            return similarities[:top_k]
            
        except Exception as e:
            print(f"Error in search: {e}")
            # Fallback to keyword search
            return self.keyword_search(query, top_k)
    
    def keyword_search(self, query: str, top_k: int = 5) -> List[Dict]:
        """Fallback keyword search"""
        query_terms = query.lower().split()
        results = []
        
        for i, doc in enumerate(self.knowledge_base):
            content_lower = doc["content"].lower()
            score = sum(content_lower.count(term) for term in query_terms)
            
            # Add priority boost
            priority_boost = doc["metadata"]["priority"] / 10
            final_score = score + priority_boost
            
            if score > 0:
                results.append({
                    "id": doc["id"],
                    "content": doc["content"],
                    "metadata": doc["metadata"],
                    "score": final_score,
                    "index": i
                })
        
        results.sort(key=lambda x: x["score"], reverse=True)
        return results[:top_k]

# Initialize the bot
print("Initializing RAGtim Bot with markdown knowledge base...")
bot = RAGtimBot()

def search_only_api(query, top_k=5):
    """API endpoint for search-only functionality"""
    try:
        results = bot.search_knowledge_base(query, top_k)
        return {
            "results": results,
            "query": query,
            "top_k": top_k,
            "search_type": "semantic",
            "total_documents": len(bot.knowledge_base)
        }
    except Exception as e:
        print(f"Error in search API: {e}")
        return {"error": str(e), "results": []}

def get_stats_api():
    """API endpoint for knowledge base statistics"""
    # Calculate document distribution by type
    doc_types = {}
    sections_by_file = {}
    
    for doc in bot.knowledge_base:
        doc_type = doc["metadata"]["type"]
        source_file = doc["metadata"]["source"]
        
        doc_types[doc_type] = doc_types.get(doc_type, 0) + 1
        sections_by_file[source_file] = sections_by_file.get(source_file, 0) + 1
    
    return {
        "total_documents": len(bot.knowledge_base),
        "document_types": doc_types,
        "sections_by_file": sections_by_file,
        "model_name": "sentence-transformers/all-MiniLM-L6-v2",
        "embedding_dimension": 384,
        "search_capabilities": ["Semantic Search", "GPU Accelerated", "Transformer Embeddings", "Markdown Knowledge Base"],
        "backend_type": "Hugging Face Space",
        "knowledge_sources": list(sections_by_file.keys())
    }

def chat_interface(message, history):
    """Chat interface with markdown knowledge base"""
    if not message.strip():
        return "Please ask me something about Raktim Mondol! I have comprehensive information loaded from his complete portfolio markdown files."
    
    try:
        # Search knowledge base
        search_results = bot.search_knowledge_base(message, top_k=6)
        
        if search_results:
            # Build comprehensive response
            response_parts = []
            response_parts.append(f"Based on my markdown knowledge base (found {len(search_results)} relevant sections):\n")
            
            # Use the best match as primary response
            best_match = search_results[0]
            response_parts.append(f"**Primary Answer** (Relevance: {best_match['score']:.2f}):")
            response_parts.append(f"Source: {best_match['metadata']['source']} - {best_match['metadata']['section']}")
            response_parts.append(f"{best_match['content']}\n")
            
            # Add additional context if available
            if len(search_results) > 1:
                response_parts.append("**Additional Context:**")
                for i, result in enumerate(search_results[1:3], 1):  # Show up to 2 additional results
                    section_info = f"{result['metadata']['source']} - {result['metadata']['section']}"
                    response_parts.append(f"{i}. {section_info} (Relevance: {result['score']:.2f})")
                    # Add a brief excerpt
                    excerpt = result['content'][:200] + "..." if len(result['content']) > 200 else result['content']
                    response_parts.append(f"   {excerpt}\n")
            
            response_parts.append("\n[Note: This response is generated from your complete markdown knowledge base. In hybrid mode, DeepSeek LLM would generate more natural responses using this context.]")
            
            return "\n".join(response_parts)
        else:
            return "I don't have specific information about that topic in my markdown knowledge base. Could you please ask something else about Raktim Mondol?"
        
    except Exception as e:
        print(f"Error in chat interface: {e}")
        return "I'm sorry, I encountered an error while processing your question. Please try again."

# Create Gradio interface
print("Creating Gradio interface...")

# Custom CSS for better styling
css = """
.gradio-container {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.chat-message {
    padding: 10px;
    margin: 5px 0;
    border-radius: 10px;
}
"""

# Create the main chat interface - UPDATED FOR GRADIO 5.34.0
with gr.Blocks(
    title="πŸ€– RAGtim Bot - Markdown Knowledge Base",
    css=css,
    theme=gr.themes.Soft(
        primary_hue="green",
        secondary_hue="blue",
        neutral_hue="slate"
    )
) as chat_demo:
    gr.Markdown(f"""
    # πŸ€– RAGtim Bot - Markdown Knowledge Base
    
    **Complete Markdown Knowledge Base**: This Hugging Face Space loads all markdown files from Raktim Mondol's portfolio with **{len(bot.knowledge_base)} knowledge sections**.
    
    **Loaded Markdown Files:**
    - πŸ“„ **about.md** - Personal information, contact details, professional summary
    - πŸ”¬ **research_details.md** - Detailed research projects, methodologies, current work
    - πŸ“š **publications_detailed.md** - Complete publication details, technical contributions
    - πŸ’» **skills_expertise.md** - Comprehensive technical skills, tools, frameworks
    - πŸ’Ό **experience_detailed.md** - Professional experience, teaching, research roles
    - πŸ“Š **statistics.md** - Statistical methods, biostatistics expertise, methodologies
    
    **Search Capabilities:**
    - πŸ” Semantic similarity search using transformers
    - πŸš€ GPU-accelerated embeddings with priority ranking
    - πŸ“Š Relevance scoring across all markdown content
    - 🎯 Section-level granular search within each file
    
    **API Endpoints:**
    - `/api/search` - Search across complete markdown knowledge base
    - `/api/stats` - Detailed statistics about loaded content
    
    **Ask me anything about Raktim Mondol:**
    - Research projects, methodologies, and innovations
    - Publications with technical details and impact
    - Technical skills, programming expertise, and tools
    - Educational background and academic achievements
    - Professional experience and teaching roles
    - Statistical methods and biostatistics applications
    - Awards, recognition, and professional development
    - Contact information and collaboration opportunities
    
    **Note**: This demo shows search results from the complete markdown knowledge base. In hybrid mode, these results are passed to DeepSeek LLM for natural response generation.
    """)
    
    chatbot = gr.Chatbot(
        height=600,
        show_label=False,
        container=True,
        type="messages"
    )
    
    with gr.Row():
        msg = gr.Textbox(
            placeholder="Ask me anything about Raktim Mondol's research, skills, experience, publications...",
            container=False,
            scale=7,
            show_label=False
        )
        submit_btn = gr.Button("Search Knowledge Base", scale=1)
    
    # Example buttons
    with gr.Row():
        examples = [
            "What is Raktim's research about?",
            "Tell me about BioFusionNet in detail",
            "What are his LLM and RAG expertise?",
            "Describe his statistical methods and biostatistics work"
        ]
        for example in examples:
            gr.Button(example, size="sm").click(
                lambda x=example: x, outputs=msg
            )
    
    def respond(message, history):
        if not message.strip():
            return history, ""
        
        # Add user message to history
        history.append({"role": "user", "content": message})
        
        # Get bot response
        bot_response = chat_interface(message, history)
        
        # Add bot response to history
        history.append({"role": "assistant", "content": bot_response})
        
        return history, ""
    
    submit_btn.click(respond, [msg, chatbot], [chatbot, msg])
    msg.submit(respond, [msg, chatbot], [chatbot, msg])

# Create API interface for search-only functionality
with gr.Blocks(title="πŸ” Search API") as search_demo:
    gr.Markdown("# πŸ” Markdown Knowledge Base Search API")
    gr.Markdown("Direct access to semantic search across all loaded markdown files")
    
    with gr.Row():
        search_input = gr.Textbox(
            label="Search Query", 
            placeholder="Enter your search query about Raktim Mondol..."
        )
        top_k_slider = gr.Slider(
            minimum=1, 
            maximum=15, 
            value=5, 
            step=1, 
            label="Top K Results"
        )
    
    search_output = gr.JSON(label="Markdown Knowledge Base Search Results")
    search_btn = gr.Button("Search")
    
    search_btn.click(
        search_only_api,
        inputs=[search_input, top_k_slider],
        outputs=search_output
    )

# Create stats interface
with gr.Blocks(title="πŸ“Š Stats API") as stats_demo:
    gr.Markdown("# πŸ“Š Knowledge Base Stats")
    gr.Markdown("Detailed statistics about the loaded markdown knowledge base")
    
    stats_output = gr.JSON(label="Markdown Knowledge Base Statistics")
    stats_btn = gr.Button("Get Statistics")
    
    stats_btn.click(
        get_stats_api,
        inputs=[],
        outputs=stats_output
    )

# Combine interfaces using TabbedInterface
demo = gr.TabbedInterface(
    [chat_demo, search_demo, stats_demo],
    ["πŸ’¬ Markdown Chat", "πŸ” Search API", "πŸ“Š Stats API"],
    title="πŸ€– RAGtim Bot - Complete Markdown Knowledge Base"
)

if __name__ == "__main__":
    print("πŸš€ Launching RAGtim Bot with Markdown Knowledge Base...")
    print(f"πŸ“š Loaded {len(bot.knowledge_base)} sections from markdown files")
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False,
        show_error=True
    )