MilanM commited on
Commit
e4ddfde
·
verified ·
1 Parent(s): 11cb24f

Update visualizer_app.py

Browse files
Files changed (1) hide show
  1. visualizer_app.py +84 -67
visualizer_app.py CHANGED
@@ -70,94 +70,94 @@ with app.setup:
70
 
71
  @app.cell
72
  def client_variables(client_instantiation_form):
73
- if client_instantiation_form.value:
74
- client_setup = client_instantiation_form.value
75
- else:
76
- client_setup = None
77
-
78
  ### Extract Credential Variables:
79
- if client_setup is not None:
80
- wx_url = client_setup["wx_region"]
81
- wx_api_key = client_setup["wx_api_key"].strip()
82
- os.environ["WATSONX_APIKEY"] = wx_api_key
83
-
84
- if client_setup["project_id"] is not None:
85
- project_id = client_setup["project_id"].strip()
86
- else:
87
- project_id = None
88
-
89
- if client_setup["space_id"] is not None:
90
- space_id = client_setup["space_id"].strip()
91
- else:
92
- space_id = None
93
-
94
  else:
95
  os.environ["WATSONX_APIKEY"] = ""
96
- project_id = None
97
- space_id = None
98
- wx_api_key = None
99
- wx_url = None
100
  return client_setup, project_id, space_id, wx_api_key, wx_url
101
 
102
 
103
- @app.cell
104
- def _(client_setup, wx_api_key):
105
- if client_setup:
106
- token = get_iam_token(wx_api_key)
107
- else:
108
- token = None
109
- return
110
 
111
  @app.cell
112
  def _():
113
- baked_in_creds = {
114
- "api_key": os.environ.get("WX_APIKEY"),
115
- "project_id": os.environ.get("WX_PROJECT_ID"),
116
- "space_id": "",
117
- }
118
- return baked_in_creds
119
 
120
 
121
  @app.cell
122
  def client_instantiation(
 
 
 
 
 
 
123
  client_setup,
 
 
124
  project_id,
125
  space_id,
 
126
  wx_api_key,
127
  wx_url,
128
  ):
129
  ### Instantiate the watsonx.ai client
130
  if client_setup:
131
  try:
132
- wx_credentials = Credentials(
133
- url=wx_url,
134
- api_key=wx_api_key
 
 
 
 
 
 
 
 
 
135
  )
136
-
137
- if project_id:
138
- project_client = APIClient(credentials=wx_credentials, project_id=project_id)
139
- else:
140
- project_client = None
141
-
142
- if space_id:
143
- deployment_client = APIClient(credentials=wx_credentials, space_id=space_id)
144
- else:
145
- deployment_client = None
146
 
147
- client_status = mo.md("### ✅ Client Instantiation Successful ✅")
 
 
 
 
 
148
  client_callout_kind = "success"
149
-
150
  except Exception as e:
151
  error_message = str(e)
152
- client_status = mo.md(f"### ❌ Client Instantiation Failed\n**Error:** {error_message}\n\nCheck your region selection and credentials")
 
 
153
  client_callout_kind = "danger"
154
- project_client = None
155
- deployment_client = None
156
  else:
157
- wx_credentials = None
158
- project_client = None
159
- deployment_client = None
160
- client_status = mo.md("### This box will turn Green if client instantiation is successful")
 
 
 
161
  client_callout_kind = "neutral"
162
 
163
  return (
@@ -366,7 +366,7 @@ def client_instantiation_form():
366
  "AU": "https://au-syd.ml.cloud.ibm.com",
367
  "CA": "https://ca-tor.ml.cloud.ibm.com"
368
  }
369
-
370
  # Create a form with multiple elements
