Download firebase_config.py from dashgaming/slyte: direct link, hf CLI and curl.
- Browser
- Download file 1.69 kB
-
https://huggingface.co/dashgaming/slyte/resolve/main/firebase_config.py
- Command line
-
hf download hf://dashgaming/slyte/firebase_config.py
-
curl -L -o firebase_config.py https://huggingface.co/dashgaming/slyte/resolve/main/firebase_config.py
1.69 kB
| import os | |
| import streamlit as st | |
| import firebase_admin | |
| from firebase_admin import credentials, firestore | |
| def initialize_firebase(): | |
| """ | |
| Initializes Firebase Admin SDK and returns Firestore client. | |
| Ensures initialization happens only once. | |
| """ | |
| # Check if Firebase is already initialized | |
| if not firebase_admin._apps: | |
| try: | |
| # 1. Try to load from Streamlit Secrets (for Cloud Deployment) | |
| has_secrets = False | |
| try: | |
| if "firebase_key" in st.secrets: | |
| has_secrets = True | |
| except Exception: | |
| pass | |
| if has_secrets: | |
| # Convert AttrDict to regular dict if needed | |
| key_dict = dict(st.secrets["firebase_key"]) | |
| cred = credentials.Certificate(key_dict) | |
| firebase_admin.initialize_app(cred) | |
| # 2. Try to load from local file (for Local Development) | |
| else: | |
| current_dir = os.path.dirname(os.path.abspath(__file__)) | |
| key_path = os.path.join(current_dir, "serviceAccountKey.json") | |
| if os.path.exists(key_path): | |
| cred = credentials.Certificate(key_path) | |
| firebase_admin.initialize_app(cred) | |
| else: | |
| st.error("Firebase credentials not found (checked secrets and local file).") | |
| st.stop() | |
| except Exception as e: | |
| st.error(f"Failed to initialize Firebase: {e}") | |
| st.stop() | |
| # Return Firestore client instance | |
| return firestore.client() | |