Spaces:
Sleeping
Sleeping
feat: introduce configurable content options and privacy controls for spiritual care messages.
Browse files- .gitignore +1 -0
- src/core/ai_client.py +42 -3
- src/interface/simplified_gradio_app.py +1 -2
.gitignore
CHANGED
|
@@ -119,3 +119,4 @@ spiritual_app.py
|
|
| 119 |
start.sh
|
| 120 |
.zshenv
|
| 121 |
src/core/verification_store.py
|
|
|
|
|
|
| 119 |
start.sh
|
| 120 |
.zshenv
|
| 121 |
src/core/verification_store.py
|
| 122 |
+
manual_tests/test_vertex_auth.py
|
src/core/ai_client.py
CHANGED
|
@@ -9,6 +9,8 @@ with automatic fallback and provider-specific optimizations.
|
|
| 9 |
import os
|
| 10 |
import json
|
| 11 |
import logging
|
|
|
|
|
|
|
| 12 |
from datetime import datetime
|
| 13 |
from typing import Optional, Dict, Any, List
|
| 14 |
from abc import ABC, abstractmethod
|
|
@@ -101,11 +103,48 @@ class GeminiClient(BaseAIClient):
|
|
| 101 |
if not GEMINI_AVAILABLE:
|
| 102 |
raise ImportError("Google GenAI library not available. Install with: pip install google-genai")
|
| 103 |
|
|
|
|
| 104 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
self.client = genai.Client(api_key=api_key)
|
| 109 |
self.model_name = model.value
|
| 110 |
|
| 111 |
def generate_response(self, system_prompt: str, user_prompt: str, temperature: Optional[float] = None) -> str:
|
|
|
|
| 9 |
import os
|
| 10 |
import json
|
| 11 |
import logging
|
| 12 |
+
import base64
|
| 13 |
+
import tempfile
|
| 14 |
from datetime import datetime
|
| 15 |
from typing import Optional, Dict, Any, List
|
| 16 |
from abc import ABC, abstractmethod
|
|
|
|
| 103 |
if not GEMINI_AVAILABLE:
|
| 104 |
raise ImportError("Google GenAI library not available. Install with: pip install google-genai")
|
| 105 |
|
| 106 |
+
gcp_base64 = os.getenv("GCP_SERVICE_ACCOUNT_B64")
|
| 107 |
api_key = os.getenv("GEMINI_API_KEY")
|
| 108 |
+
|
| 109 |
+
if gcp_base64:
|
| 110 |
+
try:
|
| 111 |
+
# Decode Service Account JSON from Base64
|
| 112 |
+
creds_json = base64.b64decode(gcp_base64).decode('utf-8')
|
| 113 |
+
|
| 114 |
+
# Create temporary file for credentials
|
| 115 |
+
# Library expects a file path in GOOGLE_APPLICATION_CREDENTIALS
|
| 116 |
+
self.temp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False)
|
| 117 |
+
self.temp_file.write(creds_json)
|
| 118 |
+
self.temp_file.flush()
|
| 119 |
+
|
| 120 |
+
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = self.temp_file.name
|
| 121 |
+
|
| 122 |
+
# Read project_id from credentials to initialize Vertex AI
|
| 123 |
+
creds_dict = json.loads(creds_json)
|
| 124 |
+
project_id = creds_dict.get("project_id")
|
| 125 |
+
|
| 126 |
+
# Initialize client with Vertex AI mode
|
| 127 |
+
self.client = genai.Client(
|
| 128 |
+
vertexai=True,
|
| 129 |
+
project=project_id,
|
| 130 |
+
location=os.getenv("GOOGLE_CLOUD_LOCATION", "us-central1")
|
| 131 |
+
)
|
| 132 |
+
logging.info(f"Initialized Gemini via Vertex AI (Project: {project_id})")
|
| 133 |
+
except Exception as e:
|
| 134 |
+
logging.error(f"Error initializing Gemini with Service Account: {e}")
|
| 135 |
+
# Fallback to API Key if Service Account fails
|
| 136 |
+
if api_key:
|
| 137 |
+
self.client = genai.Client(api_key=api_key)
|
| 138 |
+
logging.info("Fallback: Initialized Gemini via API Key after SA failure")
|
| 139 |
+
else:
|
| 140 |
+
raise ValueError(f"Failed to initialize Gemini with Service Account and no API Key found: {e}")
|
| 141 |
+
elif api_key:
|
| 142 |
+
# Traditional API Key initialization
|
| 143 |
+
self.client = genai.Client(api_key=api_key)
|
| 144 |
+
logging.info("Initialized Gemini via API Key")
|
| 145 |
+
else:
|
| 146 |
+
raise ValueError("No Gemini configuration found (GCP_SERVICE_ACCOUNT_B64 or GEMINI_API_KEY)")
|
| 147 |
|
|
|
|
| 148 |
self.model_name = model.value
|
| 149 |
|
| 150 |
def generate_response(self, system_prompt: str, user_prompt: str, temperature: Optional[float] = None) -> str:
|
src/interface/simplified_gradio_app.py
CHANGED
|
@@ -703,8 +703,7 @@ Use the **Download Summary** button below to access the complete provider summar
|
|
| 703 |
stats,
|
| 704 |
gr.update(visible=show_provider_panel), # provider_summary_content visibility
|
| 705 |
status_msg, # provider_summary_status content
|
| 706 |
-
gr.update(value=html_content, visible=True) if show_provider_panel else gr.update(visible=False)
|
| 707 |
-
spiritual_care_msg # spiritual_care_message content
|
| 708 |
)
|
| 709 |
|
| 710 |
def handle_clear(session: SimplifiedSessionData):
|
|
|
|
| 703 |
stats,
|
| 704 |
gr.update(visible=show_provider_panel), # provider_summary_content visibility
|
| 705 |
status_msg, # provider_summary_status content
|
| 706 |
+
gr.update(value=html_content, visible=True) if show_provider_panel else gr.update(visible=False) # provider_summary_display content
|
|
|
|
| 707 |
)
|
| 708 |
|
| 709 |
def handle_clear(session: SimplifiedSessionData):
|