MilanM commited on
Commit
ae59b45
·
verified ·
1 Parent(s): 1667ee6

Update visualizer_app.py

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