Mark Cuizon commited on
Commit
f8d30ca
·
1 Parent(s): e400cff

fix: analysis progress UI, Gemini models, and Step 2/3/5 results

Browse files

Shorten AI progress labels to stop duplicating full step prompts. Prefer gemini-2.5-pro/flash. Rebuild Step 2/3/5 narratives from real issues and economic forecast when LLM returns placeholders. Route Step 2 and 5 through dedicated renderers and hide empty Issues Source dumps.

modules/results.py CHANGED
@@ -7607,71 +7607,89 @@ def display_step2_issue_diagnosis(analysis_data):
7607
  summary_text = analysis_data['summary']
7608
  if isinstance(summary_text, str) and summary_text.strip():
7609
  try:
7610
- # Remove any raw dict dumps if the LLM leaked them into the summary
7611
  summary_text = _strip_step5_raw_economic_blocks(summary_text)
7612
  except Exception:
7613
  pass
7614
- st.markdown(
7615
- f'<div style="margin-bottom: 20px; padding: 15px; background: linear-gradient(135deg, #e8f5e8, #ffffff); border-left: 4px solid #28a745; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">'
7616
- f'<p style="margin: 0; font-size: 16px; line-height: 1.6; color: #2c3e50;">{sanitize_persona_and_enforce_article(summary_text.strip())}</p>'
7617
- f'</div>',
7618
- unsafe_allow_html=True
7619
- )
 
 
7620
 
7621
- # Display identified issues
7622
- if 'identified_issues' in analysis_data and analysis_data['identified_issues']:
 
7623
  st.markdown(f"#### 🚨 {t('identified_issues', 'Identified Issues')}")
7624
- issues = analysis_data['identified_issues']
7625
  if isinstance(issues, list):
