""" Convert comprehensive knowledge base TXT to ALU Brain JSON format This will make your knowledge base actually usable by the chatbot! """ import json from pathlib import Path import re def parse_comprehensive_kb(): """Parse the comprehensive knowledge base text file""" kb_file = Path("data/alu_knowledge/alu_ultimate_knowledge_base.txt") if not kb_file.exists(): print(f"āŒ Knowledge base file not found: {kb_file}") return None with open(kb_file, 'r', encoding='utf-8') as f: content = f.read() # Split by major sections (## headers) sections = re.split(r'\n##\s+', content) entries = [] entry_id = 1 for section in sections: if not section.strip(): continue lines = section.split('\n') title = lines[0].strip() # Get the content content_lines = [] current_subsection = "" for line in lines[1:]: if line.strip(): if line.startswith('###'): # Subsection current_subsection = line.replace('###', '').strip() elif line.startswith('-') or line.startswith('*'): # List item content_lines.append(line.strip()) else: content_lines.append(line.strip()) full_content = '\n'.join(content_lines) # Create entry entry = { "id": f"comprehensive_kb_{entry_id}", "question": f"Tell me about {title}", "answer": full_content, "type": "text", "keywords": [word.lower() for word in title.split() if len(word) > 3], "category": "Comprehensive Knowledge Base", "source": "ALU Comprehensive KB" } entries.append(entry) entry_id += 1 # Also create entries for common questions if "club" in title.lower(): entries.append({ "id": f"comprehensive_kb_{entry_id}", "question": "How to create a club at ALU?", "answer": full_content, "type": "text", "keywords": ["club", "create", "start", "society", "organization"], "category": "Campus Life", "source": "ALU Comprehensive KB" }) entry_id += 1 if "founder" in title.lower() or "leadership" in title.lower(): entries.append({ "id": f"comprehensive_kb_{entry_id}", "question": "Who founded ALU?", "answer": full_content, "type": "text", "keywords": ["founder", "fred", "swaniker", "leadership", "history"], "category": "About ALU", "source": "ALU Comprehensive KB" }) entry_id += 1 return entries def create_brain_json(): """Create the ALU Brain JSON file""" print("============================================================") print("🧠 CONVERTING COMPREHENSIVE KB TO ALU BRAIN FORMAT") print("============================================================\n") entries = parse_comprehensive_kb() if not entries: print("āŒ No entries created") return False # Create the brain JSON structure brain_data = { "category": "comprehensive_knowledge", "description": "Comprehensive ALU Knowledge Base - All ALU information", "version": "1.0", "last_updated": "2024-11-20", "entries": entries } # Save to alu_brain directory output_file = Path("alu_brain/comprehensive_knowledge.json") with open(output_file, 'w', encoding='utf-8') as f: json.dump(brain_data, f, indent=2, ensure_ascii=False) print(f"āœ… Created {len(entries)} entries") print(f"šŸ“ Saved to: {output_file}") print(f"\nšŸ“ Sample entries:") for entry in entries[:3]: print(f" - {entry['question']}") print("\n============================================================") print("āœ… CONVERSION COMPLETE!") print("============================================================") print("\nNext steps:") print("1. Commit and push this file to Hugging Face") print("2. Restart the Space") print("3. The chatbot will now use your comprehensive knowledge!") return True if __name__ == "__main__": create_brain_json()