GuglielmoTor commited on
Commit
0cfca76
·
verified ·
1 Parent(s): 5b325ca

Update run_agentic_pipeline.py

Browse files
Files changed (1) hide show
  1. run_agentic_pipeline.py +129 -77
run_agentic_pipeline.py CHANGED
@@ -1,106 +1,158 @@
1
- # run_agentic_pipeline.py
2
- """
3
- This module is responsible for loading and displaying pre-computed AI analysis
4
- results (reports and OKRs) that have been fetched from Bubble.io. It does not
5
- perform any new analysis.
6
- """
7
- import logging
8
  import gradio as gr
 
 
 
 
9
 
10
- # UI formatting and data reconstruction functions are still needed
 
11
  try:
12
  from ui.insights_ui_generator import (
13
- format_report_to_markdown,
14
  extract_key_results_for_selection,
15
  format_single_okr_for_display
16
  )
17
- from services.report_data_handler import fetch_and_reconstruct_data_from_bubble
18
  AGENTIC_MODULES_LOADED = True
19
  except ImportError as e:
20
  logging.error(f"Could not import agentic pipeline display modules: {e}. Tabs 3 and 4 will be disabled.")
21
  AGENTIC_MODULES_LOADED = False
22
- # Define placeholder functions if imports fail
23
- def format_report_to_markdown(report_string): return "Agentic modules not loaded. Report unavailable."
24
  def extract_key_results_for_selection(okrs_dict): return []
25
  def format_single_okr_for_display(okr_data, **kwargs): return "Agentic modules not loaded. OKR display unavailable."
26
- def fetch_and_reconstruct_data_from_bubble(df): return None
27
 
28
 
29
- def load_and_display_agentic_results(current_token_state, orchestration_raw_results_st, selected_key_result_ids_st, key_results_for_selection_st):
 
 
 
 
 
 
 
30
  """
31
- Loads pre-computed agentic analysis and OKR data from the application state
32
- (which was fetched from Bubble) and formats it for display in the Gradio UI.
 
 
 
 
 
 
 
 
 
 
 
 
33
  """
34
- logging.info("Loading and displaying pre-computed agentic results from state.")
35
-
36
- # A tuple of Gradio updates to return in case of errors or no data
37
- initial_yield_updates = (
38
- gr.update(value="Nessun dato di analisi trovato..."), # agentic_report_display_md
39
- gr.update(choices=[], value=[], interactive=False), # key_results_cbg
40
- gr.update(value="Nessun OKR trovato..."), # okr_detail_display_md
41
- None, # orchestration_raw_results_st
42
- [], # selected_key_result_ids_st
43
- [], # key_results_for_selection_st
44
- "Stato: In attesa di dati" # agentic_pipeline_status_md
 
 
 
 
 
 
 
 
45
  )
46
 
47
  if not AGENTIC_MODULES_LOADED:
48
- logging.warning("Agentic display modules not loaded. Cannot display results.")
49
- error_updates = list(initial_yield_updates)
50
- error_updates[-1] = "Errore: Moduli AI non caricati."
51
  return tuple(error_updates)
52
 
53
- # The raw DataFrame fetched from Bubble's agentic analysis table
54
- agentic_data_df = current_token_state.get('bubble_agentic_analysis_data')
55
-
56
- if agentic_data_df is None or agentic_data_df.empty:
57
- logging.warning("No agentic analysis data found in the application state.")
58
- return initial_yield_updates
59
 
60
- # Use the handler to reconstruct the report and OKRs from the DataFrame
61
- reconstructed_data = fetch_and_reconstruct_data_from_bubble(agentic_data_df)
 
62
 
63
- if not reconstructed_data:
64
- logging.warning("Could not reconstruct agentic data from the fetched DataFrame.")
65
- error_updates = list(initial_yield_updates)
66
- error_updates[0] = gr.update(value="I dati di analisi esistenti non sono nel formato corretto.")
67
- error_updates[2] = gr.update(value="Impossibile visualizzare gli OKR.")
68
- error_updates[-1] = "Stato: Errore formato dati"
69
- return tuple(error_updates)
 
 
 
 
 
 
 
 
 
 
70
 
71
- # --- Prepare UI updates with the reconstructed data ---
72
- report_str = reconstructed_data.get('report_str', "Nessun report di analisi trovato nei dati.")
73
- actionable_okrs = reconstructed_data.get('actionable_okrs') # This is the dict with 'okrs' list
 
 
 
 
 
 
74
 
