Spaces:
Running
Running
Update run_agentic_pipeline.py
Browse files- run_agentic_pipeline.py +109 -1
run_agentic_pipeline.py
CHANGED
@@ -30,7 +30,6 @@ from data_processing.analytics_data_processing import prepare_filtered_analytics
|
|
30 |
logger = logging.getLogger(__name__)
|
31 |
|
32 |
|
33 |
-
|
34 |
async def run_full_analytics_orchestration(
|
35 |
token_state: Dict[str, Any],
|
36 |
date_filter_selection: str,
|
@@ -107,3 +106,112 @@ async def run_full_analytics_orchestration(
|
|
107 |
logger.critical(f"Critical error during analytics orchestration: {e}", exc_info=True)
|
108 |
return None
|
109 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
logger = logging.getLogger(__name__)
|
31 |
|
32 |
|
|
|
33 |
async def run_full_analytics_orchestration(
|
34 |
token_state: Dict[str, Any],
|
35 |
date_filter_selection: str,
|
|
|
106 |
logger.critical(f"Critical error during analytics orchestration: {e}", exc_info=True)
|
107 |
return None
|
108 |
|
109 |
+
|
110 |
+
|
111 |
+
async def run_agentic_pipeline_autonomously(current_token_state_val):
|
112 |
+
logging.info(f"Agentic pipeline check triggered for token_state update. Current token: {'Set' if current_token_state_val.get('token') else 'Not Set'}")
|
113 |
+
# Initial state before pipeline runs or if skipped
|
114 |
+
initial_yield = (
|
115 |
+
gr.update(value="Pipeline AI: In attesa dei dati necessari..."), # agentic_report_display_md
|
116 |
+
gr.update(choices=[], value=[], interactive=False), # key_results_cbg
|
117 |
+
gr.update(value="Pipeline AI: In attesa dei dati necessari..."), # okr_detail_display_md
|
118 |
+
orchestration_raw_results_st.value, # Preserve current raw results
|
119 |
+
selected_key_result_ids_st.value, # Preserve current selection
|
120 |
+
key_results_for_selection_st.value, # Preserve current options
|
121 |
+
"Pipeline AI: In attesa dei dati..." # agentic_pipeline_status_md
|
122 |
+
)
|
123 |
+
|
124 |
+
if not current_token_state_val or not current_token_state_val.get("token"):
|
125 |
+
logging.info("Agentic pipeline: Token not available in token_state. Skipping.")
|
126 |
+
yield initial_yield
|
127 |
+
return
|
128 |
+
|
129 |
+
logging.info("Agentic pipeline starting autonomously with 'Sempre' filter.")
|
130 |
+
# Update status to indicate processing
|
131 |
+
yield (
|
132 |
+
gr.update(value="Analisi AI (Sempre) in corso..."),
|
133 |
+
gr.update(choices=[], value=[], interactive=False), # Keep CBG disabled during run
|
134 |
+
gr.update(value="Dettagli OKR (Sempre) in corso di generazione..."),
|
135 |
+
orchestration_raw_results_st.value, # Preserve
|
136 |
+
selected_key_result_ids_st.value, # Preserve
|
137 |
+
key_results_for_selection_st.value, # Preserve
|
138 |
+
"Esecuzione pipeline AI (Sempre)..."
|
139 |
+
)
|
140 |
+
|
141 |
+
if not AGENTIC_MODULES_LOADED:
|
142 |
+
logging.warning("Agentic modules not loaded. Skipping autonomous pipeline.")
|
143 |
+
yield (
|
144 |
+
gr.update(value="Moduli AI non caricati. Report non disponibile."),
|
145 |
+
gr.update(choices=[], value=[], interactive=False),
|
146 |
+
gr.update(value="Moduli AI non caricati. OKR non disponibili."),
|
147 |
+
None, [], [], "Pipeline AI: Moduli non caricati."
|
148 |
+
)
|
149 |
+
return
|
150 |
+
|
151 |
+
try:
|
152 |
+
# Parameters for 'Sempre' filter for the agentic pipeline
|
153 |
+
date_filter_val_agentic = "Sempre"
|
154 |
+
custom_start_val_agentic = None
|
155 |
+
custom_end_val_agentic = None
|
156 |
+
|
157 |
+
orchestration_output = await run_full_analytics_orchestration(
|
158 |
+
current_token_state_val,
|
159 |
+
date_filter_val_agentic,
|
160 |
+
custom_start_val_agentic,
|
161 |
+
custom_end_val_agentic
|
162 |
+
)
|
163 |
+
agentic_status_text = "Pipeline AI (Sempre) completata."
|
164 |
+
logging.info(f"Autonomous agentic pipeline finished. Output keys: {orchestration_output.keys() if orchestration_output else 'None'}")
|
165 |
+
|
166 |
+
if orchestration_output:
|
167 |
+
orchestration_results_update = orchestration_output # Store full results in state
|
168 |
+
report_str = orchestration_output.get('comprehensive_analysis_report', "Nessun report dettagliato fornito.")
|
169 |
+
agentic_report_md_update = gr.update(value=format_report_to_markdown(report_str))
|
170 |
+
|
171 |
+
actionable_okrs = orchestration_output.get('actionable_okrs_and_tasks') # This is the dict containing 'okrs' list
|
172 |
+
krs_for_ui_selection_list = extract_key_results_for_selection(actionable_okrs) # Expects the dict
|
173 |
+
|
174 |
+
krs_for_selection_update = krs_for_ui_selection_list # Update state with list of KR dicts
|
175 |
+
|
176 |
+
# Choices for CheckboxGroup: list of (label, value) tuples
|
177 |
+
kr_choices_for_cbg = [(kr['kr_description'], kr['unique_kr_id']) for kr in krs_for_ui_selection_list]
|
178 |
+
key_results_cbg_update = gr.update(choices=kr_choices_for_cbg, value=[], interactive=True) # Reset selection
|
179 |
+
|
180 |
+
# Display all OKRs by default after pipeline run
|
181 |
+
all_okrs_md_parts = []
|
182 |
+
if actionable_okrs and isinstance(actionable_okrs.get("okrs"), list):
|
183 |
+
for okr_idx, okr_item in enumerate(actionable_okrs["okrs"]):
|
184 |
+
all_okrs_md_parts.append(format_single_okr_for_display(okr_item, accepted_kr_indices=None, okr_main_index=okr_idx))
|
185 |
+
|
186 |
+
if not all_okrs_md_parts:
|
187 |
+
okr_detail_display_md_update = gr.update(value="Nessun OKR generato o trovato (Sempre).")
|
188 |
+
else:
|
189 |
+
okr_detail_display_md_update = gr.update(value="\n\n---\n\n".join(all_okrs_md_parts))
|
190 |
+
|
191 |
+
selected_krs_update = [] # Reset selected KRs state
|
192 |
+
else:
|
193 |
+
agentic_report_md_update = gr.update(value="Nessun report generato dalla pipeline AI (Sempre).")
|
194 |
+
key_results_cbg_update = gr.update(choices=[], value=[], interactive=False)
|
195 |
+
okr_detail_display_md_update = gr.update(value="Nessun OKR generato o errore nella pipeline AI (Sempre).")
|
196 |
+
orchestration_results_update = None
|
197 |
+
selected_krs_update = []
|
198 |
+
krs_for_selection_update = []
|
199 |
+
|
200 |
+
yield (
|
201 |
+
agentic_report_md_update,
|
202 |
+
key_results_cbg_update,
|
203 |
+
okr_detail_display_md_update,
|
204 |
+
orchestration_results_update, # state
|
205 |
+
selected_krs_update, # state
|
206 |
+
krs_for_selection_update, # state
|
207 |
+
agentic_status_text
|
208 |
+
)
|
209 |
+
except Exception as e:
|
210 |
+
logging.error(f"Error during autonomous agentic pipeline execution: {e}", exc_info=True)
|
211 |
+
agentic_status_text = f"Errore pipeline AI (Sempre): {str(e)}"
|
212 |
+
yield (
|
213 |
+
gr.update(value=f"Errore generazione report AI (Sempre): {str(e)}"),
|
214 |
+
gr.update(choices=[], value=[], interactive=False),
|
215 |
+
gr.update(value=f"Errore generazione OKR AI (Sempre): {str(e)}"),
|
216 |
+
None, [], [], agentic_status_text # Reset states on error
|
217 |
+
)
|