MilanM commited on
Commit
58bf5d4
·
verified ·
1 Parent(s): 683710e

Update app_v3.py

Browse files
Files changed (1) hide show
  1. app_v3.py +181 -1200
app_v3.py CHANGED
@@ -1,113 +1,22 @@
1
  import marimo
2
 
3
- __generated_with = "0.11.16"
4
- app = marimo.App(width="medium")
5
 
6
 
7
  @app.cell
8
  def _():
9
  import marimo as mo
10
- import os
11
- return mo, os
12
-
13
-
14
- @app.cell
15
- def _():
16
- def get_markdown_content(file_path):
17
- with open(file_path, 'r', encoding='utf-8') as file:
18
- content = file.read()
19
- return content
20
- return (get_markdown_content,)
21
-
22
-
23
- @app.cell
24
- def _(get_markdown_content, mo):
25
- intro_text = get_markdown_content('intro_markdown/intro.md')
26
- intro_marimo = get_markdown_content('intro_markdown/intro_marimo.md')
27
- intro_notebook = get_markdown_content('intro_markdown/intro_notebook.md')
28
- intro_comparison = get_markdown_content('intro_markdown/intro_comparison.md')
29
-
30
- intro = mo.carousel([
31
- mo.md(f"{intro_text}"),
32
- mo.md(f"{intro_marimo}"),
33
- mo.md(f"{intro_notebook}"),
34
- mo.md(f"{intro_comparison}"),
35
- ])
36
-
37
- mo.accordion({"## Notebook Introduction":intro})
38
- return intro, intro_comparison, intro_marimo, intro_notebook, intro_text
39
-
40
-
41
- @app.cell
42
- def _(os):
43
- ### Imports
44
- from typing import (
45
- Any, Dict, List, Optional, Pattern, Set, Union, Tuple
46
- )
47
- from pathlib import Path
48
- from urllib.request import urlopen
49
- # from rich.markdown import Markdown as Markd
50
- from rich.text import Text
51
- from rich import print
52
- from tqdm import tqdm
53
- from enum import Enum
54
  import pandas as pd
55
- import tempfile
56
- import requests
57
- import getpass
58
- import urllib3
59
- import base64
60
- import time
61
  import json
62
- import uuid
63
- import ssl
64
- import ast
65
- import re
66
-
67
- pd.set_option('display.max_columns', None)
68
- pd.set_option('display.max_rows', None)
69
- pd.set_option('display.max_colwidth', None)
70
- pd.set_option('display.width', None)
71
-
72
- # Set explicit temporary directory
73
- os.environ['TMPDIR'] = '/tmp'
74
-
75
- # Make sure Python's tempfile module also uses this directory
76
- tempfile.tempdir = '/tmp'
77
- return (
78
- Any,
79
- Dict,
80
- Enum,
81
- List,
82
- Optional,
83
- Path,
84
- Pattern,
85
- Set,
86
- Text,
87
- Tuple,
88
- Union,
89
- ast,
90
- base64,
91
- getpass,
92
- json,
93
- pd,
94
- print,
95
- re,
96
- requests,
97
- ssl,
98
- tempfile,
99
- time,
100
- tqdm,
101
- urllib3,
102
- urlopen,
103
- uuid,
104
- )
105
 
106
 
107
  @app.cell
108
  def _(mo):
109
- ### Credentials for the watsonx.ai SDK client
110
-
111
  # Endpoints
112
  wx_platform_url = "https://api.dataplatform.cloud.ibm.com"