7626
- for i, issue in enumerate(issues, 1):
7627
- if isinstance(issue, dict):
7628
- # Check for corrupted issues before displaying
7629
- if _is_corrupted_soil_issue(issue):
7630
- st.warning(f"⚠️ Filtered out corrupted issue for parameter: {issue.get('parameter', 'Unknown')}")
7631
- continue
7632
-
7633
- # Create a formatted card for each issue
7634
- parameter = issue.get('parameter', 'Unknown')
7635
- current_value = issue.get('current_value', 'N/A')
7636
- optimal_range = issue.get('optimal_range', 'N/A')
7637
- status = issue.get('status', 'Unknown')
7638
- severity = issue.get('severity', 'Unknown')
7639
- description = issue.get('description', '')
7640
-
7641
  st.markdown(
7642
- f'<div style="margin-bottom: 20px; padding: 15px; background: linear-gradient(135deg, #fff5f5, #ffffff); border-left: 4px solid #dc3545; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">'
7643
- f'<h4 style="margin: 0 0 10px 0; color: #dc3545; font-size: 18px;">⚠️ Issue {i}: {parameter}</h4>'
7644
- f'<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px;">'
7645
- f'<div><strong>Current Value:</strong> {current_value}</div>'
7646
- f'<div><strong>Optimal Range:</strong> {optimal_range}</div>'
7647
- f'<div><strong>Status:</strong> {status}</div>'
7648
- f'<div><strong>Severity:</strong> {severity}</div>'
7649
- f'</div>'
7650
- f'{f"<div><strong>Description:</strong> {description}</div>" if description else ""}'
7651
  f'</div>',
7652
  unsafe_allow_html=True
7653
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7654
  else:
7655
- # Handle string issues
7656
- st.markdown(
7657
- f'<div style="margin-bottom: 15px; padding: 12px; background: linear-gradient(135deg, #fff5f5, #ffffff); border-left: 4px solid #dc3545; border-radius: 6px; box-shadow: 0 2px 6px rgba(0,0,0,0.1);">'
7658
- f'<p style="margin: 0; font-size: 15px; color: #2c3e50;"><strong>Issue {i}:</strong> {sanitize_persona_and_enforce_article(str(issue))}</p>'
7659
- f'</div>',
7660
- unsafe_allow_html=True
7661
- )
7662
-
7663
- # Display visualizations
7664
- if 'visualizations' in analysis_data and analysis_data['visualizations']:
7665
- st.markdown("#### 📊 Visualizations")
7666
- try:
7667
- visualizations = analysis_data['visualizations']
7668
- if isinstance(visualizations, list):
7669
- for i, viz in enumerate(visualizations, 1):
7670
- if isinstance(viz, dict) and 'type' in viz:
7671
- display_visualization(viz, i, 2)
7672
- except Exception as e:
7673
- logger.error(f"Error displaying visualizations: {e}")
7674
- st.error("Error displaying visualizations")
7675
 
7676
  # Display tables
7677
  if 'tables' in analysis_data and analysis_data['tables']:
@@ -7684,73 +7702,40 @@ def display_step2_issue_diagnosis(analysis_data):
7684
  display_table(table_data, f"Table {i}")
7685
  except Exception as e:
7686
  logger.error(f"Error displaying tables: {e}")
7687
- st.error("Error displaying tables")
7688
 
7689
- # Display formatted_analysis or detailed_analysis if available
7690
- # Check formatted_analysis first, then detailed_analysis
7691
  analysis_text = None
7692
  if 'formatted_analysis' in analysis_data and analysis_data['formatted_analysis']:
7693
  analysis_text = analysis_data['formatted_analysis']
7694
  elif 'detailed_analysis' in analysis_data and analysis_data['detailed_analysis']:
7695
  analysis_text = analysis_data['detailed_analysis']
7696
-
7697
- if analysis_text:
 
 
7698
  st.markdown(f"#### 📋 {t('detailed_analysis', 'Detailed Analysis')}")
7699
-
7700
- if isinstance(analysis_text, dict):
7701
  analysis_text = str(analysis_text)
7702
- elif not isinstance(analysis_text, str):
7703
- analysis_text = str(analysis_text) if analysis_text is not None else ""
7704
-
7705
- # If the LLM included a prefixed "Formatted Analysis:" section, prefer its content
7706
  try:
7707
  import re
7708
- formatted_block = re.search(r"Formatted Analysis:\s*(.*)$", analysis_text, re.DOTALL | re.IGNORECASE)
7709
- if formatted_block and formatted_block.group(1).strip():
7710
- analysis_text = formatted_block.group(1).strip()
7711
- except Exception:
7712
- pass
7713
-
7714
- # Clean HTML-like tags (e.g., <br>, <br/>, <br />) and convert to line breaks
7715
- try:
7716
- import re as _re_html
7717
- # Replace <br>, <br/>, <br /> with actual line breaks
7718
- analysis_text = _re_html.sub(r'<br\s*/?>', '\n\n', analysis_text, flags=_re_html.IGNORECASE)
7719
- # Remove other common HTML tags that might leak through
7720
- analysis_text = _re_html.sub(r'</?[a-z]+[^>]*>', '', analysis_text, flags=_re_html.IGNORECASE)
7721
  except Exception:
7722
  pass
7723
-
7724
- # Unescape common escape sequences that sometimes leak from LLM
7725
- try:
7726
- analysis_text = analysis_text.replace('\\n', '\n').replace('\\t', '\t').replace('\\"', '"')
7727
- except Exception:
7728
- pass
7729
-
7730
- # Sanitize persona and enforce neutral tone before rendering
7731
  analysis_text = sanitize_persona_and_enforce_article(analysis_text)
7732
-
7733
- # Extract and render markdown tables first
7734
- analysis_text = _extract_and_render_markdown_tables(analysis_text)
7735
-
7736
- # Normalize markdown formatting
7737
- analysis_text = normalize_markdown_block_for_step3(analysis_text)
7738
-
7739
- # Split into paragraphs and display
7740
- paragraphs = analysis_text.split('\n\n') if '\n\n' in analysis_text else [analysis_text]
7741
- for paragraph in paragraphs:
7742
  if paragraph.strip():
7743
- # Check if it contains markdown formatting
7744
- if any(token in paragraph for token in ['\n- ', '\n* ', '\n1.', '\n2.', '### ', '#### ', '**']):
7745
  st.markdown(paragraph)
7746
  else:
7747
  st.markdown(
7748
- f'<div style="margin-bottom: 18px; padding: 15px; background: linear-gradient(135deg, #ffffff, #f8f9fa); border: 1px solid #e9ecef; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">'
7749
  f'<p style="margin: 0; line-height: 1.8; font-size: 16px; color: #2c3e50;">{paragraph.strip()}</p>'
7750
  f'</div>',
7751
  unsafe_allow_html=True
7752
  )
7753
- st.markdown("")
 
7754
 
7755
  def display_step5_economic_forecast(analysis_data):
7756
  """Display Step 5: Economic Impact Forecast content with consistent formatting"""
@@ -9552,34 +9537,49 @@ def display_step_block(step_result, step_number, step_title):
9552
  display_enhanced_step_result(step_result, step_number)
9553
 
9554
  def display_enhanced_step_result(step_result, step_number):
9555
- """Display enhanced step results with proper structure and formatting for non-technical users"""
9556
- # Ensure step_result is a dictionary
 
 
 
9557
  if not isinstance(step_result, dict):
9558
  logger.error(f"Step {step_number} step_result is not a dictionary: {type(step_result)}")
9559
  st.error(f"❌ Error: Step {step_number} data is not in the expected format")
9560
  return
9561
-
9562
  analysis_data = step_result
 
 
 
 
9563
 
9564
- # Debug: Log the structure of analysis_data
9565
  logger.info(f"Step {step_number} analysis_data keys: {list(analysis_data.keys()) if analysis_data else 'None'}")
9566
- if 'key_findings' in analysis_data:
9567
- logger.info(f"Step {step_number} key_findings type: {type(analysis_data['key_findings'])}, value: {analysis_data['key_findings']}")
9568
- if 'detailed_analysis' in analysis_data:
9569
- logger.info(f"Step {step_number} detailed_analysis type: {type(analysis_data['detailed_analysis'])}, length: {len(str(analysis_data['detailed_analysis'])) if analysis_data['detailed_analysis'] else 0}")
9570
-
9571
- # Special handling for STEP 1 - Data Analysis
9572
  if step_number == 1:
9573
  display_step1_data_analysis(analysis_data)
9574
  return
9575
-
9576
-
9577
-
9578
- # Special handling for STEP 3 - Solution Recommendations
9579
  if step_number == 3:
9580
  display_step3_solution_recommendations(analysis_data)
9581
  return
9582
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9583
  # Alias-mapping: normalize common capitalized keys from LLM output
9584
  try:
9585
  alias_map = {
@@ -9600,7 +9600,6 @@ def display_enhanced_step_result(step_result, step_number):
9600
  for k, v in list(analysis_data.items()):
9601
  if k in alias_map and alias_map[k] not in analysis_data:
9602
  analysis_data[alias_map[k]] = v
9603
- # Remove original capitalized keys to prevent raw dict leakage in other_fields
9604
  for original_key in list(analysis_data.keys()):
9605
  if original_key in alias_map:
9606
  try:
@@ -9610,11 +9609,13 @@ def display_enhanced_step_result(step_result, step_number):
9610
  except Exception:
9611
  pass
9612
 
9613
- # 1. SUMMARY SECTION - Always show if available
9614
  if 'summary' in analysis_data and analysis_data['summary']:
9615
- st.markdown(f"### 📋 {t('summary', 'Summary')}")
9616
  summary_text = analysis_data['summary']
9617
- if isinstance(summary_text, str) and summary_text.strip():
 
 
 
9618
  st.markdown(
9619
  f'<div style="margin-bottom: 20px; padding: 15px; background: linear-gradient(135deg, #e8f5e8, #ffffff); border-left: 4px solid #28a745; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">'
9620
  f'<p style="margin: 0; font-size: 16px; line-height: 1.6; color: #2c3e50;">{summary_text.strip()}</p>'
@@ -9622,82 +9623,41 @@ def display_enhanced_step_result(step_result, step_number):
9622
  unsafe_allow_html=True
9623
  )
9624
  st.markdown("")
9625
-
9626
- # 2. KEY FINDINGS SECTION - Removed from individual steps
9627
- # Key findings are now consolidated and displayed only after Executive Summary
9628
-
9629
- # 3. DETAILED ANALYSIS SECTION - Show if available (with filtering for all steps)
9630
  if 'detailed_analysis' in analysis_data and analysis_data['detailed_analysis']:
9631
- st.markdown(f"### 📋 {t('detailed_analysis', 'Detailed Analysis')}")
9632
  detailed_text = analysis_data['detailed_analysis']
9633
-
9634
- # Ensure detailed_text is a string
9635
  if isinstance(detailed_text, dict):
9636
  detailed_text = str(detailed_text)
9637
  elif not isinstance(detailed_text, str):
9638
- detailed_text = str(detailed_text) if detailed_text is not None else "No detailed analysis available"
9639
-
9640
- # Sanitize persona and enforce neutral tone before other filtering
9641
- detailed_text = sanitize_persona_and_enforce_article(detailed_text)
9642
-
9643
- # Filter out QuickChart URLs but preserve visual comparison text for proper chart generation
9644
- import re
9645
- # Remove QuickChart URLs but keep the visual comparison text
9646
- detailed_text = re.sub(r'!\[.*?\]\(https://quickchart\.io.*?\)', '', detailed_text, flags=re.DOTALL)
9647
-
9648
- # For Step 2, we want to keep the visual comparison text but remove the actual QuickChart URLs
9649
- # This allows the visualization generation to work properly
9650
-
9651
- # Filter out raw LLM output patterns before processing
9652
- filtered_detailed_text = filter_known_sections_from_text(detailed_text)
9653
-
9654
- if filtered_detailed_text.strip() and filtered_detailed_text != "Content filtered to prevent raw LLM output display.":
9655
- # Process HTML tables and other content
9656
- processed_text = process_html_tables(filtered_detailed_text)
9657
-
9658
- # Split into paragraphs for better formatting
9659
- paragraphs = processed_text.split('\n\n') if '\n\n' in processed_text else [processed_text]
9660
-
9661
- for paragraph in paragraphs:
9662
- if isinstance(paragraph, str) and paragraph.strip():
9663
- # Skip empty paragraphs
9664
- if paragraph.strip() == '':
9665
- continue
9666
-
9667
- # Check if this paragraph contains a table (already processed)
9668
- if '<table' in paragraph and '</table>' in paragraph:
9669
- # This is an HTML table, render it directly
9670
- st.markdown(paragraph, unsafe_allow_html=True)
9671
- else:
9672
- # Regular text paragraph
9673
- st.markdown(
9674
- f'<div style="margin-bottom: 18px; padding: 15px; background: linear-gradient(135deg, #ffffff, #f8f9fa); border: 1px solid #e9ecef; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">'
9675
- f'<p style="margin: 0; line-height: 1.8; font-size: 16px; color: #2c3e50;">{sanitize_persona_and_enforce_article(paragraph.strip())}</p>'
9676
- f'</div>',
9677
- unsafe_allow_html=True
9678
- )
9679
- else:
9680
- # If all content was filtered out, don't display anything
9681
- pass
9682
- st.markdown("")
9683
-
9684
- # 3.5. PLANTATION VALUES VS REFERENCE RANGES SECTION - REMOVED as requested
9685
- # This section has been removed to eliminate the "Visual Comparison: Plantation vs. Malaysian Reference Ranges" display
9686
-
9687
- # 3.6. SOIL ISSUES SECTION - Disabled to prevent raw or malformed 'Soil Issues' output
9688
- # Intentionally no-op: we do not render analysis_data['soil_issues'] at all
9689
-
9690
- st.markdown("")
9691
 
9692
- # 3.7. ECONOMIC SCENARIOS SECTION - Special handling for Step 5
9693
  if 'scenarios' in analysis_data and analysis_data['scenarios']:
9694
  scenarios = analysis_data['scenarios']
9695
-
9696
  st.markdown(f"### 📈 {t('investment_scenarios', 'Investment Scenarios')}")
9697
-
9698
- # Handle different formats of scenarios data
9699
  if isinstance(scenarios, dict):
9700
- # Display each scenario in a formatted card
9701
  for scenario_key, scenario_data in scenarios.items():
9702
  if isinstance(scenario_data, dict):
9703
  display_formatted_scenario(scenario_key, scenario_data)
@@ -9935,12 +9895,19 @@ def display_enhanced_step_result(step_result, step_number):
9935
  'tables', 'interpretations', 'data_quality',
9936
  'specific_recommendations', 'format_analysis', 'data_format_recommendations',
9937
  'raw_llm_output', 'raw_output', 'raw_llm',
9938
- 'scenarios', 'assumptions'
 
 
 
 
 
 
9939
  ])
