Spaces:
Running
Running
| from pydantic_settings import BaseSettings | |
| from functools import lru_cache | |
| import os | |
| import base64 | |
| import json | |
| class Settings(BaseSettings): | |
| groq_api_key: str | |
| google_drive_folder_id: str | |
| google_application_credentials: str = "credentials.json" | |
| vector_store_path: str = "data/vector_store" | |
| chunk_size: int = 800 | |
| chunk_overlap: int = 150 | |
| top_k_results: int = 3 | |
| class Config: | |
| env_file = ".env" | |
| def get_google_credentials_dict(self) -> dict: | |
| """ | |
| Get Google credentials as dictionary. | |
| Handles both local file and base64 encoded env variable (for HuggingFace Spaces) | |
| """ | |
| # Check if credentials are provided as base64 in environment variable | |
| credentials_base64 = os.getenv("GOOGLE_CREDENTIALS_BASE64") | |
| if credentials_base64: | |
| # Decode base64 credentials (for HuggingFace Spaces) | |
| try: | |
| credentials_json = base64.b64decode(credentials_base64).decode('utf-8') | |
| credentials_dict = json.loads(credentials_json) | |
| # Also write to file for compatibility | |
| with open("credentials.json", "w") as f: | |
| f.write(credentials_json) | |
| print("✅ Google credentials loaded from GOOGLE_CREDENTIALS_BASE64 environment variable") | |
| return credentials_dict | |
| except Exception as e: | |
| raise Exception(f"Error decoding Google credentials from environment variable: {str(e)}") | |
| # Otherwise, read from file (for local development) | |
| elif os.path.exists(self.google_application_credentials): | |
| with open(self.google_application_credentials, 'r') as f: | |
| credentials_dict = json.load(f) | |
| print("✅ Google credentials loaded from credentials.json file") | |
| return credentials_dict | |
| else: | |
| raise Exception( | |
| "Google credentials not found. Please either:\n" | |
| "1. Set GOOGLE_CREDENTIALS_BASE64 environment variable (for HuggingFace Spaces), or\n" | |
| "2. Place credentials.json file in the project root (for local development)" | |
| ) | |
| def get_settings(): | |
| return Settings() |