113
  regions = {
@@ -128,1219 +37,291 @@ def _(mo):
128
 
129
  {wx_api_key}
130
 
131
- {space_id}
132
- ''').style(max_height="300px", overflow="auto", border_color="blue")
 
133
  .batch(
134
  wx_region = mo.ui.dropdown(regions, label="Select your watsonx.ai region:", value="US", searchable=True),
135
  wx_api_key = mo.ui.text(placeholder="Add your IBM Cloud api-key...", label="IBM Cloud Api-key:", kind="password"),
136
- # project_id = mo.ui.text(placeholder="Add your watsonx.ai project_id...", label="Project_ID:", kind="text"),
137
  space_id = mo.ui.text(placeholder="Add your watsonx.ai space_id...", label="Space_ID:", kind="text")
138
  ,)
139
  .form(show_clear_button=True, bordered=False)
140
  )
141
 
142
-
143
- # client_instantiation_form
144
  return client_instantiation_form, regions, wx_platform_url
145
 
146
 
147
  @app.cell
148
  def _(client_instantiation_form, mo):
149
  from ibm_watsonx_ai import APIClient, Credentials
 
150
 
151
- def setup_task_credentials(deployment_client):
 
 
 
 
 
 
 
152
  # Get existing task credentials
153
- existing_credentials = deployment_client.task_credentials.get_details()
154
 
155
  # Delete existing credentials if any
156
  if "resources" in existing_credentials and existing_credentials["resources"]:
157
  for cred in existing_credentials["resources"]:
158
- cred_id = deployment_client.task_credentials.get_id(cred)
159
- deployment_client.task_credentials.delete(cred_id)
160
 
161
  # Store new credentials
162
- return deployment_client.task_credentials.store()
163
 
164
  if client_instantiation_form.value:
165
  ### Instantiate the watsonx.ai client
 
 
166
  wx_credentials = Credentials(
167
  url=client_instantiation_form.value["wx_region"],
168
- api_key=client_instantiation_form.value["wx_api_key"]
169
  )
170
 
171
- # project_client = APIClient(credentials=wx_credentials, project_id=client_instantiation_form.value["project_id"])
172
- deployment_client = APIClient(credentials=wx_credentials, space_id=client_instantiation_form.value["space_id"])
 
 
 
173
 
174
- task_credentials_details = setup_task_credentials(deployment_client)
175
  else:
176
- # project_client = None
177
- deployment_client = None
178
  task_credentials_details = None
 
 
179
 
180
- template_variant = mo.ui.dropdown(["Base","Stream Files to IBM COS [Example]"], label="Code Template:", value="Base")
181
 
182
- if deployment_client is not None:
183
  client_callout_kind = "success"
184
  else:
185
  client_callout_kind = "neutral"
186
-
187
- client_callout = mo.callout(template_variant, kind=client_callout_kind)
188
-
189
- # client_callout
190
  return (
191
  APIClient,
192
  Credentials,
193
- client_callout,
194
  client_callout_kind,
195
- deployment_client,
 
 
 
196
  setup_task_credentials,
197
  task_credentials_details,
198
- template_variant,
 
199
  wx_credentials,
200
  )
201
 
202
 
203
  @app.cell
204
- def _(
205
- client_callout,
206
- client_instantiation_form,
207
- deploy_fnc,
208
- deployment_definition,
209
- fm,
210
- function_editor,
211
- hw_selection_table,
212
- mo,
213
- purge_tabs,
214
- sc_m,
215
- schema_editors,
216
- selection_table,
217
- upload_func,
218
- ):
219
- s1 = mo.md(f'''
220
- ###**Instantiate your watsonx.ai client:**
221
-
222
- 1. Select a region from the dropdown menu
223
-
224
- 2. Provide an IBM Cloud Apikey and watsonx.ai deployment space id
225
-
226
- 3. Once you submit, the area with the code template will turn green if successful
227
-
228
- 4. Select a base (provide baseline format) or example code function template
229
-
230
- ---
231
-
232
- {client_instantiation_form}
233
-
234
- ---
235
-
236
- {client_callout}
237
-
238
- ''')
239
-
240
- sc_tabs = mo.ui.tabs(
241
- {
242
- "Schema Option Selection": sc_m,
243
- "Schema Definition": mo.md(f"""
244
- ####**Edit the schema definitions you selected in the previous tab.**<br>
245
- {schema_editors}"""),
246
- }
247
- )
248
-
249
- s2 = mo.md(f'''###**Create your function from the template:**
250
-
251
- 1. Use the code editor window to create a function to deploy
252
- <br>
253
- The function must:
254
- <br>
255
- --- Include a payload and score element
256
- <br>
257
- --- Have the same function name in both the score = <name>() segment and the Function Name input field below
258
- <br>
259
- --- Additional details can be found here -> [watsonx.ai - Writing deployable Python functions
260
- ](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/ml-deploy-py-function-write.html?utm_medium=Exinfluencer&utm_source=ibm_developer&utm_content=in_content_link&utm_term=10006555&utm_id=blogs_awb-tekton-optimizations-for-kubeflow-pipelines-2-0&context=wx&audience=wdp)
261
-
262
- 3. Click submit, then proceed to select whether you wish to add:
263
- <br>
264
- --- An input schema (describing the format of the variables the function takes) **[Optional]**
265
- <br>
266
- --- An output schema (describing the format of the output results the function returns) **[Optional]**
267
- <br>
268
- --- An sample input example (showing an example of a mapping of the input and output schema to actual values.) **[Optional]**
269
-
270
- 4. Fill in the function name field **(must be exactly the same as in the function editor)**
271
-
272
- 5. Add a description and metadata tags **[Optional]**
273
-
274
- ---
275
-
276
- {function_editor}
277
-
278
- ---
279
-
280
- {sc_tabs}
281
-
282
- ---
283
-
284
- {fm}
285
-
286
- ''')
287
-
288
- s3 = mo.md(f'''
289
- ###**Review and Upload your function**
290
-
291
- 1. Review the function metadata specs JSON
292
-
293
- 2. Select a software specification if necessary (default for python functions is pre-selected), this is the runtime environment of python that your function will run in. Environments on watsonx.ai come pre-packaged with many different libraries, if necessary install new ones by adding them into the function as a `subprocess.check_output('pip install <package_name>', shell=True)` command.
294
-
295
- 3. Once your are satisfied, click the upload function button and wait for the response.
296
-
297
- > If you see no table of software specs, you haven't activated your watsonx.ai client.
298
-
299
- ---
300
-
301
- {selection_table}
302
-
303
- ---
304
-
305
- {upload_func}
306
-
307
- ''')
308
-
309
- s4 = mo.md(f'''
310
- ###**Deploy your function:**
311
-
312
- 1. Select a hardware specification (vCPUs/GB) that you want your function deployed on
313
- <br>
314
- --- XXS and XS cost the same (0.5 CUH per hour, so XS is the better option
315
- <br>
316
- --- Select larger instances for more resource intensive tasks or runnable jobs
317
-
318
- 2. Select the type of deployment:
319
- <br>
320
- --- Function (Online) for always-on endpoints - Always available and low latency, but consume resources continuously for every hour they are deployed.
321
- <br>
322
- --- Batch (Batch) for runnable jobs - Only consume resources during job runs, but aren't as flexible to deploy.
323
-
324
- 3. If you've selected Function, pick a completely unique (globally, not just your account) deployment serving name that will be in the endpoint url.
325
-
326
- 4. Once your are satisfied, click the deploy function button and wait for the response.
327
 
328
- ---
329
 
330
- {hw_selection_table}
331
-
332
- ---
333
-
334
- {deployment_definition}
335
-
336
- ---
337
-
338
- {deploy_fnc}
339
-
340
- ''')
341
-
342
- s5 = mo.md(f'''
343
- ###**Helper Purge Functions:**
344
-
345
- These functions help you retrieve, select and delete deployments, data assets or repository assets (functions, models, etc.) that you have in the deployment space. This is meant to support fast cleanup.
346
-
347
- Select the tab based on what you want to delete, then click each of the buttons one by one after the previous gives a response.
348
-
349
- ---
350
-
351
- {purge_tabs}
352
-
353
- ''')
354
-
355
- sections = mo.accordion(
356
- {
357
- "Section 1: **watsonx.ai Credentials**": s1,
358
- "Section 2: **Function Creation**": s2,
359
- "Section 3: **Function Upload**": s3,
360
- "Section 4: **Function Deployment**": s4,
361
- "Section 5: **Helper Functions**": s5,
362
- },
363
- multiple=True
364
- )
365
-
366
- sections
367
- return s1, s2, s3, s4, s5, sc_tabs, sections
368
 
369
 
370
  @app.cell
371
- def _(mo, template_variant):
372
- # Template for WatsonX.ai deployable function
373
- if template_variant.value == "Stream Files to IBM COS [Example]":
374
- with open("stream_files_to_cos.py", "r") as file:
375
- template = file.read()
376
- else:
377
- template = '''def your_function_name():
378
-
379
- import subprocess
380
- subprocess.check_output('pip install gensim', shell=True)
381
- import gensim
382
-
383
- def score(input_data):
384
- message_from_input_payload = payload.get("input_data")[0].get("values")[0][0]
385
- response_message = "Received message - {0}".format(message_from_input_payload)
386
-
387
- # Score using the pre-defined model
388
- score_response = {
389
- 'predictions': [{'fields': ['Response_message_field', 'installed_lib_version'],
390
- 'values': [[response_message, gensim.__version__]]
391
- }]
392
- }
393
- return score_response
394
-
395
- return score
396
-
397
- score = your_function_name()
398
- '''
399
-
400
- function_editor = (
401
- mo.md('''
402
- #### **Create your function by editing the template:**
403
-
404
- {editor}
405
-
406
- ''')
407
- .batch(
408
- editor = mo.ui.code_editor(value=template, language="python", min_height=50)
409
- )
410
- .form(show_clear_button=True, bordered=False)
411
- )
412
-
413
- # function_editor
414
- return file, function_editor, template
415
-
416
-
417
- @app.cell
418
- def _(function_editor, mo, os):
419
- if function_editor.value:
420
- # Get the edited code from the function editor
421
- code = function_editor.value['editor']
422
- # Create a namespace to execute the code in
423
- namespace = {}
424
- # Execute the code
425
- exec(code, namespace)
426
-
427
- # Find the first function defined in the namespace
428
- function_name = None
429
- for name, obj in namespace.items():
430
- if callable(obj) and name != "__builtins__":
431
- function_name = name
432
- break
433
-
434
- if function_name:
435
- # Instantiate the deployable function
436
- deployable_function = namespace[function_name]
437
- # Now deployable_function contains the score function
438
- mo.md(f"Created deployable function from '{function_name}'")
439
- # Create the directory if it doesn't exist
440
- save_dir = "/tmp/notebook_functions"
441
- os.makedirs(save_dir, exist_ok=True)
442
- # Save the function code to a file
443
- file_path = os.path.join(save_dir, f"{function_name}.py")
444
- with open(file_path, "w") as f:
445
- f.write(code)
446
- else:
447
- mo.md("No function found in the editor code")
448
- return (
449
- code,
450
- deployable_function,
451
- f,
452
- file_path,
453
- function_name,
454
- name,
455
- namespace,
456
- obj,
457
- save_dir,
458
- )
459
-
460
-
461
- @app.cell
462
- def _(deployment_client, mo, pd):
463
- if deployment_client:
464
- supported_specs = deployment_client.software_specifications.list()[
465
- deployment_client.software_specifications.list()['STATE'] == 'supported'
466
- ]
467
-
468
- # Reset the index to start from 0
469
- supported_specs = supported_specs.reset_index(drop=True)
470
-
471
- # Create a mapping dictionary for framework names based on software specifications
472
- framework_mapping = {
473
- "tensorflow_rt24.1-py3.11": "TensorFlow",
474
- "pytorch-onnx_rt24.1-py3.11": "PyTorch",
475
- "onnxruntime_opset_19": "ONNX or ONNXRuntime",
476
- "runtime-24.1-py3.11": "AI Services/Python Functions/Python Scripts",
477
- "autoai-ts_rt24.1-py3.11": "AutoAI",
478
- "autoai-kb_rt24.1-py3.11": "AutoAI",
479
- "runtime-24.1-py3.11-cuda": "CUDA-enabled (GPU) Python Runtime",
480
- "runtime-24.1-r4.3": "R Runtime 4.3",
481
- "spark-mllib_3.4": "Apache Spark 3.4",
482
- "autoai-rag_rt24.1-py3.11": "AutoAI RAG"
483
- }
484
-
485
- # Define the preferred order for items to appear at the top
486
- preferred_order = [
487
- "runtime-24.1-py3.11",
488
- "runtime-24.1-py3.11-cuda",
489
- "runtime-24.1-r4.3",
490
- "ai-service-v5-software-specification",
491
- "autoai-rag_rt24.1-py3.11",
492
- "autoai-ts_rt24.1-py3.11",
493
- "autoai-kb_rt24.1-py3.11",
494
- "tensorflow_rt24.1-py3.11",
495
- "pytorch-onnx_rt24.1-py3.11",
496
- "onnxruntime_opset_19",
497
- "spark-mllib_3.4",
498
- ]
499
-
500
- # Create a new column for sorting
501
- supported_specs['SORT_ORDER'] = supported_specs['NAME'].apply(
502
- lambda x: preferred_order.index(x) if x in preferred_order else len(preferred_order)
503
- )
504
-
505
- # Sort the DataFrame by the new column
506
- supported_specs = supported_specs.sort_values('SORT_ORDER').reset_index(drop=True)
507
-
508
- # Drop the sorting column as it's no longer needed
509
- supported_specs = supported_specs.drop(columns=['SORT_ORDER'])
510
-
511
- # Drop the REPLACEMENT column if it exists and add NOTES column
512
- if 'REPLACEMENT' in supported_specs.columns:
513
- supported_specs = supported_specs.drop(columns=['REPLACEMENT'])
514
-
515
- # Add NOTES column with framework information
516
- supported_specs['NOTES'] = supported_specs['NAME'].map(framework_mapping).fillna("Other")
517
-
518
- # Create a table with single-row selection
519
- selection_table = mo.ui.table(
520
- supported_specs,
521
  selection="single", # Only allow selecting one row
522
- label="#### **Select a supported software_spec runtime for your function asset** (For Python Functions select - *'runtime-24.1-py3.11'* ):",
523
- initial_selection=[0], # Now selecting the first row, which should be runtime-24.1-py3.11
524
- page_size=6
525
  )
526
  else:
527
- sel_df = pd.DataFrame(
528
- data=[["ID", "Activate deployment_client."]],
529
- columns=["ID", "VALUE"]
530
- )
531
 
532
- selection_table = mo.ui.table(
533
- sel_df,
534
- selection="single", # Only allow selecting one row
535
- label="You haven't activated the Deployment_Client",
536
- initial_selection=[0]
537
- )
538
-
539
- return (
540
- framework_mapping,
541
- preferred_order,
542
- sel_df,
543
- selection_table,
544
- supported_specs,
545
- )
546
 
547
 
548
  @app.cell
549
- def _(mo):
550
- input_schema_checkbox = mo.ui.checkbox(label="Add input schema (optional)")
551
- output_schema_checkbox = mo.ui.checkbox(label="Add output schema (optional)")
552
- sample_input_checkbox = mo.ui.checkbox(label="Add sample input example (optional)")
553
- return input_schema_checkbox, output_schema_checkbox, sample_input_checkbox
554
-
 
555
 
556
- @app.cell
557
- def _(
558
- input_schema_checkbox,
559
- mo,
560
- output_schema_checkbox,
561
- sample_input_checkbox,
562
- selection_table,
563
- template_variant,
564
- ):
565
- if selection_table.value['ID'].iloc[0]:
566
- # Create the input fields
567
- if template_variant.value == "Stream Files to IBM COS [Example]":
568
- fnc_nm = "stream_file_to_cos"
569
- else:
570
- fnc_nm = "your_function_name"
571
-
572
- uploaded_function_name = mo.ui.text(placeholder="<Must be the same as the name in editor>", label="Function Name:", kind="text", value=f"{fnc_nm}", full_width=False)
573
- tags_editor = mo.ui.array(
574
- [mo.ui.text(placeholder="Metadata Tags..."), mo.ui.text(), mo.ui.text()],
575
- label="Optional Metadata Tags"
576
- )
577
- software_spec = selection_table.value['ID'].iloc[0]
578
-
579
- description_input = mo.ui.text_area(
580
- placeholder="Write a description for your function...)",
581
- label="Description",
582
- max_length=256,
583
- rows=5,
584
- full_width=True
585
- )
586
 
 
587
 
588
- func_metadata=mo.hstack([
589
- description_input,
590
- mo.hstack([
591
- uploaded_function_name,
592
- tags_editor,
593
- ], justify="start", gap=1, align="start", wrap=True)
594
- ],
595
- widths=[0.6,0.4],
596
- gap=2.75
597
- )
598
 
599
- schema_metadata=mo.hstack([
600
- input_schema_checkbox,
601
- output_schema_checkbox,
602
- sample_input_checkbox
603
- ],
604
- justify="center", gap=1, align="center", wrap=True
605
- )
606
 
607
- fm = mo.vstack([
608
- func_metadata,
609
- ],
610
- align="center",
611
- gap=2
612
- )
613
- sc_m = mo.vstack([
614
- schema_metadata,
615
- mo.md("**Make sure to select the checkbox options before filling in descriptions and tags or they will reset.**")
616
- ],
617
- align="center",
618
- gap=2
619
- )
620
- return (
621
- description_input,
622
- fm,
623
- fnc_nm,
624
- func_metadata,
625
- sc_m,
626
- schema_metadata,
627
- software_spec,
628
- tags_editor,
629
- uploaded_function_name,
630
  )
631
 
 
 
632
 
633
- @app.cell
634
- def _(json, mo, template_variant):
635
- if template_variant.value == "Stream Files to IBM COS [Example]":
636
- from cos_stream_schema_examples import input_schema, output_schema, sample_input
637
- else:
638
- input_schema = [
639
- {
640
- 'id': '1',
641
- 'type': 'struct',
642
- 'fields': [
643
- {
644
- 'name': '<variable name 1>',
645
- 'type': 'string',
646
- 'nullable': False,
647
- 'metadata': {}
648
- },
649
- {
650
- 'name': '<variable name 2>',
651
- 'type': 'string',
652
- 'nullable': False,
653
- 'metadata': {}
654
- }
655
- ]
656
- }
657
- ]
658
-
659
- output_schema = [
660
- {
661
- 'id': '1',
662
- 'type': 'struct',
663
- 'fields': [
664
- {
665
- 'name': '<output return name>',
666
- 'type': 'string',
667
- 'nullable': False,
668
- 'metadata': {}
669
- }
670
- ]
671
- }
672
- ]
673
-
674
- sample_input = {
675
- 'input_data': [
676
- {
677
- 'fields': ['<variable name 1>', '<variable name 2>'],
678
- 'values': [
679
- ['<sample input value for variable 1>', '<sample input value for variable 2>']
680
- ]
681
- }
682
- ]
683
- }
684
-
685
-
686
- input_schema_editor = mo.ui.code_editor(value=json.dumps(input_schema, indent=4), language="python", min_height=25)
687
- output_schema_editor = mo.ui.code_editor(value=json.dumps(output_schema, indent=4), language="python", min_height=25)
688
- sample_input_editor = mo.ui.code_editor(value=json.dumps(sample_input, indent=4), language="python", min_height=25)
689
-
690
- schema_editors = mo.accordion(
691
- {
692
- """**Input Schema Metadata Editor**""": input_schema_editor,
693
- """**Output Schema Metadata Editor**""": output_schema_editor,
694
- """**Sample Input Metadata Editor**""": sample_input_editor
695
- }, multiple=True
696
- )
697
-
698
- # schema_editors
699
- return (
700
- input_schema,
701
- input_schema_editor,
702
- output_schema,
703
- output_schema_editor,
704
- sample_input,
705
- sample_input_editor,
706
- schema_editors,
707
- )
708
 
709
 
710
  @app.cell
711
  def _(
712
- ast,
713
- deployment_client,
714
- description_input,
715
- function_editor,
716
- input_schema_checkbox,
717
- input_schema_editor,
718
- json,
719
- mo,
720
- os,
721
- output_schema_checkbox,
722
- output_schema_editor,
723
- sample_input_checkbox,
724
- sample_input_editor,
725
- selection_table,
726
- software_spec,
727
- tags_editor,
728
- uploaded_function_name,
729
  ):
730
- get_upload_status, set_upload_status = mo.state("No uploads yet")
731
-
732
- function_meta = {}
733
 
734
- if selection_table.value['ID'].iloc[0] and deployment_client is not None:
735
- # Start with the base required fields
736
- function_meta = {
737
- deployment_client.repository.FunctionMetaNames.NAME: f"{uploaded_function_name.value}" or "your_function_name",
738
- deployment_client.repository.FunctionMetaNames.SOFTWARE_SPEC_ID: software_spec or "45f12dfe-aa78-5b8d-9f38-0ee223c47309"
 
 
 
 
 
 
 
 
739
  }
740
 
741
- # Add optional fields if they exist
742
- if tags_editor.value:
743
- # Filter out empty strings from the tags list
744
- filtered_tags = [tag for tag in tags_editor.value if tag and tag.strip()]
745
- if filtered_tags: # Only add if there are non-empty tags
746
- function_meta[deployment_client.repository.FunctionMetaNames.TAGS] = filtered_tags
747
-
748
-
749
- if description_input.value:
750
- function_meta[deployment_client.repository.FunctionMetaNames.DESCRIPTION] = description_input.value
751
-
752
- # Add input schema if checkbox is checked
753
- if input_schema_checkbox.value:
754
- try:
755
- function_meta[deployment_client.repository.FunctionMetaNames.INPUT_DATA_SCHEMAS] = json.loads(input_schema_editor.value)
756
- except json.JSONDecodeError:
757
- # If JSON parsing fails, try Python literal evaluation as fallback
758
- function_meta[deployment_client.repository.FunctionMetaNames.INPUT_DATA_SCHEMAS] = ast.literal_eval(input_schema_editor.value)
759
-
760
- # Add output schema if checkbox is checked
761
- if output_schema_checkbox.value:
762
- try:
763
- function_meta[deployment_client.repository.FunctionMetaNames.OUTPUT_DATA_SCHEMAS] = json.loads(output_schema_editor.value)
764
- except json.JSONDecodeError:
765
- # If JSON parsing fails, try Python literal evaluation as fallback
766
- function_meta[deployment_client.repository.FunctionMetaNames.OUTPUT_DATA_SCHEMAS] = ast.literal_eval(output_schema_editor.value)
767
-
768
- # Add sample input if checkbox is checked
769
- if sample_input_checkbox.value:
770
- try:
771
- function_meta[deployment_client.repository.FunctionMetaNames.SAMPLE_SCORING_INPUT] = json.loads(sample_input_editor.value)
772
- except json.JSONDecodeError:
773
- # If JSON parsing fails, try Python literal evaluation as fallback
774
- function_meta[deployment_client.repository.FunctionMetaNames.SAMPLE_SCORING_INPUT] = ast.literal_eval(sample_input_editor.value)
775
-
776
- def upload_function(function_meta, use_function_object=True):
777
- """
778
- Uploads a Python function to watsonx.ai as a deployable asset.
779
- Parameters:
780
- function_meta (dict): Metadata for the function
781
- use_function_object (bool): Whether to use function object (True) or file path (False)
782
- Returns:
783
- dict: Details of the uploaded function
784
- """
785
- # Store the original working directory
786
- original_dir = os.getcwd()
787
-
788
- try:
789
- # Create temp file from the code in the editor
790
- code_to_deploy = function_editor.value['editor']
791
- # This function is defined elsewhere in the notebook
792
- func_name = uploaded_function_name.value or "your_function_name"
793
- # Ensure function_meta has the correct function name
794
- function_meta[deployment_client.repository.FunctionMetaNames.NAME] = func_name
795
- # Save the file locally first
796
- save_dir = "/tmp/notebook_functions"
797
- os.makedirs(save_dir, exist_ok=True)
798
- file_path = f"{save_dir}/{func_name}.py"
799
- with open(file_path, "w", encoding="utf-8") as f:
800
- f.write(code_to_deploy)
801
-
802
- if use_function_object:
803
- # Import the function from the file
804
- import sys
805
- import importlib.util
806
- # Add the directory to Python's path
807
- sys.path.append(save_dir)
808
- # Import the module
809
- spec = importlib.util.spec_from_file_location(func_name, file_path)
810
- module = importlib.util.module_from_spec(spec)
811
- spec.loader.exec_module(module)
812
- # Get the function object
813
- function_object = getattr(module, func_name)
814
-
815
- # Change to /tmp directory before calling IBM Watson SDK functions
816
- os.chdir('/tmp')
817
-
818
- # Upload the function object
819
- mo.md(f"Uploading function object: {func_name}")
820
- func_details = deployment_client.repository.store_function(function_object, function_meta)
821
- else:
822
- # Change to /tmp directory before calling IBM Watson SDK functions
823
- os.chdir('/tmp')
824
-
825
- # Upload using the file path approach
826
- mo.md(f"Uploading function from file: {file_path}")
827
- func_details = deployment_client.repository.store_function(file_path, function_meta)
828
-
829
- set_upload_status(f"Latest Upload - id - {func_details['metadata']['id']}")
830
- return func_details
831
- except Exception as e:
832
- set_upload_status(f"Error uploading function: {str(e)}")
833
- mo.md(f"Detailed error: {str(e)}")
834
- raise
835
- finally:
836
- # Always change back to the original directory, even if an exception occurs
837
- os.chdir(original_dir)
838
-
839
- upload_status = mo.state("No uploads yet")
840
-
841
- upload_button = mo.ui.button(
842
- label="Upload Function",
843
- on_click=lambda _: upload_function(function_meta, use_function_object=True),
844
- kind="success",
845
- tooltip="Click to upload function to watsonx.ai"
846
- )
847
-
848
- # function_meta
849
- return (
850
- filtered_tags,
851
- function_meta,
852
- get_upload_status,
853
- set_upload_status,
854
- upload_button,
855
- upload_function,
856
- upload_status,
857
- )
858
-
859
-
860
- @app.cell
861
- def _(get_upload_status, mo, upload_button):
862
- # Upload your function
863
- if upload_button.value:
864
- try:
865
- upload_result = upload_button.value
866
- artifact_id = upload_result['metadata']['id']
867
- except Exception as e:
868
- mo.md(f"Error: {str(e)}")
869
-
870
- upload_func = mo.vstack([
871
- upload_button,
872
- mo.md(f"**Status:** {get_upload_status()}")
873
- ], justify="space-around", align="center")
874
- return artifact_id, upload_func, upload_result
875
-
876
-
877
- @app.cell
878
- def _(deployment_client, mo, pd, upload_button, uuid):
879
- def reorder_hardware_specifications(df):
880
- """
881
- Reorders a hardware specifications dataframe by type and size of environment
882
- without hardcoding specific hardware types.
883
-
884
- Parameters:
885
- df (pandas.DataFrame): The hardware specifications dataframe to reorder
886
-
887
- Returns:
888
- pandas.DataFrame: Reordered dataframe with reset index
889
- """
890
- # Create a copy to avoid modifying the original dataframe
891
- result_df = df.copy()
892
-
893
- # Define a function to extract the base type and size
894
- def get_sort_key(name):
895
- # Create a custom ordering list
896
- custom_order = [
897
- "XXS", "XS", "S", "M", "L", "XL",
898
- "XS-Spark", "S-Spark", "M-Spark", "L-Spark", "XL-Spark",
899
- "K80", "K80x2", "K80x4",
900
- "V100", "V100x2",
901
- "WXaaS-XS", "WXaaS-S", "WXaaS-M", "WXaaS-L", "WXaaS-XL",
902
- "Default Spark", "Notebook Default Spark", "ML"
903
- ]
904
-
905
- # If name is in the custom order list, use its index
906
- if name in custom_order:
907
- return (0, custom_order.index(name))
908
-
909
- # For any name not in the custom order, put it at the end
910
- return (1, name)
911
-
912
- # Add a temporary column for sorting
913
- result_df['sort_key'] = result_df['NAME'].apply(get_sort_key)
914
-
915
- # Sort the dataframe and drop the temporary column
916
- result_df = result_df.sort_values('sort_key').drop('sort_key', axis=1)
917
-
918
- # Reset the index
919
- result_df = result_df.reset_index(drop=True)
920
-
921
- return result_df
922
-
923
- if deployment_client and upload_button.value:
924
-
925
- hardware_specs = deployment_client.hardware_specifications.list()
926
- hardware_specs_df = reorder_hardware_specifications(hardware_specs)
927
-
928
- # Create a table with single-row selection
929
- hw_selection_table = mo.ui.table(
930
- hardware_specs_df,
931
- selection="single", # Only allow selecting one row
932
- label="#### **Select a supported hardware_specification for your deployment** *(Default: 'XS' - 1vCPU_4GB Ram)*",
933
- initial_selection=[1],
934
- page_size=6,
935
- wrapped_columns=['DESCRIPTION']
936
- )
937
-
938
- deployment_type = mo.ui.radio(
939
- options={"Function":"Online (Function Endpoint)","Runnable Job":"Batch (Runnable Jobs)"}, value="Function", label="Select the Type of Deployment:", inline=True
940
- )
941
- uuid_suffix = str(uuid.uuid4())[:4]
942
-
943
- deployment_name = mo.ui.text(value=f"deployed_func_{uuid_suffix}", label="Deployment Name:", placeholder="<Must be completely unique>")
944
  else:
945
- hw_df = pd.DataFrame(
946
- data=[["ID", "Activate deployment_client."]],
947
- columns=["ID", "VALUE"]
948
- )
949
-
950
- hw_selection_table = mo.ui.table(
951
- hw_df,
952
- selection="single", # Only allow selecting one row
953
- label="You haven't activated the Deployment_Client",
954
- initial_selection=[0]
955
- )
956
-
957
- return (
958
- deployment_name,
959
- deployment_type,
960
- hardware_specs,
961
- hardware_specs_df,
962
- hw_df,
963
- hw_selection_table,
964
- reorder_hardware_specifications,
965
- uuid_suffix,
966
- )
967
 
968
 
969
  @app.cell
970
- def _(
971
- artifact_id,
972
- deployment_client,
973
- deployment_details,
974
- deployment_name,
975
- deployment_type,
976
- hw_selection_table,
977
- mo,
978
- print,
979
- upload_button,
980
- ):
981
- def deploy_function(artifact_id, deployment_type):
982
- """
983
- Deploys a function asset to watsonx.ai.
984
-
985
- Parameters:
986
- artifact_id (str): ID of the function artifact to deploy
987
- deployment_type (object): Type of deployment (online or batch)
988
-
989
- Returns:
990
- dict: Details of the deployed function
991
- """
992
- if not artifact_id:
993
- print("Error: No artifact ID provided. Please upload a function first.")
994
- return None
995
-
996
- if deployment_type.value == "Online (Function Endpoint)": # Changed from "Online (Function Endpoint)"
997
- deployment_props = {
998
- deployment_client.deployments.ConfigurationMetaNames.NAME: deployment_name.value,
999
- deployment_client.deployments.ConfigurationMetaNames.ONLINE: {},
1000
- deployment_client.deployments.ConfigurationMetaNames.HARDWARE_SPEC: {"id": selected_hw_config},
1001
- deployment_client.deployments.ConfigurationMetaNames.SERVING_NAME: deployment_name.value,
1002
- }
1003
- else: # "Runnable Job" instead of "Batch (Runnable Jobs)"
1004
- deployment_props = {
1005
- deployment_client.deployments.ConfigurationMetaNames.NAME: deployment_name.value,
1006
- deployment_client.deployments.ConfigurationMetaNames.BATCH: {},
1007
- deployment_client.deployments.ConfigurationMetaNames.HARDWARE_SPEC: {"id": selected_hw_config},
1008
- # batch does not use serving names
1009
- }
1010
-
1011
- try:
1012
- print(deployment_props)
1013
- # First, get the asset details to confirm it exists
1014
- asset_details = deployment_client.repository.get_details(artifact_id)
1015
- print(f"Asset found: {asset_details['metadata']['name']} with ID: {asset_details['metadata']['id']}")
1016
-
1017
- # Create the deployment
1018
- deployed_function = deployment_client.deployments.create(artifact_id, deployment_props)
1019
- print(f"Creating deployment from Asset: {artifact_id} with deployment properties {str(deployment_props)}")
1020
- return deployed_function
1021
- except Exception as e:
1022
- print(f"Deployment error: {str(e)}")
1023
- return None
1024
-
1025
- def get_deployment_id(deployed_function):
1026
- deployment_id = deployment_client.deployments.get_uid(deployment_details)
1027
- return deployment_id
1028
-
1029
- def get_deployment_info(deployment_id):
1030
- deployment_info = deployment_client.deployments.get_details(deployment_id)
1031
- return deployment_info
1032
 
1033
- deployment_status = mo.state("No deployments yet")
1034
 
1035
- if hw_selection_table.value['ID'].iloc[0]:
1036
- selected_hw_config = hw_selection_table.value['ID'].iloc[0]
1037
 
1038
- deploy_button = mo.ui.button(
1039
- label="Deploy Function",
1040
- on_click=lambda _: deploy_function(artifact_id, deployment_type),
1041
- kind="success",
1042
- tooltip="Click to deploy function to watsonx.ai"
1043
- )
1044
-
1045
- if deployment_client and upload_button.value:
1046
- deployment_definition = mo.hstack([
1047
- deployment_type,
1048
- deployment_name
1049
- ], justify="space-around")
1050
- else:
1051
- deployment_definition = mo.hstack([
1052
- "No Deployment Type Selected",
1053
- "No Deployment Name Provided"
1054
- ], justify="space-around")
1055
 
1056
- # deployment_definition
1057
  return (
1058
- deploy_button,
1059
- deploy_function,
1060
- deployment_definition,
1061
- deployment_status,
1062
- get_deployment_id,
1063
- get_deployment_info,
1064
- selected_hw_config,
1065
  )
1066
 
1067
 
1068
  @app.cell
1069
- def _(deploy_button, deployment_definition, mo):
1070
- _ = deployment_definition
1071
-
1072
- deploy_fnc = mo.vstack([
1073
- deploy_button,
1074
- deploy_button.value
1075
- ], justify="space-around", align="center")
1076
-
1077
- return (deploy_fnc,)
1078
-
1079
-
1080
- @app.cell(hide_code=True)
1081
- def _(deployment_client, mo):
1082
- ### Functions to List , Get ID's as a list and Purge of Assets
1083
-
1084
- def get_deployment_list():
1085
- dep_df = deployment_client.deployments.list()
1086
- dep_df = pd.DataFrame(dep_df)
1087
- return dep_df
1088
-
1089
- def get_deployment_ids(df):
1090
- dep_list = df['ID'].tolist()
1091
- return dep_list
1092
-
1093
- #----
1094
-
1095
- def get_data_assets_list():
1096
- data_a_df = deployment_client.data_assets.list()
1097
- data_a_df = pd.DataFrame(data_a_df)
1098
- return data_a_df
1099
-
1100
- def get_data_asset_ids(df):
1101
- data_asset_list = df['ASSET_ID'].tolist()
1102
- return data_asset_list
1103
-
1104
- #----
1105
-
1106
- def get_repository_list():
1107
- rep_list_df = deployment_client.repository.list()
1108
- rep_list_df = pd.DataFrame(rep_list_df)
1109
- return rep_list_df
1110
-
1111
- def get_repository_ids(df):
1112
- repository_list = df['ID'].tolist()
1113
- return repository_list
1114
-
1115
- #----
1116
-
1117
- def delete_with_progress(ids_list, delete_function, item_type="items"):
1118
- """
1119
- Generic wrapper that adds a progress bar to any deletion function
1120
-
1121
- Parameters:
1122
- ids_list: List of IDs to delete
1123
- delete_function: Function that deletes a single ID
1124
- item_type: String describing what's being deleted (for display)
1125
- """
1126
- with mo.status.progress_bar(
1127
- total=len(ids_list) or 1,
1128
- title=f"Purging {item_type}",
1129
- subtitle=f"Deleting {item_type}...",
1130
- completion_title="Purge Complete",
1131
- completion_subtitle=f"Successfully deleted {len(ids_list)} {item_type}"
1132
- ) as progress:
1133
- for item_id in ids_list:
1134
- delete_function(item_id)
1135
- progress.update(increment=1)
1136
- return f"Deleted {len(ids_list)} {item_type} successfully"
1137
-
1138
- # Use with existing deletion functions
1139
- def delete_deployments(deployment_ids):
1140
- return delete_with_progress(
1141
- deployment_ids,
1142
- lambda id: deployment_client.deployments.delete(id),
1143
- "deployments"
1144
- )
1145
 
1146
- def delete_data_assets(data_asset_ids):
1147
- return delete_with_progress(
1148
- data_asset_ids,
1149
- lambda id: deployment_client.data_assets.delete(id),
1150
- "data assets"
1151
- )
1152
 
1153
- def delete_repository_items(repository_ids):
1154
- return delete_with_progress(
1155
- repository_ids,
1156
- lambda id: deployment_client.repository.delete(id),
1157
- "repository items"
1158
  )
1159
- return (
1160
- delete_data_assets,
1161
- delete_deployments,
1162
- delete_repository_items,
1163
- delete_with_progress,
1164
- get_data_asset_ids,
1165
- get_data_assets_list,
1166
- get_deployment_ids,
1167
- get_deployment_list,
1168
- get_repository_ids,
1169
- get_repository_list,
1170
  )
 
1171
 
1172
- @app.cell
1173
- def _(get_data_assets_tab, get_deployments_tab, get_repository_tab, mo):
1174
- if get_deployments_tab() is not None:
1175
- deployments_table = mo.ui.table(get_deployments_tab())
1176
- else:
1177
- deployments_table = mo.md("No Table Loaded")
1178
-
1179
- if get_repository_tab() is not None:
1180
- repository_table = mo.ui.table(get_repository_tab())
1181
- else:
1182
- repository_table = mo.md("No Table Loaded")
1183
-
1184
- if get_data_assets_tab() is not None:
1185
- data_assets_table = mo.ui.table(get_data_assets_tab())
1186
- else:
1187
- data_assets_table = mo.md("No Table Loaded")
1188
-
1189
- return data_assets_table, deployments_table, repository_table
1190
 
1191
  @app.cell
1192
- def _(get_deployment_id_list, get_deployments_button, mo, purge_deployments):
1193
- deployments_purge_stack = mo.hstack([get_deployments_button, get_deployment_id_list, purge_deployments])
1194
- deployments_purge_stack_results = mo.vstack([deployments_table, get_deployment_id_list.value, purge_deployments.value])
1195
-
1196
- deployments_purge_tab = mo.vstack([deployments_purge_stack, deployments_purge_stack_results])
1197
- return (
1198
- deployments_purge_stack,
1199
- deployments_purge_stack_results,
1200
- deployments_purge_tab,
1201
- )
1202
 
1203
 
1204
  @app.cell
1205
- def _(get_repository_button, get_repository_id_list, mo, purge_repository):
1206
- repository_purge_stack = mo.hstack([get_repository_button, get_repository_id_list, purge_repository])
 
 
 
1207
 
1208
- repository_purge_stack_results = mo.vstack([repository_table, get_repository_id_list.value, purge_repository.value])
1209
-
1210
- repository_purge_tab = mo.vstack([repository_purge_stack, repository_purge_stack_results])
1211
- return (
1212
- repository_purge_stack,
1213
- repository_purge_stack_results,
1214
- repository_purge_tab,
1215
- )
1216
 
1217
 
1218
  @app.cell
1219
- def _(get_data_asset_id_list, get_data_assets_button, mo, purge_data_assets):
1220
- data_assets_purge_stack = mo.hstack([get_data_assets_button, get_data_asset_id_list, purge_data_assets])
1221
- data_assets_purge_stack_results = mo.vstack([data_assets_table, get_data_asset_id_list.value, purge_data_assets.value])
1222
 
1223
- data_assets_purge_tab = mo.vstack([data_assets_purge_stack, data_assets_purge_stack_results])
1224
- return (
1225
- data_assets_purge_stack,
1226
- data_assets_purge_stack_results,
1227
- data_assets_purge_tab,
1228
- )
1229
 
1230
 
1231
  @app.cell
1232
- def _(data_assets_purge_tab, deployments_purge_tab, mo, repository_purge_tab):
1233
- purge_tabs = mo.ui.tabs(
1234
- {"Purge Deployments": deployments_purge_tab, "Purge Repository Assets": repository_purge_tab,"Purge Data Assets": data_assets_purge_tab }, lazy=False
1235
- )
1236
 
1237
- # asset_purge
1238
- return (purge_tabs,)
1239
 
1240
  @app.cell
1241
- def _(mo):
1242
- get_deployments_tab, set_deployments_tab = mo.state(None)
1243
- get_repository_tab, set_repository_tab = mo.state(None)
1244
- get_data_assets_tab, set_data_assets_tab = mo.state(None)
1245
- return (
1246
- get_data_assets_tab,
1247
- get_deployments_tab,
1248
- get_repository_tab,
1249
- set_data_assets_tab,
1250
- set_deployments_tab,
1251
- set_repository_tab,
1252
- )
1253
-
1254
-
1255
- @app.cell(hide_code=True)
1256
- def _(
1257
- data_assets_table,
1258
- delete_data_assets,
1259
- delete_deployments,
1260
- delete_repository_items,
1261
- deployments_table,
1262
- get_data_asset_ids,
1263
- get_data_assets_list,
1264
- get_deployment_ids,
1265
- get_deployment_list,
1266
- get_repository_ids,
1267
- get_repository_list,
1268
- mo,
1269
- repository_table,
1270
- set_data_assets_tab,
1271
- set_deployments_tab,
1272
- set_repository_tab,
1273
- ):
1274
- ### Temporary Function Purge - Assets
1275
- get_data_assets_button = mo.ui.button(
1276
- label="Get Data Assets Dataframe",
1277
- on_click=lambda _: get_data_assets_list(),
1278
- on_change=lambda value: set_data_assets_tab(value),
1279
- kind="neutral",
1280
- )
1281
-
1282
- get_data_asset_id_list = mo.ui.button(
1283
- label="Turn Dataframe into List of IDs",
1284
- on_click=lambda _: get_data_asset_ids(data_assets_table.value),
1285
- kind="neutral",
1286
- )
1287
-
1288
- purge_data_assets = mo.ui.button(
1289
- label="Purge Data Assets",
1290
- on_click=lambda _: delete_data_assets(get_data_asset_id_list.value),
1291
- kind="danger",
1292
- )
1293
-
1294
- ### Temporary Function Purge - Deployments
1295
- get_deployments_button = mo.ui.button(
1296
- label="Get Deployments Dataframe",
1297
- on_click=lambda _: get_deployment_list(),
1298
- on_change=lambda value: set_deployments_tab(value),
1299
- kind="neutral",
1300
- )
1301
-
1302
- get_deployment_id_list = mo.ui.button(
1303
- label="Turn Dataframe into List of IDs",
1304
- on_click=lambda _: get_deployment_ids(deployments_table.value),
1305
- kind="neutral",
1306
- )
1307
-
1308
- purge_deployments = mo.ui.button(
1309
- label="Purge Deployments",
1310
- on_click=lambda _: delete_deployments(get_deployment_id_list.value),
1311
- kind="danger",
1312
- )
1313
-
1314
- ### Repository Items Purge
1315
- get_repository_button = mo.ui.button(
1316
- label="Get Repository Dataframe",
1317
- on_click=lambda _: get_repository_list(),
1318
- on_change=lambda value: set_repository_tab(value),
1319
- kind="neutral",
1320
- )
1321
-
1322
- get_repository_id_list = mo.ui.button(
1323
- label="Turn Dataframe into List of IDs",
1324
- on_click=lambda _: get_repository_ids(repository_table.value),
1325
- kind="neutral",
1326
- )
1327
-
1328
- purge_repository = mo.ui.button(
1329
- label="Purge Repository Items",
1330
- on_click=lambda _: delete_repository_items(get_repository_id_list.value),
1331
- kind="danger",
1332
- )
1333
- return (
1334
- get_data_asset_id_list,
1335
- get_data_assets_button,
1336
- get_deployment_id_list,
1337
- get_deployments_button,
1338
- get_repository_button,
1339
- get_repository_id_list,
1340
- purge_data_assets,
1341
- purge_deployments,
1342
- purge_repository,
1343
- )
1344
 
1345
 
1346
  if __name__ == "__main__":
 
1
  import marimo
2
 
3
+ __generated_with = "0.11.17"
4
+ app = marimo.App(width="full")
5
 
6
 
7
  @app.cell
8
  def _():
9
  import marimo as mo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  import pandas as pd
11
+ import os
12
+ import io
 
 
 
 
13
  import json
14
+ return io, json, mo, os, pd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
 
17
  @app.cell
18
  def _(mo):
19
+ ### Credentials for the watsonx.ai SDK clien
 
20
  # Endpoints
21
  wx_platform_url = "https://api.dataplatform.cloud.ibm.com"
22
  regions = {
 
37
 
38
  {wx_api_key}
39
 
40
+ {project_id}
41
+
42
+ ''')
43
  .batch(
44
  wx_region = mo.ui.dropdown(regions, label="Select your watsonx.ai region:", value="US", searchable=True),
45
  wx_api_key = mo.ui.text(placeholder="Add your IBM Cloud api-key...", label="IBM Cloud Api-key:", kind="password"),
46
+ project_id = mo.ui.text(placeholder="Add your watsonx.ai project_id...", label="Project_ID:", kind="text"),
47
  space_id = mo.ui.text(placeholder="Add your watsonx.ai space_id...", label="Space_ID:", kind="text")
48
  ,)
49
  .form(show_clear_button=True, bordered=False)
50
  )