9940
  excluded_keys.update([
9941
  'Key Findings', 'Specific Recommendations', 'Tables', 'Interpretations', 'Visualizations',
9942
  'Yield Forecast', 'Format Analysis', 'Data Format Recommendations', 'Plantation Values vs. Malaysian Reference Ranges',
9943
- 'Soil Issues', 'Issues Source'
 
9944
  ])
9945
  # Also exclude capitalized variants that may appear from LLM outputs
9946
  excluded_keys.update([
@@ -9952,8 +9919,14 @@ def display_enhanced_step_result(step_result, step_number):
9952
  'raw_llm_output', 'raw_output', 'raw_llm', 'deterministic', 'llm_output',
9953
  'Item 0', 'Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6', 'Item 7', 'Item 8', 'Item 9'
9954
  ])
9955
- other_fields = [k for k in analysis_data.keys() if k not in excluded_keys and analysis_data.get(k) is not None and analysis_data.get(k) != ""]
9956
-
 
 
 
 
 
 
9957
  has_key_findings = bool(analysis_data.get('key_findings'))
9958
  if has_key_findings or other_fields:
9959
  st.markdown(f"### 📊 {t('additional_analysis_results', 'Additional Analysis Results')}")
 
7607
  summary_text = analysis_data['summary']
7608
  if isinstance(summary_text, str) and summary_text.strip():
7609
  try:
 
7610
  summary_text = _strip_step5_raw_economic_blocks(summary_text)
7611
  except Exception:
7612
  pass
7613
+ # Hide useless placeholder summaries
7614
+ if summary_text.strip().lower() not in ('analysis completed', 'detailed analysis not available'):
7615
+ st.markdown(
7616
+ f'<div style="margin-bottom: 20px; padding: 15px; background: linear-gradient(135deg, #e8f5e8, #ffffff); border-left: 4px solid #28a745; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">'
7617
+ f'<p style="margin: 0; font-size: 16px; line-height: 1.6; color: #2c3e50;">{sanitize_persona_and_enforce_article(summary_text.strip())}</p>'
7618
+ f'</div>',
7619
+ unsafe_allow_html=True
7620
+ )
7621
 
7622
+ # Display identified issues (supports both LLM and deterministic shapes)
7623
+ issues = analysis_data.get('identified_issues') or []
7624
+ if issues:
7625
  st.markdown(f"#### 🚨 {t('identified_issues', 'Identified Issues')}")
 
7626
  if isinstance(issues, list):
7627
+ issue_num = 0
7628
+ for issue in issues:
7629
+ if not isinstance(issue, dict):
7630
+ issue_num += 1
 
 
 
 
 
 
 
 
 
 
 
7631
  st.markdown(
7632
+ f'<div style="margin-bottom: 15px; padding: 12px; background: linear-gradient(135deg, #fff5f5, #ffffff); border-left: 4px solid #dc3545; border-radius: 6px;">'
7633
+ f'<p style="margin: 0; font-size: 15px; color: #2c3e50;"><strong>Issue {issue_num}:</strong> {sanitize_persona_and_enforce_article(str(issue))}</p>'
 
 
 
 
 
 
 
7634
  f'</div>',
7635
  unsafe_allow_html=True
7636
  )
7637
+ continue
7638
+ if issue.get('type') == 'Summary' or issue.get('parameter') == 'Summary':
7639
+ continue
7640
+ try:
7641
+ if _is_corrupted_soil_issue(issue):
7642
+ continue
7643
+ except Exception:
7644
+ pass
7645
+
7646
+ issue_num += 1
7647
+ parameter = issue.get('parameter', 'Unknown')
7648
+ itype = issue.get('type') or issue.get('issue_type') or 'Issue'
7649
+ current_value = issue.get('current_value', issue.get('value', 'N/A'))
7650
+ optimal_range = issue.get('optimal_range', issue.get('threshold', 'See MPOB standards'))
7651
+ severity = issue.get('severity', issue.get('status', 'Unknown'))
7652
+ cause = issue.get('cause', '')
7653
+ impact = issue.get('impact', '')
7654
+ recommendation = issue.get('recommendation', '')
7655
+ description = issue.get('description', '') or issue.get('issue_description', '')
7656
+
7657
+ detail_bits = []
7658
+ if cause:
7659
+ detail_bits.append(f"<div><strong>Cause:</strong> {cause}</div>")
7660
+ if impact:
7661
+ detail_bits.append(f"<div><strong>Impact:</strong> {impact}</div>")
7662
+ if recommendation:
7663
+ detail_bits.append(f"<div><strong>Recommended action:</strong> {recommendation}</div>")
7664
+ if description:
7665
+ detail_bits.append(f"<div><strong>Details:</strong> {description}</div>")
7666
+ details_html = "".join(detail_bits)
7667
+
7668
+ st.markdown(
7669
+ f'<div style="margin-bottom: 20px; padding: 15px; background: linear-gradient(135deg, #fff5f5, #ffffff); border-left: 4px solid #dc3545; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">'
7670
+ f'<h4 style="margin: 0 0 10px 0; color: #dc3545; font-size: 18px;">⚠️ Issue {issue_num}: {parameter} <span style="font-size:14px;color:#666;">({itype})</span></h4>'
7671
+ f'<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px;">'
7672
+ f'<div><strong>Current Value:</strong> {current_value}</div>'
7673
+ f'<div><strong>Reference:</strong> {optimal_range}</div>'
7674
+ f'<div><strong>Severity:</strong> {severity}</div>'
7675
+ f'</div>'
7676
+ f'{details_html}'
7677
+ f'</div>',
7678
+ unsafe_allow_html=True
7679
+ )
7680
+
7681
+ # Soil / leaf issue lists (from standards comparator) when present as strings
7682
+ for label, key in (("Soil Issues", "soil_issues"), ("Leaf Issues", "leaf_issues")):
7683
+ vals = analysis_data.get(key) or []
7684
+ if isinstance(vals, list) and vals and not issues:
7685
+ st.markdown(f"#### 🌱 {label}" if "Soil" in label else f"#### 🍃 {label}")
7686
+ for i, item in enumerate(vals, 1):
7687
+ if isinstance(item, dict):
7688
+ text = item.get('description') or item.get('issue') or item.get('message') or str(item)
7689
  else:
7690
+ text = str(item)
7691
+ if text.strip():
7692
+ st.markdown(f"{i}. {sanitize_persona_and_enforce_article(text.strip())}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7693
 
7694
  # Display tables
7695
  if 'tables' in analysis_data and analysis_data['tables']:
 
7702
  display_table(table_data, f"Table {i}")
7703
  except Exception as e:
7704
  logger.error(f"Error displaying tables: {e}")
 
7705
 
7706
+ # Detailed analysis narrative
 
7707
  analysis_text = None
7708
  if 'formatted_analysis' in analysis_data and analysis_data['formatted_analysis']:
7709
  analysis_text = analysis_data['formatted_analysis']
7710
  elif 'detailed_analysis' in analysis_data and analysis_data['detailed_analysis']:
7711
  analysis_text = analysis_data['detailed_analysis']
7712
+
7713
+ if analysis_text and str(analysis_text).strip().lower() not in (
7714
+ 'detailed analysis not available', 'analysis completed', ''
7715
+ ):
7716
  st.markdown(f"#### 📋 {t('detailed_analysis', 'Detailed Analysis')}")
7717
+ if not isinstance(analysis_text, str):
 
7718
  analysis_text = str(analysis_text)
 
 
 
 
7719
  try:
7720
  import re
7721
+ analysis_text = re.sub(r'<br\s*/?>', '\n\n', analysis_text, flags=re.I)
7722
+ analysis_text = re.sub(r'</?[a-z]+[^>]*>', '', analysis_text, flags=re.I)
 
 
 
 
 
 
 
 
 
 
 
7723
  except Exception:
7724
  pass
 
 
 
 
 
 
 
 
7725
  analysis_text = sanitize_persona_and_enforce_article(analysis_text)
7726
+ for paragraph in (analysis_text.split('\n\n') if '\n\n' in analysis_text else [analysis_text]):
 
 
 
 
 
 
 
 
 
7727
  if paragraph.strip():
7728
+ if any(token in paragraph for token in ['\n- ', '\n* ', '### ', '**']):
 
7729
  st.markdown(paragraph)
7730
  else:
7731
  st.markdown(
7732
+ f'<div style="margin-bottom: 18px; padding: 15px; background: linear-gradient(135deg, #ffffff, #f8f9fa); border: 1px solid #e9ecef; border-radius: 8px;">'
7733
  f'<p style="margin: 0; line-height: 1.8; font-size: 16px; color: #2c3e50;">{paragraph.strip()}</p>'
7734
  f'</div>',
7735
  unsafe_allow_html=True
7736
  )
7737
+ elif not issues:
7738
+ st.info("No critical agronomic issues were flagged from the current soil and leaf averages.")
7739
 
7740
  def display_step5_economic_forecast(analysis_data):
7741
  """Display Step 5: Economic Impact Forecast content with consistent formatting"""
 
9537
  display_enhanced_step_result(step_result, step_number)
9538
 
9539
  def display_enhanced_step_result(step_result, step_number):
9540
+ """Display enhanced step results with proper structure and formatting for non-technical users.
9541
+
9542
+ NOTE: This is the active implementation (defined last). Step-specific renderers must be
9543
+ routed here so Step 2/5 content is not lost to the generic "Additional Analysis Results" dump.
9544
+ """
9545
  if not isinstance(step_result, dict):
9546
  logger.error(f"Step {step_number} step_result is not a dictionary: {type(step_result)}")
9547
  st.error(f"❌ Error: Step {step_number} data is not in the expected format")
9548
  return
9549
+
9550
  analysis_data = step_result
9551
+ try:
9552
+ step_number = int(step_number)
9553
+ except Exception:
9554
+ pass
9555
 
 
9556
  logger.info(f"Step {step_number} analysis_data keys: {list(analysis_data.keys()) if analysis_data else 'None'}")
9557
+
9558
+ # Route specialized steps first
 
 
 
 
9559
  if step_number == 1:
9560
  display_step1_data_analysis(analysis_data)
9561
  return
9562
+ if step_number == 2:
9563
+ display_step2_issue_diagnosis(analysis_data)
9564
+ return
 
9565
  if step_number == 3:
9566
  display_step3_solution_recommendations(analysis_data)
9567
  return
9568
+ if step_number == 4:
9569
+ try:
9570
+ display_regenerative_agriculture_content(analysis_data)
9571
+ return
9572
+ except Exception:
9573
+ pass
9574
+ if step_number == 5:
9575
+ try:
9576
+ display_step5_economic_forecast(analysis_data)
9577
+ return
9578
+ except Exception as e:
9579
+ logger.error(f"Step 5 special handling failed: {e}", exc_info=True)
9580
+ st.error(f"Error displaying Step 5 economic forecast: {e}")
9581
+ # fall through to generic
9582
+
9583
  # Alias-mapping: normalize common capitalized keys from LLM output
9584
  try:
9585
  alias_map = {
 
9600
  for k, v in list(analysis_data.items()):
9601
  if k in alias_map and alias_map[k] not in analysis_data:
9602
  analysis_data[alias_map[k]] = v
 
9603
  for original_key in list(analysis_data.keys()):
9604
  if original_key in alias_map:
9605
  try:
 
9609
  except Exception:
9610
  pass
9611
 
9612
+ # 1. SUMMARY SECTION
9613
  if 'summary' in analysis_data and analysis_data['summary']:
 
9614
  summary_text = analysis_data['summary']
9615
+ if isinstance(summary_text, str) and summary_text.strip().lower() not in (
9616
+ 'analysis completed', 'detailed analysis not available', ''
9617
+ ):
9618
+ st.markdown(f"### 📋 {t('summary', 'Summary')}")
9619
  st.markdown(
9620
  f'<div style="margin-bottom: 20px; padding: 15px; background: linear-gradient(135deg, #e8f5e8, #ffffff); border-left: 4px solid #28a745; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.1);">'
9621
  f'<p style="margin: 0; font-size: 16px; line-height: 1.6; color: #2c3e50;">{summary_text.strip()}</p>'
 
9623
  unsafe_allow_html=True
9624
  )
9625
  st.markdown("")
9626
+
9627
+ # 3. DETAILED ANALYSIS SECTION
 
 
 
9628
  if 'detailed_analysis' in analysis_data and analysis_data['detailed_analysis']:
 
9629
  detailed_text = analysis_data['detailed_analysis']
 
 
9630
  if isinstance(detailed_text, dict):
9631
  detailed_text = str(detailed_text)
9632
  elif not isinstance(detailed_text, str):
9633
+ detailed_text = str(detailed_text) if detailed_text is not None else ""
9634
+ if detailed_text.strip().lower() not in ('detailed analysis not available', 'analysis completed', ''):
9635
+ st.markdown(f"### 📋 {t('detailed_analysis', 'Detailed Analysis')}")
9636
+ detailed_text = sanitize_persona_and_enforce_article(detailed_text)
9637
+ import re
9638
+ detailed_text = re.sub(r'!\[.*?\]\(https://quickchart\.io.*?\)', '', detailed_text, flags=re.DOTALL)
9639
+ filtered_detailed_text = filter_known_sections_from_text(detailed_text)
9640
+ if filtered_detailed_text.strip() and filtered_detailed_text != "Content filtered to prevent raw LLM output display.":
9641
+ processed_text = process_html_tables(filtered_detailed_text)
9642
+ paragraphs = processed_text.split('\n\n') if '\n\n' in processed_text else [processed_text]
9643
+ for paragraph in paragraphs:
9644
+ if isinstance(paragraph, str) and paragraph.strip():
9645
+ if '<table' in paragraph and '</table>' in paragraph:
9646
+ st.markdown(paragraph, unsafe_allow_html=True)
9647
+ else:
9648
+ st.markdown(
9649
+ f'<div style="margin-bottom: 18px; padding: 15px; background: linear-gradient(135deg, #ffffff, #f8f9fa); border: 1px solid #e9ecef; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.05);">'
9650
+ f'<p style="margin: 0; line-height: 1.8; font-size: 16px; color: #2c3e50;">{sanitize_persona_and_enforce_article(paragraph.strip())}</p>'
9651
+ f'</div>',
9652
+ unsafe_allow_html=True
9653
+ )
9654
+ st.markdown("")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9655
 
9656
+ # Economic scenarios if present (non-step-5 path)
9657
  if 'scenarios' in analysis_data and analysis_data['scenarios']:
9658
  scenarios = analysis_data['scenarios']
 
9659
  st.markdown(f"### 📈 {t('investment_scenarios', 'Investment Scenarios')}")
 
 
9660
  if isinstance(scenarios, dict):
 
9661
  for scenario_key, scenario_data in scenarios.items():
9662
  if isinstance(scenario_data, dict):
9663
  display_formatted_scenario(scenario_key, scenario_data)
 
9895
  'tables', 'interpretations', 'data_quality',
9896
  'specific_recommendations', 'format_analysis', 'data_format_recommendations',
9897
  'raw_llm_output', 'raw_output', 'raw_llm',
9898
+ 'scenarios', 'assumptions',
9899
+ # Internal / already-rendered diagnosis fields (do not dump as "Analysis:")
9900
+ 'identified_issues', 'soil_issues', 'leaf_issues', 'issues_source',
9901
+ 'total_issues', 'economic_forecast', 'economic_forecast_source',
9902
+ 'solution_options', 'nutrient_comparisons', 'visualizations_source',
9903
+ 'success', 'processing_time', 'error_details', 'confidence_level', 'analysis',
9904
+ 'regenerative_practices',
9905
  ])
9906
  excluded_keys.update([
9907
  'Key Findings', 'Specific Recommendations', 'Tables', 'Interpretations', 'Visualizations',
9908
  'Yield Forecast', 'Format Analysis', 'Data Format Recommendations', 'Plantation Values vs. Malaysian Reference Ranges',
9909
+ 'Soil Issues', 'Issues Source', 'Identified Issues', 'Soil Parameters Diagnosis',
9910
+ 'Leaf Nutrient Diagnosis', 'Leaf Issues',
9911
  ])
9912
  # Also exclude capitalized variants that may appear from LLM outputs
9913
  excluded_keys.update([
 
9919
  'raw_llm_output', 'raw_output', 'raw_llm', 'deterministic', 'llm_output',
9920
  'Item 0', 'Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6', 'Item 7', 'Item 8', 'Item 9'
9921
  ])
9922
+ other_fields = [
9923
+ k for k in analysis_data.keys()
9924
+ if k not in excluded_keys
9925
+ and analysis_data.get(k) is not None
9926
+ and analysis_data.get(k) != ""
9927
+ and not str(k).lower().endswith('_source')
9928
+ ]
9929
+
9930
  has_key_findings = bool(analysis_data.get('key_findings'))
9931
  if has_key_findings or other_fields:
9932
  st.markdown(f"### 📊 {t('additional_analysis_results', 'Additional Analysis Results')}")
utils/analysis_engine.py CHANGED
@@ -2623,12 +2623,14 @@ class PromptAnalyzer:
2623
  except Exception:
2624
  configured_model = None
