MilanM commited on
Commit
1acbd8b
·
verified ·
1 Parent(s): 17baa27

Update main_app.py

Browse files
Files changed (1) hide show
  1. main_app.py +91 -104
main_app.py CHANGED
@@ -229,7 +229,7 @@ def _():
229
  def _(cloudant, cloudant_db):
230
  variant_selectors = cloudant_retrieve_documents(
231
  client=cloudant,
232
- db_name=cloudant_db.value,
233
  selectors={"filename": {"$exists": True}},
234
  fields=["category"],
235
  )
@@ -246,6 +246,21 @@ def _(mo):
246
  template_variant = mo.ui.dropdown(template_variants, label="Code Template:", value="Base")
247
  return (template_variant,)
248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
 
250
  @app.cell
251
  def _(client_callout_kind, client_instantiation_form, mo, template_variant):
@@ -511,34 +526,6 @@ def _(mo, purging_section):
511
 
512
  @app.cell
513
  def _(mo, template_variant):
514
- # Template for WatsonX.ai deployable function
515
- if template_variant.value == "Stream Files to IBM COS [Example]":
516
- with open("stream_files_to_cos.py", "r") as file:
517
- template = file.read()
518
- else:
519
- template = '''def your_function_name():
520
-
521
- import subprocess
522
- subprocess.check_output('pip install gensim', shell=True)
523
- import gensim
524
-
525
- def score(input_data):
526
- message_from_input_payload = payload.get("input_data")[0].get("values")[0][0]
527
- response_message = "Received message - {0}".format(message_from_input_payload)
528
-
529
- # Score using the pre-defined model
530
- score_response = {
531
- 'predictions': [{'fields': ['Response_message_field', 'installed_lib_version'],
532
- 'values': [[response_message, gensim.__version__]]
533
- }]
534
- }
535
- return score_response
536
-
537
- return score
538
-
539
- score = your_function_name()
540
- '''
541
-
542
  function_editor = (
543
  mo.md('''
544
  #### **Create your function by editing the template:**
@@ -547,7 +534,7 @@ def _(mo, template_variant):
547
 
548
  ''')
549
  .batch(
550
- editor = mo.ui.code_editor(value=template, language="python", min_height=200, theme="dark")
551
  )
552
  .form(show_clear_button=True, bordered=False)
553
  )
@@ -593,54 +580,31 @@ def _(ast, function_editor, mo, os):
593
 
594
  @app.cell
595
  def _():
 
 
 
 
 
 
 
596
  yaml_templates = {
597
  "empty": """dependencies:
598
  - pip
599
  - pip:
600
  - <library_name>
601
  - ...
602
- """,
603
- "llama_index_and_scikit": """dependencies:
604
- - pip
605
- - pip:
606
- - scikit-learn==1.6.1
607
- - scikit-llm==1.4.1
608
- - plotly==6.0.1
609
- - altair==5.5.0
610
- - PyMuPDF==1.25.1
611
- - llama-index==0.12.24
612
- - llama-index-readers-file==0.4.6
613
- - llama-index-readers-web==0.3.8
614
- """,
615
- "data_product_hub": """dependencies:
616
- - pip
617
- - pip:
618
- - PyMuPDF==1.25.1
619
- - llama-index==0.12.24
620
- - llama-index-readers-file==0.4.6
621
- - llama-index-readers-web==0.3.8
622
- - plotly==6.0.1
623
- - altair==5.5.0
624
- - dask==2025.2.0
625
- - dph-services==0.4.0
626
- """,
627
- "watsonx_data": """dependencies:
628
- - pip
629
- - pip:
630
- - prestodb
631
- - PyMuPDF==1.25.1
632
- - llama-index==0.12.24
633
- - llama-index-readers-file==0.4.6
634
- - llama-index-readers-web==0.3.8
635
- - ibm-watsonxdata==0.4.0
636
  """
637
  }
 
 
 
 
638
  return (yaml_templates,)
639
 
640
 
641
  @app.cell
642
  def _(mo, yaml_templates):
643
- yaml_template = mo.ui.dropdown(yaml_templates, searchable=True, label="**Select a template:**", value="empty")
644
  return (yaml_template,)
