MSU576 commited on
Commit
ad36b34
·
verified ·
1 Parent(s): 6eb1af3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -5
app.py CHANGED
@@ -27,18 +27,22 @@ from reportlab.lib import colors
27
  # ---------------------------
28
  # Authenticate Earth Engine (with HF Secrets)
29
  # ---------------------------
30
- import ee, os, json
31
 
32
  service_account = os.environ.get("EE_SERVICE_ACCOUNT") # service account email
33
- key_json = os.environ.get("EE_PRIVATE_KEY") # should be one-line JSON
34
 
35
  if service_account and key_json:
36
- key_dict = json.loads(key_json) # 👈 this only works if EE_PRIVATE_KEY is compact JSON (no line breaks)
37
- creds = ee.ServiceAccountCredentials(service_account, key_dict)
 
 
 
 
 
38
  ee.Initialize(creds)
39
  else:
40
  raise RuntimeError("Earth Engine authentication failed. Did you set HF secrets?")
41
-
42
  # Helper Functions
43
  # ---------------------------
44
  def _fmt_pct(x):
 
27
  # ---------------------------
28
  # Authenticate Earth Engine (with HF Secrets)
29
  # ---------------------------
30
+ import ee, os, tempfile
31
 
32
  service_account = os.environ.get("EE_SERVICE_ACCOUNT") # service account email
33
+ key_json = os.environ.get("EE_PRIVATE_KEY") # the JSON text as stored in HF Secrets
34
 
35
  if service_account and key_json:
36
+ # Write the JSON string to a temporary file
37
+ with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f:
38
+ f.write(key_json)
39
+ temp_key_path = f.name
40
+
41
+ # Authenticate using the file path
42
+ creds = ee.ServiceAccountCredentials(service_account, temp_key_path)
43
  ee.Initialize(creds)
44
  else:
45
  raise RuntimeError("Earth Engine authentication failed. Did you set HF secrets?")
 
46
  # Helper Functions
47
  # ---------------------------
48
  def _fmt_pct(x):