Martín Santillán Cooper commited on
Commit
14277b2
1 Parent(s): 1a38959

Move all components to the block

Browse files
Files changed (1) hide show
  1. app.py +29 -35
app.py CHANGED
@@ -10,35 +10,14 @@ import os
10
  from time import sleep
11
 
12
  catalog = {}
13
- all_test_cases = []
14
 
15
  with open('catalog.json') as f:
16
  catalog = json.load(f)
17
 
18
- starting_test_case = [t for sub_catalog_name, sub_catalog in catalog.items() for t in sub_catalog if t['name'] == 'Harmful' and sub_catalog_name == "Harmful content in user message"][0]
19
- test_case_name = gr.HTML(f'<h2>{starting_test_case["name"]}</h2>')
20
- criteria = gr.Textbox(label="Definition", lines=3, interactive=False, value=starting_test_case['criteria'], elem_classes=['read-only'])
21
- context = gr.Textbox(label="Context", lines=3, interactive=True, value=starting_test_case['context'], visible=False)
22
- user_message = gr.Textbox(label="User Message", lines=3, interactive=True, value=starting_test_case['user_message'])
23
- assistant_message = gr.Textbox(label="Assistant Message", lines=3, interactive=True, visible=False, value=starting_test_case['assistant_message'])
24
- catalog_buttons: dict[str,dict[str,gr.Button]] = {}
25
- result_text = gr.Textbox(label="Result", interactive=False, elem_classes=['read-only'])
26
- result_certainty = gr.Number(label="Certainty", interactive=False, value='', elem_classes=['read-only'])
27
-
28
- for sub_catalog_name, sub_catalog in catalog.items():
29
- catalog_buttons[sub_catalog_name] = {}
30
- for test_case in sub_catalog:
31
- elem_classes=['catalog-button']
32
- elem_id=f"{sub_catalog_name}_{test_case['name']}"
33
- if elem_id == "Harmful content in user message_Harmful":
34
- elem_classes.append('selected')
35
- catalog_buttons[sub_catalog_name][test_case['name']] = \
36
- gr.Button(test_case['name'], elem_classes=elem_classes, variant='secondary', size='sm', elem_id=elem_id)
37
-
38
  def on_test_case_click(link, event: gr.EventData):
39
  target_sub_catalog_name, target_test_case_name = event.target.elem_id.split('_')
 
40
  selected_test_case = [t for sub_catalog_name, sub_catalog in catalog.items() for t in sub_catalog if t['name'] == link and sub_catalog_name == target_sub_catalog_name][0]