645
 
646
 
@@ -677,10 +641,10 @@ def _(mo, yaml_template):
677
  package_name = mo.ui.text(placeholder="Python Package for...",
678
  label="Package Extension Name:",
679
  kind="text",
680
- value="Custom Python Package"),
681
  software_spec_name = mo.ui.text(placeholder="Software Spec Name",
682
  label="Custom Software Spec Name:",
683
- kind="text", value="Extended Python Function Software Spec"),
684
  package_description = mo.ui.text_area(placeholder="Write a description for your package.",
685
  label="Package Description:",
686
  value=" "),
@@ -1152,7 +1116,6 @@ def _(hw_selection_table, set_selected_hw_spec):
1152
  def _(mo):
1153
  input_schema_checkbox = mo.ui.checkbox(label="Add input schema (optional)")
1154
  output_schema_checkbox = mo.ui.checkbox(label="Add output schema (optional)")
1155
- # sample_input_checkbox = mo.ui.checkbox(label="Add sample input example (optional)")
1156
  return input_schema_checkbox, output_schema_checkbox
1157
 
1158
 
@@ -1171,15 +1134,13 @@ def _(
1171
  if function_name is not None:
1172
  fnc_nm = function_name
1173
  else:
1174
- if template_variant.value == "Stream Files to IBM COS [Example]":
1175
- fnc_nm = "stream_file_to_cos"
1176
- else:
1177
- fnc_nm = "your_function_name"
1178
 
1179
  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)
1180
  tags_editor = mo.ui.array(
1181
- [mo.ui.text(placeholder="Metadata Tags..."), mo.ui.text(), mo.ui.text()],
1182
- label="Optional Metadata Tags"
 
1183
  )
1184
  software_spec = get_selected_sw_spec()
1185
 
@@ -1190,8 +1151,17 @@ def _(
1190
  rows=5,
1191
  full_width=True
1192
  )
 
 
 
 
 
 
 
1193
 
