GuglielmoTor commited on
Commit
edddb7c
·
verified ·
1 Parent(s): dcb1fff

Update services/agentic_handlers.py

Browse files
Files changed (1) hide show
  1. services/agentic_handlers.py +40 -44
services/agentic_handlers.py CHANGED
@@ -40,16 +40,17 @@ class AgenticHandlers:
40
  self.agentic_modules_really_loaded = AGENTIC_MODULES_LOADED
41
  logging.info(f"AgenticHandlers initialized. Modules loaded: {self.agentic_modules_really_loaded}")
42
 
43
- def _safe_checkbox_update(self, choices=None, value=None, interactive=None):
44
  """
45
- Create a safe update for CheckboxGroup that handles version compatibility.
 
46
  """
47
- update_dict = {}
48
-
49
- # Only include parameters that are supported and valid
50
- if choices is not None:
51
- # Ensure choices is a list of tuples (display_value, return_value)
52
- if isinstance(choices, list):
53
  formatted_choices = []
54
  for choice in choices:
55
  if isinstance(choice, tuple) and len(choice) == 2:
@@ -57,32 +58,22 @@ class AgenticHandlers:
57
  else:
58
  # Convert single values to (display, value) tuples
59
  formatted_choices.append((str(choice), choice))
60
- update_dict['choices'] = formatted_choices
61
- else:
62
- update_dict['choices'] = []
63
-
64
- if value is not None:
65
- # Ensure value is a list and only contains valid choices
66
- if isinstance(value, list):
67
- if choices is not None:
68
- # Filter value to only include items that exist in choices
69
- valid_values = [choice[1] if isinstance(choice, tuple) else choice for choice in (choices or [])]
70
- filtered_value = [v for v in value if v in valid_values]
71
- update_dict['value'] = filtered_value
72
  else:
73
- update_dict['value'] = []
74
- else:
75
- update_dict['value'] = []
76
-
77
- # Try to include interactive parameter, but handle gracefully if not supported
78
- if interactive is not None:
79
- try:
80
- update_dict['interactive'] = interactive
81
- except:
82
- # If interactive parameter is not supported, continue without it
83
- pass
84
-
85
- return gr.update(**update_dict)
86
 
87
  async def run_agentic_pipeline_autonomously_on_update(self, current_token_state_val):
88
  """
@@ -108,12 +99,8 @@ class AgenticHandlers:
108
  report_status_md_update = gr.update(value=initial_report_status) if self.report_components.get("agentic_pipeline_status_md") else gr.update()
109
  report_display_md_update = gr.update()
110
 
111
- # Use safe checkbox update method
112
- okrs_cbg_update = self._safe_checkbox_update(
113
- choices=initial_okr_cbg_choices,
114
- value=initial_okr_cbg_value,
115
- interactive=initial_okr_cbg_interactive
116
- ) if self.okrs_components.get("key_results_cbg") else gr.update()
117
 
118
  okrs_detail_md_update = gr.update(value=initial_okr_details) if self.okrs_components.get("okr_detail_display_md") else gr.update()
119
 
@@ -154,7 +141,7 @@ class AgenticHandlers:
154
 
155
  # Update for key_results_cbg (CheckboxGroup) in error case
156
  if self.okrs_components.get("key_results_cbg"):
157
- okrs_cbg_update = self._safe_checkbox_update(choices=[], value=[], interactive=False)
158
 
159
  if self.okrs_components.get("okr_detail_display_md"):
160
  okrs_detail_md_update = gr.update(value=error_status)
@@ -209,11 +196,14 @@ class AgenticHandlers:
209
  kr_choices_for_cbg.append((kr_desc, kr['unique_kr_id']))
210
 
211
  if self.okrs_components.get("key_results_cbg"):
212
- okrs_cbg_update = self._safe_checkbox_update(
 
 
 
213
  choices=kr_choices_for_cbg,
214
- value=[],
215
- interactive=True
216
  )
 
217
 
218
  all_okrs_md_parts = []
219
  if actionable_okrs and isinstance(actionable_okrs.get("okrs"), list):
@@ -235,7 +225,13 @@ class AgenticHandlers:
235
 
236
  # Update for key_results_cbg (CheckboxGroup) if no output
237
  if self.okrs_components.get("key_results_cbg"):
238
- okrs_cbg_update = self._safe_checkbox_update(choices=[], value=[], interactive=False)
 
 
 
 
 
 
239
 
240
  if self.okrs_components.get("okr_detail_display_md"):
241
  okrs_detail_md_update = gr.update(value="Nessun OKR generato o errore nella pipeline AI (Sempre).")
 
40
  self.agentic_modules_really_loaded = AGENTIC_MODULES_LOADED
41
  logging.info(f"AgenticHandlers initialized. Modules loaded: {self.agentic_modules_really_loaded}")
42
 
43
+ def _update_checkbox_component_directly(self, component, choices=None, value=None):
44
  """
