Skip to content

Instantly share code, notes, and snippets.

View randyphoa's full-sized avatar

Randy Phoa randyphoa

  • IBM Data Science and AI Elite
View GitHub Profile
@randyphoa
randyphoa / TestCustomAction.java
Last active July 21, 2023 08:06
Sample OpenPages Custom Workflow Action
package com.randyphoa;
import java.io.File;
import java.util.List;
import org.apache.log4j.Logger;
import com.ibm.openpages.api.resource.IStringField;
import com.ibm.openpages.api.resource.IGRCObject;
import com.ibm.openpages.api.workflow.actions.AbstractCustomAction;
import com.ibm.openpages.api.workflow.actions.IWFCustomProperty;
@randyphoa
randyphoa / python-function-sample.py
Created November 11, 2022 07:07
Sample Python Function that handles request from R Shiny app
def func():
import os
import ibm_db
import subprocess
import sqlalchemy
import numpy as np
import pandas as pd
import ibm_watson_studio_lib
import ibm_watson_machine_learning
@randyphoa
randyphoa / index.html
Created November 9, 2022 05:20
R Shiny App index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>RARAS - IBM Cloud Pak for Data</title>
{{ headContent() }}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.1/dist/leaflet.css" integrity="sha256-sA+zWATbFveLLNqWO2gtiw3HL/lh1giY/Inf1BJ0z14=" crossorigin="" />
<link rel="stylesheet" href="https://unpkg.com/react-leaflet-markercluster/dist/styles.min.css" />
@randyphoa
randyphoa / app.R
Last active November 9, 2022 05:20
R Shiny App
library(httr)
library(shiny)
library(jsonlite)
DEPLOYMENT_ID <- "93562b3a-19d5-42db-a52a-f4846dd7d487"
VERSION <- "2022-09-27"
SPACE_ID = "399a2bce-40b5-457a-8e18-de978043e2c8"
API_KEY = "xxx"
CPD_URL = "https://cpd-cpd.itzroks-550003aw18-rmaez9-6ccd7f378ae819553d37d5f2ee142bd6-0000.au-syd.containers.appdomain.cloud"
USERNAME = "admin"
@randyphoa
randyphoa / deploy-r.py
Created November 7, 2022 19:22
Deploy R Shiny app
shiny_details = wml_client.shiny.store(meta_props={wml_client.shiny.ConfigurationMetaNames.NAME: SHINY_APP_NAME}, file_path="app.R.zip")
shiny_asset_id = wml_client.shiny.get_id(shiny_details)
deployment_details = wml_client.deployments.create(
artifact_uid=shiny_asset_id,
meta_props={
wml_client.deployments.ConfigurationMetaNames.NAME: SHINY_APP_DEPLOYMENT_NAME,
wml_client.deployments.ConfigurationMetaNames.R_SHINY: {"authentication": "anyone_with_url"},
wml_client.deployments.ConfigurationMetaNames.HARDWARE_SPEC: {"name": "M", "num_nodes": 1},
},
)
@randyphoa
randyphoa / python-function.py
Last active November 7, 2022 19:16
Mapping actions to functions
ACTIONS_MAPPING = {
"GET_ACCIDENT_DATA": get_accident_data,
"GET_ACCIDENT_DETAILS": get_accident_details,
"GET_CLUSTERS": get_clusters,
"GET_CLUSTERS_DETAILS": get_cluster_details,
"SAVE_CLUSTERS_RESULTS": save_clusters_results,
"GET_PREDICTIONS": get_predictions,
"GET_MODELS": get_models,
"GET_PREDICT_INPUT_FILES": get_predict_input_files,
}
@randyphoa
randyphoa / app.R
Created November 7, 2022 19:06
Populate require information in app.R
library(httr)
library(shiny)
library(jsonlite)
DEPLOYMENT_ID <- "21d4e28f-2e58-4b9b-9d82-5b6938655991"
VERSION <- "2022-09-27"
SPACE_ID = "399a2bce-40b5-457a-8e18-de978043e2c8"
API_KEY = "xxx"
CPD_URL = "https://cpd-cpd.itzroks-550003aw18-rmaez9-6ccd7f378ae819553d37d5f2ee142bd6-0000.au-syd.containers.appdomain.cloud"
USERNAME = "admin"
@randyphoa
randyphoa / app.R
Last active November 7, 2022 19:08
UI function that handles GET or POST requests
ui <- function(req) {
if (identical(req$REQUEST_METHOD, "GET")) {
htmlTemplate("index.html")
} else if (identical(req$REQUEST_METHOD, "POST")) {
body <- rawToChar(req$rook.input$read(-1))
url <- stringr::str_interp("${CPD_URL}/ml/v4/deployments/${DEPLOYMENT_ID}/predictions")
query <- list(version=VERSION)
r <- POST(
@randyphoa
randyphoa / get-accident-data.js
Last active November 7, 2022 18:57
Get accident data in React JS app
const getAccidentData = (countries) => {
const payload = {
input_data: [{
values: [{
ACTION: "GET_ACCIDENT_DATA",
PARAMS: { COUNTRY: countries.map(x => x.toUpperCase()) }
}]
}]
};
axios.post(URL, payload).then((response) => {
import sklearn
import sklearn.tree
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
def get_rules(tree, feature_names, class_names):
tree_ = tree.tree_
feature_name = [feature_names[i] if i != sklearn.tree._tree.TREE_UNDEFINED else "undefined!" for i in tree_.feature]
paths = []
path = []