harivarshannn commited on
Commit
4f5826b
·
1 Parent(s): 76064c1

fix docker robust secret fetching via /run/secrets

Browse files
Files changed (1) hide show
  1. model.py +18 -2
model.py CHANGED
@@ -27,8 +27,16 @@ def check_ollama_connection() -> bool:
27
  global client
28
  api_key = os.getenv("GROQ_API_KEY")
29
 
 
 
 
 
 
 
 
 
30
  if not api_key:
31
- print("Error: GROQ_API_KEY not found in environment variables.", flush=True)
32
  return False
33
 
34
  try:
@@ -51,9 +59,17 @@ def generate_with_ollama(prompt: str, model: str = OLLAMA_MODEL) -> str:
51
  global client
52
  api_key = os.getenv("GROQ_API_KEY")
53
 
 
 
 
 
 
 
 
 
54
  if not client:
55
  if not api_key:
56
- return "Error: Groq API key not found in environment variables."
57
  try:
58
  client = Groq(api_key=api_key)
59
  except Exception as e:
 
27
  global client
28
  api_key = os.getenv("GROQ_API_KEY")
29
 
30
+ # Hugging Face Docker Spaces secure secret mounting mechanism
31
+ if not api_key and os.path.exists("/run/secrets/GROQ_API_KEY"):
32
+ try:
33
+ with open("/run/secrets/GROQ_API_KEY", "r") as f:
34
+ api_key = f.read().strip()
35
+ except:
36
+ pass
37
+
38
  if not api_key:
39
+ print("Error: GROQ_API_KEY not found in environment variables or /run/secrets.", flush=True)
40
  return False
41
 
42
  try:
 
59
  global client
60
  api_key = os.getenv("GROQ_API_KEY")
61
 
62
+ # Hugging Face Docker Spaces secure secret mounting mechanism
63
+ if not api_key and os.path.exists("/run/secrets/GROQ_API_KEY"):
64
+ try:
65
+ with open("/run/secrets/GROQ_API_KEY", "r") as f:
66
+ api_key = f.read().strip()
67
+ except:
68
+ pass
69
+
70
  if not client:
71
  if not api_key:
72
+ return "Error: Groq API key not found in environment variables or /run/secrets."
73
  try:
74
  client = Groq(api_key=api_key)
75
  except Exception as e: