Spaces:
Sleeping
Sleeping
Create utils/state_management.py
Browse files- utils/state_management.py +128 -0
utils/state_management.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
def start_evaluation(state, ui_components):
|
| 4 |
+
"""
|
| 5 |
+
Disables UI components when evaluation starts.
|
| 6 |
+
|
| 7 |
+
Args:
|
| 8 |
+
state (dict): Current evaluation state.
|
| 9 |
+
ui_components (dict): Dictionary of UI components to update.
|
| 10 |
+
|
| 11 |
+
Returns:
|
| 12 |
+
list: List of updated state and UI components.
|
| 13 |
+
"""
|
| 14 |
+
if state["running"]:
|
| 15 |
+
return [
|
| 16 |
+
state,
|
| 17 |
+
*[gr.update(interactive=False) for _ in range(8)], # 8 UI components to disable
|
| 18 |
+
gr.update(visible=True), # cancel_button
|
| 19 |
+
"Evaluation already in progress. Please wait.", # results_output
|
| 20 |
+
None, # results_table
|
| 21 |
+
gr.update(visible=False) # results_table_container
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
# Update state to running
|
| 25 |
+
state["running"] = True
|
| 26 |
+
|
| 27 |
+
# Create updates for UI components
|
| 28 |
+
updates = [
|
| 29 |
+
state, # Updated state
|
| 30 |
+
gr.update(interactive=False), # subject_selection_mode
|
| 31 |
+
gr.update(interactive=False), # num_subjects_slider
|
| 32 |
+
gr.update(interactive=False), # specific_subjects
|
| 33 |
+
gr.update(interactive=False), # all_questions_checkbox
|
| 34 |
+
gr.update(interactive=False), # num_questions_slider
|
| 35 |
+
gr.update(interactive=False), # model1_dropdown
|
| 36 |
+
gr.update(interactive=False), # model2_dropdown
|
| 37 |
+
gr.update(interactive=False), # eval_button
|
| 38 |
+
gr.update(visible=True), # cancel_button
|
| 39 |
+
"Starting evaluation...", # results_output
|
| 40 |
+
None, # results_table
|
| 41 |
+
gr.update(visible=False) # results_table_container
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
return updates
|
| 45 |
+
|
| 46 |
+
def finish_evaluation(state):
|
| 47 |
+
"""
|
| 48 |
+
Updates state when evaluation finishes.
|
| 49 |
+
|
| 50 |
+
Args:
|
| 51 |
+
state (dict): Current evaluation state.
|
| 52 |
+
|
| 53 |
+
Returns:
|
| 54 |
+
dict: Updated state.
|
| 55 |
+
"""
|
| 56 |
+
state["running"] = False
|
| 57 |
+
return state
|
| 58 |
+
|
| 59 |
+
def cancel_evaluation(state, ui_components):
|
| 60 |
+
"""
|
| 61 |
+
Re-enables UI components when evaluation is canceled.
|
| 62 |
+
|
| 63 |
+
Args:
|
| 64 |
+
state (dict): Current evaluation state.
|
| 65 |
+
ui_components (dict): Dictionary of UI components to update.
|
| 66 |
+
|
| 67 |
+
Returns:
|
| 68 |
+
list: List of updated state and UI components.
|
| 69 |
+
"""
|
| 70 |
+
# Update state to not running
|
| 71 |
+
state["running"] = False
|
| 72 |
+
|
| 73 |
+
# Create updates for UI components
|
| 74 |
+
updates = [
|
| 75 |
+
state, # Updated state
|
| 76 |
+
gr.update(interactive=True), # subject_selection_mode
|
| 77 |
+
gr.update(interactive=True), # num_subjects_slider
|
| 78 |
+
gr.update(interactive=True), # specific_subjects
|
| 79 |
+
gr.update(interactive=True), # all_questions_checkbox
|
| 80 |
+
gr.update(interactive=True), # num_questions_slider
|
| 81 |
+
gr.update(interactive=True), # model1_dropdown
|
| 82 |
+
gr.update(interactive=True), # model2_dropdown
|
| 83 |
+
gr.update(interactive=True), # eval_button
|
| 84 |
+
gr.update(visible=False), # cancel_button
|
| 85 |
+
"⚠️ Evaluation canceled by user (note: backend process may continue running)", # results_output
|
| 86 |
+
None, # results_table
|
| 87 |
+
gr.update(visible=False) # results_table_container
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
return updates
|
| 91 |
+
|
| 92 |
+
def handle_evaluation_results(eval_results, ui_components):
|
| 93 |
+
"""
|
| 94 |
+
Updates UI components based on evaluation results.
|
| 95 |
+
|
| 96 |
+
Args:
|
| 97 |
+
eval_results (dict): Results from evaluation.
|
| 98 |
+
ui_components (dict): Dictionary of UI components to update.
|
| 99 |
+
|
| 100 |
+
Returns:
|
| 101 |
+
list: List of updated UI components.
|
| 102 |
+
"""
|
| 103 |
+
if eval_results['success']:
|
| 104 |
+
return [
|
| 105 |
+
eval_results['report'], # results_output
|
| 106 |
+
eval_results['comparison_df'], # results_table
|
| 107 |
+
gr.update(interactive=True), # eval_button
|
| 108 |
+
gr.update(visible=False), # cancel_button
|
| 109 |
+
gr.update(interactive=True), # subject_selection_mode
|
| 110 |
+
gr.update(interactive=True), # num_subjects_slider
|
| 111 |
+
gr.update(interactive=True), # all_questions_checkbox
|
| 112 |
+
gr.update(interactive=True), # num_questions_slider
|
| 113 |
+
gr.update(interactive=True), # model1_dropdown
|
| 114 |
+
gr.update(visible=True) # results_table_container
|
| 115 |
+
]
|
| 116 |
+
else:
|
| 117 |
+
return [
|
| 118 |
+
eval_results['report'], # results_output
|
| 119 |
+
None, # results_table
|
| 120 |
+
gr.update(interactive=True), # eval_button
|
| 121 |
+
gr.update(visible=False), # cancel_button
|
| 122 |
+
gr.update(interactive=True), # subject_selection_mode
|
| 123 |
+
gr.update(interactive=True), # num_subjects_slider
|
| 124 |
+
gr.update(interactive=True), # all_questions_checkbox
|
| 125 |
+
gr.update(interactive=True), # num_questions_slider
|
| 126 |
+
gr.update(interactive=True), # model1_dropdown
|
| 127 |
+
gr.update(visible=False) # results_table_container
|
| 128 |
+
]
|