2625
 
 
2626
  preferred_models = []
2627
  for m in (
2628
- configured_model,
2629
  "gemini-2.5-pro",
2630
  "gemini-2.5-flash",
 
2631
  "gemini-2.0-flash",
 
2632
  "gemini-1.5-pro",
2633
  "gemini-1.5-flash",
2634
  "gemini-1.5-pro-002",
@@ -2728,43 +2730,63 @@ class PromptAnalyzer:
2728
  """Extract steps dynamically from prompt text"""
2729
  try:
2730
  steps = []
2731
-
 
 
 
 
 
 
 
 
 
2732
  # Use regex to find step patterns
2733
  step_pattern = r'Step\s+(\d+):\s*([^\n]+)'
2734
  matches = re.findall(step_pattern, prompt_text, re.IGNORECASE)
2735
-
2736
- # Debug: Log what we found
2737
  self.logger.info(f"DEBUG: Found {len(matches)} step matches in prompt")
2738
  for i, (step_num, step_title) in enumerate(matches):
2739
  self.logger.info(f"DEBUG: Match {i+1}: Step {step_num}: {step_title[:100]}...")
2740
-
2741
  for step_num, step_title in matches:
2742
- # Extract description (content after the title until next step or end)
 
2743
  start_pos = prompt_text.find(f"Step {step_num}:")
 
 
 
2744
  if start_pos == -1:
2745
  continue
2746
-
2747
- # Find next step or end of text
2748
- next_step_pos = prompt_text.find(f"Step {int(step_num) + 1}:", start_pos)
2749
- if next_step_pos == -1:
2750
- description = prompt_text[start_pos:].strip()
2751
  else:
2752
- description = prompt_text[start_pos:next_step_pos].strip()
2753
-
2754
- # Truncate overly long descriptions to prevent token limits
2755
- max_description_length = 1500 # Reasonable limit for step descriptions
 
 
 
 
 
 
 
 
 
2756
  if len(description) > max_description_length:
2757
  description = description[:max_description_length] + "... [truncated for processing efficiency]"
2758
-
2759
  steps.append({
2760
- 'number': int(step_num),
2761
- 'title': step_title.strip(),
2762
  'description': description
2763
  })
2764
-
2765
  self.logger.info(f"Extracted {len(steps)} steps from prompt")
2766
  return steps
2767
-
2768
  except Exception as e:
2769
  self.logger.error(f"Error extracting steps from prompt: {str(e)}")
2770
  return []
@@ -8250,24 +8272,107 @@ class AnalysisEngine:
8250
  except Exception as _e:
8251
  self.logger.warning(f"Could not build Step 1 visualizations: {_e}")
8252
 
8253
- # Enhanced Step 2 processing with real issue analysis
8254
  try:
8255
  for i, sr in enumerate(step_results):
8256
  if sr and sr.get('step_number') == 2:
8257
- # Always rebuild Step 2 with REAL soil and leaf issues for accuracy
8258
- sr['identified_issues'] = self._build_step2_issues(soil_params, leaf_params, all_issues)
8259
- sr['soil_issues'] = soil_issues
8260
- sr['leaf_issues'] = leaf_issues
8261
- sr['total_issues'] = len(all_issues)
8262
- sr['issues_source'] = 'deterministic'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8263
  step_results[i] = sr
8264
  break
8265
  except Exception as _e:
8266
  self.logger.warning(f"Could not build Step 2 issues: {_e}")
8267
 
8268
- # Step 6 should NOT have economic forecast data injected - net profit forecasts removed from Step 6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8269
 
8270
- # Enhanced Step 5 processing with complete economic forecast
8271
  try:
8272
  for i, sr in enumerate(step_results):
8273
  if sr and sr.get('step_number') == 5:
@@ -8296,7 +8401,26 @@ class AnalysisEngine:
8296
  650, 750, scenario_name
8297
  )
8298
  scenario_data['yearly_data'] = yearly_data
8299
- sr['economic_forecast_source'] = 'deterministic'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8300
  step_results[i] = sr
8301
  self.logger.info(f"Injected complete economic forecast with yearly_data into Step 5")
8302
  break
 
2623
  except Exception:
2624
  configured_model = None
2625
 
2626
+ # Prefer latest high-quality Gemini models first (probe verifies availability).
2627
  preferred_models = []
