Create visual_env_utils.py
Browse files- visual_env_utils.py +94 -0
visual_env_utils.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import netrc
|
2 |
+
import os
|
3 |
+
from typing import List, Optional, Tuple
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
def initialize_env_variables(prod_mode: bool = False, additional_env_vars: Optional[List[str]] = None) -> None:
|
8 |
+
if additional_env_vars is None:
|
9 |
+
additional_env_vars = []
|
10 |
+
|
11 |
+
if not prod_mode:
|
12 |
+
# In non-prod mode, prioritize environment variables
|
13 |
+
st.session_state.SAMBANOVA_API_KEY = os.environ.get(
|
14 |
+
'SAMBANOVA_API_KEY', st.session_state.get('SMABANOVA_API_KEY', '')
|
15 |
+
)
|
16 |
+
for var in additional_env_vars:
|
17 |
+
st.session_state[var] = os.environ.get(var, st.session_state.get(var, ''))
|
18 |
+
else:
|
19 |
+
# In prod mode, only use session state
|
20 |
+
if 'SAMBANOVA_API_KEY' not in st.session_state:
|
21 |
+
st.session_state.SAMBANOVA_API_KEY = ''
|
22 |
+
for var in additional_env_vars:
|
23 |
+
if var not in st.session_state:
|
24 |
+
st.session_state[var] = ''
|
25 |
+
|
26 |
+
|
27 |
+
def set_env_variables(api_key, additional_vars=None, prod_mode=False):
|
28 |
+
st.session_state.SAMBANOVA_API_KEY = api_key
|
29 |
+
if additional_vars:
|
30 |
+
for key, value in additional_vars.items():
|
31 |
+
st.session_state[key] = value
|
32 |
+
if not prod_mode:
|
33 |
+
# In non-prod mode, also set environment variables
|
34 |
+
os.environ['SAMBANOVA_API_KEY'] = api_key
|
35 |
+
if additional_vars:
|
36 |
+
for key, value in additional_vars.items():
|
37 |
+
os.environ[key] = value
|
38 |
+
|
39 |
+
|
40 |
+
def env_input_fields(additional_env_vars=None) -> Tuple[str, str]:
|
41 |
+
if additional_env_vars is None:
|
42 |
+
additional_env_vars = []
|
43 |
+
|
44 |
+
api_key = st.text_input('Sambanova API Key', value=st.session_state.SAMBANOVA_API_KEY, type='password')
|
45 |
+
|
46 |
+
additional_vars = {}
|
47 |
+
for var in additional_env_vars:
|
48 |
+
additional_vars[var] = st.text_input(f'{var}', value=st.session_state.get(var, ''), type='password')
|
49 |
+
|
50 |
+
return api_key, additional_vars
|
51 |
+
|
52 |
+
|
53 |
+
def are_credentials_set(additional_env_vars=None) -> bool:
|
54 |
+
if additional_env_vars is None:
|
55 |
+
additional_env_vars = []
|
56 |
+
|
57 |
+
base_creds_set = bool(st.session_state.SAMBANOVA_API_KEY)
|
58 |
+
additional_creds_set = all(bool(st.session_state.get(var, '')) for var in additional_env_vars)
|
59 |
+
|
60 |
+
return base_creds_set and additional_creds_set
|
61 |
+
|
62 |
+
|
63 |
+
def save_credentials(api_key, additional_vars=None, prod_mode=False) -> str:
|
64 |
+
set_env_variables(api_key, additional_vars, prod_mode)
|
65 |
+
return 'Credentials saved successfully!'
|
66 |
+
|
67 |
+
|
68 |
+
def get_wandb_key():
|
69 |
+
# Check for WANDB_API_KEY in environment variables
|
70 |
+
env_wandb_api_key = os.getenv('WANDB_API_KEY')
|
71 |
+
|
72 |
+
# Check for WANDB_API_KEY in ~/.netrc
|
73 |
+
try:
|
74 |
+
netrc_path = os.path.expanduser('~/.netrc')
|
75 |
+
netrc_data = netrc.netrc(netrc_path)
|
76 |
+
netrc_wandb_api_key = netrc_data.authenticators('api.wandb.ai')
|
77 |
+
except (FileNotFoundError, netrc.NetrcParseError):
|
78 |
+
netrc_wandb_api_key = None
|
79 |
+
|
80 |
+
# If both are set, handle the conflict
|
81 |
+
if env_wandb_api_key and netrc_wandb_api_key:
|
82 |
+
print('WANDB_API_KEY is set in both the environment and ~/.netrc. Prioritizing environment variable.')
|
83 |
+
# Optionally, you can choose to remove one of them, here we remove the env variable
|
84 |
+
del os.environ['WANDB_API_KEY'] # Remove from environment to prioritize ~/.netrc
|
85 |
+
return netrc_wandb_api_key[2] if netrc_wandb_api_key else None # Return the key from .netrc
|
86 |
+
|
87 |
+
# Return the key from environment if available, otherwise from .netrc
|
88 |
+
if env_wandb_api_key:
|
89 |
+
return env_wandb_api_key
|
90 |
+
elif netrc_wandb_api_key:
|
91 |
+
return netrc_wandb_api_key[2] if netrc_wandb_api_key else None
|
92 |
+
|
93 |
+
# If neither is set, return None
|
94 |
+
return None
|