75
- # 1. Update Report Tab
76
- agentic_report_md_update = gr.update(value=format_report_to_markdown(report_str))
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
- # 2. Update OKR Tab components
79
- if actionable_okrs and isinstance(actionable_okrs.get("okrs"), list):
80
- krs_for_ui_selection_list = extract_key_results_for_selection(actionable_okrs)
81
- kr_choices_for_cbg = [(kr['kr_description'], kr['unique_kr_id']) for kr in krs_for_ui_selection_list]
82
- key_results_cbg_update = gr.update(choices=kr_choices_for_cbg, value=[], interactive=True)
83
- krs_for_selection_state_update = krs_for_ui_selection_list
 
 
 
 
 
 
 
 
 
84
 
85
- all_okrs_md_parts = [
86
- format_single_okr_for_display(okr_item, accepted_kr_indices=None, okr_main_index=okr_idx)
87
- for okr_idx, okr_item in enumerate(actionable_okrs["okrs"])
88
- ]
89
- okr_detail_display_md_update = gr.update(value="\n\n---\n\n".join(all_okrs_md_parts))
90
- else:
91
- # Handle case where there are no OKRs in the data
92
- krs_for_selection_state_update = []
93
- key_results_cbg_update = gr.update(choices=[], value=[], interactive=False)
94
- okr_detail_display_md_update = gr.update(value="Nessun OKR trovato nei dati di analisi caricati.")
95
-
96
- # Return all the final updates for the Gradio interface
97
- return (
98
- agentic_report_md_update,
99
- key_results_cbg_update,
100
- okr_detail_display_md_update,
101
- reconstructed_data, # Store the full reconstructed data dict in the state
102
- [], # Reset the selected KR IDs state
103
- krs_for_selection_state_update, # Update the state with all available KRs
104
- "Stato: Dati di analisi caricati correttamente da Bubble" # Final status message
105
- )
106
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import pandas as pd
3
+ import json
4
+ import logging
5
+ from typing import Dict, Any, List, Optional
6
 
7
+ # Assuming these functions are in ui.insights_ui_generator
8
+ # Make sure to have this file updated with the new `format_report_for_display` function
9
  try:
10
  from ui.insights_ui_generator import (
11
+ format_report_for_display,
12
  extract_key_results_for_selection,
13
  format_single_okr_for_display
14
  )
 
15
  AGENTIC_MODULES_LOADED = True
16
  except ImportError as e:
17
  logging.error(f"Could not import agentic pipeline display modules: {e}. Tabs 3 and 4 will be disabled.")
18
  AGENTIC_MODULES_LOADED = False
19
+ def format_report_for_display(report_data): return "Agentic modules not loaded. Report unavailable."
 
20
  def extract_key_results_for_selection(okrs_dict): return []
21
  def format_single_okr_for_display(okr_data, **kwargs): return "Agentic modules not loaded. OKR display unavailable."
 
22
 
23
 
24
+ logger = logging.getLogger(__name__)
25
+
26
+ def load_and_display_agentic_results(
27
+ token_state: dict,
28
+ orchestration_raw_results_st: Optional[dict],
29
+ selected_key_result_ids_st: List[str],
30
+ key_results_for_selection_st: List[dict]
31
+ ):
32
  """
33
+ Loads pre-computed agentic results from the state, populates the report library dropdown,
34
+ and displays the latest report and its associated OKRs by default.
35
+
36
+ This function is designed to work with the UI defined in app.py and expects a specific
37
+ order of outputs.
38
+
39
+ Args:
40
+ token_state: The main state dictionary containing the bubble_agentic_analysis_data DataFrame.
41
+ orchestration_raw_results_st: The state holding the raw JSON/dict of the currently displayed report.
42
+ selected_key_result_ids_st: The state for the IDs of selected Key Results.
43
+ key_results_for_selection_st: The state holding the list of all available Key Results for selection.
44
+
45
+ Returns:
46
+ A tuple of Gradio updates matching the `agentic_display_outputs` list in `app.py`.
47
  """
48
+ # Default empty/initial return values that match the output components list
49
+ # The order is critical:
50
+ # 1. agentic_report_display_md
51
+ # 2. report_selector_dd
52
+ # 3. key_results_cbg
53
+ # 4. okr_detail_display_md
54
+ # 5. orchestration_raw_results_st
55
+ # 6. selected_key_result_ids_st
56
+ # 7. key_results_for_selection_st
57
+ # 8. agentic_pipeline_status_md
58
+ initial_updates = (
59
+ "No agentic analysis data found in Bubble.",
60
+ gr.update(choices=[], value=None, interactive=False),
61
+ gr.update(choices=[], value=[], interactive=False),
62
+ "No OKRs to display.",
63
+ None,
64
+ [],
65
+ [],
66
+ "Status: No agentic analysis data found."
67
  )