2628
  for m in (
 
2629
  "gemini-2.5-pro",
2630
  "gemini-2.5-flash",
2631
+ configured_model,
2632
  "gemini-2.0-flash",
2633
+ "gemini-2.0-pro",
2634
  "gemini-1.5-pro",
2635
  "gemini-1.5-flash",
2636
  "gemini-1.5-pro-002",
 
2730
  """Extract steps dynamically from prompt text"""
2731
  try:
2732
  steps = []
2733
+ # Known short titles when the prompt dumps the full instruction after "Step N:"
2734
+ default_titles = {
2735
+ 1: "Your Soil and Leaf Test Results and Analysis",
2736
+ 2: "Diagnose Agronomic Issues",
2737
+ 3: "Recommend Solutions",
2738
+ 4: "Regenerative Agriculture Integration",
2739
+ 5: "Economic Impact Forecast",
2740
+ 6: "Yield Forecast Graph",
2741
+ }
2742
+
2743
  # Use regex to find step patterns
2744
  step_pattern = r'Step\s+(\d+):\s*([^\n]+)'
2745
  matches = re.findall(step_pattern, prompt_text, re.IGNORECASE)
2746
+
 
2747
  self.logger.info(f"DEBUG: Found {len(matches)} step matches in prompt")
2748
  for i, (step_num, step_title) in enumerate(matches):
2749
  self.logger.info(f"DEBUG: Match {i+1}: Step {step_num}: {step_title[:100]}...")
2750
+
2751
  for step_num, step_title in matches:
2752
+ num = int(step_num)
2753
+ # Extract full block for description
2754
  start_pos = prompt_text.find(f"Step {step_num}:")
2755
+ if start_pos == -1:
2756
+ start_pos = re.search(rf'Step\s+{step_num}:', prompt_text, re.I)
2757
+ start_pos = start_pos.start() if start_pos else -1
2758
  if start_pos == -1:
2759
  continue
2760
+
2761
+ next_m = re.search(rf'Step\s+{num + 1}:', prompt_text[start_pos + 1:], re.I)
2762
+ if next_m:
2763
+ description = prompt_text[start_pos:start_pos + 1 + next_m.start()].strip()
 
2764
  else:
2765
+ description = prompt_text[start_pos:].strip()
2766
+
2767
+ # Short UI title: first clause only (avoid dumping entire prompt into progress/UI)
2768
+ raw_title = step_title.strip()
2769
+ short_title = raw_title
2770
+ for sep in (". ", ": ", " — ", " - "):
2771
+ if sep in short_title and short_title.index(sep) < 90:
2772
+ short_title = short_title.split(sep, 1)[0].strip()
2773
+ break
2774
+ if len(short_title) > 90 or len(raw_title) > 120:
2775
+ short_title = default_titles.get(num, short_title[:80].rstrip() + "…")
2776
+
2777
+ max_description_length = 2500
2778
  if len(description) > max_description_length:
2779
  description = description[:max_description_length] + "... [truncated for processing efficiency]"
2780
+
2781
  steps.append({
2782
+ 'number': num,
2783
+ 'title': short_title,
2784
  'description': description
2785
  })
2786
+
2787
  self.logger.info(f"Extracted {len(steps)} steps from prompt")
2788
  return steps
2789
+
2790
  except Exception as e:
2791
  self.logger.error(f"Error extracting steps from prompt: {str(e)}")
2792
  return []
 
8272
  except Exception as _e:
8273
  self.logger.warning(f"Could not build Step 1 visualizations: {_e}")
8274
 
8275
+ # Enhanced Step 2 processing with real issue analysis + readable narrative
8276
  try:
8277
  for i, sr in enumerate(step_results):
8278
  if sr and sr.get('step_number') == 2:
8279
+ issues = self._build_step2_issues(soil_params, leaf_params, all_issues)
8280
+ sr['identified_issues'] = issues
8281
+ sr['soil_issues'] = soil_issues or []
8282
+ sr['leaf_issues'] = leaf_issues or []
8283
+ sr['total_issues'] = len(all_issues) if all_issues else max(0, len(issues) - 1)
8284
+ sr['issues_source'] = 'standards_comparison'
8285
+
8286
+ # Replace empty/placeholder LLM text with real diagnosis narrative
8287
+ empty_summaries = {
8288
+ '', 'analysis completed', 'detailed analysis not available',
8289
+ 'analysis completed using fallback processing',
8290
+ }
8291
+ sum_l = str(sr.get('summary') or '').strip().lower()
8292
+ det_l = str(sr.get('detailed_analysis') or '').strip().lower()
8293
+ real_issues = [x for x in issues if isinstance(x, dict) and x.get('type') != 'Summary']
8294
+ soil_n = len([x for x in real_issues if x.get('type') == 'Soil'])
8295
+ leaf_n = len([x for x in real_issues if x.get('type') == 'Leaf'])
8296
+
8297
+ if sum_l in empty_summaries or not sum_l:
8298
+ sr['summary'] = (
8299
+ f"Issue diagnosis identified {len(real_issues)} agronomic concern(s) "
8300
+ f"({soil_n} soil, {leaf_n} leaf) against Malaysian oil palm standards."
8301
+ )
8302
+ if det_l in empty_summaries or not det_l or len(det_l) < 40:
8303
+ lines = [
8304
+ "The following diagnosis is based on measured soil and leaf averages "
8305
+ "compared with Malaysian (MPOB-aligned) adequacy ranges.",
8306
+ "",
8307
+ ]
8308
+ for iss in real_issues[:12]:
8309
+ param = iss.get('parameter', 'Parameter')
8310
+ sev = iss.get('severity', 'Unknown')
8311
+ val = iss.get('current_value', 'N/A')
8312
+ cause = iss.get('cause', '')
8313
+ impact = iss.get('impact', '')
8314
+ rec = iss.get('recommendation', '')
8315
+ lines.append(f"**{param}** ({iss.get('type', 'Issue')}) — {sev}")
8316
+ lines.append(f"- Current average: {val}")
8317
+ if cause:
8318
+ lines.append(f"- Cause: {cause}")
8319
+ if impact:
8320
+ lines.append(f"- Impact: {impact}")
8321
+ if rec:
8322
+ lines.append(f"- Action: {rec}")
8323
+ lines.append("")
8324
+ if not real_issues:
8325
+ lines.append(
8326
+ "No critical out-of-range parameters were flagged from the current averages. "
8327
+ "Continue routine monitoring and maintain balanced fertilization."
8328
+ )
8329
+ sr['detailed_analysis'] = "\n".join(lines).strip()
8330
+ if not sr.get('key_findings'):
8331
+ sr['key_findings'] = [
8332
+ f"{iss.get('parameter')}: {iss.get('severity')}"
8333
+ for iss in real_issues[:6]
8334
+ ] or ["No critical issues flagged from standards comparison"]
8335
  step_results[i] = sr
8336
  break
8337
  except Exception as _e:
8338
  self.logger.warning(f"Could not build Step 2 issues: {_e}")
8339
 
8340
+ # Step 3: enrich empty recommendations from identified issues / recommendations list
8341
+ try:
8342
+ for i, sr in enumerate(step_results):
8343
+ if sr and sr.get('step_number') == 3:
8344
+ empty = {
8345
+ '', 'analysis completed', 'detailed analysis not available',
8346
+ 'solution recommendations prepared',
8347
+ }
8348
+ sum_l = str(sr.get('summary') or '').strip().lower()
8349
+ det_l = str(sr.get('detailed_analysis') or '').strip().lower()
8350
+ sols = sr.get('solution_options') or sr.get('specific_recommendations') or []
8351
+ if (sum_l in empty or det_l in empty or not sols) and recommendations:
8352
+ if sum_l in empty:
8353
+ sr['summary'] = (
8354
+ f"Solution framework prepared with {len(recommendations)} recommendation area(s) "
8355
+ f"prioritized from diagnosed soil and leaf gaps."
8356
+ )
8357
+ if det_l in empty or len(det_l) < 40:
8358
+ lines = ["Recommended corrective actions based on diagnosed nutrient and soil issues:", ""]
8359
+ for idx, rec in enumerate(recommendations[:10], 1):
8360
+ if isinstance(rec, dict):
8361
+ action = rec.get('action') or rec.get('recommendation') or rec.get('title') or str(rec)
8362
+ timing = rec.get('timeline') or rec.get('priority') or ''
8363
+ lines.append(f"{idx}. {action}" + (f" ({timing})" if timing else ""))
8364
+ else:
8365
+ lines.append(f"{idx}. {rec}")
8366
+ sr['detailed_analysis'] = "\n".join(lines)
8367
+ if not sols:
8368
+ sr['solution_options'] = recommendations
8369
+ sr['specific_recommendations'] = recommendations
8370
+ step_results[i] = sr
8371
+ break
8372
+ except Exception as _e:
8373
+ self.logger.warning(f"Could not enrich Step 3 solutions: {_e}")
8374
 
8375
+ # Enhanced Step 5 processing with complete economic forecast + narrative
8376
  try:
8377
  for i, sr in enumerate(step_results):
8378
  if sr and sr.get('step_number') == 5:
 
8401
  650, 750, scenario_name
8402
  )
8403
  scenario_data['yearly_data'] = yearly_data
