File size: 3,390 Bytes
a71f11a
 
 
 
e034bd8
a71f11a
e034bd8
 
a71f11a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e034bd8
a71f11a
 
 
 
 
 
 
 
 
 
e034bd8
 
 
a71f11a
 
 
 
 
 
 
 
 
 
e034bd8
 
 
a71f11a
 
 
 
 
 
 
e034bd8
 
 
 
 
 
a71f11a
 
 
 
 
 
 
 
 
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
# Modified data/content.py for Hugging Face Spaces
import os
import json
from transformers import pipeline

# Mock content database for demo purposes
class ContentManager:
    def __init__(self):
        # Initialize with sample content
        self.content_db = {
            "content1": {
                "title": "Introduction to Machine Learning",
                "text": "Machine learning is a branch of artificial intelligence that focuses on building applications that learn from data and improve their accuracy over time without being programmed to do so.",
                "tags": ["AI", "beginner"]
            },
            "content2": {
                "title": "Python for Data Science",
                "text": "Python has become the most popular programming language for data science due to its simplicity and the vast ecosystem of data-oriented libraries.",
                "tags": ["programming", "intermediate"]
            }
        }
        
        # Try to initialize Firebase if credentials exist
        try:
            self._init_firebase()
        except Exception as e:
            print(f"Firebase initialization failed: {e}")
            print("Using mock content database instead")
    
    def _init_firebase(self):
        import firebase_admin
        from firebase_admin import credentials
        from firebase_admin import firestore
        
        # Check if already initialized
        if not firebase_admin._apps:
            # Try to get credentials from environment variable
            if 'FIREBASE_CONFIG' in os.environ:
                config_json = os.environ.get('FIREBASE_CONFIG')
                cred = credentials.Certificate(json.loads(config_json))
                firebase_admin.initialize_app(cred)
                self.db = firestore.client()
                self.collection = self.db.collection('content')
                self.use_firebase = True
            else:
                raise ValueError("Firebase credentials not found")
    
    def get_by_id(self, content_id):
        """Retrieve content by ID"""
        # Try mock database first
        if content_id in self.content_db:
            return self.content_db[content_id]
        
        # If not in mock DB and Firebase is initialized, try Firebase
        if hasattr(self, 'use_firebase') and self.use_firebase:
            doc = self.collection.document(content_id).get()
            return doc.to_dict() if doc.exists else None
        
        return None
    
    def save_content(self, content_data):
        """Save new content"""
        # Generate a simple ID for mock database
        if not hasattr(self, 'use_firebase') or not self.use_firebase:
            content_id = f"content{len(self.content_db) + 1}"
            self.content_db[content_id] = content_data
            return content_id
        
        # Otherwise use Firebase
        doc_ref = self.collection.document()
        doc_ref.set(content_data)
        return doc_ref.id
    
    def update_content(self, content_id, updates):
        """Update existing content"""
        if content_id in self.content_db:
            self.content_db[content_id].update(updates)
            return True
            
        if hasattr(self, 'use_firebase') and self.use_firebase:
            self.collection.document(content_id).update(updates)
            return True
            
        return False