68
 
69
  if not AGENTIC_MODULES_LOADED:
70
+ error_updates = list(initial_updates)
71
+ error_updates[7] = "Status: Critical module import error."
 
72
  return tuple(error_updates)
73
 
74
+ agentic_df = token_state.get("bubble_agentic_analysis_data")
 
 
 
 
 
75
 
76
+ if agentic_df is None or agentic_df.empty:
77
+ logger.warning("Agentic analysis DataFrame is missing or empty in the state.")
78
+ return initial_updates
79
 
80
+ try:
81
+ # --- 1. Prepare Report Library ---
82
+ if 'Created Date' not in agentic_df.columns or '_id' not in agentic_df.columns:
83
+ raise KeyError("Required columns ('Created Date', '_id') not found in agentic data.")
84
+
85
+ # Ensure 'Created Date' is datetime, then sort to get the latest report first
86
+ agentic_df['Created Date'] = pd.to_datetime(agentic_df['Created Date'])
87
+ agentic_df = agentic_df.sort_values(by='Created Date', ascending=False).reset_index(drop=True)
88
+
89
+ # Create choices for the dropdown: (Display Name, Unique ID)
90
+ report_choices = [
91
+ (f"{row.get('report_type', 'Report')} - {row['Created Date'].strftime('%Y-%m-%d %H:%M')}", row['_id'])
92
+ for _, row in agentic_df.iterrows()
93
+ ]
94
+
95
+ if not report_choices:
96
+ return initial_updates
97
 
98
+ # --- 2. Load and Display the Latest Report by Default ---
99
+ latest_report_series = agentic_df.iloc[0]
100
+ latest_report_id = latest_report_series['_id']
101
+
102
+ # Format the latest report's content for the Markdown display
103
+ report_display_md = format_report_for_display(latest_report_series)
104
+
105
+ # Create the update for the report library dropdown
106
+ report_selector_update = gr.update(choices=report_choices, value=latest_report_id, interactive=True)
107
 
108
+ # --- 3. Load and Prepare OKRs from the Latest Report ---
109
+ raw_results_state = None
110
+ okr_details_md = "No OKRs found in the latest report."
111
+ key_results_cbg_update = gr.update(choices=[], value=[], interactive=False)
112
+ all_krs_state = []
113
+
114
+ # Assumption: The full JSON from the agent is stored in 'orchestration_results'.
115
+ if 'orchestration_results' in latest_report_series and pd.notna(latest_report_series['orchestration_results']):
116
+ try:
117
+ raw_results_state = json.loads(latest_report_series['orchestration_results'])
118
+ except json.JSONDecodeError:
119
+ logger.error(f"Failed to parse 'orchestration_results' JSON for report ID {latest_report_id}")
120
+ raw_results_state = {} # Avoid crashing, proceed with empty data
121
+ else:
122
+ raw_results_state = {}
123
 
124
+ actionable_okrs_dict = raw_results_state.get("actionable_okrs", {})
125
+
126
+ if actionable_okrs_dict:
127
+ all_krs_state = extract_key_results_for_selection(actionable_okrs_dict)
128
+ if all_krs_state:
129
+ kr_choices = [(kr['kr_description'], kr['unique_kr_id']) for kr in all_krs_state]
130
+ key_results_cbg_update = gr.update(choices=kr_choices, value=[], interactive=True)
131
+
132
+ # Format all OKRs for initial display
133
+ okrs_list = actionable_okrs_dict.get("okrs", [])
134
+ output_md_parts = [
135
+ format_single_okr_for_display(okr_data, okr_main_index=okr_idx)
136
+ for okr_idx, okr_data in enumerate(okrs_list)
137
+ ]
138
+ okr_details_md = "\n\n---\n\n".join(output_md_parts) if output_md_parts else okr_details_md
139
 
140
+ status_update = f"Status: Loaded {len(agentic_df)} reports. Displaying the latest from {latest_report_series['Created Date'].strftime('%Y-%m-%d')}."
141
+
142
+ return (
143
+ report_display_md,
144
+ report_selector_update,
145
+ key_results_cbg_update,
146
+ okr_details_md,
147
+ raw_results_state,
148
+ [], # Reset selected KRs
149
+ all_krs_state,
150
+ status_update
151
+ )
 
 
 
 
 
 
 
 
 
152
 
153
+ except Exception as e:
154
+ logger.error(f"Failed to process and display agentic results: {e}", exc_info=True)
155
+ error_updates = list(initial_updates)
156
+ error_updates[0] = f"An error occurred while loading reports: {e}"
157
+ error_updates[7] = f"Status: Error - {e}"
158
+ return tuple(error_updates)