Spaces:
Running on Zero

File size: 5,166 Bytes
f2baa25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Knowledge Manager - Wikipedia Retrieval and Vector Database
Automatically learns new topics and stores them offline
"""

import os
import json
import wikipediaapi
import chromadb
from chromadb.config import Settings
from sentence_transformers import SentenceTransformer
from typing import List, Dict, Any, Optional
import logging

logger = logging.getLogger(__name__)

class KnowledgeManager:
    """Manages knowledge base with Wikipedia learning and ChromaDB storage."""

    def __init__(self):
        self.db_path = "db"
        self.collection_name = "knowledge_base"

        # Initialize ChromaDB
        self.client = chromadb.PersistentClient(path=self.db_path, settings=Settings(anonymized_telemetry=False))

        # Get or create collection
        try:
            self.collection = self.client.get_collection(name=self.collection_name)
        except:
            self.collection = self.client.create_collection(name=self.collection_name)

        # Initialize embedding model
        self.embed_model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

        # Initialize Wikipedia API
        self.wiki = wikipediaapi.Wikipedia(
            language='en',
            extract_format=wikipediaapi.ExtractFormat.WIKI,
            user_agent='AI-Trustworthiness-System/1.0'
        )

        logger.info("Knowledge Manager initialized")

    def get_stats(self) -> Dict[str, int]:
        """Get knowledge base statistics."""
        try:
            count = self.collection.count()
            return {"documents": count, "chunks": count}
        except:
            return {"documents": 0, "chunks": 0}

    def search(self, query: str, top_k: int = 5) -> List[Dict]:
        """Search knowledge base for relevant documents."""

        # Check if we have any documents
        if self.collection.count() == 0:
            # Try to learn about the topic
            self._learn_topic(query)
            if self.collection.count() == 0:
                return []

        # Embed query
        query_embedding = self.embed_model.encode([query])[0]

        # Search
        results = self.collection.query(
            query_embeddings=[query_embedding],
            n_results=top_k,
            include=['documents', 'metadatas']
        )

        # Format results
        documents = []
        if results['documents'] and len(results['documents']) > 0:
            for i, doc in enumerate(results['documents'][0]):
                metadata = results['metadatas'][0][i] if results['metadatas'] else {}
                documents.append({
                    'page_content': doc,
                    'metadata': metadata
                })

        return documents

    def _learn_topic(self, topic: str):
        """Learn about a topic from Wikipedia and store in database."""

        try:
            # Search for the topic
            page = self.wiki.page(topic)

            if not page.exists():
                # Try a more general search
                search_results = self.wiki.search(topic, results=1)
                if search_results:
                    page = self.wiki.page(search_results[0])

            if page.exists():
                # Get page content
                content = page.text

                # Clean and chunk the content
                chunks = self._chunk_text(content, chunk_size=500, overlap=50)

                # Embed chunks
                embeddings = self.embed_model.encode(chunks)

                # Store in database
                ids = [f"{topic}_{i}" for i in range(len(chunks))]
                metadatas = [{"topic": topic, "source": "wikipedia", "url": page.fullurl} for _ in chunks]

                self.collection.add(
                    embeddings=embeddings,
                    documents=chunks,
                    metadatas=metadatas,
                    ids=ids
                )

                logger.info(f"Learned about topic: {topic} ({len(chunks)} chunks)")

        except Exception as e:
            logger.error(f"Failed to learn about topic {topic}: {e}")

    def _chunk_text(self, text: str, chunk_size: int = 500, overlap: int = 50) -> List[str]:
        """Split text into overlapping chunks."""

        words = text.split()
        chunks = []

        for i in range(0, len(words), chunk_size - overlap):
            chunk = words[i:i + chunk_size]
            if len(chunk) > 0:
                chunks.append(' '.join(chunk))

        return chunks

    def find_related_video(self, query: str) -> Optional[Dict[str, str]]:
        """Find related educational video (placeholder - would integrate with YouTube API)."""

        # This is a placeholder - in a real implementation, you'd search YouTube
        # For now, return a sample video
        return {
            "video_url": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
            "title": "Related Educational Video"
        }

    def get_source_url(self, query: str) -> Optional[str]:
        """Get source URL for a query."""

        results = self.search(query, top_k=1)
        if results:
            return results[0].get('metadata', {}).get('url')

        return None