Spaces:
Paused
Paused
feat: Updated arena format
Browse files- app.py +82 -52
- utils/__init__.py +0 -1
app.py
CHANGED
|
@@ -44,45 +44,53 @@ def new_comparison(dialog_dict: dict) -> Tuple[str, str, str, str, gr.Row, gr.Ro
|
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
-
def
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
"""
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
Args:
|
| 59 |
-
choice (str): User's choice ('A' or 'B').
|
| 60 |
-
m1 (str): Model 1 name.
|
| 61 |
-
m2 (str): Model 2 name.
|
| 62 |
-
d1 (str): Dialog 1.
|
| 63 |
-
d2 (str): Dialog 2.
|
| 64 |
-
dialog_dict (dict): Dictionary of dialogs per model.
|
| 65 |
-
is_dev (bool): Flag indicating if in development mode.
|
| 66 |
-
result_file_path (str, optional): Path to save the results.
|
| 67 |
-
|
| 68 |
-
Returns:
|
| 69 |
-
Tuple[str, str, str, str, gr.Row, str, gr.Button]:
|
| 70 |
-
(New model1 name, new dialog1, new model2 name, new dialog2, arena row visibility update, message)
|
| 71 |
"""
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
if not
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
with open(result_file_path, "a") as f:
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
#
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
|
| 88 |
|
|
@@ -139,20 +147,28 @@ with gr.Blocks(title="1:1 Outpatient Model Simulation Arena", css=css) as demo:
|
|
| 139 |
|
| 140 |
# New comparison button
|
| 141 |
btn_new = gr.Button("π₯ Start Arena!! π₯")
|
| 142 |
-
msg = gr.Markdown("")
|
| 143 |
state_dict = gr.State(dialog_dict)
|
| 144 |
|
| 145 |
# Showing two model simulations side by side
|
| 146 |
with gr.Row(visible=False) as arena_row:
|
| 147 |
with gr.Column():
|
| 148 |
model1_name = gr.Textbox(label="Model A", interactive=False, visible=is_dev)
|
| 149 |
-
dialog1_box = gr.Markdown(label="Simulation A", elem_classes="dialog-box")
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
with gr.Column():
|
| 153 |
model2_name = gr.Textbox(label="Model B", interactive=False, visible=is_dev)
|
| 154 |
-
dialog2_box = gr.Markdown(label="Simulation B", elem_classes="dialog-box")
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
# Submit button
|
| 158 |
with gr.Row(visible=False) as submit_row:
|
|
@@ -166,23 +182,37 @@ with gr.Blocks(title="1:1 Outpatient Model Simulation Arena", css=css) as demo:
|
|
| 166 |
outputs=[model1_name, dialog1_box, model2_name, dialog2_box, arena_row, submit_row, btn_new],
|
| 167 |
)
|
| 168 |
|
|
|
|
| 169 |
vote1.click(
|
| 170 |
-
fn=lambda
|
| 171 |
-
inputs=[
|
| 172 |
-
outputs=[
|
| 173 |
)
|
| 174 |
-
|
| 175 |
vote2.click(
|
| 176 |
-
fn=lambda
|
| 177 |
-
inputs=[
|
| 178 |
-
outputs=[
|
| 179 |
)
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
# Launch the app
|
| 188 |
if is_dev:
|
|
|
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
+
def update_scores(score_a: Optional[int],
|
| 48 |
+
score_b: Optional[int],
|
| 49 |
+
m1: str,
|
| 50 |
+
m2: str,
|
| 51 |
+
d1: str,
|
| 52 |
+
d2: str,
|
| 53 |
+
dialog_dict: dict,
|
| 54 |
+
score_state: dict,
|
| 55 |
+
result_file_path: str):
|
| 56 |
"""
|
| 57 |
+
Update score_state. If both scores exist, record and sample new dialog pair.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
"""
|
| 59 |
+
scores = score_state.copy()
|
| 60 |
+
if score_a is not None:
|
| 61 |
+
scores["A"] = score_a
|
| 62 |
+
if score_b is not None:
|
| 63 |
+
scores["B"] = score_b
|
| 64 |
+
print(scores)
|
| 65 |
+
|
| 66 |
+
# Save the data
|
| 67 |
+
if "A" in scores and "B" in scores:
|
| 68 |
+
# if not is_dev:
|
| 69 |
with open(result_file_path, "a") as f:
|
| 70 |
+
f.write(f"{scores['A']}\t{scores['B']}\t{m1}\t{m2}\n")
|
| 71 |
+
|
| 72 |
+
new_m1, new_d1, new_m2, new_d2 = sample_pair(dialog_dict)
|
| 73 |
+
return (
|
| 74 |
+
new_m1, new_d1, new_m2, new_d2,
|
| 75 |
+
gr.update(visible=True), # arena
|
| 76 |
+
gr.update(visible=False), # rate A button
|
| 77 |
+
gr.update(visible=False), # rate B button
|
| 78 |
+
gr.update(visible=True), # vote A button
|
| 79 |
+
gr.update(visible=True), # vote B button
|
| 80 |
+
"β
Both scores recorded!", {}
|
| 81 |
+
)
|
| 82 |
|
| 83 |
+
# Waiting the other score
|
| 84 |
+
else:
|
| 85 |
+
return (
|
| 86 |
+
m1, d1, m2, d2,
|
| 87 |
+
gr.update(visible=True), # arena
|
| 88 |
+
gr.update(visible=True), # rate A button
|
| 89 |
+
gr.update(visible=True), # rate B button
|
| 90 |
+
gr.update(visible=False), # vote A button
|
| 91 |
+
gr.update(visible=False), # vote B button
|
| 92 |
+
"π Waiting for the other score...", scores
|
| 93 |
+
)
|
| 94 |
|
| 95 |
|
| 96 |
|
|
|
|
| 147 |
|
| 148 |
# New comparison button
|
| 149 |
btn_new = gr.Button("π₯ Start Arena!! π₯")
|
|
|
|
| 150 |
state_dict = gr.State(dialog_dict)
|
| 151 |
|
| 152 |
# Showing two model simulations side by side
|
| 153 |
with gr.Row(visible=False) as arena_row:
|
| 154 |
with gr.Column():
|
| 155 |
model1_name = gr.Textbox(label="Model A", interactive=False, visible=is_dev)
|
| 156 |
+
dialog1_box = gr.Markdown(label="Simulation A", elem_classes="dialog-box")
|
| 157 |
+
with gr.Row(visible=True) as vote1_row:
|
| 158 |
+
vote1 = gr.Button("π Choose A")
|
| 159 |
+
with gr.Row(visible=False) as scoreA_row:
|
| 160 |
+
scoreA_buttons = [gr.Button(str(i)) for i in range(1, 6)]
|
| 161 |
|
| 162 |
with gr.Column():
|
| 163 |
model2_name = gr.Textbox(label="Model B", interactive=False, visible=is_dev)
|
| 164 |
+
dialog2_box = gr.Markdown(label="Simulation B", elem_classes="dialog-box")
|
| 165 |
+
with gr.Row(visible=True) as vote2_row:
|
| 166 |
+
vote2 = gr.Button("π Choose B")
|
| 167 |
+
with gr.Row(visible=False) as scoreB_row:
|
| 168 |
+
scoreB_buttons = [gr.Button(str(i)) for i in range(1, 6)]
|
| 169 |
+
|
| 170 |
+
# Showing the status
|
| 171 |
+
msg = gr.Markdown("")
|
| 172 |
|
| 173 |
# Submit button
|
| 174 |
with gr.Row(visible=False) as submit_row:
|
|
|
|
| 182 |
outputs=[model1_name, dialog1_box, model2_name, dialog2_box, arena_row, submit_row, btn_new],
|
| 183 |
)
|
| 184 |
|
| 185 |
+
# First step: Arena, Choose the only one!
|
| 186 |
vote1.click(
|
| 187 |
+
fn=lambda: [gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)],
|
| 188 |
+
inputs=[],
|
| 189 |
+
outputs=[scoreA_row, scoreB_row, vote1_row, vote2_row],
|
| 190 |
)
|
|
|
|
| 191 |
vote2.click(
|
| 192 |
+
fn=lambda: [gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False)],
|
| 193 |
+
inputs=[],
|
| 194 |
+
outputs=[scoreA_row, scoreB_row, vote1_row, vote2_row],
|
| 195 |
)
|
| 196 |
|
| 197 |
+
# Second step: Rate, Rate the each score!
|
| 198 |
+
score_state = gr.State({})
|
| 199 |
+
for btn in scoreA_buttons:
|
| 200 |
+
btn.click(
|
| 201 |
+
fn=lambda score, m1, m2, d1, d2, scores: update_scores(int(score), None, m1, m2, d1, d2, dialog_dict, scores, is_dev, result_save_path),
|
| 202 |
+
inputs=[gr.State(btn.value), model1_name, model2_name, dialog1_box, dialog2_box, score_state],
|
| 203 |
+
outputs=[model1_name, dialog1_box, model2_name, dialog2_box, arena_row, scoreA_row, scoreB_row, vote1_row, vote2_row, msg, score_state],
|
| 204 |
+
)
|
| 205 |
+
for btn in scoreB_buttons:
|
| 206 |
+
btn.click(
|
| 207 |
+
fn=lambda score, m1, m2, d1, d2, scores: update_scores(None, int(score), m1, m2, d1, d2, dialog_dict, scores, is_dev, result_save_path),
|
| 208 |
+
inputs=[gr.State(btn.value), model1_name, model2_name, dialog1_box, dialog2_box, score_state],
|
| 209 |
+
outputs=[model1_name, dialog1_box, model2_name, dialog2_box, arena_row, scoreA_row, scoreB_row, vote1_row, vote2_row, msg, score_state],
|
| 210 |
+
)
|
| 211 |
+
submit_btn.click(
|
| 212 |
+
fn=save_data,
|
| 213 |
+
inputs=[gr.State(result_save_path)],
|
| 214 |
+
outputs=[submit_msg],
|
| 215 |
+
)
|
| 216 |
|
| 217 |
# Launch the app
|
| 218 |
if is_dev:
|
utils/__init__.py
CHANGED
|
@@ -5,7 +5,6 @@ import logging.config
|
|
| 5 |
|
| 6 |
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 7 |
try:
|
| 8 |
-
print(base_path)
|
| 9 |
version_file_path = os.path.join(base_path, 'version.txt')
|
| 10 |
LOGGING_NAME = f"Simulation_Arena_{open(version_file_path).read().strip()}"
|
| 11 |
except:
|
|
|
|
| 5 |
|
| 6 |
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 7 |
try:
|
|
|
|
| 8 |
version_file_path = os.path.join(base_path, 'version.txt')
|
| 9 |
LOGGING_NAME = f"Simulation_Arena_{open(version_file_path).read().strip()}"
|
| 10 |
except:
|