erikjm commited on
Commit
1c1063c
·
verified ·
1 Parent(s): 65aa93d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +166 -0
app.py ADDED
@@ -0,0 +1,166 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from gradio_interfaces.conversation_labeling.interface_utils import *
4
+
5
+ maxim = 'quantity'
6
+ submaxims = ["The response provides a sufficient amount of information.",
7
+ "The response does not contain unnecessary details."]
8
+ checkbox_choices = [
9
+ ["Yes", "No", "NA"],
10
+ ["Yes", "No", "NA"]
11
+ ]
12
+
13
+ conversation_data = load_from_jsonl('./unlabeled/conversations_unlabeled.jsonl')
14
+ max_conversation_length = max([len(conversation['transcript']) for conversation in conversation_data])
15
+ conversation = get_conversation(conversation_data)
16
+
17
+
18
+ def save_labels(conv_id, skipped, submaxim_0=None, submaxim_1=None):
19
+ data = {
20
+ 'conv_id': conv_id,
21
+ 'maxim': maxim,
22
+ 'skipped': skipped,
23
+ 'submaxim_0': submaxim_0,
24
+ 'submaxim_1': submaxim_1,
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
+ new_transcript = pad_transcript(new_conversation['transcript'], max_conversation_length)
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(f"""  **{new_transcript[i]['speaker']}**:      {new_transcript[i]['response']}""",
40
+ visible=True)
41
+ else:
42
+ markdown_blocks[i] = gr.Markdown("", visible=False)
43
+
44
+ new_last_response = gr.Text(value=get_last_response(new_transcript),
45
+ label="",
46
+ lines=1,
47
+ container=False,
48
+ interactive=False,
49
+ autoscroll=True,
50
+ visible=True)
51
+ new_radio_0_base = gr.Radio(label=submaxims[0],
52
+ choices=checkbox_choices[0],
53
+ value=None,
54
+ visible=True)
55
+ new_radio_1_base = gr.Radio(label=submaxims[1],
56
+ choices=checkbox_choices[1],
57
+ value=None,
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(conversation_data)
72
+ return update_interface(new_conversation)
73
+
74
+
75
+ def skip(*args):
76
+ conv_id = args[0]
77
+ save_labels(conv_id, skipped=True)
78
+
79
+ new_conversation = get_conversation(conversation_data)
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):
91
+ with gr.Group():
92
+ gr.Markdown("""<span style='font-size: 16px;'>&nbsp;&nbsp;&nbsp;&nbsp;**Conversational context** </span>""",
93
+ visible=True)
94
+ for i in range(max_conversation_length):
95
+ markdown_blocks[i] = gr.Markdown(f"""&nbsp;&nbsp;**{padded_transcript[i]['speaker']}**: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{padded_transcript[i]['response']}""")
96
+ if i >= conv_len.value:
97
+ markdown_blocks[i].visible = False
98
+
99
+ with gr.Row():
100
+ with gr.Group(elem_classes="bottom-aligned-group"):
101
+ speaker_adapted = gr.Markdown(
102
+ f"""<span style='font-size: 16px;'>&nbsp;&nbsp;&nbsp;&nbsp;**Response to label** </span>""",
103
+ visible=True)
104
+ last_response = gr.Textbox(value=get_last_response(transcript),
105
+ label="",
106
+ lines=1,
107
+ container=False,
108
+ interactive=False,
109
+ autoscroll=True,
110
+ visible=True)
111
+ radio_submaxim_0_base = gr.Radio(label=submaxims[0],
112
+ choices=checkbox_choices[0],
113
+ value=None,
114
+ visible=True)
115
+ radio_submaxim_1_base = gr.Radio(label=submaxims[1],
116
+ choices=checkbox_choices[1],
117
+ value=None,
118
+ visible=True)
119
+
120
+ submit_button = gr.Button("Submit")
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] + \
128
+ [radio_submaxim_1_base] + \
129
+ [conv_len]
130
+ submit_button.click(
131
+ fn=submit,
132
+ inputs=input_list,
133
+ outputs=[conv_id_element,
134
+ *markdown_blocks,
135
+ last_response,
136
+ radio_submaxim_0_base,
137
+ radio_submaxim_1_base,
138
+ conv_len]
139
+ )
140
+ skip_button.click(
141
+ fn=skip,
142
+ inputs=input_list,
143
+ outputs=[conv_id_element,
144
+ *markdown_blocks,
145
+ last_response,
146
+ radio_submaxim_0_base,
147
+ radio_submaxim_1_base,
148
+ conv_len]
149
+ )
150
+
151
+ css = """
152
+ #textbox_id textarea {
153
+ background-color: white;
154
+ }
155
+
156
+ .bottom-aligned-group {
157
+ display: flex;
158
+ flex-direction: column;
159
+ justify-content: flex-end;
160
+ height: 100%;
161
+ }
162
+ """
163
+ interface.css = css
164
+ interface.launch()
165
+
166
+