Spaces:
Sleeping
Sleeping
Update app/config.py
Browse files- app/config.py +36 -48
app/config.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
from pydantic_settings import BaseSettings
|
| 2 |
from functools import lru_cache
|
|
|
|
| 3 |
import base64
|
| 4 |
import json
|
| 5 |
|
| 6 |
class Settings(BaseSettings):
|
| 7 |
groq_api_key: str
|
| 8 |
-
google_credentials_base64: str
|
| 9 |
google_drive_folder_id: str
|
|
|
|
| 10 |
vector_store_path: str = "data/vector_store"
|
| 11 |
chunk_size: int = 800
|
| 12 |
chunk_overlap: int = 150
|
|
@@ -17,54 +18,41 @@ class Settings(BaseSettings):
|
|
| 17 |
|
| 18 |
def get_google_credentials_dict(self) -> dict:
|
| 19 |
"""
|
| 20 |
-
|
|
|
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
credentials_json = base64.b64decode(self.google_credentials_base64).decode('utf-8')
|
| 25 |
-
# Parse JSON
|
| 26 |
-
return json.loads(credentials_json)
|
| 27 |
-
except Exception as e:
|
| 28 |
-
raise ValueError(f"Failed to decode Google credentials: {str(e)}")
|
| 29 |
-
|
| 30 |
-
@lru_cache()
|
| 31 |
-
def get_settings():
|
| 32 |
-
return Settings()
|
| 33 |
-
# from pydantic_settings import BaseSettings
|
| 34 |
-
# from functools import lru_cache
|
| 35 |
-
# import base64
|
| 36 |
-
# import json
|
| 37 |
-
# import tempfile
|
| 38 |
-
# import os
|
| 39 |
-
|
| 40 |
-
# class Settings(BaseSettings):
|
| 41 |
-
# groq_api_key: str
|
| 42 |
-
# google_credentials_base64: str
|
| 43 |
-
# google_drive_folder_id: str
|
| 44 |
-
# vector_store_path: str = "data/vector_store"
|
| 45 |
-
# chunk_size: int = 800
|
| 46 |
-
# chunk_overlap: int = 150
|
| 47 |
-
# top_k_results: int = 3
|
| 48 |
-
|
| 49 |
-
# class Config:
|
| 50 |
-
# env_file = ".env"
|
| 51 |
-
|
| 52 |
-
# def get_google_credentials_path(self) -> str:
|
| 53 |
-
# """
|
| 54 |
-
# Decode base64 credentials and write to a temporary file.
|
| 55 |
-
# Returns the path to the credentials file.
|
| 56 |
-
# """
|
| 57 |
-
# # Decode base64 string
|
| 58 |
-
# credentials_json = base64.b64decode(self.google_credentials_base64).decode('utf-8')
|
| 59 |
|
| 60 |
-
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from pydantic_settings import BaseSettings
|
| 2 |
from functools import lru_cache
|
| 3 |
+
import os
|
| 4 |
import base64
|
| 5 |
import json
|
| 6 |
|
| 7 |
class Settings(BaseSettings):
|
| 8 |
groq_api_key: str
|
|
|
|
| 9 |
google_drive_folder_id: str
|
| 10 |
+
google_application_credentials: str = "credentials.json"
|
| 11 |
vector_store_path: str = "data/vector_store"
|
| 12 |
chunk_size: int = 800
|
| 13 |
chunk_overlap: int = 150
|
|
|
|
| 18 |
|
| 19 |
def get_google_credentials_dict(self) -> dict:
|
| 20 |
"""
|
| 21 |
+
Get Google credentials as dictionary.
|
| 22 |
+
Handles both local file and base64 encoded env variable (for HuggingFace Spaces)
|
| 23 |
"""
|
| 24 |
+
# Check if credentials are provided as base64 in environment variable
|
| 25 |
+
credentials_base64 = os.getenv("GOOGLE_CREDENTIALS_BASE64")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
if credentials_base64:
|
| 28 |
+
# Decode base64 credentials (for HuggingFace Spaces)
|
| 29 |
+
try:
|
| 30 |
+
credentials_json = base64.b64decode(credentials_base64).decode('utf-8')
|
| 31 |
+
credentials_dict = json.loads(credentials_json)
|
| 32 |
+
|
| 33 |
+
# Also write to file for compatibility
|
| 34 |
+
with open("credentials.json", "w") as f:
|
| 35 |
+
f.write(credentials_json)
|
| 36 |
+
|
| 37 |
+
print("✅ Google credentials loaded from GOOGLE_CREDENTIALS_BASE64 environment variable")
|
| 38 |
+
return credentials_dict
|
| 39 |
+
except Exception as e:
|
| 40 |
+
raise Exception(f"Error decoding Google credentials from environment variable: {str(e)}")
|
| 41 |
|
| 42 |
+
# Otherwise, read from file (for local development)
|
| 43 |
+
elif os.path.exists(self.google_application_credentials):
|
| 44 |
+
with open(self.google_application_credentials, 'r') as f:
|
| 45 |
+
credentials_dict = json.load(f)
|
| 46 |
+
print("✅ Google credentials loaded from credentials.json file")
|
| 47 |
+
return credentials_dict
|
| 48 |
+
|
| 49 |
+
else:
|
| 50 |
+
raise Exception(
|
| 51 |
+
"Google credentials not found. Please either:\n"
|
| 52 |
+
"1. Set GOOGLE_CREDENTIALS_BASE64 environment variable (for HuggingFace Spaces), or\n"
|
| 53 |
+
"2. Place credentials.json file in the project root (for local development)"
|
| 54 |
+
)
|
| 55 |
|
| 56 |
+
@lru_cache()
|
| 57 |
+
def get_settings():
|
| 58 |
+
return Settings()
|