Spaces:
Running
on
Zero
Running
on
Zero
Martín Santillán Cooper
commited on
Commit
•
6617373
1
Parent(s):
8135919
add mock model call functionality
Browse files- .env.example +2 -1
- app.py +21 -17
- model.py +0 -1
- styles.css +5 -0
.env.example
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
MODEL_PATH='../dmf_models/granite-guardian-8b-pipecleaner-r241024a'
|
2 |
-
USE_CONDA='true'
|
|
|
|
1 |
MODEL_PATH='../dmf_models/granite-guardian-8b-pipecleaner-r241024a'
|
2 |
+
USE_CONDA='true'
|
3 |
+
MOCK_MODEL_CALL='false'
|
app.py
CHANGED
@@ -5,9 +5,9 @@ from utils import get_prompt_from_test_case
|
|
5 |
load_dotenv()
|
6 |
import json
|
7 |
from model import generate_text
|
8 |
-
import
|
9 |
-
|
10 |
-
|
11 |
|
12 |
catalog = {}
|
13 |
all_test_cases = []
|
@@ -17,13 +17,13 @@ with open('catalog.json') as 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'])
|
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)
|
26 |
-
result_certainty = gr.Number(label="Certainty", interactive=False, value='')
|
27 |
|
28 |
for sub_catalog_name, sub_catalog in catalog.items():
|
29 |
catalog_buttons[sub_catalog_name] = {}
|
@@ -43,7 +43,7 @@ def on_test_case_click(link, event: gr.EventData):
|
|
43 |
test_case_name: f'<h2>{selected_test_case["name"]}</h2>',
|
44 |
criteria: selected_test_case['criteria'],
|
45 |
context: selected_test_case['context'] if selected_test_case['context'] is not None else gr.update(visible=False, value=''),
|
46 |
-
user_message: selected_test_case['user_message'],
|
47 |
assistant_message: gr.update(value=selected_test_case['assistant_message'], visible=True) if selected_test_case['assistant_message'] is not None else gr.update(visible=False, value=''),
|
48 |
result_text: gr.update(value=''),
|
49 |
result_certainty: gr.update(value='')
|
@@ -53,16 +53,20 @@ def change_button_color(event: gr.EventData):
|
|
53 |
return [gr.update(elem_classes=['catalog-button', 'selected']) if v.elem_id == event.target.elem_id else gr.update(elem_classes=['catalog-button']) for c in catalog_buttons.values() for v in c.values()]
|
54 |
|
55 |
def on_submit(inputs):
|
56 |
-
|
57 |
-
|
58 |
-
'
|
59 |
-
|
60 |
-
'
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
|
66 |
|
67 |
with gr.Blocks(
|
68 |
title='Granite Guardian',
|
|
|
5 |
load_dotenv()
|
6 |
import json
|
7 |
from model import generate_text
|
8 |
+
from logger import logger
|
9 |
+
import os
|
10 |
+
from time import sleep
|
11 |
|
12 |
catalog = {}
|
13 |
all_test_cases = []
|
|
|
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] = {}
|
|
|
43 |
test_case_name: f'<h2>{selected_test_case["name"]}</h2>',
|
44 |
criteria: selected_test_case['criteria'],
|
45 |
context: selected_test_case['context'] if selected_test_case['context'] is not None else gr.update(visible=False, value=''),
|
46 |
+
user_message: gr.update(value=selected_test_case['user_message'], elem_classes=[], interactive=True) if target_sub_catalog_name != 'Harmful content in assistant message' else gr.update(value=selected_test_case['user_message'], interactive=False, elem_classes=['read-only']),
|
47 |
assistant_message: gr.update(value=selected_test_case['assistant_message'], visible=True) if selected_test_case['assistant_message'] is not None else gr.update(visible=False, value=''),
|
48 |
result_text: gr.update(value=''),
|
49 |
result_certainty: gr.update(value='')
|
|
|
53 |
return [gr.update(elem_classes=['catalog-button', 'selected']) if v.elem_id == event.target.elem_id else gr.update(elem_classes=['catalog-button']) for c in catalog_buttons.values() for v in c.values()]
|
54 |
|
55 |
def on_submit(inputs):
|
56 |
+
mock_model_call = os.getenv('MOCK_MODEL_CALL') == 'true'
|
57 |
+
if mock_model_call:
|
58 |
+
logger.debug('Returning mocked model result.')
|
59 |
+
sleep(3)
|
60 |
+
return 'Yes', 0.97
|
61 |
+
else:
|
62 |
+
prompt = get_prompt_from_test_case({
|
63 |
+
'criteria': inputs[criteria],
|
64 |
+
'context': inputs[context],
|
65 |
+
'user_message': inputs[user_message],
|
66 |
+
'assistant_message': inputs[assistant_message],
|
67 |
+
})
|
68 |
+
result = generate_text(prompt)
|
69 |
+
return result['assessment'], result['certainty']
|
70 |
|
71 |
with gr.Blocks(
|
72 |
title='Granite Guardian',
|
model.py
CHANGED
@@ -17,7 +17,6 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
17 |
device_map=device if use_conda else None
|
18 |
)
|
19 |
|
20 |
-
|
21 |
def generate_text(prompt):
|
22 |
logger.debug('Starting evaluation...')
|
23 |
logger.debug(f'Prompts content is: \n{prompt["content"]}')
|
|
|
17 |
device_map=device if use_conda else None
|
18 |
)
|
19 |
|
|
|
20 |
def generate_text(prompt):
|
21 |
logger.debug('Starting evaluation...')
|
22 |
logger.debug(f'Prompts content is: \n{prompt["content"]}')
|
styles.css
CHANGED
@@ -40,3 +40,8 @@
|
|
40 |
text-align: left;
|
41 |
font-weight: 600 !important;
|
42 |
}
|
|
|
|
|
|
|
|
|
|
|
|
40 |
text-align: left;
|
41 |
font-weight: 600 !important;
|
42 |
}
|
43 |
+
|
44 |
+
|
45 |
+
.read-only label textarea,input {
|
46 |
+
cursor: not-allowed !important;
|
47 |
+
}
|