Spaces:
Running
Running
Update run_agentic_pipeline.py
Browse files- run_agentic_pipeline.py +91 -42
run_agentic_pipeline.py
CHANGED
@@ -10,18 +10,51 @@ from services.report_data_handler import fetch_and_reconstruct_data_from_bubble
|
|
10 |
try:
|
11 |
from ui.insights_ui_generator import (
|
12 |
format_report_for_display,
|
13 |
-
extract_key_results_for_selection,
|
14 |
-
format_single_okr_for_display
|
15 |
)
|
|
|
|
|
|
|
16 |
AGENTIC_MODULES_LOADED = True
|
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 |
-
|
21 |
-
|
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 |
|
26 |
logger = logging.getLogger(__name__)
|
27 |
|
@@ -35,9 +68,10 @@ def load_and_display_agentic_results(token_state: dict, session_cache: dict):
|
|
35 |
session_cache: The session-specific cache for reconstructed data.
|
36 |
|
37 |
Returns:
|
38 |
-
A tuple of Gradio updates, including the updated cache.
|
|
|
39 |
"""
|
40 |
-
# Define placeholder content for empty or error states to match
|
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>
|
@@ -53,22 +87,24 @@ def load_and_display_agentic_results(token_state: dict, session_cache: dict):
|
|
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),
|
66 |
-
gr.update(value=session_cache)
|
|
|
67 |
)
|
68 |
|
69 |
if not AGENTIC_MODULES_LOADED:
|
70 |
logger.error("Agentic modules not loaded, returning placeholder updates.")
|
71 |
-
# Ensure error updates match the
|
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 (
|
@@ -81,7 +117,8 @@ def load_and_display_agentic_results(token_state: dict, session_cache: dict):
|
|
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")
|
@@ -93,13 +130,13 @@ def load_and_display_agentic_results(token_state: dict, session_cache: dict):
|
|
93 |
try:
|
94 |
if 'Created Date' not in agentic_df.columns or '_id' not in agentic_df.columns:
|
95 |
raise KeyError("Required columns ('Created Date', '_id') not found.")
|
96 |
-
|
97 |
agentic_df['Created Date'] = pd.to_datetime(agentic_df['Created Date'])
|
98 |
agentic_df = agentic_df.sort_values(by='Created Date', ascending=False).reset_index(drop=True)
|
99 |
-
|
100 |
report_choices = [(f"{row.get('report_type', 'Report')} - {row['Created Date'].strftime('%Y-%m-%d %H:%M')}", row['_id'])
|
101 |
for _, row in agentic_df.iterrows()]
|
102 |
-
|
103 |
if not report_choices:
|
104 |
return initial_updates
|
105 |
|
@@ -111,7 +148,7 @@ def load_and_display_agentic_results(token_state: dict, session_cache: dict):
|
|
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']
|
@@ -121,47 +158,57 @@ def load_and_display_agentic_results(token_state: dict, session_cache: dict):
|
|
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 |
-
|
130 |
if reconstructed_data:
|
131 |
raw_results_state = reconstructed_data
|
132 |
actionable_okrs_dict = raw_results_state.get("actionable_okrs", {})
|
133 |
if actionable_okrs_dict:
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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."
|
|
|
146 |
|
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),
|
151 |
-
report_selector_update,
|
152 |
-
key_results_cbg_update,
|
153 |
-
gr.update(value=okr_details_md),
|
154 |
-
gr.update(value=raw_results_state),
|
155 |
-
gr.update(value=[]),
|
156 |
-
gr.update(value=all_krs_state),
|
157 |
-
gr.update(value=report_header_content),
|
158 |
-
gr.update(value=report_body_content),
|
159 |
-
gr.update(value=updated_cache)
|
|
|
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
|
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>
|
@@ -187,5 +234,7 @@ def load_and_display_agentic_results(token_state: dict, session_cache: dict):
|
|
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 |
)
|
|
|
|
10 |
try:
|
11 |
from ui.insights_ui_generator import (
|
12 |
format_report_for_display,
|
13 |
+
# REMOVED: extract_key_results_for_selection, - No longer directly used here for display
|
14 |
+
# REMOVED: format_single_okr_for_display - No longer directly used here for display
|
15 |
)
|
16 |
+
# NEW: Import the enhanced OKR display functions
|
17 |
+
from ui.okr_ui_generator import format_okrs_for_enhanced_display, get_initial_okr_display
|
18 |
+
|
19 |
AGENTIC_MODULES_LOADED = True
|
20 |
except ImportError as e:
|
21 |
logging.error(f"Could not import agentic pipeline display modules: {e}. Tabs 3 and 4 will be disabled.")
|
22 |
AGENTIC_MODULES_LOADED = False
|
23 |
+
# Placeholder functions for when modules are not loaded, ensuring return signatures match
|
24 |
+
def format_report_for_display(report_data):
|
25 |
return {'header_html': '<h1>Agentic modules not loaded.</h1>', 'body_markdown': 'Report display unavailable.'}
|
26 |
+
# These functions are now used by app.py directly for placeholder returns if modules are not loaded,
|
27 |
+
# but their definitions might still be expected in some contexts, so providing minimal ones.
|
28 |
+
# We still need placeholders for the *return values* if the import fails, even if they're not called
|
29 |
+
# directly within this file, to maintain the correct function signature for load_and_display_agentic_results.
|
30 |
def extract_key_results_for_selection(okrs_dict): return []
|
31 |
def format_single_okr_for_display(okr_data, **kwargs): return "Agentic modules not loaded."
|
32 |
+
# Placeholder for the new OKR UI display
|
33 |
+
def format_okrs_for_enhanced_display(raw_results): return get_initial_okr_display()
|
34 |
+
def get_initial_okr_display(): return """
|
35 |
+
<div class="okr-container">
|
36 |
+
<div class="okr-header">
|
37 |
+
<div class="okr-title">🎯 AI-Generated OKRs & Strategic Tasks</div>
|
38 |
+
<div class="okr-subtitle">Intelligent objectives and key results based on your LinkedIn analytics</div>
|
39 |
+
</div>
|
40 |
+
<div class="okr-stats-bar">
|
41 |
+
<div class="stat-card"><div class="stat-number">0</div><div class="stat-label">Objectives</div></div>
|
42 |
+
<div class="stat-card"><div class="stat-number">0</div><div class="stat-label">Key Results</div></div>
|
43 |
+
<div class="stat-card"><div class="stat-number">0</div><div class="stat-label">Tasks</div></div>
|
44 |
+
<div class="stat-card"><div class="stat-number">0</div><div class="stat-label">High Priority</div></div>
|
45 |
+
</div>
|
46 |
+
<div class="okr-content">
|
47 |
+
<div class="empty-state">
|
48 |
+
<div class="empty-state-icon">⚠️</div>
|
49 |
+
<div class="empty-state-title">Modules Not Loaded</div>
|
50 |
+
<div class="empty-state-description">
|
51 |
+
Agentic analysis modules could not be loaded. OKR display unavailable.
|
52 |
+
</div>
|
53 |
+
</div>
|
54 |
+
</div>
|
55 |
+
</div>
|
56 |
+
"""
|
57 |
+
|
58 |
|
59 |
logger = logging.getLogger(__name__)
|
60 |
|
|
|
68 |
session_cache: The session-specific cache for reconstructed data.
|
69 |
|
70 |
Returns:
|
71 |
+
A tuple of Gradio updates, including the updated cache and the new enhanced OKR HTML.
|
72 |
+
This function now returns 11 values to match the expected outputs in app.py.
|
73 |
"""
|
74 |
+
# Define placeholder content for empty or error states to match 11 outputs
|
75 |
empty_header_html = """
|
76 |
<div class="report-title">📊 Comprehensive Analysis Report</div>
|
77 |
<div class="report-subtitle">AI-Generated Insights from Your LinkedIn Data</div>
|
|
|
87 |
</div>
|
88 |
"""
|
89 |
|
90 |
+
# Default initial updates for 11 outputs
|
91 |
initial_updates = (
|
92 |
gr.update(value="Status: No agentic analysis data found."), # 0: agentic_pipeline_status_md
|
93 |
gr.update(choices=[], value=None, interactive=False), # 1: report_selector_dd
|
94 |
+
gr.update(choices=[], value=[], interactive=False), # 2: key_results_cbg (hidden in app.py)
|
95 |
+
gr.update(value="No OKRs to display."), # 3: okr_detail_display_md (hidden in app.py)
|
96 |
gr.update(value=None), # 4: orchestration_raw_results_st
|
97 |
+
gr.update(value=[]), # 5: selected_key_result_ids_st (hidden in app.py)
|
98 |
+
gr.update(value=[]), # 6: key_results_for_selection_st (hidden in app.py)
|
99 |
gr.update(value=empty_header_html), # 7: report_header_html_display
|
100 |
+
gr.update(value=empty_body_markdown_no_selection), # 8: report_body_markdown_display
|
101 |
+
gr.update(value=session_cache), # 9: reconstruction_cache_st
|
102 |
+
gr.update(value=get_initial_okr_display()) # 10: enhanced_okr_display_html
|
103 |
)
|
104 |
|
105 |
if not AGENTIC_MODULES_LOADED:
|
106 |
logger.error("Agentic modules not loaded, returning placeholder updates.")
|
107 |
+
# Ensure error updates match the 11-item signature
|
108 |
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>'
|
109 |
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>'
|
110 |
return (
|
|
|
117 |
gr.update(value=[]), # 6
|
118 |
gr.update(value=error_header_html), # 7
|
119 |
gr.update(value=error_body_markdown), # 8
|
120 |
+
gr.update(value=session_cache), # 9
|
121 |
+
gr.update(value=get_initial_okr_display()) # 10: Placeholder for enhanced OKR display
|
122 |
)
|
123 |
|
124 |
agentic_df = token_state.get("bubble_agentic_analysis_data")
|
|
|
130 |
try:
|
131 |
if 'Created Date' not in agentic_df.columns or '_id' not in agentic_df.columns:
|
132 |
raise KeyError("Required columns ('Created Date', '_id') not found.")
|
133 |
+
|
134 |
agentic_df['Created Date'] = pd.to_datetime(agentic_df['Created Date'])
|
135 |
agentic_df = agentic_df.sort_values(by='Created Date', ascending=False).reset_index(drop=True)
|
136 |
+
|
137 |
report_choices = [(f"{row.get('report_type', 'Report')} - {row['Created Date'].strftime('%Y-%m-%d %H:%M')}", row['_id'])
|
138 |
for _, row in agentic_df.iterrows()]
|
139 |
+
|
140 |
if not report_choices:
|
141 |
return initial_updates
|
142 |
|
|
|
148 |
latest_report_series = quarterly_reports_df.iloc[0]
|
149 |
|
150 |
latest_report_id = latest_report_series['_id']
|
151 |
+
|
152 |
# Split the formatted report content into header and body
|
153 |
formatted_report_parts = format_report_for_display(latest_report_series)
|
154 |
report_header_content = formatted_report_parts['header_html']
|
|
|
158 |
|
159 |
# --- MODIFIED: Use the session cache for data reconstruction ---
|
160 |
reconstructed_data, updated_cache = fetch_and_reconstruct_data_from_bubble(latest_report_series, session_cache)
|
161 |
+
|
162 |
raw_results_state = None
|
163 |
+
okr_details_md = "No OKRs found in the latest report." # This is for the old, hidden component
|
164 |
+
enhanced_okr_html_content = get_initial_okr_display() # Default to loading state or empty
|
165 |
key_results_cbg_update = gr.update(choices=[], value=[], interactive=False)
|
166 |
all_krs_state = []
|
167 |
+
|
168 |
if reconstructed_data:
|
169 |
raw_results_state = reconstructed_data
|
170 |
actionable_okrs_dict = raw_results_state.get("actionable_okrs", {})
|
171 |
if actionable_okrs_dict:
|
172 |
+
# Format for the new enhanced HTML display
|
173 |
+
enhanced_okr_html_content = format_okrs_for_enhanced_display(raw_results_state)
|
174 |
+
|
175 |
+
# The following is for the old, hidden components. Keep for signature compatibility.
|
176 |
+
# all_krs_state = extract_key_results_for_selection(actionable_okrs_dict) # REMOVED direct usage
|
177 |
+
# if all_krs_state:
|
178 |
+
# kr_choices = [(kr['kr_description'], kr['unique_kr_id']) for kr in all_krs_state]
|
179 |
+
# key_results_cbg_update = gr.update(choices=kr_choices, value=[], interactive=True)
|
180 |
+
# okrs_list = actionable_okrs_dict.get("okrs", [])
|
181 |
+
# output_md_parts = [format_single_okr_for_display(okr, okr_main_index=i) for i, okr in enumerate(okrs_list)]
|
182 |
+
# okr_details_md = "\n\n---\n\n".join(output_md_parts) if output_md_parts else okr_details_md
|
183 |
+
# else:
|
184 |
+
# logger.info(f"No actionable_okrs found in reconstructed data for report ID {latest_report_id}.")
|
185 |
else:
|
186 |
+
logger.info(f"No 'actionable_okrs' key found in reconstructed data for report ID {latest_report_id}.")
|
187 |
+
enhanced_okr_html_content = get_initial_okr_display() # Show empty state if no OKRs
|
188 |
else:
|
189 |
logger.error(f"Failed to reconstruct data for latest report ID {latest_report_id}")
|
190 |
okr_details_md = "Error: Could not reconstruct OKR data for this report."
|
191 |
+
enhanced_okr_html_content = get_initial_okr_display() # Show empty state on reconstruction error
|
192 |
|
193 |
status_update = f"Status: Loaded {len(agentic_df)} reports. Displaying latest from {latest_report_series['Created Date'].strftime('%Y-%m-%d')}."
|
194 |
+
|
195 |
return (
|
196 |
+
gr.update(value=status_update), # 0: agentic_pipeline_status_md
|
197 |
+
report_selector_update, # 1: report_selector_dd
|
198 |
+
key_results_cbg_update, # 2: key_results_cbg (kept for compatibility)
|
199 |
+
gr.update(value=okr_details_md), # 3: okr_detail_display_md (kept for compatibility)
|
200 |
+
gr.update(value=raw_results_state), # 4: orchestration_raw_results_st
|
201 |
+
gr.update(value=[]), # 5: selected_key_result_ids_st (kept for compatibility)
|
202 |
+
gr.update(value=all_krs_state), # 6: key_results_for_selection_st (kept for compatibility)
|
203 |
+
gr.update(value=report_header_content), # 7: report_header_html_display
|
204 |
+
gr.update(value=report_body_content), # 8: report_body_markdown_display
|
205 |
+
gr.update(value=updated_cache), # 9: reconstruction_cache_st
|
206 |
+
gr.update(value=enhanced_okr_html_content) # 10: NEW: The enhanced HTML display for OKRs
|
207 |
)
|
208 |
|
209 |
except Exception as e:
|
210 |
logger.error(f"Failed to process and display agentic results: {e}", exc_info=True)
|
211 |
+
# Ensure error returns match the 11-item signature
|
212 |
error_header_html = """
|
213 |
<div class="report-title">⚠️ Error Loading Report</div>
|
214 |
<div class="report-subtitle">An error occurred during data processing.</div>
|
|
|
234 |
gr.update(value=[]), # 6
|
235 |
gr.update(value=error_header_html), # 7
|
236 |
gr.update(value=error_body_markdown), # 8
|
237 |
+
gr.update(value=session_cache), # 9
|
238 |
+
gr.update(value=get_initial_okr_display()) # 10: Placeholder for enhanced OKR display on error
|
239 |
)
|
240 |
+
|