import marimo
__generated_with = "0.11.17"
app = marimo.App(width="medium")
@app.cell
def _():
import marimo as mo
import os
return mo, os
@app.cell
def _(os):
### Imports
from typing import (
Any, Dict, List, Optional, Pattern, Set, Union, Tuple
)
from pathlib import Path
from urllib.request import urlopen
# from rich.markdown import Markdown as Markd
from rich.console import Console
from rich.theme import Theme
from rich.text import Text
from rich import print
from tqdm import tqdm
from enum import Enum
import pandas as pd
import tempfile
import requests
import getpass
import urllib3
import base64
import time
import json
import uuid
import ssl
import ast
import re
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_colwidth', None)
pd.set_option('display.width', None)
custom_theme = Theme({
"info": "blue_violet", "warning": "yellow", "danger": "red", "python": "blue_violet", "string": "cyan", "number": "magenta", "keyword": "bright_blue", "comment": "dim blue_violet", "json":"blue_violet"
})
# Set explicit temporary directory
os.environ['TMPDIR'] = '/tmp'
# Make sure Python's tempfile module also uses this directory
tempfile.tempdir = '/tmp'
### Prepares the console
console = Console(width=250, color_system="auto", force_jupyter=True)
return (
Any,
Console,
Dict,
Enum,
List,
Optional,
Path,
Pattern,
Set,
Text,
Theme,
Tuple,
Union,
ast,
base64,
console,
custom_theme,
getpass,
json,
pd,
print,
re,
requests,
ssl,
tempfile,
time,
tqdm,
urllib3,
urlopen,
uuid,
)
@app.cell
def _(mo):
### Credentials for the watsonx.ai SDK client
# Endpoints
wx_platform_url = "https://api.dataplatform.cloud.ibm.com"
regions = {
"US": "https://us-south.ml.cloud.ibm.com",
"EU": "https://eu-de.ml.cloud.ibm.com",
"GB": "https://eu-gb.ml.cloud.ibm.com",
"JP": "https://jp-tok.ml.cloud.ibm.com",
"AU": "https://au-syd.ml.cloud.ibm.com",
"CA": "https://ca-tor.ml.cloud.ibm.com"
}
# Create a form with multiple elements
client_instantiation_form = (
mo.md('''
###**watsonx.ai credentials:**
{wx_region}
{wx_api_key}
{space_id}
''').style(max_height="300px", overflow="auto", border_color="blue")
.batch(
wx_region = mo.ui.dropdown(regions, label="Select your watsonx.ai region:", value="US", searchable=True),
wx_api_key = mo.ui.text(placeholder="Add your IBM Cloud api-key...", label="IBM Cloud Api-key:", kind="password"),
# project_id = mo.ui.text(placeholder="Add your watsonx.ai project_id...", label="Project_ID:", kind="text"),
space_id = mo.ui.text(placeholder="Add your watsonx.ai space_id...", label="Space_ID:", kind="text")
,)
.form(show_clear_button=True, bordered=False)
)
client_instantiation_form
return client_instantiation_form, regions, wx_platform_url
@app.cell
def _(client_instantiation_form, mo):
from ibm_watsonx_ai import APIClient, Credentials
def setup_task_credentials(deployment_client):
# Get existing task credentials
existing_credentials = deployment_client.task_credentials.get_details()
# Delete existing credentials if any
if "resources" in existing_credentials and existing_credentials["resources"]:
for cred in existing_credentials["resources"]:
cred_id = deployment_client.task_credentials.get_id(cred)
deployment_client.task_credentials.delete(cred_id)
# Store new credentials
return deployment_client.task_credentials.store()
if client_instantiation_form.value:
### Instantiate the watsonx.ai client
wx_credentials = Credentials(
url=client_instantiation_form.value["wx_region"],
api_key=client_instantiation_form.value["wx_api_key"]
)
# project_client = APIClient(credentials=wx_credentials, project_id=client_instantiation_form.value["project_id"])
deployment_client = APIClient(credentials=wx_credentials, space_id=client_instantiation_form.value["space_id"])
task_credentials_details = setup_task_credentials(deployment_client)
else:
# project_client = None
deployment_client = None
task_credentials_details = None
template_variant = mo.ui.dropdown(["Base","Stream Files to IBM COS [Example]"], label="Code Template:", value="Base")
if deployment_client is not None:
client_callout_kind = "success"
else:
client_callout_kind = "neutral"
client_callout = mo.callout(template_variant, kind=client_callout_kind)
client_callout
return (
APIClient,
Credentials,
client_callout,
client_callout_kind,
deployment_client,
# project_client,
task_credentials_details,
template_variant,
wx_credentials,
)
@app.cell
def _(mo, template_variant):
# Template for WatsonX.ai deployable function
if template_variant.value == "Stream Files to IBM COS [Example]":
with open("stream_files_to_cos.py", "r") as file:
template = file.read()
else:
template = '''def your_function_name():
import subprocess
subprocess.check_output('pip install gensim', shell=True)
import gensim
def score(input_data):
message_from_input_payload = payload.get("input_data")[0].get("values")[0][0]
response_message = "Received message - {0}".format(message_from_input_payload)
# Score using the pre-defined model
score_response = {
'predictions': [{'fields': ['Response_message_field', 'installed_lib_version'],
'values': [[response_message, gensim.__version__]]
}]
}
return score_response
return score
score = your_function_name()
'''
function_editor = (
mo.md('''
#### **Create your function by editing the template:**
{editor}
''')
.batch(
editor = mo.ui.code_editor(value=template, language="python", min_height=50)
)
.form(show_clear_button=True, bordered=False)
)
function_editor
return file, function_editor, template
@app.cell
def _(function_editor, mo, os):
if function_editor.value:
# Get the edited code from the function editor
code = function_editor.value['editor']
# Create a namespace to execute the code in
namespace = {}
# Execute the code
exec(code, namespace)
# Find the first function defined in the namespace
function_name = None
for name, obj in namespace.items():
if callable(obj) and name != "__builtins__":
function_name = name
break
if function_name:
# Instantiate the deployable function
deployable_function = namespace[function_name]
# Now deployable_function contains the score function
mo.md(f"Created deployable function from '{function_name}'")
# Create the directory if it doesn't exist
save_dir = "/tmp/notebook_functions"
os.makedirs(save_dir, exist_ok=True)
# Save the function code to a file
file_path = os.path.join(save_dir, f"{function_name}.py")
with open(file_path, "w") as f:
f.write(code)
else:
mo.md("No function found in the editor code")
return (
code,
deployable_function,
f,
file_path,
function_name,
name,
namespace,
obj,
save_dir,
)
@app.cell
def _(deployment_client, mo, pd):
if deployment_client:
supported_specs = deployment_client.software_specifications.list()[
deployment_client.software_specifications.list()['STATE'] == 'supported'
]
# Reset the index to start from 0
supported_specs = supported_specs.reset_index(drop=True)
# Create a mapping dictionary for framework names based on software specifications
framework_mapping = {
"tensorflow_rt24.1-py3.11": "TensorFlow",
"pytorch-onnx_rt24.1-py3.11": "PyTorch",
"onnxruntime_opset_19": "ONNX or ONNXRuntime",
"runtime-24.1-py3.11": "AI Services/Python Functions/Python Scripts",
"autoai-ts_rt24.1-py3.11": "AutoAI",
"autoai-kb_rt24.1-py3.11": "AutoAI",
"runtime-24.1-py3.11-cuda": "CUDA-enabled (GPU) Python Runtime",
"runtime-24.1-r4.3": "R Runtime 4.3",
"spark-mllib_3.4": "Apache Spark 3.4",
"autoai-rag_rt24.1-py3.11": "AutoAI RAG"
}
# Define the preferred order for items to appear at the top
preferred_order = [
"runtime-24.1-py3.11",
"runtime-24.1-py3.11-cuda",
"runtime-24.1-r4.3",
"ai-service-v5-software-specification",
"autoai-rag_rt24.1-py3.11",
"autoai-ts_rt24.1-py3.11",
"autoai-kb_rt24.1-py3.11",
"tensorflow_rt24.1-py3.11",
"pytorch-onnx_rt24.1-py3.11",
"onnxruntime_opset_19",
"spark-mllib_3.4",
]
# Create a new column for sorting
supported_specs['SORT_ORDER'] = supported_specs['NAME'].apply(
lambda x: preferred_order.index(x) if x in preferred_order else len(preferred_order)
)
# Sort the DataFrame by the new column
supported_specs = supported_specs.sort_values('SORT_ORDER').reset_index(drop=True)
# Drop the sorting column as it's no longer needed
supported_specs = supported_specs.drop(columns=['SORT_ORDER'])
# Drop the REPLACEMENT column if it exists and add NOTES column
if 'REPLACEMENT' in supported_specs.columns:
supported_specs = supported_specs.drop(columns=['REPLACEMENT'])
# Add NOTES column with framework information
supported_specs['NOTES'] = supported_specs['NAME'].map(framework_mapping).fillna("Other")
# Create a table with single-row selection
selection_table = mo.ui.table(
supported_specs,
selection="single", # Only allow selecting one row
label="#### **Select a supported software_spec runtime for your function asset** (For Python Functions select - *'runtime-24.1-py3.11'* ):",
initial_selection=[0], # Now selecting the first row, which should be runtime-24.1-py3.11
page_size=6
)
else:
sel_df = pd.DataFrame(
data=[["ID", "Activate deployment_client."]],
columns=["ID", "VALUE"]
)
selection_table = mo.ui.table(
sel_df,
selection="single", # Only allow selecting one row
label="You haven't activated the Deployment_Client",
initial_selection=[0]
)
# Display the table
mo.md(f"""---
{selection_table}
---
""")
return (
framework_mapping,
preferred_order,
sel_df,
selection_table,
supported_specs,
)
@app.cell
def _(mo):
input_schema_checkbox = mo.ui.checkbox(label="Add input schema (optional)")
output_schema_checkbox = mo.ui.checkbox(label="Add output schema (optional)")
sample_input_checkbox = mo.ui.checkbox(label="Add sample input example (optional)")
return input_schema_checkbox, output_schema_checkbox, sample_input_checkbox
@app.cell
def _(
input_schema_checkbox,
mo,
output_schema_checkbox,
sample_input_checkbox,
selection_table,
template_variant,
):
if selection_table.value['ID'].iloc[0]:
# Create the input fields
if template_variant.value == "Stream Files to IBM COS [Example]":
fnc_nm = "stream_file_to_cos"
else:
fnc_nm = "your_function_name"
uploaded_function_name = mo.ui.text(placeholder="", label="Function Name:", kind="text", value=f"{fnc_nm}", full_width=False)
tags_editor = mo.ui.array(
[mo.ui.text(placeholder="Metadata Tags..."), mo.ui.text(), mo.ui.text()],
label="Optional Metadata Tags"
)
software_spec = selection_table.value['ID'].iloc[0]
description_input = mo.ui.text_area(
placeholder="Write a description for your function...)",
label="Description",
max_length=256,
rows=5,
full_width=True
)
func_metadata=mo.hstack([
description_input,
mo.hstack([
uploaded_function_name,
tags_editor,
], justify="start", gap=1, align="start", wrap=True)
],
widths=[0.6,0.4],
gap=2.75
)
schema_metadata=mo.hstack([
input_schema_checkbox,
output_schema_checkbox,
sample_input_checkbox
],
justify="center", gap=1, align="center", wrap=True
)
# Display the metadata inputs
mo.vstack([
func_metadata,
mo.md("**Make sure to click the checkboxes before filling in descriptions and tags or they will reset.**"),
schema_metadata
],
align="center",
gap=2
)
return (
description_input,
fnc_nm,
func_metadata,
schema_metadata,
software_spec,
tags_editor,
uploaded_function_name,
)
@app.cell
def _(json, mo):
if template_variant.value == "Stream Files to IBM COS [Example]":
from cos_stream_schema_examples import input_schema, output_schema, sample_input
else:
input_schema = [
{
'id': '1',
'type': 'struct',
'fields': [
{
'name': '',
'type': 'string',
'nullable': False,
'metadata': {}
},
{
'name': '',
'type': 'string',
'nullable': False,
'metadata': {}
}
]
}
]
output_schema = [
{
'id': '1',
'type': 'struct',
'fields': [
{
'name': '