371
  client_instantiation_form = (
372
  mo.md('''
@@ -384,13 +384,30 @@ def client_instantiation_form():
384
  > If you provide both you can switch the active one in the dropdown.
385
  ''')
386
  .batch(
387
- wx_region = mo.ui.dropdown(regions, label="Select your watsonx.ai region:", value="EU", searchable=True),
388
- wx_api_key = mo.ui.text(placeholder="Add your IBM Cloud api-key...", label="IBM Cloud Api-key:",
389
- kind="password", value=get_cred_value('api_key', creds_var_name='baked_in_creds')),
390
- project_id = mo.ui.text(placeholder="Add your watsonx.ai project_id...", label="Project_ID:",
391
- kind="password", value=get_cred_value('project_id', creds_var_name='baked_in_creds')),
392
- space_id = mo.ui.text(placeholder="Add your watsonx.ai space_id...", label="Space_ID:",
393
- kind="password", value=get_cred_value('space_id', creds_var_name='baked_in_creds'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
394
  ,)
395
  .form(show_clear_button=True, bordered=False)
396
  )
 
70
 
71
  @app.cell
72
  def client_variables(client_instantiation_form):
73
+ client_setup = client_instantiation_form.value or None
74
+
 
 
 
75
  ### Extract Credential Variables:
76
+ if client_setup:
77
+ wx_url = client_setup["wx_region"] if if client_setup["wx_region"] else "EU"
78
+ wx_api_key = client_setup["wx_api_key"].strip() if client_setup["wx_api_key"] else None
79
+ os.environ["WATSONX_APIKEY"] = wx_api_key or ""
80
+
81
+ project_id = client_setup["project_id"].strip() if client_setup["project_id"] else None
82
+ space_id = client_setup["space_id"].strip() if client_setup["space_id"] else None
 
 
 
 
 
 
 
 
83
  else:
84
  os.environ["WATSONX_APIKEY"] = ""
85
+ project_id = space_id = wx_api_key = wx_url = None
86
+
 
 
87
  return client_setup, project_id, space_id, wx_api_key, wx_url
88
 
89
 
90
+ # @app.cell
91
+ # def _(client_setup, wx_api_key):
92
+ # if client_setup:
93
+ # token = get_iam_token(wx_api_key)
94
+ # else:
95
+ # token = None
96
+ # return
97
 
98
  @app.cell
99
  def _():
100
+ from baked_in_credentials.creds import credentials
101
+ return credentials
 
 
 
 
102
 
103
 
104
  @app.cell
105
  def client_instantiation(
106
+ APIClient,
107
+ Credentials,
108
+ client,
109
+ client_key,
110
+ client_options,
111
+ client_selector,
112
  client_setup,
113
+ get_key_by_value,
114
+ mo,
115
  project_id,
116
  space_id,
117
+ wrap_with_spaces,
118
  wx_api_key,
119
  wx_url,
120
  ):
121
  ### Instantiate the watsonx.ai client
122
  if client_setup:
123
  try:
124
+ wx_credentials = Credentials(url=wx_url, api_key=wx_api_key)
125
+
126
+ project_client = (
127
+ APIClient(credentials=wx_credentials, project_id=project_id)
128
+ if project_id
129
+ else None
130
+ )
131
+
132
+ deployment_client = (
133
+ APIClient(credentials=wx_credentials, space_id=space_id)
134
+ if space_id
135
+ else None
136
  )
 
 
 
 
 
 
 
 
 
 
137
 
138
+ active_client_name = get_key_by_value(client_options, client_key)
139
+ client_status = mo.md(
140
+ f"### ✅ Client Instantiation Successful ✅\n\n"
141
+ f"{client_selector}\n\n"
142
+ f"**Active Client:**{wrap_with_spaces(active_client_name, prefix_spaces=5)}"
143
+ )
144
  client_callout_kind = "success"
145
+
146
  except Exception as e:
147
  error_message = str(e)
148
+ client_status = mo.md(
149
+ f"### ❌ Client Instantiation Failed\n**Error:** {error_message}\n\nCheck your region selection and credentials"
150
+ )
151
  client_callout_kind = "danger"
152
+ wx_credentials = project_client = deployment_client = None
 
153
  else:
154
+ wx_credentials = project_client = deployment_client = None
155
+ active_client_name = get_key_by_value(client_options, client_key)
156
+ client_status = mo.md(
157
+ f"### Client Instantiation Status will turn Green When Ready\n\n"
158
+ f"{client_selector}\n\n"
159
+ f"**Active Client:**{wrap_with_spaces(active_client_name, prefix_spaces=5)}"
160
+ )
161
  client_callout_kind = "neutral"
162
 
163
  return (
 
366
  "AU": "https://au-syd.ml.cloud.ibm.com",
367
  "CA": "https://ca-tor.ml.cloud.ibm.com"
368
  }
369
+ baked_in_creds = credentials
370
  # Create a form with multiple elements
371
  client_instantiation_form = (
372
  mo.md('''
 
384
  > If you provide both you can switch the active one in the dropdown.
385
  ''')
386
  .batch(
387
+ wx_region = mo.ui.dropdown(
388
+ wx_regions,
389
+ label="Select your watsonx.ai region:",
390
+ value=get_cred_value('region', creds_var_name='baked_in_creds'),
391
+ searchable=True
392
+ ),
393
+ wx_api_key = mo.ui.text(
394
+ placeholder="Add your IBM Cloud api-key...",
395
+ label="IBM Cloud Api-key:",
396
+ kind="password",
397
+ value=get_cred_value('api_key', creds_var_name='baked_in_creds')
398
+ ),
399
+ project_id = mo.ui.text(
400
+ placeholder="Add your watsonx.ai project_id...",
401
+ label="Project_ID:",
402
+ kind="text",
403
+ value=get_cred_value('project_id', creds_var_name='baked_in_creds')
404
+ ),
405
+ space_id = mo.ui.text(
406
+ placeholder="Add your watsonx.ai space_id...",
407
+ label="Space_ID:",
408
+ kind="text",
409
+ value=get_cred_value('space_id', creds_var_name='baked_in_creds')
410
+ )
411
  ,)
412
  .form(show_clear_button=True, bordered=False)
413
  )