51
 
 
 
52
  return client_instantiation_form, regions, wx_platform_url
53
 
54
 
55
  @app.cell
56
  def _(client_instantiation_form, mo):
57
  from ibm_watsonx_ai import APIClient, Credentials
58
+ import requests
59
 
60
+ def get_iam_token(api_key):
61
+ return requests.post(
62
+ 'https://iam.cloud.ibm.com/identity/token',
63
+ headers={'Content-Type': 'application/x-www-form-urlencoded'},
64
+ data={'grant_type': 'urn:ibm:params:oauth:grant-type:apikey', 'apikey': api_key}
65
+ ).json()['access_token']
66
+
67
+ def setup_task_credentials(project_client):
68
  # Get existing task credentials
69
+ existing_credentials = project_client.task_credentials.get_details()
70
 
71
  # Delete existing credentials if any
72
  if "resources" in existing_credentials and existing_credentials["resources"]:
73
  for cred in existing_credentials["resources"]:
74
+ cred_id = project_client.task_credentials.get_id(cred)
75
+ project_client.task_credentials.delete(cred_id)
76
 
77
  # Store new credentials
78
+ return project_client.task_credentials.store()
79
 
80
  if client_instantiation_form.value:
81
  ### Instantiate the watsonx.ai client
82
+ wx_api_key = client_instantiation_form.value["wx_api_key"]
83
+
84
  wx_credentials = Credentials(
85
  url=client_instantiation_form.value["wx_region"],
86
+ api_key=wx_api_key
87
  )
