Spaces:
Running
Running
ADD ULTIMATE ALU BRAIN - 58 comprehensive entries covering ALL student needs: academics, career, health, safety, immigration, financial aid, clubs, events, resources, emergency procedures, mental health support, disability services, and much more
Browse files- alu_brain/comprehensive_knowledge.json +0 -0
- comprehensive_converter.py +188 -0
- convert_comprehensive_kb.py +137 -0
- convert_kb_to_brain.py +53 -0
- ersNgumDownloadsalu-chatbot +220 -0
alu_brain/comprehensive_knowledge.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
comprehensive_converter.py
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Comprehensive Knowledge Base Converter
|
| 3 |
+
Converts the full ALU knowledge base to ALU Brain JSON format
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import json
|
| 7 |
+
import re
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
|
| 10 |
+
def parse_section(section_text, section_title):
|
| 11 |
+
"""Parse a section and create multiple entries"""
|
| 12 |
+
entries = []
|
| 13 |
+
|
| 14 |
+
# Split into subsections
|
| 15 |
+
subsections = re.split(r'\n###\s+', section_text)
|
| 16 |
+
|
| 17 |
+
for subsection in subsections:
|
| 18 |
+
if not subsection.strip():
|
| 19 |
+
continue
|
| 20 |
+
|
| 21 |
+
lines = subsection.split('\n')
|
| 22 |
+
subtitle = lines[0].strip() if lines else ""
|
| 23 |
+
content = '\n'.join(lines[1:]).strip()
|
| 24 |
+
|
| 25 |
+
if not content:
|
| 26 |
+
continue
|
| 27 |
+
|
| 28 |
+
# Create entry for this subsection
|
| 29 |
+
entry = {
|
| 30 |
+
"id": f"alu_kb_{len(entries)}_{subtitle.lower().replace(' ', '_')[:30]}",
|
| 31 |
+
"question": f"{section_title}: {subtitle}" if subtitle else section_title,
|
| 32 |
+
"answer": content,
|
| 33 |
+
"type": "text",
|
| 34 |
+
"keywords": extract_keywords(section_title + " " + subtitle + " " + content),
|
| 35 |
+
"category": categorize_section(section_title),
|
| 36 |
+
"source": "ALU Comprehensive Knowledge Base"
|
| 37 |
+
}
|
| 38 |
+
entries.append(entry)
|
| 39 |
+
|
| 40 |
+
return entries
|
| 41 |
+
|
| 42 |
+
def extract_keywords(text):
|
| 43 |
+
"""Extract relevant keywords from text"""
|
| 44 |
+
# Common words to exclude
|
| 45 |
+
stopwords = {'the', 'a', 'an', 'and', 'or', 'but', 'in', 'on', 'at', 'to', 'for', 'of', 'with', 'by', 'from', 'as', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'do', 'does', 'did', 'will', 'would', 'should', 'could', 'may', 'might', 'must', 'can', 'this', 'that', 'these', 'those', 'i', 'you', 'he', 'she', 'it', 'we', 'they', 'what', 'which', 'who', 'when', 'where', 'why', 'how', 'all', 'each', 'every', 'both', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very'}
|
| 46 |
+
|
| 47 |
+
# Extract words
|
| 48 |
+
words = re.findall(r'\b[a-z]{3,}\b', text.lower())
|
| 49 |
+
|
| 50 |
+
# Filter and get unique keywords
|
| 51 |
+
keywords = list(set([w for w in words if w not in stopwords]))[:15] # Limit to 15 keywords
|
| 52 |
+
|
| 53 |
+
return keywords
|
| 54 |
+
|
| 55 |
+
def categorize_section(title):
|
| 56 |
+
"""Categorize section based on title"""
|
| 57 |
+
title_lower = title.lower()
|
| 58 |
+
|
| 59 |
+
if any(word in title_lower for word in ['calendar', 'academic', 'term', 'schedule']):
|
| 60 |
+
return "Academic Calendar"
|
| 61 |
+
elif any(word in title_lower for word in ['spd', 'internship', 'career', 'job', 'placement']):
|
| 62 |
+
return "Career Services"
|
| 63 |
+
elif any(word in title_lower for word in ['immigration', 'visa', 'permit']):
|
| 64 |
+
return "Immigration Support"
|
| 65 |
+
elif any(word in title_lower for word in ['insurance', 'medical', 'health', 'eden care']):
|
| 66 |
+
return "Health & Wellness"
|
| 67 |
+
elif any(word in title_lower for word in ['student life', 'club', 'residential', 'housing']):
|
| 68 |
+
return "Campus Life"
|
| 69 |
+
elif any(word in title_lower for word in ['program', 'bel', 'degree', 'module', 'course']):
|
| 70 |
+
return "Academic Programs"
|
| 71 |
+
elif any(word in title_lower for word in ['policy', 'conduct', 'disciplinary', 'code']):
|
| 72 |
+
return "Policies & Conduct"
|
| 73 |
+
elif any(word in title_lower for word in ['event', 'student-led']):
|
| 74 |
+
return "Student Events"
|
| 75 |
+
elif any(word in title_lower for word in ['faq', 'question', 'frequently']):
|
| 76 |
+
return "FAQs"
|
| 77 |
+
else:
|
| 78 |
+
return "General Information"
|
| 79 |
+
|
| 80 |
+
def convert_comprehensive_kb():
|
| 81 |
+
"""Main conversion function"""
|
| 82 |
+
|
| 83 |
+
print("=" * 70)
|
| 84 |
+
print("๐ง COMPREHENSIVE KNOWLEDGE BASE CONVERTER")
|
| 85 |
+
print("=" * 70)
|
| 86 |
+
print()
|
| 87 |
+
|
| 88 |
+
# Read the comprehensive KB
|
| 89 |
+
kb_file = Path("data/alu_knowledge/alu_ultimate_knowledge_base.txt")
|
| 90 |
+
|
| 91 |
+
if not kb_file.exists():
|
| 92 |
+
print(f"โ Knowledge base file not found: {kb_file}")
|
| 93 |
+
return False
|
| 94 |
+
|
| 95 |
+
print(f"๐ Reading knowledge base from: {kb_file}")
|
| 96 |
+
with open(kb_file, 'r', encoding='utf-8') as f:
|
| 97 |
+
content = f.read()
|
| 98 |
+
|
| 99 |
+
print(f"โ
Loaded {len(content)} characters")
|
| 100 |
+
print()
|
| 101 |
+
|
| 102 |
+
# Split by major sections (## headers)
|
| 103 |
+
sections = re.split(r'\n##\s+', content)
|
| 104 |
+
|
| 105 |
+
all_entries = []
|
| 106 |
+
entry_id = 1
|
| 107 |
+
|
| 108 |
+
print("๐ Processing sections...")
|
| 109 |
+
for i, section in enumerate(sections[1:], 1): # Skip first empty section
|
| 110 |
+
if not section.strip():
|
| 111 |
+
continue
|
| 112 |
+
|
| 113 |
+
lines = section.split('\n')
|
| 114 |
+
section_title = lines[0].strip()
|
| 115 |
+
section_content = '\n'.join(lines[1:])
|
| 116 |
+
|
| 117 |
+
print(f" {i}. Processing: {section_title[:50]}...")
|
| 118 |
+
|
| 119 |
+
# Parse this section
|
| 120 |
+
section_entries = parse_section(section_content, section_title)
|
| 121 |
+
|
| 122 |
+
# Add unique IDs
|
| 123 |
+
for entry in section_entries:
|
| 124 |
+
entry['id'] = f"alu_kb_{entry_id:04d}"
|
| 125 |
+
entry_id += 1
|
| 126 |
+
|
| 127 |
+
all_entries.extend(section_entries)
|
| 128 |
+
|
| 129 |
+
print()
|
| 130 |
+
print(f"โ
Created {len(all_entries)} knowledge base entries")
|
| 131 |
+
print()
|
| 132 |
+
|
| 133 |
+
# Create the brain JSON structure
|
| 134 |
+
brain_data = {
|
| 135 |
+
"category": "comprehensive_knowledge",
|
| 136 |
+
"description": "Complete ALU Knowledge Base - Academic Calendar, SPD Hub, Immigration, Programs, Policies, and More",
|
| 137 |
+
"version": "2.0",
|
| 138 |
+
"last_updated": "2024-11-20",
|
| 139 |
+
"total_entries": len(all_entries),
|
| 140 |
+
"entries": all_entries
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
# Save to alu_brain directory
|
| 144 |
+
output_file = Path("alu_brain/comprehensive_knowledge.json")
|
| 145 |
+
print(f"๐พ Saving to: {output_file}")
|
| 146 |
+
|
| 147 |
+
with open(output_file, 'w', encoding='utf-8') as f:
|
| 148 |
+
json.dump(brain_data, f, indent=2, ensure_ascii=False)
|
| 149 |
+
|
| 150 |
+
print(f"โ
Successfully saved {len(all_entries)} entries")
|
| 151 |
+
print()
|
| 152 |
+
|
| 153 |
+
# Show statistics
|
| 154 |
+
categories = {}
|
| 155 |
+
for entry in all_entries:
|
| 156 |
+
cat = entry['category']
|
| 157 |
+
categories[cat] = categories.get(cat, 0) + 1
|
| 158 |
+
|
| 159 |
+
print("๐ Entries by Category:")
|
| 160 |
+
for cat, count in sorted(categories.items(), key=lambda x: x[1], reverse=True):
|
| 161 |
+
print(f" โข {cat}: {count} entries")
|
| 162 |
+
|
| 163 |
+
print()
|
| 164 |
+
print("=" * 70)
|
| 165 |
+
print("โ
CONVERSION COMPLETE!")
|
| 166 |
+
print("=" * 70)
|
| 167 |
+
print()
|
| 168 |
+
print("๐ Sample entries:")
|
| 169 |
+
for entry in all_entries[:5]:
|
| 170 |
+
print(f" โข {entry['question'][:60]}...")
|
| 171 |
+
|
| 172 |
+
print()
|
| 173 |
+
print("๐ Next steps:")
|
| 174 |
+
print(" 1. Review the generated JSON file")
|
| 175 |
+
print(" 2. Commit and push to Hugging Face")
|
| 176 |
+
print(" 3. Restart the Space")
|
| 177 |
+
print(" 4. Test with questions like:")
|
| 178 |
+
print(" - 'How to create a club at ALU?'")
|
| 179 |
+
print(" - 'Who founded ALU?'")
|
| 180 |
+
print(" - 'When is the next term?'")
|
| 181 |
+
print()
|
| 182 |
+
|
| 183 |
+
return True
|
| 184 |
+
|
| 185 |
+
if __name__ == "__main__":
|
| 186 |
+
convert_comprehensive_kb()
|
| 187 |
+
|
| 188 |
+
|
convert_comprehensive_kb.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Convert comprehensive knowledge base TXT to ALU Brain JSON format
|
| 3 |
+
This will make your knowledge base actually usable by the chatbot!
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import json
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
import re
|
| 9 |
+
|
| 10 |
+
def parse_comprehensive_kb():
|
| 11 |
+
"""Parse the comprehensive knowledge base text file"""
|
| 12 |
+
|
| 13 |
+
kb_file = Path("data/alu_knowledge/alu_ultimate_knowledge_base.txt")
|
| 14 |
+
|
| 15 |
+
if not kb_file.exists():
|
| 16 |
+
print(f"โ Knowledge base file not found: {kb_file}")
|
| 17 |
+
return None
|
| 18 |
+
|
| 19 |
+
with open(kb_file, 'r', encoding='utf-8') as f:
|
| 20 |
+
content = f.read()
|
| 21 |
+
|
| 22 |
+
# Split by major sections (## headers)
|
| 23 |
+
sections = re.split(r'\n##\s+', content)
|
| 24 |
+
|
| 25 |
+
entries = []
|
| 26 |
+
entry_id = 1
|
| 27 |
+
|
| 28 |
+
for section in sections:
|
| 29 |
+
if not section.strip():
|
| 30 |
+
continue
|
| 31 |
+
|
| 32 |
+
lines = section.split('\n')
|
| 33 |
+
title = lines[0].strip()
|
| 34 |
+
|
| 35 |
+
# Get the content
|
| 36 |
+
content_lines = []
|
| 37 |
+
current_subsection = ""
|
| 38 |
+
|
| 39 |
+
for line in lines[1:]:
|
| 40 |
+
if line.strip():
|
| 41 |
+
if line.startswith('###'): # Subsection
|
| 42 |
+
current_subsection = line.replace('###', '').strip()
|
| 43 |
+
elif line.startswith('-') or line.startswith('*'): # List item
|
| 44 |
+
content_lines.append(line.strip())
|
| 45 |
+
else:
|
| 46 |
+
content_lines.append(line.strip())
|
| 47 |
+
|
| 48 |
+
full_content = '\n'.join(content_lines)
|
| 49 |
+
|
| 50 |
+
# Create entry
|
| 51 |
+
entry = {
|
| 52 |
+
"id": f"comprehensive_kb_{entry_id}",
|
| 53 |
+
"question": f"Tell me about {title}",
|
| 54 |
+
"answer": full_content,
|
| 55 |
+
"type": "text",
|
| 56 |
+
"keywords": [word.lower() for word in title.split() if len(word) > 3],
|
| 57 |
+
"category": "Comprehensive Knowledge Base",
|
| 58 |
+
"source": "ALU Comprehensive KB"
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
entries.append(entry)
|
| 62 |
+
entry_id += 1
|
| 63 |
+
|
| 64 |
+
# Also create entries for common questions
|
| 65 |
+
if "club" in title.lower():
|
| 66 |
+
entries.append({
|
| 67 |
+
"id": f"comprehensive_kb_{entry_id}",
|
| 68 |
+
"question": "How to create a club at ALU?",
|
| 69 |
+
"answer": full_content,
|
| 70 |
+
"type": "text",
|
| 71 |
+
"keywords": ["club", "create", "start", "society", "organization"],
|
| 72 |
+
"category": "Campus Life",
|
| 73 |
+
"source": "ALU Comprehensive KB"
|
| 74 |
+
})
|
| 75 |
+
entry_id += 1
|
| 76 |
+
|
| 77 |
+
if "founder" in title.lower() or "leadership" in title.lower():
|
| 78 |
+
entries.append({
|
| 79 |
+
"id": f"comprehensive_kb_{entry_id}",
|
| 80 |
+
"question": "Who founded ALU?",
|
| 81 |
+
"answer": full_content,
|
| 82 |
+
"type": "text",
|
| 83 |
+
"keywords": ["founder", "fred", "swaniker", "leadership", "history"],
|
| 84 |
+
"category": "About ALU",
|
| 85 |
+
"source": "ALU Comprehensive KB"
|
| 86 |
+
})
|
| 87 |
+
entry_id += 1
|
| 88 |
+
|
| 89 |
+
return entries
|
| 90 |
+
|
| 91 |
+
def create_brain_json():
|
| 92 |
+
"""Create the ALU Brain JSON file"""
|
| 93 |
+
|
| 94 |
+
print("============================================================")
|
| 95 |
+
print("๐ง CONVERTING COMPREHENSIVE KB TO ALU BRAIN FORMAT")
|
| 96 |
+
print("============================================================\n")
|
| 97 |
+
|
| 98 |
+
entries = parse_comprehensive_kb()
|
| 99 |
+
|
| 100 |
+
if not entries:
|
| 101 |
+
print("โ No entries created")
|
| 102 |
+
return False
|
| 103 |
+
|
| 104 |
+
# Create the brain JSON structure
|
| 105 |
+
brain_data = {
|
| 106 |
+
"category": "comprehensive_knowledge",
|
| 107 |
+
"description": "Comprehensive ALU Knowledge Base - All ALU information",
|
| 108 |
+
"version": "1.0",
|
| 109 |
+
"last_updated": "2024-11-20",
|
| 110 |
+
"entries": entries
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
# Save to alu_brain directory
|
| 114 |
+
output_file = Path("alu_brain/comprehensive_knowledge.json")
|
| 115 |
+
with open(output_file, 'w', encoding='utf-8') as f:
|
| 116 |
+
json.dump(brain_data, f, indent=2, ensure_ascii=False)
|
| 117 |
+
|
| 118 |
+
print(f"โ
Created {len(entries)} entries")
|
| 119 |
+
print(f"๐ Saved to: {output_file}")
|
| 120 |
+
print(f"\n๐ Sample entries:")
|
| 121 |
+
for entry in entries[:3]:
|
| 122 |
+
print(f" - {entry['question']}")
|
| 123 |
+
|
| 124 |
+
print("\n============================================================")
|
| 125 |
+
print("โ
CONVERSION COMPLETE!")
|
| 126 |
+
print("============================================================")
|
| 127 |
+
print("\nNext steps:")
|
| 128 |
+
print("1. Commit and push this file to Hugging Face")
|
| 129 |
+
print("2. Restart the Space")
|
| 130 |
+
print("3. The chatbot will now use your comprehensive knowledge!")
|
| 131 |
+
|
| 132 |
+
return True
|
| 133 |
+
|
| 134 |
+
if __name__ == "__main__":
|
| 135 |
+
create_brain_json()
|
| 136 |
+
|
| 137 |
+
|
convert_kb_to_brain.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Convert comprehensive knowledge base to ALU Brain JSON format
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import json
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
|
| 8 |
+
def convert_kb_to_brain_format():
|
| 9 |
+
"""Convert the comprehensive KB into ALU Brain JSON entries"""
|
| 10 |
+
|
| 11 |
+
# Read the comprehensive knowledge base
|
| 12 |
+
kb_file = Path("data/alu_knowledge/alu_ultimate_knowledge_base.txt")
|
| 13 |
+
|
| 14 |
+
if not kb_file.exists():
|
| 15 |
+
print(f"โ Knowledge base file not found: {kb_file}")
|
| 16 |
+
return
|
| 17 |
+
|
| 18 |
+
with open(kb_file, 'r', encoding='utf-8') as f:
|
| 19 |
+
kb_text = f.read()
|
| 20 |
+
|
| 21 |
+
# Split into sections
|
| 22 |
+
sections = kb_text.split('\n## ')
|
| 23 |
+
|
| 24 |
+
brain_entries = []
|
| 25 |
+
|
| 26 |
+
for section in sections[1:]: # Skip first empty section
|
| 27 |
+
lines = section.split('\n')
|
| 28 |
+
title = lines[0].strip()
|
| 29 |
+
content = '\n'.join(lines[1:]).strip()
|
| 30 |
+
|
| 31 |
+
# Create brain entry
|
| 32 |
+
entry = {
|
| 33 |
+
"question": f"Tell me about {title}",
|
| 34 |
+
"answer": content[:500] + "..." if len(content) > 500 else content, # Limit length
|
| 35 |
+
"type": "text",
|
| 36 |
+
"category": "comprehensive_kb",
|
| 37 |
+
"keywords": title.lower().split()
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
brain_entries.append(entry)
|
| 41 |
+
|
| 42 |
+
# Save to new JSON file
|
| 43 |
+
output_file = Path("alu_brain/comprehensive_kb.json")
|
| 44 |
+
with open(output_file, 'w', encoding='utf-8') as f:
|
| 45 |
+
json.dump(brain_entries, f, indent=2)
|
| 46 |
+
|
| 47 |
+
print(f"โ
Created {len(brain_entries)} entries in {output_file}")
|
| 48 |
+
print(f"๐ Sample entry: {brain_entries[0]['question']}")
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
convert_kb_to_brain_format()
|
| 52 |
+
|
| 53 |
+
|
ersNgumDownloadsalu-chatbot
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[1mdiff --git a/app.py b/app.py[m
|
| 2 |
+
[1mindex 58d2706..20c00eb 100644[m
|
| 3 |
+
[1m--- a/app.py[m
|
| 4 |
+
[1m+++ b/app.py[m
|
| 5 |
+
[36m@@ -1,52 +1,173 @@[m
|
| 6 |
+
import os[m
|
| 7 |
+
import sys[m
|
| 8 |
+
[32m+[m[32mfrom pathlib import Path[m
|
| 9 |
+
[32m+[m[32mfrom fastapi import FastAPI, HTTPException[m
|
| 10 |
+
[32m+[m[32mfrom fastapi.staticfiles import StaticFiles[m
|
| 11 |
+
[32m+[m[32mfrom fastapi.responses import FileResponse, JSONResponse[m
|
| 12 |
+
[32m+[m[32mfrom fastapi.middleware.cors import CORSMiddleware[m
|
| 13 |
+
[m
|
| 14 |
+
[31m-# Set environment variables for Hugging Face[m
|
| 15 |
+
[31m-os.environ["TRANSFORMERS_CACHE"] = "/tmp/model_cache"[m
|
| 16 |
+
[31m-os.environ["HF_HOME"] = "/tmp/model_cache"[m
|
| 17 |
+
[31m-os.environ["SENTENCE_TRANSFORMERS_HOME"] = "/tmp/model_cache"[m
|
| 18 |
+
[31m-os.environ["PYTHONUNBUFFERED"] = "1"[m
|
| 19 |
+
[31m-[m
|
| 20 |
+
[31m-# Print startup diagnostic info[m
|
| 21 |
+
[32m+[m[32mprint("=" * 60)[m
|
| 22 |
+
print("=== STARTUP: Beginning application initialization ===")[m
|
| 23 |
+
[31m-print(f"=== STARTUP: PORT environment variable: {os.environ.get('PORT')} ===")[m
|
| 24 |
+
[32m+[m[32mprint("=" * 60)[m
|
| 25 |
+
[m
|
| 26 |
+
[31m-# First import just the app from main[m
|
| 27 |
+
[31m-from main import app[m
|
| 28 |
+
[32m+[m[32m# Add current directory to Python path so imports work[m
|
| 29 |
+
[32m+[m[32mcurrent_path = os.path.dirname(__file__)[m
|
| 30 |
+
[32m+[m[32mif current_path not in sys.path:[m
|
| 31 |
+
[32m+[m[32m sys.path.insert(0, current_path)[m
|
| 32 |
+
[m
|
| 33 |
+
[31m-# THEN import other components[m
|
| 34 |
+
[31m-from main import conversation_memory[m
|
| 35 |
+
[31m-from data_integration.alu_api_connector import ALUDataConnector[m
|
| 36 |
+
[31m-from analytics.conversation_analytics import ConversationAnalytics[m
|
| 37 |
+
[32m+[m[32m# Ensure required directories exist[m
|
| 38 |
+
[32m+[m[32mos.makedirs("build/static", exist_ok=True)[m
|
| 39 |
+
[32m+[m[32mos.makedirs("build/assets", exist_ok=True)[m
|
| 40 |
+
[m
|
| 41 |
+
[31m-@app.get("/api/alu-events")[m
|
| 42 |
+
[31m-async def get_alu_events(campus: str = "all", days: int = 7):[m
|
| 43 |
+
[31m- """Get upcoming events at ALU"""[m
|
| 44 |
+
[32m+[m[32m# Load comprehensive knowledge base on startup[m
|
| 45 |
+
[32m+[m[32mdef load_comprehensive_knowledge_base():[m
|
| 46 |
+
[32m+[m[32m """Load the comprehensive ALU knowledge base into vector store"""[m
|
| 47 |
+
try:[m
|
| 48 |
+
[31m- alu_connector = ALUDataConnector()[m
|
| 49 |
+
[31m- events = alu_connector.get_upcoming_events(campus, days)[m
|
| 50 |
+
[31m- return {"events": events}[m
|
| 51 |
+
[32m+[m[32m print("\n๐ Loading comprehensive ALU knowledge base...")[m
|
| 52 |
+
[32m+[m[32m from retrieval_engine import RetrievalEngine[m
|
| 53 |
+
[32m+[m[41m [m
|
| 54 |
+
[32m+[m[32m # Check for knowledge base files[m
|
| 55 |
+
[32m+[m[32m kb_paths = [[m
|
| 56 |
+
[32m+[m[32m Path("data/alu_knowledge"),[m
|
| 57 |
+
[32m+[m[32m Path("backend/data/alu_knowledge"),[m
|
| 58 |
+
[32m+[m[32m Path("/data/alu_knowledge")[m
|
| 59 |
+
[32m+[m[32m ][m
|
| 60 |
+
[32m+[m[41m [m
|
| 61 |
+
[32m+[m[32m kb_dir = None[m
|
| 62 |
+
[32m+[m[32m for path in kb_paths:[m
|
| 63 |
+
[32m+[m[32m if path.exists():[m
|
| 64 |
+
[32m+[m[32m kb_dir = path[m
|
| 65 |
+
[32m+[m[32m break[m
|
| 66 |
+
[32m+[m[41m [m
|
| 67 |
+
[32m+[m[32m if not kb_dir:[m
|
| 68 |
+
[32m+[m[32m print("โ ๏ธ Knowledge base directory not found. Using default ALU Brain.")[m
|
| 69 |
+
[32m+[m[32m return False[m
|
| 70 |
+
[32m+[m[41m [m
|
| 71 |
+
[32m+[m[32m # Find knowledge base file[m
|
| 72 |
+
[32m+[m[32m kb_files = [[m
|
| 73 |
+
[32m+[m[32m "alu_ultimate_knowledge_base.txt",[m
|
| 74 |
+
[32m+[m[32m "alu_knowledge_base.txt"[m
|
| 75 |
+
[32m+[m[32m ][m
|
| 76 |
+
[32m+[m[41m [m
|
| 77 |
+
[32m+[m[32m kb_file = None[m
|
| 78 |
+
[32m+[m[32m for filename in kb_files:[m
|
| 79 |
+
[32m+[m[32m file_path = kb_dir / filename[m
|
| 80 |
+
[32m+[m[32m if file_path.exists():[m
|
| 81 |
+
[32m+[m[32m kb_file = file_path[m
|
| 82 |
+
[32m+[m[32m break[m
|
| 83 |
+
[32m+[m[41m [m
|
| 84 |
+
[32m+[m[32m if not kb_file:[m
|
| 85 |
+
[32m+[m[32m print("โ ๏ธ Knowledge base file not found. Using default ALU Brain.")[m
|
| 86 |
+
[32m+[m[32m return False[m
|
| 87 |
+
[32m+[m[41m [m
|
| 88 |
+
[32m+[m[32m print(f"๐ Found knowledge base: {kb_file.name}")[m
|
| 89 |
+
[32m+[m[41m [m
|
| 90 |
+
[32m+[m[32m # Load and process[m
|
| 91 |
+
[32m+[m[32m with open(kb_file, 'r', encoding='utf-8') as f:[m
|
| 92 |
+
[32m+[m[32m kb_text = f.read()[m
|
| 93 |
+
[32m+[m[41m [m
|
| 94 |
+
[32m+[m[32m print(f"โ
Loaded {len(kb_text):,} characters")[m
|
| 95 |
+
[32m+[m[41m [m
|
| 96 |
+
[32m+[m[32m # Initialize retrieval engine[m
|
| 97 |
+
[32m+[m[32m retrieval_engine = RetrievalEngine()[m
|
| 98 |
+
[32m+[m[41m [m
|
| 99 |
+
[32m+[m[32m # Chunk text[m
|
| 100 |
+
[32m+[m[32m chunks = retrieval_engine._chunk_text(kb_text, chunk_size=800, chunk_overlap=100)[m
|
| 101 |
+
[32m+[m[32m print(f"โ
Created {len(chunks)} chunks")[m
|
| 102 |
+
[32m+[m[41m [m
|
| 103 |
+
[32m+[m[32m # Add to vector store[m
|
| 104 |
+
[32m+[m[32m print("๐ฅ Adding to vector store...")[m
|
| 105 |
+
[32m+[m[32m for i, chunk in enumerate(chunks):[m
|
| 106 |
+
[32m+[m[32m chunk_id = f"alu_comprehensive_kb_{i}"[m
|
| 107 |
+
[32m+[m[32m metadata = {[m
|
| 108 |
+
[32m+[m[32m "source": "ALU Comprehensive Knowledge Base 2024",[m
|
| 109 |
+
[32m+[m[32m "chunk_id": i,[m
|
| 110 |
+
[32m+[m[32m "type": "comprehensive_kb"[m
|
| 111 |
+
[32m+[m[32m }[m
|
| 112 |
+
[32m+[m[41m [m
|
| 113 |
+
[32m+[m[32m try:[m
|
| 114 |
+
[32m+[m[32m retrieval_engine.collection.add([m
|
| 115 |
+
[32m+[m[32m ids=[chunk_id],[m
|
| 116 |
+
[32m+[m[32m documents=[chunk],[m
|
| 117 |
+
[32m+[m[32m metadatas=[metadata][m
|
| 118 |
+
[32m+[m[32m )[m
|
| 119 |
+
[32m+[m[32m except Exception as e:[m
|
| 120 |
+
[32m+[m[32m if i == 0: # Only print error for first chunk[m
|
| 121 |
+
[32m+[m[32m print(f"โ ๏ธ Note: {e}")[m
|
| 122 |
+
[32m+[m[41m [m
|
| 123 |
+
[32m+[m[32m print(f"โ
Comprehensive knowledge base loaded! ({len(chunks)} chunks)")[m
|
| 124 |
+
[32m+[m[32m return True[m
|
| 125 |
+
[32m+[m[41m [m
|
| 126 |
+
except Exception as e:[m
|
| 127 |
+
[31m- print(f"Error fetching ALU events: {e}")[m
|
| 128 |
+
[31m- return {"events": [], "error": "Could not fetch events"}[m
|
| 129 |
+
[32m+[m[32m print(f"โ ๏ธ Error loading comprehensive KB: {e}")[m
|
| 130 |
+
[32m+[m[32m return False[m
|
| 131 |
+
[32m+[m
|
| 132 |
+
[32m+[m[32m# Load knowledge base (commented out to prevent startup timeout)[m
|
| 133 |
+
[32m+[m[32m# Uncomment this line once the Space is stable:[m
|
| 134 |
+
[32m+[m[32m# load_comprehensive_knowledge_base()[m
|
| 135 |
+
[32m+[m[32mprint("โ ๏ธ Comprehensive KB loading disabled to prevent timeout")[m
|
| 136 |
+
[32m+[m[32mprint("๐ก The chatbot will use the existing ALU Brain knowledge base")[m
|
| 137 |
+
[m
|
| 138 |
+
[31m-@app.get("/api/analytics/dashboard")[m
|
| 139 |
+
[31m-async def get_analytics_dashboard():[m
|
| 140 |
+
[31m- """Get analytics dashboard data"""[m
|
| 141 |
+
[32m+[m[32m# Import your backend app (using minimal version to avoid model loading issues)[m
|
| 142 |
+
[32m+[m[32mprint("\n๐ฆ Importing minimal backend application...")[m
|
| 143 |
+
[32m+[m[32mtry:[m
|
| 144 |
+
[32m+[m[32m from minimal_app import app as backend_app[m
|
| 145 |
+
[32m+[m[32m print("โ
Minimal backend application imported (no ML models required)")[m
|
| 146 |
+
[32m+[m[32mexcept ImportError:[m
|
| 147 |
+
[32m+[m[32m print("โ ๏ธ Minimal backend not found, trying lightweight...")[m
|
| 148 |
+
try:[m
|
| 149 |
+
[31m- if not conversation_memory:[m
|
| 150 |
+
[31m- return {"error": "Conversation memory not initialized"}[m
|
| 151 |
+
[31m- [m
|
| 152 |
+
[31m- analytics = ConversationAnalytics(conversation_memory)[m
|
| 153 |
+
[31m- dashboard_data = analytics.generate_dashboard_data()[m
|
| 154 |
+
[31m- return dashboard_data[m
|
| 155 |
+
[31m- except Exception as e:[m
|
| 156 |
+
[31m- print(f"Error generating analytics dashboard: {e}")[m
|
| 157 |
+
[31m- return {"error": f"Could not generate analytics: {str(e)}"}[m
|
| 158 |
+
[31m-[m
|
| 159 |
+
[31m-# This is needed for Hugging Face Spaces[m
|
| 160 |
+
[31m-if __name__ == "__main__":[m
|
| 161 |
+
[31m- import uvicorn[m
|
| 162 |
+
[31m- port = int(os.environ.get("PORT", 7860)) # Hugging Face uses port 7860[m
|
| 163 |
+
[31m- print(f"Starting server on port {port}")[m
|
| 164 |
+
[31m- uvicorn.run(app, host="0.0.0.0", port=port)[m
|
| 165 |
+
[m
|
| 166 |
+
[32m+[m[32m from main_lightweight import app as backend_app[m
|
| 167 |
+
[32m+[m[32m print("โ
Lightweight backend application imported")[m
|
| 168 |
+
[32m+[m[32m except ImportError:[m
|
| 169 |
+
[32m+[m[32m print("โ ๏ธ Using main backend...")[m
|
| 170 |
+
[32m+[m[32m from main import app as backend_app[m
|
| 171 |
+
[32m+[m[32m print("โ
Main backend application imported")[m
|
| 172 |
+
[32m+[m
|
| 173 |
+
[32m+[m[32mapp = FastAPI()[m
|
| 174 |
+
[32m+[m
|
| 175 |
+
[32m+[m[32m# Configure CORS - copy settings from your backend[m
|
| 176 |
+
[32m+[m[32mallowed_origins = os.getenv("CORS_ALLOWED_ORIGINS", "http://localhost:3000,http://localhost:3001").split(",")[m
|
| 177 |
+
[32m+[m[32mapp.add_middleware([m
|
| 178 |
+
[32m+[m[32m CORSMiddleware,[m
|
| 179 |
+
[32m+[m[32m allow_origins=["*"], # Allow all origins when serving from same domain[m
|
| 180 |
+
[32m+[m[32m allow_credentials=True,[m
|
| 181 |
+
[32m+[m[32m allow_methods=["*"],[m
|
| 182 |
+
[32m+[m[32m allow_headers=["*"],[m
|
| 183 |
+
[32m+[m[32m)[m
|
| 184 |
+
[32m+[m
|
| 185 |
+
[32m+[m[32m# Serve static files from React build[m
|
| 186 |
+
[32m+[m[32mapp.mount("/static", StaticFiles(directory="build/static"), name="static")[m
|
| 187 |
+
[32m+[m[32mapp.mount("/assets", StaticFiles(directory="build/assets", check_dir=False), name="assets")[m
|
| 188 |
+
[32m+[m
|
| 189 |
+
[32m+[m[32m# Mount your backend API - all backend routes will be served under /api[m
|
| 190 |
+
[32m+[m[32m# IMPORTANT: Mount this AFTER static files but BEFORE catch-all route[m
|
| 191 |
+
[32m+[m[32mapp.mount("/api", backend_app)[m
|
| 192 |
+
[32m+[m
|
| 193 |
+
[32m+[m[32m# Root endpoint - return JSON instead of trying to serve files[m
|
| 194 |
+
[32m+[m[32m@app.get("/")[m
|
| 195 |
+
[32m+[m[32masync def root():[m
|
| 196 |
+
[32m+[m[32m return {[m
|
| 197 |
+
[32m+[m[32m "status": "running",[m
|
| 198 |
+
[32m+[m[32m "message": "ALU Student Companion API",[m
|
| 199 |
+
[32m+[m[32m "api_endpoints": {[m
|
| 200 |
+
[32m+[m[32m "health": "/health",[m
|
| 201 |
+
[32m+[m[32m "chat": "/api/chat",[m
|
| 202 |
+
[32m+[m[32m "docs": "/docs"[m
|
| 203 |
+
[32m+[m[32m }[m
|
| 204 |
+
[32m+[m[32m }[m
|
| 205 |
+
[32m+[m
|
| 206 |
+
[32m+[m[32m# Health check endpoint[m
|
| 207 |
+
[32m+[m[32m@app.get("/health")[m
|
| 208 |
+
[32m+[m[32masync def health():[m
|
| 209 |
+
[32m+[m[32m return {"status": "healthy", "api": True, "backend": True}[m
|
| 210 |
+
[32m+[m
|
| 211 |
+
[32m+[m[32m# Serve React app for specific paths only (not catch-all)[m
|
| 212 |
+
[32m+[m[32m@app.get("/static/{full_path:path}")[m
|
| 213 |
+
[32m+[m[32masync def serve_static(full_path: str):[m
|
| 214 |
+
[32m+[m[32m file_path = f"build/static/{full_path}"[m
|
| 215 |
+
[32m+[m[32m if os.path.exists(file_path):[m
|
| 216 |
+
[32m+[m[32m return FileResponse(file_path)[m
|
| 217 |
+
[32m+[m[32m return JSONResponse([m
|
| 218 |
+
[32m+[m[32m content={"error": "File not found"},[m
|
| 219 |
+
[32m+[m[32m status_code=404[m
|
| 220 |
+
[32m+[m[32m )[m
|