Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,10 +34,22 @@ daily_queries = [0, 0, 0, 0, 0, 6, 8, 10, 7, 9, 12, 15, 11, 14] # Mock data for
|
|
| 34 |
|
| 35 |
# Initialize Gemini API with service account credentials
|
| 36 |
def init_gemini_api():
|
| 37 |
-
"""Initialize Google Gemini API with service account credentials"""
|
| 38 |
try:
|
| 39 |
-
#
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
credentials = service_account.Credentials.from_service_account_file(
|
| 42 |
credentials_path,
|
| 43 |
scopes=["https://www.googleapis.com/auth/cloud-platform"]
|
|
@@ -45,15 +57,18 @@ def init_gemini_api():
|
|
| 45 |
|
| 46 |
# Configure Gemini API with credentials
|
| 47 |
genai.configure(credentials=credentials)
|
| 48 |
-
print("Gemini API initialized with service account credentials")
|
| 49 |
return True
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
print(f"Error initializing Gemini API: {e}")
|
|
|
|
| 52 |
# Fallback to API key method if service account fails
|
| 53 |
try:
|
| 54 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY", ""))
|
| 55 |
print("Gemini API initialized with API key")
|
| 56 |
return True
|
|
|
|
| 57 |
except Exception as e2:
|
| 58 |
print(f"Fallback to API key also failed: {e2}")
|
| 59 |
return False
|
|
|
|
| 34 |
|
| 35 |
# Initialize Gemini API with service account credentials
|
| 36 |
def init_gemini_api():
|
| 37 |
+
"""Initialize Google Gemini API with service account credentials from Hugging Face Secrets"""
|
| 38 |
try:
|
| 39 |
+
# Retrieve service account JSON from Hugging Face Secrets
|
| 40 |
+
service_account_json = os.getenv("SERVICE_ACCOUNT_JSON")
|
| 41 |
+
if not service_account_json:
|
| 42 |
+
raise ValueError("SERVICE_ACCOUNT_JSON is not set in environment variables")
|
| 43 |
+
|
| 44 |
+
# Convert the single-line string back to JSON format
|
| 45 |
+
service_account_dict = json.loads(service_account_json)
|
| 46 |
+
|
| 47 |
+
# Write it to a temporary JSON file
|
| 48 |
+
credentials_path = "service_account.json"
|
| 49 |
+
with open(credentials_path, "w") as f:
|
| 50 |
+
json.dump(service_account_dict, f)
|
| 51 |
+
|
| 52 |
+
# Load credentials from the temporary file
|
| 53 |
credentials = service_account.Credentials.from_service_account_file(
|
| 54 |
credentials_path,
|
| 55 |
scopes=["https://www.googleapis.com/auth/cloud-platform"]
|
|
|
|
| 57 |
|
| 58 |
# Configure Gemini API with credentials
|
| 59 |
genai.configure(credentials=credentials)
|
| 60 |
+
print("Gemini API initialized with service account credentials from Hugging Face Secrets")
|
| 61 |
return True
|
| 62 |
+
|
| 63 |
except Exception as e:
|
| 64 |
+
print(f"Error initializing Gemini API with service account: {e}")
|
| 65 |
+
|
| 66 |
# Fallback to API key method if service account fails
|
| 67 |
try:
|
| 68 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY", ""))
|
| 69 |
print("Gemini API initialized with API key")
|
| 70 |
return True
|
| 71 |
+
|
| 72 |
except Exception as e2:
|
| 73 |
print(f"Fallback to API key also failed: {e2}")
|
| 74 |
return False
|