Martín Santillán Cooper commited on
Commit
3ae940c
1 Parent(s): 0cb3cfc

Make layout narrower

Browse files
Files changed (2) hide show
  1. app.py +74 -60
  2. styles.css +0 -4
app.py CHANGED
@@ -54,7 +54,7 @@ def on_submit(criteria, context, user_message, assistant_message, state):
54
  # result_str = f"Is the {evaluated_component} message harmful based on the risk definition: {result_label}"
55
  html_str = f"<p>{get_result_description(state['selected_sub_catalog'], state['selected_criteria_name'])} <strong>{result_label}</strong></p>"
56
  # html_str = f"Is the {evaluated_component} {evaluated_component_adjective}: {result_label}"
57
- return [gr.update(value=html_str), gr.update(visible=True), gr.update(visible=True)]
58
 
59
  def on_show_prompt_click(criteria, context, user_message, assistant_message, state):
60
  prompt = get_prompt_from_test_case({
@@ -68,7 +68,7 @@ def on_show_prompt_click(criteria, context, user_message, assistant_message, sta
68
  prompt = json.dumps(prompt, indent=4)
69
  return gr.Markdown(prompt)
70
 
71
- ibm_blue = blue = gr.themes.Color(
72
  name="ibm-blue",
73
  c50="#eff6ff",
74
  c100="#dbeafe",
@@ -83,66 +83,79 @@ ibm_blue = blue = gr.themes.Color(
83
  c950="#1d3660"
84
  )
85
 
 
 
 
 
 
 
 
 
 
 
 
86
  with gr.Blocks(
87
  title='Granite Guardian',
88
  theme=gr.themes.Soft(
89
  primary_hue=ibm_blue,
90
- font=[gr.themes.GoogleFont("IBM Plex Sans"), gr.themes.GoogleFont('Source Sans 3')]),
 
 
 
91
  css='styles.css') as demo:
92
 
93
- with gr.Blocks(elem_classes="roott"):
94
-
95
- state = gr.State(value={
96
- 'selected_sub_catalog': 'harmful_content_in_user_prompt',
97
- 'selected_criteria_name': 'general_harm'
98
- })
99
-
100
- starting_test_case = [t for sub_catalog_name, sub_catalog in catalog.items() for t in sub_catalog if t['name'] == state.value['selected_criteria_name'] and sub_catalog_name == state.value['selected_sub_catalog']][0]
101
-
102
- with gr.Row(elem_classes='title-row'):
103
- with gr.Column(scale=4):
104
- gr.HTML('<h2>IBM Granite Guardian 3.0</h2>', elem_classes='title')
105
- gr.HTML(elem_classes='system-description', value='<p>Granite Guardian models are specialized language models in the Granite family that allow you to detect harms and risks in generative AI systems. The Granite Guardian models can be used with any other large language models to make interactions with generative AI systems safe. Select an example in the left panel to see how the model evaluates harms and risks in user prompts, assistant responses, and for hallucinations in retrieval-augmented generation. In this demo, we use granite-guardian-3.0-8B.</p>')
106
- with gr.Row(elem_classes='column-gap'):
107
- with gr.Column(scale=0, elem_classes='no-gap'):
108
- title_display_left = gr.HTML("<h2>Harms & Risks</h2>", elem_classes=['subtitle', 'subtitle-harms'])
109
- accordions = []
110
- catalog_buttons: dict[str,dict[str,gr.Button]] = {}
111
- for i, (sub_catalog_name, sub_catalog) in enumerate(catalog.items()):
112
- with gr.Accordion(to_title_case(sub_catalog_name), open=(i==0), elem_classes='accordion') as accordion:
113
- for test_case in sub_catalog:
114
- elem_classes=['catalog-button']
115
- elem_id=f"{sub_catalog_name}---{test_case['name']}"
116
- if starting_test_case == test_case:
117
- elem_classes.append('selected')
118
-
119
- if not sub_catalog_name in catalog_buttons:
120
- catalog_buttons[sub_catalog_name] = {}
121
-
122
- catalog_buttons[sub_catalog_name][test_case['name']] = \
123
- gr.Button(to_title_case(test_case['name']), elem_classes=elem_classes, variant='secondary', size='sm', elem_id=elem_id)
124
-
125
- accordions.append(accordion)
126
-
127
- with gr.Column(visible=True, scale=1) as test_case_content:
128
- with gr.Row():
129
- test_case_name = gr.HTML(f'<h2>{to_title_case(starting_test_case["name"])}</h2>', elem_classes='subtitle')
130
- show_propt_button = gr.Button('Show prompt', size='sm', scale=0, min_width=110)
131
-
132
- criteria = gr.Textbox(label="Evaluation Criteria", lines=3, interactive=False, value=starting_test_case['criteria'], elem_classes=['read-only', 'input-box', 'margin-bottom'])
133
- gr.HTML(elem_classes=['block', 'content-gap'])
134
- context = gr.Textbox(label="Context", lines=3, interactive=True, value=starting_test_case['context'], visible=False, elem_classes=['input-box'])
135
- user_message = gr.Textbox(label="User Prompt", lines=3, interactive=True, value=starting_test_case['user_message'], elem_classes=['input-box'])
136
- assistant_message = gr.Textbox(label="Assistant Response", lines=3, interactive=True, visible=False, value=starting_test_case['assistant_message'], elem_classes=['input-box'])
137
-
138
- submit_button = gr.Button("Evaluate", variant='primary',icon=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'send-white.png'), elem_classes='submit-button')
139
-
140
- with gr.Column(elem_classes="result-container", visible=False) as result_container:
141
- evaluation_results_label = gr.HTML("<span>Results</span>", elem_classes='result-title', visible=False)
142
- result_text = gr.HTML(label="Result", elem_classes=['result-text', 'input-box', 'read-only', 'block'], visible=False, value='')
143
-
144
- with Modal(visible=False, elem_classes='modal') as modal:
145
- prompt = gr.Markdown('')
146
 
147
  show_propt_button.click(
148
  on_show_prompt_click,
@@ -152,13 +165,14 @@ with gr.Blocks(
152
 
153
  submit_button \
154
  .click(
155
- lambda: [gr.update(visible=True), gr.update(visible=True)],
156
  inputs=None,
157
- outputs=[result_container, result_text]) \
158
- .then(
159
  on_submit,
160
  inputs=[criteria, context, user_message, assistant_message, state],
161
- outputs=[result_text, evaluation_results_label, result_container])
 
162
 
163
  for button in [t for sub_catalog_name, sub_catalog_buttons in catalog_buttons.items() for t in sub_catalog_buttons.values()]:
164
  button.click(update_selected_test_case, inputs=[button, state], outputs=[state])\
 
54
  # result_str = f"Is the {evaluated_component} message harmful based on the risk definition: {result_label}"
55
  html_str = f"<p>{get_result_description(state['selected_sub_catalog'], state['selected_criteria_name'])} <strong>{result_label}</strong></p>"
56
  # html_str = f"Is the {evaluated_component} {evaluated_component_adjective}: {result_label}"
57
+ return [gr.update(value=html_str, visible=True), gr.update(visible=True)]
58
 
59
  def on_show_prompt_click(criteria, context, user_message, assistant_message, state):
60
  prompt = get_prompt_from_test_case({
 
68
  prompt = json.dumps(prompt, indent=4)
69
  return gr.Markdown(prompt)
70
 
71
+ ibm_blue = gr.themes.Color(
72
  name="ibm-blue",
73
  c50="#eff6ff",
74
  c100="#dbeafe",
 
83
  c950="#1d3660"
84
  )
85
 
86
+ head_style = """
87
+ <style>
88
+ @media (min-width: 1536px)
89
+ {
90
+ .gradio-container {
91
+ max-width: 1024px !important;
92
+ }
93
+ }
94
+ </style>
95
+ """
96
+
97
  with gr.Blocks(
98
  title='Granite Guardian',
99
  theme=gr.themes.Soft(
100
  primary_hue=ibm_blue,
101
+ font=[gr.themes.GoogleFont("IBM Plex Sans"), gr.themes.GoogleFont('Source Sans 3')],
102
+ ),
103
+ head=head_style,
104
+ fill_width=False,
105
  css='styles.css') as demo:
106
 
107
+
108
+ state = gr.State(value={
109
+ 'selected_sub_catalog': 'harmful_content_in_user_prompt',
110
+ 'selected_criteria_name': 'general_harm'
111
+ })
112
+
113
+ starting_test_case = [t for sub_catalog_name, sub_catalog in catalog.items() for t in sub_catalog if t['name'] == state.value['selected_criteria_name'] and sub_catalog_name == state.value['selected_sub_catalog']][0]
114
+
115
+ with gr.Row(elem_classes='title-row'):
116
+ with gr.Column(scale=4):
117
+ gr.HTML('<h2>IBM Granite Guardian 3.0</h2>', elem_classes='title')
118
+ gr.HTML(elem_classes='system-description', value='<p>Granite Guardian models are specialized language models in the Granite family that allow you to detect harms and risks in generative AI systems. The Granite Guardian models can be used with any other large language models to make interactions with generative AI systems safe. Select an example in the left panel to see how the model evaluates harms and risks in user prompts, assistant responses, and for hallucinations in retrieval-augmented generation. In this demo, we use granite-guardian-3.0-8B.</p>')
119
+ with gr.Row(elem_classes='column-gap'):
120
+ with gr.Column(scale=0, elem_classes='no-gap'):
121
+ title_display_left = gr.HTML("<h2>Harms & Risks</h2>", elem_classes=['subtitle', 'subtitle-harms'])
122
+ accordions = []
123
+ catalog_buttons: dict[str,dict[str,gr.Button]] = {}
124
+ for i, (sub_catalog_name, sub_catalog) in enumerate(catalog.items()):
125
+ with gr.Accordion(to_title_case(sub_catalog_name), open=(i==0), elem_classes='accordion') as accordion:
126
+ for test_case in sub_catalog:
127
+ elem_classes=['catalog-button']
128
+ elem_id=f"{sub_catalog_name}---{test_case['name']}"
129
+ if starting_test_case == test_case:
130
+ elem_classes.append('selected')
131
+
132
+ if not sub_catalog_name in catalog_buttons:
133
+ catalog_buttons[sub_catalog_name] = {}
134
+
135
+ catalog_buttons[sub_catalog_name][test_case['name']] = \
136
+ gr.Button(to_title_case(test_case['name']), elem_classes=elem_classes, variant='secondary', size='sm', elem_id=elem_id)
137
+
138
+ accordions.append(accordion)
139
+
140
+ with gr.Column(visible=True, scale=1) as test_case_content:
141
+ with gr.Row():
142
+ test_case_name = gr.HTML(f'<h2>{to_title_case(starting_test_case["name"])}</h2>', elem_classes='subtitle')
143
+ show_propt_button = gr.Button('Show prompt', size='sm', scale=0, min_width=110)
144
+
145
+ criteria = gr.Textbox(label="Evaluation Criteria", lines=3, interactive=False, value=starting_test_case['criteria'], elem_classes=['read-only', 'input-box', 'margin-bottom'])
146
+ gr.HTML(elem_classes=['block', 'content-gap'])
147
+ context = gr.Textbox(label="Context", lines=3, interactive=True, value=starting_test_case['context'], visible=False, elem_classes=['input-box'])
148
+ user_message = gr.Textbox(label="User Prompt", lines=3, interactive=True, value=starting_test_case['user_message'], elem_classes=['input-box'])
149
+ assistant_message = gr.Textbox(label="Assistant Response", lines=3, interactive=True, visible=False, value=starting_test_case['assistant_message'], elem_classes=['input-box'])
150
+
151
+ submit_button = gr.Button("Evaluate", variant='primary',icon=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'send-white.png'), elem_classes='submit-button')
152
+
153
+ with gr.Column(elem_classes="result-container", visible=False, show_progress=False) as result_container:
154
+ evaluation_results_label = gr.HTML("<span>Results</span>", elem_classes='result-title', visible=False)
155
+ result_text = gr.HTML(elem_classes=['result-text', 'input-box', 'read-only', 'block'], visible=False, value='')
156
+
157
+ with Modal(visible=False, elem_classes='modal') as modal:
158
+ prompt = gr.Markdown('')
 
159
 
160
  show_propt_button.click(
161
  on_show_prompt_click,
 
165
 
166
  submit_button \
167
  .click(
168
+ lambda: [gr.update(visible=True), gr.update(visible=False), gr.update(visible=True, value='')],
169
  inputs=None,
170
+ outputs=[result_container, evaluation_results_label, result_text]
171
+ ).then(
172
  on_submit,
173
  inputs=[criteria, context, user_message, assistant_message, state],
174
+ outputs=[result_text, evaluation_results_label]
175
+ )
176
 
177
  for button in [t for sub_catalog_name, sub_catalog_buttons in catalog_buttons.items() for t in sub_catalog_buttons.values()]:
178
  button.click(update_selected_test_case, inputs=[button, state], outputs=[state])\
styles.css CHANGED
@@ -1,7 +1,3 @@
1
- .roott {
2
- width: 500px !important;
3
- }
4
-
5
  .title-row {
6
  margin-bottom: 0.75rem;
7
  }
 
 
 
 
 
1
  .title-row {
2
  margin-bottom: 0.75rem;
3
  }