Spaces:
Sleeping
Sleeping
Update run_agentic_pipeline.py
Browse files- run_agentic_pipeline.py +98 -20
run_agentic_pipeline.py
CHANGED
|
@@ -17,7 +17,9 @@ try:
|
|
| 17 |
except ImportError as e:
|
| 18 |
logging.error(f"Could not import agentic pipeline display modules: {e}. Tabs 3 and 4 will be disabled.")
|
| 19 |
AGENTIC_MODULES_LOADED = False
|
| 20 |
-
def format_report_for_display(report_data):
|
|
|
|
|
|
|
| 21 |
def extract_key_results_for_selection(okrs_dict): return []
|
| 22 |
def format_single_okr_for_display(okr_data, **kwargs): return "Agentic modules not loaded."
|
| 23 |
|
|
@@ -35,19 +37,52 @@ def load_and_display_agentic_results(token_state: dict, session_cache: dict):
|
|
| 35 |
Returns:
|
| 36 |
A tuple of Gradio updates, including the updated cache.
|
| 37 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
initial_updates = (
|
| 39 |
-
"No agentic analysis data found
|
| 40 |
-
gr.update(choices=[], value=None, interactive=False),
|
| 41 |
-
gr.update(choices=[], value=[], interactive=False),
|
| 42 |
-
"No OKRs to display.",
|
| 43 |
-
None,
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
)
|
| 46 |
|
| 47 |
if not AGENTIC_MODULES_LOADED:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
agentic_df = token_state.get("bubble_agentic_analysis_data")
|
| 53 |
|
|
@@ -69,16 +104,26 @@ def load_and_display_agentic_results(token_state: dict, session_cache: dict):
|
|
| 69 |
return initial_updates
|
| 70 |
|
| 71 |
quarterly_reports_df = agentic_df[agentic_df['report_type'] == 'Quarter'].copy()
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
latest_report_id = latest_report_series['_id']
|
| 74 |
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
report_selector_update = gr.update(choices=report_choices, value=latest_report_id, interactive=True)
|
| 77 |
|
| 78 |
# --- MODIFIED: Use the session cache for data reconstruction ---
|
| 79 |
reconstructed_data, updated_cache = fetch_and_reconstruct_data_from_bubble(latest_report_series, session_cache)
|
| 80 |
|
| 81 |
-
raw_results_state
|
|
|
|
| 82 |
key_results_cbg_update = gr.update(choices=[], value=[], interactive=False)
|
| 83 |
all_krs_state = []
|
| 84 |
|
|
@@ -93,6 +138,8 @@ def load_and_display_agentic_results(token_state: dict, session_cache: dict):
|
|
| 93 |
okrs_list = actionable_okrs_dict.get("okrs", [])
|
| 94 |
output_md_parts = [format_single_okr_for_display(okr, okr_main_index=i) for i, okr in enumerate(okrs_list)]
|
| 95 |
okr_details_md = "\n\n---\n\n".join(output_md_parts) if output_md_parts else okr_details_md
|
|
|
|
|
|
|
| 96 |
else:
|
| 97 |
logger.error(f"Failed to reconstruct data for latest report ID {latest_report_id}")
|
| 98 |
okr_details_md = "Error: Could not reconstruct OKR data for this report."
|
|
@@ -100,14 +147,45 @@ def load_and_display_agentic_results(token_state: dict, session_cache: dict):
|
|
| 100 |
status_update = f"Status: Loaded {len(agentic_df)} reports. Displaying latest from {latest_report_series['Created Date'].strftime('%Y-%m-%d')}."
|
| 101 |
|
| 102 |
return (
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
except Exception as e:
|
| 109 |
logger.error(f"Failed to process and display agentic results: {e}", exc_info=True)
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
except ImportError as e:
|
| 18 |
logging.error(f"Could not import agentic pipeline display modules: {e}. Tabs 3 and 4 will be disabled.")
|
| 19 |
AGENTIC_MODULES_LOADED = False
|
| 20 |
+
def format_report_for_display(report_data):
|
| 21 |
+
# Ensure this placeholder returns a dictionary matching the expected structure
|
| 22 |
+
return {'header_html': '<h1>Agentic modules not loaded.</h1>', 'body_markdown': 'Report display unavailable.'}
|
| 23 |
def extract_key_results_for_selection(okrs_dict): return []
|
| 24 |
def format_single_okr_for_display(okr_data, **kwargs): return "Agentic modules not loaded."
|
| 25 |
|
|
|
|
| 37 |
Returns:
|
| 38 |
A tuple of Gradio updates, including the updated cache.
|
| 39 |
"""
|
| 40 |
+
# Define placeholder content for empty or error states to match 10 outputs
|
| 41 |
+
empty_header_html = """
|
| 42 |
+
<div class="report-title">📊 Comprehensive Analysis Report</div>
|
| 43 |
+
<div class="report-subtitle">AI-Generated Insights from Your LinkedIn Data</div>
|
| 44 |
+
<div class="status-badge">Generated from Bubble.io</div>
|
| 45 |
+
"""
|
| 46 |
+
empty_body_markdown_no_selection = """
|
| 47 |
+
<div class="empty-state">
|
| 48 |
+
<div class="empty-state-icon">📄</div>
|
| 49 |
+
<div class="empty-state-title">No Report Selected</div>
|
| 50 |
+
<div class="empty-state-description">
|
| 51 |
+
Please select a report from the library above to view its detailed analysis and insights.
|
| 52 |
+
</div>
|
| 53 |
+
</div>
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
initial_updates = (
|
| 57 |
+
gr.update(value="Status: No agentic analysis data found."), # 0: agentic_pipeline_status_md
|
| 58 |
+
gr.update(choices=[], value=None, interactive=False), # 1: report_selector_dd
|
| 59 |
+
gr.update(choices=[], value=[], interactive=False), # 2: key_results_cbg
|
| 60 |
+
gr.update(value="No OKRs to display."), # 3: okr_detail_display_md
|
| 61 |
+
gr.update(value=None), # 4: orchestration_raw_results_st
|
| 62 |
+
gr.update(value=[]), # 5: selected_key_result_ids_st
|
| 63 |
+
gr.update(value=[]), # 6: key_results_for_selection_st
|
| 64 |
+
gr.update(value=empty_header_html), # 7: report_header_html_display
|
| 65 |
+
gr.update(value=empty_body_markdown_no_selection), # 8: report_body_markdown_display
|
| 66 |
+
gr.update(value=session_cache) # 9: reconstruction_cache_st
|
| 67 |
)
|
| 68 |
|
| 69 |
if not AGENTIC_MODULES_LOADED:
|
| 70 |
+
logger.error("Agentic modules not loaded, returning placeholder updates.")
|
| 71 |
+
# Ensure error updates match the 10-item signature
|
| 72 |
+
error_header_html = '<div class="report-title">Error Loading Report</div><div class="report-subtitle">Agentic modules not loaded.</div><div class="status-badge">Error</div>'
|
| 73 |
+
error_body_markdown = '<div class="empty-state"><div class="empty-state-icon">❌</div><div class="empty-state-title">Module Error</div><div class="empty-state-description">Agentic analysis modules could not be loaded. Report display unavailable.</div></div>'
|
| 74 |
+
return (
|
| 75 |
+
gr.update(value="Status: Critical module import error."), # 0
|
| 76 |
+
gr.update(choices=[], value=None, interactive=False), # 1
|
| 77 |
+
gr.update(choices=[], value=[], interactive=False), # 2
|
| 78 |
+
gr.update(value="Agentic modules not loaded. No OKRs to display."), # 3
|
| 79 |
+
gr.update(value=None), # 4
|
| 80 |
+
gr.update(value=[]), # 5
|
| 81 |
+
gr.update(value=[]), # 6
|
| 82 |
+
gr.update(value=error_header_html), # 7
|
| 83 |
+
gr.update(value=error_body_markdown), # 8
|
| 84 |
+
gr.update(value=session_cache) # 9
|
| 85 |
+
)
|
| 86 |
|
| 87 |
agentic_df = token_state.get("bubble_agentic_analysis_data")
|
| 88 |
|
|
|
|
| 104 |
return initial_updates
|
| 105 |
|
| 106 |
quarterly_reports_df = agentic_df[agentic_df['report_type'] == 'Quarter'].copy()
|
| 107 |
+
if quarterly_reports_df.empty:
|
| 108 |
+
logger.warning("No quarterly reports found, falling back to latest available report.")
|
| 109 |
+
latest_report_series = agentic_df.iloc[0] # Use the absolute latest if no quarterly
|
| 110 |
+
else:
|
| 111 |
+
latest_report_series = quarterly_reports_df.iloc[0]
|
| 112 |
+
|
| 113 |
latest_report_id = latest_report_series['_id']
|
| 114 |
|
| 115 |
+
# Split the formatted report content into header and body
|
| 116 |
+
formatted_report_parts = format_report_for_display(latest_report_series)
|
| 117 |
+
report_header_content = formatted_report_parts['header_html']
|
| 118 |
+
report_body_content = formatted_report_parts['body_markdown']
|
| 119 |
+
|
| 120 |
report_selector_update = gr.update(choices=report_choices, value=latest_report_id, interactive=True)
|
| 121 |
|
| 122 |
# --- MODIFIED: Use the session cache for data reconstruction ---
|
| 123 |
reconstructed_data, updated_cache = fetch_and_reconstruct_data_from_bubble(latest_report_series, session_cache)
|
| 124 |
|
| 125 |
+
raw_results_state = None
|
| 126 |
+
okr_details_md = "No OKRs found in the latest report."
|
| 127 |
key_results_cbg_update = gr.update(choices=[], value=[], interactive=False)
|
| 128 |
all_krs_state = []
|
| 129 |
|
|
|
|
| 138 |
okrs_list = actionable_okrs_dict.get("okrs", [])
|
| 139 |
output_md_parts = [format_single_okr_for_display(okr, okr_main_index=i) for i, okr in enumerate(okrs_list)]
|
| 140 |
okr_details_md = "\n\n---\n\n".join(output_md_parts) if output_md_parts else okr_details_md
|
| 141 |
+
else:
|
| 142 |
+
logger.info(f"No actionable_okrs found in reconstructed data for report ID {latest_report_id}.")
|
| 143 |
else:
|
| 144 |
logger.error(f"Failed to reconstruct data for latest report ID {latest_report_id}")
|
| 145 |
okr_details_md = "Error: Could not reconstruct OKR data for this report."
|
|
|
|
| 147 |
status_update = f"Status: Loaded {len(agentic_df)} reports. Displaying latest from {latest_report_series['Created Date'].strftime('%Y-%m-%d')}."
|
| 148 |
|
| 149 |
return (
|
| 150 |
+
gr.update(value=status_update), # 0: agentic_pipeline_status_md
|
| 151 |
+
report_selector_update, # 1: report_selector_dd
|
| 152 |
+
key_results_cbg_update, # 2: key_results_cbg
|
| 153 |
+
gr.update(value=okr_details_md), # 3: okr_detail_display_md
|
| 154 |
+
gr.update(value=raw_results_state), # 4: orchestration_raw_results_st
|
| 155 |
+
gr.update(value=[]), # 5: selected_key_result_ids_st (always start empty for selection)
|
| 156 |
+
gr.update(value=all_krs_state), # 6: key_results_for_selection_st
|
| 157 |
+
gr.update(value=report_header_content), # 7: report_header_html_display
|
| 158 |
+
gr.update(value=report_body_content), # 8: report_body_markdown_display
|
| 159 |
+
gr.update(value=updated_cache) # 9: reconstruction_cache_st
|
| 160 |
)
|
| 161 |
|
| 162 |
except Exception as e:
|
| 163 |
logger.error(f"Failed to process and display agentic results: {e}", exc_info=True)
|
| 164 |
+
# Ensure error returns match the 10-item signature
|
| 165 |
+
error_header_html = """
|
| 166 |
+
<div class="report-title">⚠️ Error Loading Report</div>
|
| 167 |
+
<div class="report-subtitle">An error occurred during data processing.</div>
|
| 168 |
+
<div class="status-badge" style="background: #e74c3c;">Error</div>
|
| 169 |
+
"""
|
| 170 |
+
error_body_markdown = f"""
|
| 171 |
+
<div class="empty-state">
|
| 172 |
+
<div class="empty-state-icon">🚨</div>
|
| 173 |
+
<div class="empty-state-title">Report Loading Failed</div>
|
| 174 |
+
<div class="empty-state-description">
|
| 175 |
+
An error occurred while loading or processing the report data: {e}.
|
| 176 |
+
Please try again or contact support if the issue persists.
|
| 177 |
+
</div>
|
| 178 |
+
</div>
|
| 179 |
+
"""
|
| 180 |
+
return (
|
| 181 |
+
gr.update(value=f"Status: An error occurred: {e}"), # 0
|
| 182 |
+
gr.update(choices=[], value=None, interactive=False), # 1
|
| 183 |
+
gr.update(choices=[], value=[], interactive=False), # 2
|
| 184 |
+
gr.update(value="Error: Could not display OKRs due to an error."), # 3
|
| 185 |
+
gr.update(value=None), # 4
|
| 186 |
+
gr.update(value=[]), # 5
|
| 187 |
+
gr.update(value=[]), # 6
|
| 188 |
+
gr.update(value=error_header_html), # 7
|
| 189 |
+
gr.update(value=error_body_markdown), # 8
|
| 190 |
+
gr.update(value=session_cache) # 9
|
| 191 |
+
)
|