88
 
89
+ project_client = APIClient(credentials=wx_credentials, project_id=client_instantiation_form.value["project_id"])
90
+ # deployment_client = APIClient(credentials=wx_credentials, space_id=client_instantiation_form.value["space_id"])
91
+
92
+ token = get_iam_token(wx_api_key)
93
+ task_credentials_details = setup_task_credentials(project_client)
94
 
 
95
  else:
96
+ project_client = None
97
+ # deployment_client = None
98
  task_credentials_details = None
99
+ wx_api_key = None
100
+ token = None
101
 
102
+ client_status = mo.md("### Client Instantiation Status will turn Green When Ready")
103
 
104
+ if project_client is not None:
105
  client_callout_kind = "success"
106
  else:
107
  client_callout_kind = "neutral"
 
 
 
 
108
  return (
109
  APIClient,
110
  Credentials,
 
111
  client_callout_kind,
112
+ client_status,
113
+ get_iam_token,
114
+ project_client,
115
+ requests,
116
  setup_task_credentials,
117
  task_credentials_details,
118
+ token,
119
+ wx_api_key,
120
  wx_credentials,
121
  )
122
 
123
 
124
  @app.cell
125
+ def _(client_callout_kind, client_instantiation_form, client_status, mo):
126
+ client_callout = mo.callout(client_status, kind=client_callout_kind)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
 