41
- print(selected_test_case['assistant_message'])
42
  return {
43
  test_case_name: f'<h2>{selected_test_case["name"]}</h2>',
44
  criteria: selected_test_case['criteria'],
@@ -71,41 +50,56 @@ def on_submit(inputs):
71
  with gr.Blocks(
72
  title='Granite Guardian',
73
  theme=gr.themes.Soft(font=[gr.themes.GoogleFont("IBM Plex Sans")]), css='styles.css') as demo:
 
 
 
74
  with gr.Row():
75
  gr.HTML('<h1>Granite Guardian</h1>', elem_classes='title')
76
  with gr.Row(elem_classes='column-gap'):
77
  with gr.Column(scale=0):
78
  title_display_left = gr.HTML("<h2>Catalog - Harms & Risks</h2>")
79
  accordions = []
 
80
  for i, (sub_catalog_name, sub_catalog) in enumerate(catalog.items()):
81
  with gr.Accordion(sub_catalog_name, open=i==0, elem_classes='accordion-align') as accordion:
82
  for test_case in sub_catalog:
 
 
 
 
 
 
 
 
 
 
 
83
  link = catalog_buttons[sub_catalog_name][test_case['name']]
84
- link.render()
85
- link.click(on_test_case_click, link, {test_case_name, criteria, context, user_message, assistant_message, result_text, result_certainty}) \
86
- .then(change_button_color, None, [v for c in catalog_buttons.values() for v in c.values()])
87
  accordions.append(accordion)
88
- def on_accordion_open(open):
89
- pass
90
  with gr.Column(visible=True) as test_case_content:
91
- test_case_name.render()
 
92
  gr.HTML("Evaluation Criteria", elem_classes='subtitle')
93
- criteria.render()
94
 
95
  gr.HTML("Test Data", elem_classes='subtitle')
96
- context.render()
97
- user_message.render()
98
- assistant_message.render()
99
 
100
  submit_button = gr.Button("Evaluate", variant='primary')
101
  gr.HTML("Evaluation results", elem_classes='subtitle')
102
  with gr.Row():
103
- result_text.render()
104
- result_certainty.render()
105
 
106
  submit_button.click(
107
  on_submit,
108
  inputs={test_case_name, criteria, context, user_message, assistant_message},
109
  outputs=[result_text, result_certainty])
110
-
 
 
 
111
  demo.launch(server_name='0.0.0.0')
 
10
  from time import sleep
11
 
12
  catalog = {}
 
13
 
14
  with open('catalog.json') as f:
15
  catalog = json.load(f)
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def on_test_case_click(link, event: gr.EventData):
18
  target_sub_catalog_name, target_test_case_name = event.target.elem_id.split('_')
19
+ logger.debug(f'Changing to test case "{target_test_case_name}" from catalog "{target_sub_catalog_name}".')
20
  selected_test_case = [t for sub_catalog_name, sub_catalog in catalog.items() for t in sub_catalog if t['name'] == link and sub_catalog_name == target_sub_catalog_name][0]
 
21
  return {
22
  test_case_name: f'<h2>{selected_test_case["name"]}</h2>',
23
  criteria: selected_test_case['criteria'],
 
50
  with gr.Blocks(
51
  title='Granite Guardian',
52
  theme=gr.themes.Soft(font=[gr.themes.GoogleFont("IBM Plex Sans")]), css='styles.css') as demo:
53
+
54
+ starting_test_case = [t for sub_catalog_name, sub_catalog in catalog.items() for t in sub_catalog if t['name'] == 'Harmful' and sub_catalog_name == "Harmful content in user message"][0]
55
+
56
  with gr.Row():
57
  gr.HTML('<h1>Granite Guardian</h1>', elem_classes='title')
58
  with gr.Row(elem_classes='column-gap'):
59
  with gr.Column(scale=0):
60
  title_display_left = gr.HTML("<h2>Catalog - Harms & Risks</h2>")
61
  accordions = []
62
+ catalog_buttons: dict[str,dict[str,gr.Button]] = {}
63
  for i, (sub_catalog_name, sub_catalog) in enumerate(catalog.items()):
64
  with gr.Accordion(sub_catalog_name, open=i==0, elem_classes='accordion-align') as accordion:
65
  for test_case in sub_catalog:
66
+ elem_classes=['catalog-button']
67
+ elem_id=f"{sub_catalog_name}_{test_case['name']}"
68
+ if elem_id == "Harmful content in user message_Harmful":
69
+ elem_classes.append('selected')
70
+
71
+ if not sub_catalog_name in catalog_buttons:
72
+ catalog_buttons[sub_catalog_name] = {}
73
+
74
+ catalog_buttons[sub_catalog_name][test_case['name']] = \
75
+ gr.Button(test_case['name'], elem_classes=elem_classes, variant='secondary', size='sm', elem_id=elem_id)
76
+
77
  link = catalog_buttons[sub_catalog_name][test_case['name']]
78
+
 
 
79
  accordions.append(accordion)
 
 
80
  with gr.Column(visible=True) as test_case_content:
81
+ test_case_name = gr.HTML(f'<h2>{starting_test_case["name"]}</h2>')
82
+
83
  gr.HTML("Evaluation Criteria", elem_classes='subtitle')
84
+ criteria = gr.Textbox(label="Definition", lines=3, interactive=False, value=starting_test_case['criteria'], elem_classes=['read-only'])
85
 
86
  gr.HTML("Test Data", elem_classes='subtitle')
87
+ context = gr.Textbox(label="Context", lines=3, interactive=True, value=starting_test_case['context'], visible=False)
88
+ user_message = gr.Textbox(label="User Message", lines=3, interactive=True, value=starting_test_case['user_message'])
89
+ assistant_message = gr.Textbox(label="Assistant Message", lines=3, interactive=True, visible=False, value=starting_test_case['assistant_message'])
90
 
91
  submit_button = gr.Button("Evaluate", variant='primary')
92
  gr.HTML("Evaluation results", elem_classes='subtitle')
93
  with gr.Row():
94
+ result_text = gr.Textbox(label="Result", interactive=False, elem_classes=['read-only'])
95
+ result_certainty = gr.Number(label="Certainty", interactive=False, value='', elem_classes=['read-only'])
96
 
97
  submit_button.click(
98
  on_submit,
99
  inputs={test_case_name, criteria, context, user_message, assistant_message},
100
  outputs=[result_text, result_certainty])
101
+ for link in [t for sub_catalog_name, sub_catalog_buttons in catalog_buttons.items() for t in sub_catalog_buttons.values()]:
102
+ link.click(on_test_case_click, link, {test_case_name, criteria, context, user_message, assistant_message, result_text, result_certainty}) \
103
+ .then(change_button_color, None, [v for c in catalog_buttons.values() for v in c.values()])
104
+
105
  demo.launch(server_name='0.0.0.0')