8404
+ sr['economic_forecast_source'] = 'calculated'
8405
+ empty = {'', 'analysis completed', 'detailed analysis not available', 'economic impact assessment prepared'}
8406
+ sum_l = str(sr.get('summary') or '').strip().lower()
8407
+ det_l = str(sr.get('detailed_analysis') or '').strip().lower()
8408
+ if economic_forecast and (sum_l in empty or det_l in empty):
8409
+ land = economic_forecast.get('land_size_hectares') or land_yield_data.get('land_size', 'N/A')
8410
+ yld = economic_forecast.get('current_yield_tonnes_per_ha') or land_yield_data.get('current_yield', 'N/A')
8411
+ scenarios = list((economic_forecast.get('scenarios') or {}).keys())
8412
+ if sum_l in empty:
8413
+ sr['summary'] = (
8414
+ f"Economic impact forecast prepared for {land} ha at a current yield baseline "
8415
+ f"of {yld} t/ha, with {len(scenarios)} investment scenario(s)."
8416
+ )
8417
+ if det_l in empty:
8418
+ sr['detailed_analysis'] = (
8419
+ "Five-year economic projections compare low, medium, and high investment "
8420
+ "pathways for nutrient correction. Tables below show yearly yield, cost, "
8421
+ "revenue, and net position for each scenario. Use these figures to prioritize "
8422
+ "affordable interventions that close the largest nutrient gaps first."
8423
+ )
8424
  step_results[i] = sr
8425
  self.logger.info(f"Injected complete economic forecast with yearly_data into Step 5")
8426
  break
utils/streamlit_runtime.py CHANGED
@@ -48,12 +48,30 @@ class AnalysisProgressReporter:
48
  working=message,
49
  )
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  def on_step_start(self, step_number: int, step_title: str, total_steps: int) -> None:
52
  self._total_steps = max(total_steps, 1)
53
  fraction = max(0.0, (step_number - 1) / self._total_steps)
54
- self.on_phase(
55
- f"🤖 **AI Step {step_number}/{total_steps}:** {step_title}...",
56
- fraction=fraction,
 
 
 
57
  )
58
 
59
  def on_stream_chunk(self, step_number: int, chars_received: int) -> None:
@@ -63,7 +81,7 @@ class AnalysisProgressReporter:
63
  self._last_flush = now
64
  fraction = min(0.99, ((step_number - 1) + min(chars_received / 4000.0, 0.95)) / self._total_steps)
65
  progress = int(self.base_progress + (self.progress_span * fraction))
66
- stream_msg = f"Receiving AI output... {chars_received:,} characters"
67
  self._update(
68
  progress=progress,
69
  status=f"🤖 **AI Step {step_number}/{self._total_steps}:** {stream_msg}",
@@ -73,9 +91,10 @@ class AnalysisProgressReporter:
73
 
74
  def on_step_complete(self, step_number: int) -> None:
75
  fraction = min(1.0, step_number / self._total_steps)
76
- self.on_phase(
77
- f"✅ **AI Step {step_number}/{self._total_steps}** complete",
78
- fraction=fraction,
 
79
  )
80
 
81
  def _update(
@@ -90,13 +109,17 @@ class AnalysisProgressReporter:
90
  self.progress_bar.progress(min(max(int(progress), 0), 100))
91
  if self.status_text is not None:
92
  self.status_text.markdown(status)
 
93
  if self.working_indicator is not None and working:
94
- self.working_indicator.markdown(f"🔄 **Processing:** {working}")
 
 
 
95
  if self.stream_indicator is not None and stream:
96
  self.stream_indicator.caption(stream)
97
  if self.time_estimate is not None:
98
  self.time_estimate.markdown(
99
- "⏱️ Analysis is still running — keep this tab open."
100
  )
101
  if self.step_indicator is not None:
102
  self.step_indicator.markdown("📋 AI analysis in progress")
 
48
  working=message,
49
  )
50
 
51
+ @staticmethod
52
+ def _short_title(step_title: str, max_len: int = 72) -> str:
53
+ """Collapse long step prompts into a short progress label (no duplicate walls of text)."""
54
+ text = " ".join(str(step_title or "").split())
55
+ if not text:
56
+ return "Analysis"
57
+ # Prefer text before first period / colon after a short phrase
58
+ for sep in (". ", ": ", " — ", " - "):
59
+ if sep in text and text.index(sep) < max_len:
60
+ text = text.split(sep, 1)[0].strip()
61
+ break
62
+ if len(text) > max_len:
63
+ text = text[: max_len - 1].rstrip() + "…"
64
+ return text
65
+
66
  def on_step_start(self, step_number: int, step_title: str, total_steps: int) -> None:
67
  self._total_steps = max(total_steps, 1)
68
  fraction = max(0.0, (step_number - 1) / self._total_steps)
69
+ short = self._short_title(step_title)
70
+ # Single concise status line — do NOT repeat the full prompt in Processing
71
+ self._update(
72
+ progress=self.base_progress + (self.progress_span * fraction),
73
+ status=f"🤖 **AI Step {step_number}/{total_steps}:** {short}",
74
+ working=f"Step {step_number} of {total_steps} — generating analysis…",
75
  )
76
 
77
  def on_stream_chunk(self, step_number: int, chars_received: int) -> None:
 
81
  self._last_flush = now
82
  fraction = min(0.99, ((step_number - 1) + min(chars_received / 4000.0, 0.95)) / self._total_steps)
83
  progress = int(self.base_progress + (self.progress_span * fraction))
84
+ stream_msg = f"Receiving AI output… {chars_received:,} characters"
85
  self._update(
86
  progress=progress,
87
  status=f"🤖 **AI Step {step_number}/{self._total_steps}:** {stream_msg}",
 
91
 
92
  def on_step_complete(self, step_number: int) -> None:
93
  fraction = min(1.0, step_number / self._total_steps)
94
+ self._update(
95
+ progress=self.base_progress + (self.progress_span * fraction),
96
+ status=f"✅ **AI Step {step_number}/{self._total_steps}** complete",
97
+ working=f"Step {step_number} finished",
98
  )
99
 
100
  def _update(
 
109
  self.progress_bar.progress(min(max(int(progress), 0), 100))
110
  if self.status_text is not None:
111
  self.status_text.markdown(status)
112
+ # Avoid duplicating the same long status under Processing
113
  if self.working_indicator is not None and working:
114
+ if working.strip() != status.replace("**", "").strip():
115
+ self.working_indicator.markdown(f"🔄 **Processing:** {working}")
116
+ else:
117
+ self.working_indicator.markdown("🔄 **Processing:** analysis in progress…")
118
  if self.stream_indicator is not None and stream:
119
  self.stream_indicator.caption(stream)
120
  if self.time_estimate is not None:
121
  self.time_estimate.markdown(
122
+ "⏱️ **Keep this page open** during analysis (typically 5–8 minutes)."
123
  )
124
  if self.step_indicator is not None:
125
  self.step_indicator.markdown("📋 AI analysis in progress")