Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +36 -24
- interface_utils.py +18 -11
app.py
CHANGED
|
@@ -10,14 +10,16 @@ checkbox_choices = [
|
|
| 10 |
["Yes", "No", "NA"]
|
| 11 |
]
|
| 12 |
|
| 13 |
-
conversation_data = load_from_jsonl('./
|
| 14 |
-
max_conversation_length = max([len(conversation['transcript']) for conversation in
|
| 15 |
-
conversation = get_conversation(conversation_data)
|
| 16 |
|
|
|
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
data = {
|
| 20 |
'conv_id': conv_id,
|
|
|
|
| 21 |
'maxim': maxim,
|
| 22 |
'skipped': skipped,
|
| 23 |
'submaxim_0': submaxim_0,
|
|
@@ -25,19 +27,21 @@ def save_labels(conv_id, skipped, submaxim_0=None, submaxim_1=None):
|
|
| 25 |
}
|
| 26 |
os.makedirs("./labels", exist_ok=True)
|
| 27 |
|
| 28 |
-
with open(f"./labels/{maxim}_human_labels_{conv_id}.json", 'w') as f:
|
| 29 |
json.dump(data, f, indent=4)
|
| 30 |
|
| 31 |
|
| 32 |
def update_interface(new_conversation):
|
| 33 |
new_conv_id = new_conversation['conv_id']
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
markdown_blocks = [None] * max_conversation_length
|
| 37 |
for i in range(max_conversation_length):
|
| 38 |
-
if new_transcript[i]['speaker'] != '':
|
| 39 |
-
markdown_blocks[i] = gr.Markdown(
|
| 40 |
-
|
|
|
|
| 41 |
else:
|
| 42 |
markdown_blocks[i] = gr.Markdown("", visible=False)
|
| 43 |
|
|
@@ -58,33 +62,35 @@ def update_interface(new_conversation):
|
|
| 58 |
visible=True)
|
| 59 |
conv_len = gr.Number(value=len(new_transcript), visible=False)
|
| 60 |
|
| 61 |
-
return [new_conv_id] + list(markdown_blocks) + [new_last_response] + [new_radio_0_base] + [new_radio_1_base] + [conv_len]
|
| 62 |
|
| 63 |
|
| 64 |
def submit(*args):
|
| 65 |
conv_id = args[0]
|
|
|
|
| 66 |
submaxim_0 = args[-3]
|
| 67 |
submaxim_1 = args[-2]
|
| 68 |
|
| 69 |
-
save_labels(conv_id, skipped=False, submaxim_0=submaxim_0, submaxim_1=submaxim_1)
|
| 70 |
|
| 71 |
-
new_conversation = get_conversation(
|
| 72 |
return update_interface(new_conversation)
|
| 73 |
|
| 74 |
|
| 75 |
def skip(*args):
|
| 76 |
conv_id = args[0]
|
| 77 |
-
|
|
|
|
| 78 |
|
| 79 |
-
new_conversation = get_conversation(
|
| 80 |
-
return update_interface(new_conversation)
|
| 81 |
|
| 82 |
|
| 83 |
with gr.Blocks(theme=gr.themes.Default()) as interface:
|
| 84 |
conv_id = conversation['conv_id']
|
|
|
|
| 85 |
transcript = conversation['transcript']
|
| 86 |
conv_len = gr.Number(value=len(transcript), visible=False)
|
| 87 |
-
padded_transcript = pad_transcript(transcript, max_conversation_length)
|
| 88 |
|
| 89 |
markdown_blocks = [None] * max_conversation_length
|
| 90 |
with gr.Column(scale=1, min_width=600):
|
|
@@ -92,7 +98,11 @@ with gr.Blocks(theme=gr.themes.Default()) as interface:
|
|
| 92 |
gr.Markdown("""<span style='font-size: 16px;'> **Conversational context** </span>""",
|
| 93 |
visible=True)
|
| 94 |
for i in range(max_conversation_length):
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if i >= conv_len.value:
|
| 97 |
markdown_blocks[i].visible = False
|
| 98 |
|
|
@@ -121,7 +131,9 @@ with gr.Blocks(theme=gr.themes.Default()) as interface:
|
|
| 121 |
skip_button = gr.Button("Skip")
|
| 122 |
|
| 123 |
conv_id_element = gr.Text(value=conv_id, visible=False)
|
|
|
|
| 124 |
input_list = [conv_id_element] + \
|
|
|
|
| 125 |
markdown_blocks + \
|
| 126 |
[last_response] + \
|
| 127 |
[radio_submaxim_0_base] + \
|
|
@@ -131,6 +143,7 @@ with gr.Blocks(theme=gr.themes.Default()) as interface:
|
|
| 131 |
fn=submit,
|
| 132 |
inputs=input_list,
|
| 133 |
outputs=[conv_id_element,
|
|
|
|
| 134 |
*markdown_blocks,
|
| 135 |
last_response,
|
| 136 |
radio_submaxim_0_base,
|
|
@@ -141,6 +154,7 @@ with gr.Blocks(theme=gr.themes.Default()) as interface:
|
|
| 141 |
fn=skip,
|
| 142 |
inputs=input_list,
|
| 143 |
outputs=[conv_id_element,
|
|
|
|
| 144 |
*markdown_blocks,
|
| 145 |
last_response,
|
| 146 |
radio_submaxim_0_base,
|
|
@@ -150,17 +164,15 @@ with gr.Blocks(theme=gr.themes.Default()) as interface:
|
|
| 150 |
|
| 151 |
css = """
|
| 152 |
#textbox_id textarea {
|
| 153 |
-
|
| 154 |
}
|
| 155 |
|
| 156 |
.bottom-aligned-group {
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
}
|
| 162 |
"""
|
| 163 |
interface.css = css
|
| 164 |
interface.launch()
|
| 165 |
-
|
| 166 |
-
|
|
|
|
| 10 |
["Yes", "No", "NA"]
|
| 11 |
]
|
| 12 |
|
| 13 |
+
conversation_data = load_from_jsonl('./data/conversations_unlabeled_sliced.jsonl')
|
| 14 |
+
max_conversation_length = max([len(conversation['transcript']) for conversation in conversation_data_sliced])
|
|
|
|
| 15 |
|
| 16 |
+
conversation = get_conversation(conversation_data_sliced)
|
| 17 |
|
| 18 |
+
|
| 19 |
+
def save_labels(conv_id, slice_idx, skipped, submaxim_0=None, submaxim_1=None):
|
| 20 |
data = {
|
| 21 |
'conv_id': conv_id,
|
| 22 |
+
'slice_idx': int(slice_idx),
|
| 23 |
'maxim': maxim,
|
| 24 |
'skipped': skipped,
|
| 25 |
'submaxim_0': submaxim_0,
|
|
|
|
| 27 |
}
|
| 28 |
os.makedirs("./labels", exist_ok=True)
|
| 29 |
|
| 30 |
+
with open(f"./labels/{maxim}_human_labels_{conv_id}_{slice_idx}.json", 'w') as f:
|
| 31 |
json.dump(data, f, indent=4)
|
| 32 |
|
| 33 |
|
| 34 |
def update_interface(new_conversation):
|
| 35 |
new_conv_id = new_conversation['conv_id']
|
| 36 |
+
new_slice_idx = new_conversation['slice_idx']
|
| 37 |
+
new_transcript = new_conversation['transcript']
|
| 38 |
|
| 39 |
markdown_blocks = [None] * max_conversation_length
|
| 40 |
for i in range(max_conversation_length):
|
| 41 |
+
if i < len(new_transcript) and new_transcript[i]['speaker'] != '':
|
| 42 |
+
markdown_blocks[i] = gr.Markdown(
|
| 43 |
+
f""" **{new_transcript[i]['speaker']}**: {new_transcript[i]['response']}""",
|
| 44 |
+
visible=True)
|
| 45 |
else:
|
| 46 |
markdown_blocks[i] = gr.Markdown("", visible=False)
|
| 47 |
|
|
|
|
| 62 |
visible=True)
|
| 63 |
conv_len = gr.Number(value=len(new_transcript), visible=False)
|
| 64 |
|
| 65 |
+
return [new_conv_id] + [new_slice_idx] + list(markdown_blocks) + [new_last_response] + [new_radio_0_base] + [new_radio_1_base] + [conv_len]
|
| 66 |
|
| 67 |
|
| 68 |
def submit(*args):
|
| 69 |
conv_id = args[0]
|
| 70 |
+
slice_idx = args[1]
|
| 71 |
submaxim_0 = args[-3]
|
| 72 |
submaxim_1 = args[-2]
|
| 73 |
|
| 74 |
+
save_labels(conv_id, slice_idx, skipped=False, submaxim_0=submaxim_0, submaxim_1=submaxim_1)
|
| 75 |
|
| 76 |
+
new_conversation = get_conversation(conversation_data_sliced)
|
| 77 |
return update_interface(new_conversation)
|
| 78 |
|
| 79 |
|
| 80 |
def skip(*args):
|
| 81 |
conv_id = args[0]
|
| 82 |
+
slice_idx = args[1]
|
| 83 |
+
save_labels(conv_id, slice_idx, skipped=True)
|
| 84 |
|
| 85 |
+
new_conversation = get_conversation(conversation_data_sliced)
|
| 86 |
+
return update_interface(new_conversation, slice_idx)
|
| 87 |
|
| 88 |
|
| 89 |
with gr.Blocks(theme=gr.themes.Default()) as interface:
|
| 90 |
conv_id = conversation['conv_id']
|
| 91 |
+
slice_idx = conversation['slice_idx']
|
| 92 |
transcript = conversation['transcript']
|
| 93 |
conv_len = gr.Number(value=len(transcript), visible=False)
|
|
|
|
| 94 |
|
| 95 |
markdown_blocks = [None] * max_conversation_length
|
| 96 |
with gr.Column(scale=1, min_width=600):
|
|
|
|
| 98 |
gr.Markdown("""<span style='font-size: 16px;'> **Conversational context** </span>""",
|
| 99 |
visible=True)
|
| 100 |
for i in range(max_conversation_length):
|
| 101 |
+
if i < len(transcript):
|
| 102 |
+
markdown_blocks[i] = gr.Markdown(
|
| 103 |
+
f""" **{transcript[i]['speaker']}**: {transcript[i]['response']}""")
|
| 104 |
+
else:
|
| 105 |
+
markdown_blocks[i] = gr.Markdown("")
|
| 106 |
if i >= conv_len.value:
|
| 107 |
markdown_blocks[i].visible = False
|
| 108 |
|
|
|
|
| 131 |
skip_button = gr.Button("Skip")
|
| 132 |
|
| 133 |
conv_id_element = gr.Text(value=conv_id, visible=False)
|
| 134 |
+
slice_idx_element = gr.Text(value=slice_idx, visible=False)
|
| 135 |
input_list = [conv_id_element] + \
|
| 136 |
+
[slice_idx_element] + \
|
| 137 |
markdown_blocks + \
|
| 138 |
[last_response] + \
|
| 139 |
[radio_submaxim_0_base] + \
|
|
|
|
| 143 |
fn=submit,
|
| 144 |
inputs=input_list,
|
| 145 |
outputs=[conv_id_element,
|
| 146 |
+
slice_idx_element,
|
| 147 |
*markdown_blocks,
|
| 148 |
last_response,
|
| 149 |
radio_submaxim_0_base,
|
|
|
|
| 154 |
fn=skip,
|
| 155 |
inputs=input_list,
|
| 156 |
outputs=[conv_id_element,
|
| 157 |
+
slice_idx_element,
|
| 158 |
*markdown_blocks,
|
| 159 |
last_response,
|
| 160 |
radio_submaxim_0_base,
|
|
|
|
| 164 |
|
| 165 |
css = """
|
| 166 |
#textbox_id textarea {
|
| 167 |
+
background-color: white;
|
| 168 |
}
|
| 169 |
|
| 170 |
.bottom-aligned-group {
|
| 171 |
+
display: flex;
|
| 172 |
+
flex-direction: column;
|
| 173 |
+
justify-content: flex-end;
|
| 174 |
+
height: 100%;
|
| 175 |
}
|
| 176 |
"""
|
| 177 |
interface.css = css
|
| 178 |
interface.launch()
|
|
|
|
|
|
interface_utils.py
CHANGED
|
@@ -31,17 +31,24 @@ def save_to_jsonl(data, filename):
|
|
| 31 |
file.write(json_line + '\n')
|
| 32 |
|
| 33 |
|
| 34 |
-
def get_conversation(
|
| 35 |
-
conv = random.choice(
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
|
| 47 |
def get_last_response(transcript):
|
|
|
|
| 31 |
file.write(json_line + '\n')
|
| 32 |
|
| 33 |
|
| 34 |
+
def get_conversation(data, min_length=0):
|
| 35 |
+
conv = random.choice(data)
|
| 36 |
+
transcript = conv['transcript']
|
| 37 |
+
slice_index = random.randint(min_length, len(transcript) - 1)
|
| 38 |
+
conv_slice = transcript[slice_index]
|
| 39 |
+
return {
|
| 40 |
+
'conv_id': conv['conv_id'],
|
| 41 |
+
'slice_idx': slice_index,
|
| 42 |
+
'transcript': conv_slice
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# def pad_transcript(transcript, max_length):
|
| 47 |
+
# padding_count = max_length - len(transcript)
|
| 48 |
+
# if padding_count > 0:
|
| 49 |
+
# for _ in range(padding_count):
|
| 50 |
+
# transcript.append({'speaker': '', 'response': ''})
|
| 51 |
+
# return transcript
|
| 52 |
|
| 53 |
|
| 54 |
def get_last_response(transcript):
|