45
+ Update CheckboxGroup component directly instead of using gr.update()
46
+ since gr.update() doesn't support choices parameter in some Gradio versions.
47
  """
48
+ try:
49
+ if component is None:
50
+ return gr.update()
51
+
52
+ # Format choices properly
53
+ if choices is not None:
54
  formatted_choices = []
55
  for choice in choices:
56
  if isinstance(choice, tuple) and len(choice) == 2:
 
58
  else:
59
  # Convert single values to (display, value) tuples
60
  formatted_choices.append((str(choice), choice))
61
+
62
+ # Update component properties directly
63
+ component.choices = formatted_choices
64
+
65
+ if value is not None:
66
+ if isinstance(value, list):
67
+ component.value = value
 
 
 
 
 
68
  else:
69
+ component.value = []
70
+
71
+ # Return a simple update that just refreshes the component
72
+ return gr.update()
73
+
74
+ except Exception as e:
75
+ logging.error(f"Error updating checkbox component: {e}")
76
+ return gr.update()
 
 
 
 
 
77
 
78
  async def run_agentic_pipeline_autonomously_on_update(self, current_token_state_val):
79
  """
 
99
  report_status_md_update = gr.update(value=initial_report_status) if self.report_components.get("agentic_pipeline_status_md") else gr.update()
100
  report_display_md_update = gr.update()
101
 
102
+ # Initialize checkbox component - no update needed initially
103
+ okrs_cbg_update = gr.update() if self.okrs_components.get("key_results_cbg") else gr.update()
 
 
 
 
104
 
105
  okrs_detail_md_update = gr.update(value=initial_okr_details) if self.okrs_components.get("okr_detail_display_md") else gr.update()
106
 
 
141
 
142
  # Update for key_results_cbg (CheckboxGroup) in error case
143
  if self.okrs_components.get("key_results_cbg"):
144
+ okrs_cbg_update = gr.update()
145
 
146
  if self.okrs_components.get("okr_detail_display_md"):
147
  okrs_detail_md_update = gr.update(value=error_status)
 
196
  kr_choices_for_cbg.append((kr_desc, kr['unique_kr_id']))
197
 
198
  if self.okrs_components.get("key_results_cbg"):
199
+ # Update the checkbox component directly
200
+ checkbox_component = self.okrs_components["key_results_cbg"]
201
+ self._update_checkbox_component_directly(
202
+ checkbox_component,
203
  choices=kr_choices_for_cbg,
204
+ value=[]
 
205
  )
206
+ okrs_cbg_update = gr.update()
207
 
208
  all_okrs_md_parts = []
209
  if actionable_okrs and isinstance(actionable_okrs.get("okrs"), list):
 
225
 
226
  # Update for key_results_cbg (CheckboxGroup) if no output
227
  if self.okrs_components.get("key_results_cbg"):
228
+ checkbox_component = self.okrs_components["key_results_cbg"]
229
+ self._update_checkbox_component_directly(
230
+ checkbox_component,
231
+ choices=[],
232
+ value=[]
233
+ )
234
+ okrs_cbg_update = gr.update()
235
 
236
  if self.okrs_components.get("okr_detail_display_md"):
237
  okrs_detail_md_update = gr.update(value="Nessun OKR generato o errore nella pipeline AI (Sempre).")