128
+ client_stack = mo.hstack([client_instantiation_form, client_callout], align="center", justify="space-around")
129
 
130
+ client_stack
131
+ return client_callout, client_stack
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
 
134
  @app.cell
135
+ def _(mo, project_client):
136
+ if project_client:
137
+ # model_specs = project_client.foundation_models.get_chat_model_specs() ### if you want models that support chat_completions via the RestAPI or Python SDK.
138
+ model_specs = project_client.foundation_models.get_model_specs()
139
+ resources = model_specs["resources"]
140
+ model_id_list = []
141
+ for resource in resources:
142
+ model_id_list.append(resource["model_id"])
143
+ model_selection = mo.ui.table(
144
+ model_id_list,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  selection="single", # Only allow selecting one row
146
+ label="Select a model to use.",
147
+ page_size=30,
148
+ initial_selection=[16]
149
  )
150
  else:
151
+ model_specs = []
152
+ resources = []
153
+ model_id_list = []
154
+ model_selection = mo.md("**Instantiate a watsonx.ai client with a valid project_id**")
155
 
156
+ return model_id_list, model_selection, model_specs, resource, resources
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
 
159
  @app.cell
160
+ def _(mo, model_selection):
161
+ from ibm_watsonx_ai.foundation_models import ModelInference
162
+ from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
163
+ # Create a form with multiple elements
164
+ llm_parameters = (
165
+ mo.md('''
166
+ ###**LLM parameters:**
167
 
168
+ {decoding_method}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
+ {repetition_penalty}
171
 
172
+ {min_tokens}
 
 
 
 
 
 
 
 
 
173
 
174
+ {max_tokens}
 
 
 
 
 
 
175
 
176
+ {stop_sequences}
177
+ ''')
178
+ .batch(
179
+ ### Temporary version with preset credentials
180
+ decoding_method = mo.ui.dropdown(options=["greedy", "sample"], value="greedy",label="Decoding Method:"),
181
+ min_tokens = mo.ui.number(start=1, stop=1, label="Minimum Output Tokens:"),
182
+ max_tokens = mo.ui.number(start=1, stop=8096, value=500, label="Maximum Output Tokens:"),
183
+ repetition_penalty = mo.ui.number(start=1.0, stop=2.0, step=0.01, label="Repetition Penalty:"),
184
+ stop_sequences = mo.ui.text(label="Stopping Sequences:", value="['<|end_of_text|>','</s>']", placeholder="List of Strings, e.g. ['<|end_of_text|>','</s>']", full_width=False)
185
+ ).form(show_clear_button=True, bordered=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
186
  )
187
 
188
+ # llm_parameters
189
+ llm_setup = mo.hstack([model_selection, llm_parameters], align="center", justify="space-around")
190
 
191
+ llm_setup
192
+ return GenParams, ModelInference, llm_parameters, llm_setup
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
 
194
 
195
  @app.cell
196
  def _(
197
+ GenParams,
198
+ ModelInference,
199
+ llm_parameters,
200
+ model_selection,
201
+ project_client,
 
 
 
 
 
 
 
 
 
 
 
 
202
  ):
203
+ import ast
 
 
204
 
205
+ if llm_parameters.value:
206
+ params = {
207
+ GenParams.DECODING_METHOD: llm_parameters.value['decoding_method'],
208
+ GenParams.MAX_NEW_TOKENS: llm_parameters.value['max_tokens'],
209
+ GenParams.MIN_NEW_TOKENS: llm_parameters.value['min_tokens'],
210
+ GenParams.REPETITION_PENALTY: llm_parameters.value['repetition_penalty'],
211
+ GenParams.STOP_SEQUENCES: ast.literal_eval(llm_parameters.value['stop_sequences']),
212
+ GenParams.RETURN_OPTIONS: {
213
+ 'input_text': False,
214
+ 'generated_tokens': False,
215
+ 'input_tokens': True,
216
+ 'token_logprobs': False
217
+ }
218
  }
219
 
220
+ inf_model = ModelInference(api_client=project_client, model_id=model_selection.value[0], params=params)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
221
  else:
222
+ params = {}
223
+ inf_model = None
224
+ return ast, inf_model, params
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
 
227
  @app.cell
228
+ def _(mo):
229
+ prompt_template_mistral = """[INST] write your prompt here [/INST]"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
231
+ prompt_template_llama = """<|start_header_id|>user<|end_header_id|>\n\n write your prompt here <|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"""
232
 
 
 
233
 
234
+ templates = {
235
+ "mistral": prompt_template_mistral,
236
+ "llama": prompt_template_llama
237
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
238
 
239
+ template_selector = mo.ui.dropdown(templates, value="mistral", label="Select Prompt Template with Syntax:")
240
  return (
241
+ prompt_template_llama,
242
+ prompt_template_mistral,
243
+ template_selector,
244
+ templates,
 
 
 
245
  )
246
 
247
 
248
  @app.cell
249
+ def _(mo, template_selector):
250
+ prompt_editor = (
251
+ mo.md('''
252
+ #### **Create your prompt here by editing the template:**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
253
 
254
+ {editor}
 
 
 
 
 
255
 
256
+ ''')
257
+ .batch(
258
+ editor = mo.ui.code_editor(value=template_selector.value, language="python", min_height=50)
 
 
259
  )
260
+ .form(show_clear_button=True, bordered=False)
 
 
 
 
 
 
 
 
 
 
261
  )
262
+ return (prompt_editor,)
263
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
264
 
265
  @app.cell
266
+ def _(mo, prompt_editor, template_selector):
267
+ prompt_setup = mo.vstack([template_selector, prompt_editor])
268
+ prompt_setup
269
+ return (prompt_setup,)
 
 
 
 
 
 
270
 
271
 
272
  @app.cell
273
+ def _(mo, prompt_editor):
274
+ if prompt_editor.value['editor']:
275
+ prompt_printout = mo.md(f"**Current prompt template:**\n\n {prompt_editor.value['editor']}")
276
+ else:
277
+ prompt_printout = mo.md(f"**Submit a prompt template.**")
278
 
279
+ prompt_printout
280
+ return (prompt_printout,)
 
 
 
 
 
 
281
 
282
 
283
  @app.cell
284
+ def _(inf_model, params, process_with_llm, prompt_editor):
285
+ prompt_response = process_with_llm(inf_model, prompt_template=prompt_editor.value['editor'], params=params, return_full_json_response=False)
 
286
 
287
+ prompt_response_full = process_with_llm(inf_model, prompt_template=prompt_editor.value['editor'], params=params, return_full_json_response=True)
288
+ return prompt_response, prompt_response_full
 
 
 
 
289
 
290
 
291
  @app.cell
292
+ def _(mo, prompt_response, prompt_response_full):
293
+ mo.vstack([mo.md(prompt_response),prompt_response_full], align="center",justify="space-around")
294
+ return
 
295
 
 
 
296
 
297
  @app.cell
298
+ def _():
299
+ def process_with_llm(inf_model, prompt_template, params, return_full_json_response=False):
300
+ """
301
+ Process a prompt with an LLM model.
302
+
303
+ Returns full JSON response or just text based on return_full_json_response parameter.
304
+ """
305
+ # Check for required model
306
+ if not inf_model:
307
+ print("Missing required inference model")
308
+ return None
309
+
310
+ # Extract prompt value if it's a dict
311
+ if hasattr(prompt_template, 'get') and prompt_template.get('value'):
312
+ prompt_template = prompt_template['value']
313
+
314
+ try:
315
+ # Call appropriate method based on return type preference
316
+ if return_full_json_response:
317
+ return inf_model.generate(prompt=prompt_template, params=params)
318
+ else:
319
+ return inf_model.generate_text(prompt=prompt_template, params=params)
320
+
321
+ except Exception as e:
322
+ print(f"Error during inference: {str(e)}")
323
+ return None
324
+ return (process_with_llm,)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
325
 
326
 
327
  if __name__ == "__main__":