# 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