1194
-
 
 
1195
  func_metadata=mo.hstack([
1196
  description_input,
1197
  mo.hstack([
@@ -1202,42 +1172,47 @@ def _(
1202
  widths=[0.6,0.4],
1203
  gap=2.75
1204
  )
 
1205
 
 
 
 
1206
  schema_metadata=mo.hstack([
1207
  input_schema_checkbox,
1208
  output_schema_checkbox,
1209
- # sample_input_checkbox
1210
  ],
1211
  justify="center", gap=1, align="center", wrap=True
1212
  )
 
1213
 
1214
- fm = mo.vstack([
1215
- func_metadata,
1216
- ],
1217
- align="center",
1218
- gap=2
1219
- )
1220
- sc_m = mo.vstack([
1221
- schema_metadata,
1222
- mo.md("**Make sure to select the checkbox options before filling in descriptions and tags or they will reset.**")
1223
- ],
1224
- align="center",
1225
- gap=2
1226
- )
1227
- return (
1228
- description_input,
1229
- fm,
1230
- sc_m,
1231
- software_spec,
1232
- tags_editor,
1233
- uploaded_function_name,
1234
- )
 
1235
 
1236
 
1237
  @app.cell
1238
  def _(json, mo, template_variant):
1239
- if template_variant.value == "Stream Files to IBM COS [Example]":
1240
- from cos_stream_schema_examples import input_schema, output_schema
1241
  else:
1242
  input_schema = [
1243
  {
@@ -1259,7 +1234,10 @@ def _(json, mo, template_variant):
1259
  ]
1260
  }
1261
  ]
1262
-
 
 
 
1263
  output_schema = [
1264
  {
1265
  'id': '1',
@@ -1275,21 +1253,30 @@ def _(json, mo, template_variant):
1275
  }
1276
  ]
1277
 
 
 
 
 
1278
 
1279
 
 
 
 
 
 
1280
  input_schema_editor = mo.ui.code_editor(value=json.dumps(input_schema, indent=4), language="python", min_height=100,theme="dark")
1281
  output_schema_editor = mo.ui.code_editor(value=json.dumps(output_schema, indent=4), language="python", min_height=100,theme="dark")
 
1282
 
 
 
1283
  schema_editors = mo.accordion(
1284
  {
1285
  """**Input Schema Metadata Editor**""": input_schema_editor,
1286
  """**Output Schema Metadata Editor**""": output_schema_editor,
1287
  }, multiple=True
1288
  )
1289
-
1290
- # schema_editors
1291
- return input_schema_editor, output_schema_editor, schema_editors
1292
-
1293
 
1294
  @app.cell
1295
  def _(
 
229
  def _(cloudant, cloudant_db):
230
  variant_selectors = cloudant_retrieve_documents(
231
  client=cloudant,
232
+ db_name=cloudant_db,
233
  selectors={"filename": {"$exists": True}},
234
  fields=["category"],
235
  )
 
246
  template_variant = mo.ui.dropdown(template_variants, label="Code Template:", value="Base")
247
  return (template_variant,)
248
 
249
+ @app.cell
250
+ def _(cloudant, cloudant_db, template_variant, refresh):
251
+ refresh.value
252
+ retrieved_template = (
253
+ cloudant_retrieve_documents(
254
+ client=cloudant,
255
+ db_name=cloudant_db,
256
+ selectors={"category": {"$eq": template_variant.value}},
257
+ fields=json.loads(os.getenv("FIELDS_TO_RETRIEVE")) or [],
258
+ )
259
+ if template_variant.value
260
+ else None
261
+ )
262
+ template = retrieved_template["docs"][0] if retrieved_template else None
263
+ return (template,)
264
 
265
  @app.cell
266
  def _(client_callout_kind, client_instantiation_form, mo, template_variant):
 
526
 
527
  @app.cell
528
  def _(mo, template_variant):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
529
  function_editor = (
530
  mo.md('''
531
  #### **Create your function by editing the template:**
 
534
 
535
  ''')
536
  .batch(
537
+ editor = mo.ui.code_editor(value=template["function_code"], language="python", min_height=200, theme="dark")
538
  )
539
  .form(show_clear_button=True, bordered=False)
540
  )
 
580
 
581
  @app.cell
582
  def _():
583
+ dependencies = template["dependencies"]
584
+ yaml_template = f"""dependencies:
585
+ - pip
586
+ - pip:
587
+ {'\n'.join(f' - {dep}' for dep in dependencies)}
588
+ """
589
+
590
  yaml_templates = {
591
  "empty": """dependencies:
592
  - pip
593
  - pip:
594
  - <library_name>
595
  - ...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
596
  """
597
  }
598
+
599
+ dependency_pack_name = template["function_name"] + "_dependencies"
600
+ # After the yaml_template definition, add:
601
+ yaml_templates[dependency_pack_name] = yaml_template
602
  return (yaml_templates,)
603
 
604
 
605
  @app.cell
606
  def _(mo, yaml_templates):
607
+ yaml_template = mo.ui.dropdown(yaml_templates, searchable=True, label="**Select a template:**", value=dependency_pack_name)
608
  return (yaml_template,)
609
 
610
 
 
641
  package_name = mo.ui.text(placeholder="Python Package for...",
642
  label="Package Extension Name:",
643
  kind="text",
644
+ value=f"Custom Python Package: {dependency_pack_name}"),
645
  software_spec_name = mo.ui.text(placeholder="Software Spec Name",
646
  label="Custom Software Spec Name:",
647
+ kind="text", value=f"Extended Python Function Software Spec - {dependency_pack_name}"),
648
  package_description = mo.ui.text_area(placeholder="Write a description for your package.",
649
  label="Package Description:",
650
  value=" "),
 
1116
  def _(mo):
1117
  input_schema_checkbox = mo.ui.checkbox(label="Add input schema (optional)")
1118
  output_schema_checkbox = mo.ui.checkbox(label="Add output schema (optional)")
 
1119
  return input_schema_checkbox, output_schema_checkbox
1120
 
1121
 
 
1134
  if function_name is not None:
1135
  fnc_nm = function_name
1136
  else:
1137
+ fnc_nm = "custom_pythoN_functin"
 
 
 
1138
 
1139
  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)
1140
  tags_editor = mo.ui.array(
1141
+ [mo.ui.text(placeholder="Metadata Tags...", value=use_case) for use_case in template["use_cases"]] +
1142
+ [mo.ui.text(placeholder="Metadata Tags...") for _ in range(max(0, 4 - len(template["use_cases"])))],
1143
+ label="Optional Metadata Tags"
1144
  )
1145
  software_spec = get_selected_sw_spec()
1146
 
 
1151
  rows=5,
1152
  full_width=True
1153
  )
1154
+
1155
+ return (
1156
+ description_input,
1157
+ software_spec,
1158
+ tags_editor,
1159
+ uploaded_function_name,
1160
+ )
1161
 
1162
+ @app.cell
1163
+ def _(mo, selection_table, description_input, uploaded_function_name, tags_editor):
1164
+ if selection_table.value is not None:
1165
  func_metadata=mo.hstack([
1166
  description_input,
1167
  mo.hstack([
 
1172
  widths=[0.6,0.4],
1173
  gap=2.75
1174
  )
1175
+ return func_metadata
1176
 
1177
+ @app.cell
1178
+ def _(mo, selection_table, input_schema_checkbox, output_schema_checkbox):
1179
+ if selection_table.value is not None:
1180
  schema_metadata=mo.hstack([
1181
  input_schema_checkbox,
1182
  output_schema_checkbox,
 
1183
  ],
1184
  justify="center", gap=1, align="center", wrap=True
1185
  )
1186
+ return schema_metadata
1187
 
1188
+ @app.cell
1189
+ def _(mo, selection_table, func_metadata):
1190
+ if selection_table.value is not None:
1191
+ fm = mo.vstack([
1192
+ func_metadata,
1193
+ ],
1194
+ align="center",
1195
+ gap=2
1196
+ )
1197
+ return fm
1198
+
1199
+ @app.cell
1200
+ def _(mo, selection_table, schema_metadata):
1201
+ if selection_table.value is not None:
1202
+ sc_m = mo.vstack([
1203
+ schema_metadata,
1204
+ mo.md("**Make sure to select the checkbox options before filling in descriptions and tags or they will reset.**")
1205
+ ],
1206
+ align="center",
1207
+ gap=2
1208
+ )
1209
+ return sc_m
1210
 
1211
 
1212
  @app.cell
1213
  def _(json, mo, template_variant):
1214
+ if template["input_schema"]:
1215
+ input_schema = template["input_schema"]
1216
  else:
1217
  input_schema = [
1218
  {
 
1234
  ]
1235
  }
1236
  ]
1237
+
1238
+ if template["output_schema"]:
1239
+ output_schema = template["output_schema"]
1240
+ else:
1241
  output_schema = [
1242
  {
1243
  'id': '1',
 
1253
  }
1254
  ]
1255
 
1256
+ return input_schema, output_schema
1257
+
1258
+ input_schema_editor = mo.ui.code_editor(value=json.dumps(input_schema, indent=4), language="python", min_height=100,theme="dark")
1259
+ output_schema_editor = mo.ui.code_editor(value=json.dumps(output_schema, indent=4), language="python", min_height=100,theme="dark")
1260
 
1261
 
1262
+ # schema_editors
1263
+ return input_schema_editor, output_schema_editor
1264
+
1265
+ @app.cell
1266
+ def _(mo, input_schema, output_schema):
1267
  input_schema_editor = mo.ui.code_editor(value=json.dumps(input_schema, indent=4), language="python", min_height=100,theme="dark")
1268
  output_schema_editor = mo.ui.code_editor(value=json.dumps(output_schema, indent=4), language="python", min_height=100,theme="dark")
1269
+ return input_schema_editor, output_schema_editor
1270
 
1271
+ @app.cell
1272
+ def _(mo, input_schema_editor, output_schema_editor):
1273
  schema_editors = mo.accordion(
1274
  {
1275
  """**Input Schema Metadata Editor**""": input_schema_editor,
1276
  """**Output Schema Metadata Editor**""": output_schema_editor,
1277
  }, multiple=True
1278
  )
1279
+ return schema_editors
 
 
 
1280
 
1281
  @app.cell
1282
  def _(