Spaces:
Running
Running
# run_agentic_pipeline.py | |
""" | |
This module is responsible for loading and displaying pre-computed AI analysis | |
results (reports and OKRs) that have been fetched from Bubble.io. It does not | |
perform any new analysis. | |
""" | |
import logging | |
import gradio as gr | |
# UI formatting and data reconstruction functions are still needed | |
try: | |
from ui.insights_ui_generator import ( | |
format_report_to_markdown, | |
extract_key_results_for_selection, | |
format_single_okr_for_display | |
) | |
from services.report_data_handler import fetch_and_reconstruct_data_from_bubble | |
AGENTIC_MODULES_LOADED = True | |
except ImportError as e: | |
logging.error(f"Could not import agentic pipeline display modules: {e}. Tabs 3 and 4 will be disabled.") | |
AGENTIC_MODULES_LOADED = False | |
# Define placeholder functions if imports fail | |
def format_report_to_markdown(report_string): return "Agentic modules not loaded. Report unavailable." | |
def extract_key_results_for_selection(okrs_dict): return [] | |
def format_single_okr_for_display(okr_data, **kwargs): return "Agentic modules not loaded. OKR display unavailable." | |
def fetch_and_reconstruct_data_from_bubble(df): return None | |
def load_and_display_agentic_results(current_token_state, orchestration_raw_results_st, selected_key_result_ids_st, key_results_for_selection_st): | |
""" | |
Loads pre-computed agentic analysis and OKR data from the application state | |
(which was fetched from Bubble) and formats it for display in the Gradio UI. | |
""" | |
logging.info("Loading and displaying pre-computed agentic results from state.") | |
# A tuple of Gradio updates to return in case of errors or no data | |
initial_yield_updates = ( | |
gr.update(value="Nessun dato di analisi trovato..."), # agentic_report_display_md | |
gr.update(choices=[], value=[], interactive=False), # key_results_cbg | |
gr.update(value="Nessun OKR trovato..."), # okr_detail_display_md | |
None, # orchestration_raw_results_st | |
[], # selected_key_result_ids_st | |
[], # key_results_for_selection_st | |
"Stato: In attesa di dati" # agentic_pipeline_status_md | |
) | |
if not AGENTIC_MODULES_LOADED: | |
logging.warning("Agentic display modules not loaded. Cannot display results.") | |
error_updates = list(initial_yield_updates) | |
error_updates[-1] = "Errore: Moduli AI non caricati." | |
return tuple(error_updates) | |
# The raw DataFrame fetched from Bubble's agentic analysis table | |
agentic_data_df = current_token_state.get('bubble_agentic_analysis_data') | |
if agentic_data_df is None or agentic_data_df.empty: | |
logging.warning("No agentic analysis data found in the application state.") | |
return initial_yield_updates | |
# Use the handler to reconstruct the report and OKRs from the DataFrame | |
reconstructed_data = fetch_and_reconstruct_data_from_bubble(agentic_data_df) | |
if not reconstructed_data: | |
logging.warning("Could not reconstruct agentic data from the fetched DataFrame.") | |
error_updates = list(initial_yield_updates) | |
error_updates[0] = gr.update(value="I dati di analisi esistenti non sono nel formato corretto.") | |
error_updates[2] = gr.update(value="Impossibile visualizzare gli OKR.") | |
error_updates[-1] = "Stato: Errore formato dati" | |
return tuple(error_updates) | |
# --- Prepare UI updates with the reconstructed data --- | |
report_str = reconstructed_data.get('report_str', "Nessun report di analisi trovato nei dati.") | |
actionable_okrs = reconstructed_data.get('actionable_okrs') # This is the dict with 'okrs' list | |
# 1. Update Report Tab | |
agentic_report_md_update = gr.update(value=format_report_to_markdown(report_str)) | |
# 2. Update OKR Tab components | |
if actionable_okrs and isinstance(actionable_okrs.get("okrs"), list): | |
krs_for_ui_selection_list = extract_key_results_for_selection(actionable_okrs) | |
kr_choices_for_cbg = [(kr['kr_description'], kr['unique_kr_id']) for kr in krs_for_ui_selection_list] | |
key_results_cbg_update = gr.update(choices=kr_choices_for_cbg, value=[], interactive=True) | |
krs_for_selection_state_update = krs_for_ui_selection_list | |
all_okrs_md_parts = [ | |
format_single_okr_for_display(okr_item, accepted_kr_indices=None, okr_main_index=okr_idx) | |
for okr_idx, okr_item in enumerate(actionable_okrs["okrs"]) | |
] | |
okr_detail_display_md_update = gr.update(value="\n\n---\n\n".join(all_okrs_md_parts)) | |
else: | |
# Handle case where there are no OKRs in the data | |
krs_for_selection_state_update = [] | |
key_results_cbg_update = gr.update(choices=[], value=[], interactive=False) | |
okr_detail_display_md_update = gr.update(value="Nessun OKR trovato nei dati di analisi caricati.") | |
# Return all the final updates for the Gradio interface | |
return ( | |
agentic_report_md_update, | |
key_results_cbg_update, | |
okr_detail_display_md_update, | |
reconstructed_data, # Store the full reconstructed data dict in the state | |
[], # Reset the selected KR IDs state | |
krs_for_selection_state_update, # Update the state with all available KRs | |
"Stato: Dati di analisi caricati correttamente da Bubble" # Final status message | |
) | |