Neom99 commited on
Commit
bd73b8d
·
verified ·
1 Parent(s): 7e17f8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +228 -140
app.py CHANGED
@@ -83,6 +83,11 @@ global_analysis_df_for_export = pd.DataFrame()
83
  global_raw_markdown_for_export = ""
84
  global_html_report_for_export = "" # New global variable to store the full HTML report for PDF conversion
85
 
 
 
 
 
 
86
  # --- Custom Document Structure ---
87
  # Documents are dictionaries: {"page_content": "...", "metadata": {...}}
88
 
@@ -283,7 +288,7 @@ def generate_summary_html_by_category(df):
283
 
284
  summary_html = ""
285
  # Ensure all required columns exist, fill with N/A if missing
286
- required_cols = ['Issue Description (Summary)', 'Sub-Category', 'Root Cause (Inferred)',
287
  'Potential Resolution Steps', 'Future Prediction', 'Proactive Measures', 'Count']
288
  for col in required_cols:
289
  if col not in df.columns:
@@ -318,9 +323,9 @@ def generate_summary_html_by_category(df):
318
  sub_category_summary += "</ul>" if 'Sub-Category' in category_df.columns and not sub_cat_counts.empty else "<p>No sub-categories identified.</p>"
319
 
320
 
321
- # --- Root Cause (Inferred) ---
322
  root_causes_list = []
323
- for rc_text in category_df['Root Cause (Inferred)'].astype(str).drop_duplicates().tolist():
324
  lines = re.split(r'\s*[\*\-]\s*|\s*\n\s*', rc_text)
325
  for line in lines:
326
  line = line.strip()
@@ -380,8 +385,7 @@ def generate_summary_html_by_category(df):
380
  <p style="font-weight: bold; color: #333; margin-bottom: 5px;">Sub-Categories:</p>
381
  {sub_category_summary}
382
 
383
- <p style="font-weight: bold; color: #333; margin-bottom: 5px;">Root Cause:</p>
384
- {root_causes}
385
 
386
  <p style="font-weight: bold; color: #333; margin-bottom: 5px;">Resolution Steps:</p>
387
  {resolution_steps}
@@ -489,7 +493,10 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
489
  """
490
  global global_analysis_df_for_export
491
  global global_raw_markdown_for_export
492
- global global_html_report_for_export # Use new global variable
 
 
 
493
 
494
  start_time = time.time()
495
  error_message_markdown = ""
@@ -500,66 +507,122 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
500
  display_time = format_time_for_display(now_utc, selected_timezone)
501
 
502
  def append_log_message(message):
503
- timestamp = datetime.now().strftime("%H:%M:%S")
504
  log_messages.append(f"[{timestamp}] {message}")
505
- yield gr.update(value=None), "\n".join(log_messages)
506
-
507
- # Initial yield to clear previous outputs and show empty log
508
- yield "", ""
509
- yield from append_log_message("Starting analysis...")
 
 
 
510
  progress(0, desc="Starting analysis...")
 
 
511
 
512
  if not rag_initialized:
513
  error_message_markdown = "RAG system not initialized. Please ensure your GOOGLE_API_KEY is configured correctly and the specified AI model is accessible."
514
- yield from append_log_message(f"Error: {error_message_markdown}")
515
- yield f"<p style='color:#CC3300;'>{error_message_markdown}</p>", "\n".join(log_messages)
516
- return
517
-
518
  if gemini_llm_model is None:
519
  error_message_markdown = "Google Generative AI LLM was not initialized. Please check startup logs for API key or model access issues."
520
- yield from append_log_message(f"Error: {error_message_markdown}")
521
- yield f"<p style='color:#CC3300;'>{error_message_markdown}</p>", "\n".join(log_messages)
522
- return
523
 
524
  if file is None:
525
- error_message_markdown = "Please upload a customer ticket file."
526
- yield from append_log_message(f"Error: {error_message_markdown}")
527
- yield f"<p style='color:#CC3300;'>{error_message_markdown}</p>", "\n".join(log_messages)
528
- return
529
-
530
  file_path = file.name
531
  filename = os.path.basename(file_path)
532
  file_extension = os.path.splitext(filename)[1].lower()
 
 
 
 
 
 
 
 
533
 
534
- disclaimer_text_for_raw = "\n\n**Note:** 'Root Cause', 'Future Prediction', and 'Proactive Measures' are AI-inferred based on descriptions provided. For critical decisions, human expert review is essential."
 
535
 
536
  try:
537
  # --- PHASE 1: Initial Categorization of ALL Tickets ---
538
- yield from append_log_message("Phase 1/2: Initializing categorization...")
539
  progress(0.1, desc="Phase 1/2: Initializing categorization...")
 
540
 
541
  # Using a generator for `get_ticket_documents_iterator` for memory efficiency
542
  ticket_iterator = get_ticket_documents_iterator(file_path, file_extension, filename, chunksize=5000)
543
 
544
  all_categorized_tickets_data = [] # To accumulate all categorized ticket dictionaries
545
- total_tickets_count = 0
 
 
 
 
 
 
 
 
 
 
 
 
 
546
 
547
- for i, (chunk_documents, current_total_processed) in enumerate(ticket_iterator):
548
- total_tickets_count = current_total_processed
549
- # Estimate total chunks for progress bar, assuming average chunk size
550
- # This is a rough estimate and might not be perfectly linear
551
- estimated_total_chunks = max(1, (total_tickets_count // 5000) + (1 if total_tickets_count % 5000 > 0 else 0))
 
 
 
552
 
553
- progress_val = 0.1 + (0.4 * (i+1) / estimated_total_chunks)
554
- progress_desc = f"Phase 1/2: Categorizing tickets... {total_tickets_count} processed so far."
555
- yield from append_log_message(progress_desc)
556
- progress(progress_val, desc=progress_desc)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557
 
558
  if not chunk_documents:
559
  continue
560
 
561
  categorization_context_parts = []
562
- ticket_id_map = {} # Map ticket ID back to original content for later retrieval
563
  for doc in chunk_documents:
564
  ticket_id_for_prompt = str(doc["metadata"].get("ticket_id", f"Row {doc['metadata']['ticket_row_number']}"))
565
  categorization_context_parts.append(f"--- Ticket ID: {ticket_id_for_prompt} ---\n{doc['page_content']}\n")
@@ -586,10 +649,10 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
586
  """
587
 
588
  try:
589
- yield from append_log_message(f"Calling LLM for categorization of {len(chunk_documents)} tickets...")
590
  categorization_response = gemini_llm_model.generate_content(categorization_prompt)
591
  categorization_raw_output = categorization_response.text
592
- yield from append_log_message("LLM categorization response received. Parsing...")
593
 
594
  for line in categorization_raw_output.split('\n'):
595
  line = line.strip()
@@ -607,8 +670,8 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
607
  })
608
  elif line:
609
  # Fallback for malformed lines: try to extract best effort
610
- print(f"Warning: Line did not fully match categorization format in chunk {i}: {line}", file=sys.stderr)
611
- yield from append_log_message(f"Warning: Malformed categorization line in chunk {i}, attempting partial parse: {line}")
612
  parts = line.split("Category:")
613
  ticket_id_part = parts[0].replace("Ticket ID:", "").strip() if len(parts) > 0 else f"Unparsed_Line_{len(all_categorized_tickets_data)+1}"
614
  category_part = parts[1].split("Issue Summary:")[0].strip() if len(parts) > 1 and "Issue Summary:" in parts[1] else "Uncategorized/Parsing Error"
@@ -622,8 +685,8 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
622
  })
623
 
624
  except Exception as e:
625
- yield from append_log_message(f"Error during categorization of chunk {i}: {e}")
626
- print(f"Error during categorization of chunk {i}: {e}", file=sys.stderr)
627
  # If an error occurs during API call for a chunk, categorize its tickets as error
628
  for doc in chunk_documents:
629
  ticket_id = str(doc["metadata"].get("ticket_id", f"Row {doc['metadata']['ticket_row_number']}"))
@@ -637,25 +700,28 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
637
  final_categorized_df = pd.DataFrame(all_categorized_tickets_data)
638
  if final_categorized_df.empty:
639
  error_message_markdown = "AI failed to categorize any tickets. Please ensure the ticket data is clear or increase AI model's tolerance."
640
- yield from append_log_message(f"Error: {error_message_markdown}")
641
- yield f"<p style='color:#CC3300;'>{error_message_markdown}</p>", "\n".join(log_messages)
642
  return
643
 
644
  # --- PHASE 2: Detailed Analysis per Category ---
645
- yield from append_log_message("Phase 2/2: Detailing analysis for categories...")
646
  progress(0.5, desc=f"Phase 2/2: Detailing analysis for categories...")
 
647
 
648
  final_analysis_rows = []
649
  unique_categories = final_categorized_df['category'].unique()
650
- yield from append_log_message(f"Identified {len(unique_categories)} unique categories for detailed analysis.")
651
 
652
  category_batch_size = 5 # Process categories in small batches if many unique categories exist
653
 
654
- for i, category_chunk in enumerate(np.array_split(unique_categories, max(1, len(unique_categories) // category_batch_size))):
655
- progress_val = (0.5 + (0.45 * i / max(1, len(np.array_split(unique_categories, max(1, len(unique_categories) // category_batch_size))))))
656
- progress_desc = f"Phase 2/2: Detailing analysis for category batch {i+1}..."
657
- yield from append_log_message(progress_desc)
658
- progress(progress_val, desc=progress_desc)
 
 
 
659
 
660
  for category in category_chunk:
661
  category_specific_tickets_df = final_categorized_df[final_categorized_df['category'] == category]
@@ -663,7 +729,7 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
663
  category_tickets_content = "\n\n".join(category_specific_tickets_df['original_content'].tolist())
664
 
665
  category_count = len(category_specific_tickets_df)
666
- yield from append_log_message(f"Analyzing category: '{category}' with {category_count} tickets.")
667
 
668
  # --- START: OPTIMIZED PROMPT FOR DETAILED ANALYSIS ---
669
  detail_prompt = f"""
@@ -673,7 +739,7 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
673
 
674
  **Crucial Instructions:**
675
  1. **Strict Output Format:** Your output MUST be a **single Markdown table row**. Do NOT include the header row, only the data row.
676
- **Table Columns (MUST be in this exact order):** `Category`, `Sub-Category`, `Count`, `Issue Description (Summary)`, `Root Cause (Inferred)`, `Potential Resolution Steps`, `Future Prediction`, `Proactive Measures`.
677
  2. **Category:** This MUST be '{category}'.
678
  3. **Count:** This MUST be the **exact number of tickets provided for this category ({category_count})**.
679
  4. **Issue Description (Summary):**
@@ -685,8 +751,7 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
685
  * Based on internal patterns, identify one or two **most prominent, granular sub-categories**.
686
  * If no clear sub-category emerges, state 'General {category}' or 'N/A'.
687
  * Examples: For 'Technical Issue' -> 'Login Failure', 'Software Bug'; for 'Billing Inquiry' -> 'Incorrect Charge', 'Subscription Error'.
688
- 6. **Root Cause (Inferred):**
689
- * Perform an **in-depth inference** of the **underlying systemic or causal factors** leading to these issues. Do not just restate the symptom.
690
  * Present each distinct root cause as a new bullet point (e.g., "* Root cause 1\\n* Root cause 2").
691
  * Think about why the problem exists. (e.g., "* System misconfiguration", "* User error due to lack of documentation", "* Third-party integration failure", "* Defect in recent software update").
692
  * If truly undetermined from the data, state 'Undetermined from available data'.
@@ -709,14 +774,14 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
709
  # --- END: OPTIMIZED PROMPT FOR DETAILED ANALYSIS ---
710
 
711
  try:
712
- yield from append_log_message(f"Calling LLM for detailed analysis of category '{category}'...")
713
  detail_response = gemini_llm_model.generate_content(detail_prompt)
714
  detail_raw_output = detail_response.text
715
- yield from append_log_message(f"LLM detailed response for '{category}' received. Parsing...")
716
 
717
  detailed_df, parsing_err = parse_markdown_table_to_df(detail_raw_output)
718
  if parsing_err or detailed_df.empty:
719
- yield from append_log_message(f"Warning: Parsing error or empty DataFrame for category '{category}': {parsing_err or 'Empty DataFrame'}")
720
  print(f"Warning: Parsing error or empty DataFrame for category '{category}': {parsing_err or 'Empty DataFrame'}", file=sys.stderr)
721
  print(f"Raw AI output for detail: {detail_raw_output}", file=sys.stderr)
722
  # Fallback row if parsing fails
@@ -725,13 +790,13 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
725
  "Sub-Category": "N/A",
726
  "Count": category_count,
727
  "Issue Description (Summary)": category_specific_tickets_df['issue_summary_short'].iloc[0] if not category_specific_tickets_df.empty else "Error in summary generation/parsing for this category.",
728
- "Root Cause (Inferred)": "Undetermined due to parsing error or insufficient data.",
729
  "Potential Resolution Steps": "N/A",
730
  "Future Prediction": "N/A",
731
  "Proactive Measures": "N/A"
732
  }
733
  # Add a check to ensure all expected columns are present, add if missing
734
- for col in ['Sub-Category', 'Root Cause (Inferred)', 'Potential Resolution Steps', 'Future Prediction', 'Proactive Measures']:
735
  if col not in default_row:
736
  default_row[col] = 'N/A'
737
  final_analysis_rows.append(default_row)
@@ -745,7 +810,7 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
745
  try:
746
  ai_reported_count = pd.to_numeric(detailed_row['Count'], errors='coerce')
747
  if pd.isna(ai_reported_count) or int(ai_reported_count) != category_count:
748
- yield from append_log_message(f"Warning: AI-reported count {ai_reported_count} for '{category}' does not match actual count {category_count}. Correcting.")
749
  print(f"Warning: AI-reported count {ai_reported_count} for '{category}' does not match actual count {category_count}. Correcting.", file=sys.stderr)
750
  detailed_row['Count'] = category_count
751
  except: # Catch any other conversion errors
@@ -754,13 +819,13 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
754
  detailed_row['Count'] = category_count
755
 
756
  # Ensure all required columns are present in the parsed row
757
- for col in ['Sub-Category', 'Root Cause (Inferred)', 'Potential Resolution Steps', 'Future Prediction', 'Proactive Measures']:
758
  if col not in detailed_row:
759
  detailed_row[col] = 'N/A' # Default to N/A if LLM missed a column
760
  final_analysis_rows.append(detailed_row)
761
 
762
  except Exception as e:
763
- yield from append_log_message(f"Error detailing analysis for category '{category}': {e}")
764
  print(f"Error detailing analysis for category '{category}': {e}", file=sys.stderr)
765
  # Fallback row if API call fails for a category
766
  default_row = {
@@ -768,7 +833,7 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
768
  "Sub-Category": "N/A",
769
  "Count": category_count,
770
  "Issue Description (Summary)": f"Error during detailed analysis for this category: {e}",
771
- "Root Cause (Inferred)": "Undetermined due to error.",
772
  "Potential Resolution Steps": "N/A",
773
  "Future Prediction": "N/A",
774
  "Proactive Measures": "N/A"
@@ -781,48 +846,47 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
781
  analysis_df['Count'] = pd.to_numeric(analysis_df['Count'], errors='coerce').fillna(0).astype(int)
782
 
783
  summarized_count = analysis_df['Count'].sum()
784
- if summarized_count != total_tickets_count:
785
- yield from append_log_message(f"CRITICAL WARNING: Total count in summarized DF ({summarized_count}) does not match original tickets ({total_tickets_count}). This might indicate a parsing or AI categorization issue.")
786
- print(f"CRITICAL WARNING: Total count in summarized DF ({summarized_count}) does not match original tickets ({total_tickets_count}). This might indicate a parsing or AI categorization issue.", file=sys.stderr)
787
 
788
  global_analysis_df_for_export = analysis_df.copy()
789
  global_raw_markdown_for_export = analysis_df.to_markdown(index=False) + disclaimer_text_for_raw
790
 
791
  except ValueError as e:
792
  error_message_markdown = f"Ticket Analysis Error: {e}"
793
- yield from append_log_message(f"Error: {error_message_markdown}")
794
  global_raw_markdown_for_export = error_message_markdown + disclaimer_text_for_raw
795
  global_analysis_df_for_export = pd.DataFrame() # Clear dataframe on error
796
- yield f"<p style='color:#CC3300;'>{error_message_markdown}</p>", "\n".join(log_messages)
797
  return
798
  except Exception as e:
799
  error_message_markdown = f"An unexpected error occurred during analysis: {e}. Check server logs for details."
800
- yield from append_log_message(f"Error: {error_message_markdown}")
801
  print(f"Error during RAG analysis: {e}", file=sys.stderr)
802
- global_raw_markdown_for_export = error_message_markdown + disclaimer_text_for_raw
 
803
  global_analysis_df_for_export = pd.DataFrame() # Clear dataframe on error
804
- yield f"<p style='color:#CC3300;'>{error_message_markdown}</p>", "\n".join(log_messages)
805
  return
806
 
807
- yield from append_log_message("Generating summary report for display...")
808
  progress(0.98, desc="Generating summary report for display...")
 
809
 
810
  summarized_analysis_html = ""
811
  try:
812
  if not analysis_df.empty:
813
  summarized_analysis_html = generate_summary_html_by_category(analysis_df)
814
- yield from append_log_message("HTML summary generated successfully.")
815
  else:
816
  summarized_analysis_html = "<p style='color:#CC3300;'>AI output parsed into an empty DataFrame or no categories were generated. Please check the uploaded file and try again.</p>"
817
- yield from append_log_message("Warning: No categories generated, HTML summary might be empty.")
818
 
819
  except Exception as e:
820
- yield from append_log_message(f"Failed to generate HTML summary from DataFrame: {e}")
821
  print(f"Failed to generate HTML summary from DataFrame: {e}", file=sys.stderr)
822
  summarized_analysis_html = f"""
823
  <div style="margin-bottom: 20px;">
824
  <p style="color: #CC3300;'>An unexpected error occurred while attempting to transform AI output into a visual summary: {e}</p>
825
- <p style="color: #CC3300;'>Raw AI output (if available):</p>
826
  <pre style="background-color: #f0f0f0; padding: 15px; border: 1px solid #ddd; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;">{global_raw_markdown_for_export}</pre>
827
  </div>
828
  """
@@ -940,9 +1004,9 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
940
  This report presents a comprehensive analysis of recent customer support tickets, leveraging advanced AI to identify key trends, underlying root causes, and actionable recommendations.
941
  </p>
942
  <p>
943
- <strong style="color: #283593;">Total Tickets Processed:</strong> {total_tickets_count}<br>
944
  <strong style="color: #283593;">Total Categories Identified:</strong> {len(unique_categories) if 'unique_categories' in locals() else 0}
945
- {'<p class="warning-text">WARNING: Summarized ticket count ({summarized_count}) does not match original total ({total_tickets_count}). Data parsing or AI categorization may have occurred.</p>' if summarized_count != total_tickets_count else ''}
946
  </p>
947
  </div>
948
 
@@ -967,10 +1031,11 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
967
 
968
  global_html_report_for_export = full_html_report # Store the full HTML content
969
 
970
- yield from append_log_message("Analysis complete. Report generated.")
971
  progress(1, desc="Analysis complete. Report generated.")
 
972
 
973
- yield full_html_report, "\n".join(log_messages)
974
 
975
 
976
  # --- Output Download Functions (using global_html_report_for_export) ---
@@ -980,16 +1045,19 @@ def generate_pdf_report(selected_timezone):
980
  # Use the pre-generated HTML report
981
  html_content = global_html_report_for_export
982
 
983
- if not html_content:
984
  return gr.update(value=None, visible=False)
985
 
986
  try:
987
  html_obj = HTML(string=html_content)
988
- with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
989
- html_obj.write_pdf(tmp_file.name)
990
- temp_file_path = tmp_file.name
991
 
992
- return gr.update(value=temp_file_path, visible=True, label="Download PDF Report")
 
 
 
 
 
 
993
 
994
  except Exception as e:
995
  print(f"Error generating PDF with WeasyPrint: {e}", file=sys.stderr)
@@ -998,7 +1066,7 @@ def generate_pdf_report(selected_timezone):
998
 
999
  def generate_docx_report(selected_timezone):
1000
  df = global_analysis_df_for_export
1001
- if df.empty:
1002
  return gr.update(value=None, visible=False)
1003
 
1004
  doc = DocxDocument()
@@ -1026,12 +1094,11 @@ def generate_docx_report(selected_timezone):
1026
  total_tickets_processed = df['Count'].sum() if 'Count' in df.columns else 0
1027
  total_categories_identified = len(df['Category'].unique()) if 'Category' in df.columns else 0
1028
 
1029
- doc.add_paragraph(f"Total Tickets Processed: {total_tickets_processed}")
1030
  doc.add_paragraph(f"Total Categories Identified: {total_categories_identified}")
1031
 
1032
  # Add warning if counts don't match
1033
- summarized_count = df['Count'].sum() if 'Count' in df.columns else 0 # Recalculate or use global
1034
- if global_analysis_df_for_export['Count'].sum() != total_tickets_processed: # Use global total if available and not matching
1035
  warning_p = doc.add_paragraph("WARNING: Summarized ticket count does not match original total. Data parsing or AI categorization may have occurred.")
1036
  warning_p.runs[0].font.color.rgb = RGBColor(0xFF, 0x00, 0x00) # Red color for warning
1037
  warning_p.runs[0].bold = True
@@ -1061,9 +1128,9 @@ def generate_docx_report(selected_timezone):
1061
  issue_descriptions_list.append(line)
1062
  if issue_descriptions_list:
1063
  for item in issue_descriptions_list:
1064
- doc.add_paragraph(item, style='List Bullet') # FIX: Removed "• "
1065
  else:
1066
- doc.add_paragraph("No specific summary available.", style='List Bullet') # FIX: Removed "• "
1067
  doc.add_paragraph("")
1068
 
1069
  # Sub-Categories
@@ -1073,16 +1140,16 @@ def generate_docx_report(selected_timezone):
1073
  sub_cat_counts = category_df.groupby('Sub-Category')['Count'].sum().sort_values(ascending=False)
1074
  for sub_cat, count in sub_cat_counts.items():
1075
  if str(sub_cat).strip() and str(sub_cat).strip().lower() != 'n/a':
1076
- doc.add_paragraph(f"{sub_cat} (Count: {count})", style='List Bullet') # FIX: Removed "• "
1077
  sub_cat_found = True
1078
  if not sub_cat_found:
1079
- doc.add_paragraph("No sub-categories identified.", style='List Bullet') # FIX: Removed "• "
1080
  doc.add_paragraph("")
1081
 
1082
- # Root Cause
1083
- doc.add_paragraph("Root Cause:").runs[0].bold = True
1084
  root_causes_list = []
1085
- for rc_text in category_df['Root Cause (Inferred)'].astype(str).drop_duplicates().tolist():
1086
  lines = re.split(r'\s*[\*\-]\s*|\s*\n\s*', rc_text)
1087
  for line in lines:
1088
  line = line.strip()
@@ -1090,9 +1157,9 @@ def generate_docx_report(selected_timezone):
1090
  root_causes_list.append(line)
1091
  if root_causes_list:
1092
  for item in root_causes_list:
1093
- doc.add_paragraph(item, style='List Bullet') # FIX: Removed "• "
1094
  else:
1095
- doc.add_paragraph("Undetermined.", style='List Bullet') # FIX: Removed "• "
1096
  doc.add_paragraph("")
1097
 
1098
  # Resolution Steps
@@ -1106,9 +1173,9 @@ def generate_docx_report(selected_timezone):
1106
  resolution_steps_list.append(line)
1107
  if resolution_steps_list:
1108
  for item in resolution_steps_list:
1109
- doc.add_paragraph(item, style='List Bullet') # FIX: Removed "• "
1110
  else:
1111
- doc.add_paragraph("No specific steps identified.", style='List Bullet') # FIX: Removed "• "
1112
  doc.add_paragraph("")
1113
 
1114
  # Insights & Recommendations section
@@ -1125,9 +1192,9 @@ def generate_docx_report(selected_timezone):
1125
  future_prediction_items.append(line)
1126
  if future_prediction_items:
1127
  for item in future_prediction_items:
1128
- doc.add_paragraph(item, style='List Bullet') # FIX: Removed "• "
1129
  else:
1130
- doc.add_paragraph("No future predictions identified.", style='List Bullet') # FIX: Removed "• "
1131
  doc.add_paragraph("")
1132
 
1133
  # Proactive Measures
@@ -1141,9 +1208,9 @@ def generate_docx_report(selected_timezone):
1141
  proactive_measures_items.append(line)
1142
  if proactive_measures_items:
1143
  for item in proactive_measures_items:
1144
- doc.add_paragraph(item, style='List Bullet') # FIX: Removed "• "
1145
  else:
1146
- doc.add_paragraph("No proactive measures identified.", style='List Bullet') # FIX: Removed "• "
1147
  doc.add_paragraph("")
1148
 
1149
 
@@ -1153,11 +1220,13 @@ def generate_docx_report(selected_timezone):
1153
  )
1154
  doc.add_paragraph(f"Report Generation Timestamp: {current_gen_time}")
1155
 
1156
- with tempfile.NamedTemporaryFile(delete=False, suffix=".docx") as tmp_file:
1157
- doc.save(tmp_file)
1158
- temp_file_path = tmp_file.name
 
 
1159
 
1160
- return gr.update(value=temp_file_path, visible=True, label="Download Word Report")
1161
 
1162
 
1163
  # --- Gradio Interface Setup ---
@@ -1178,33 +1247,42 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
1178
  color: #333;
1179
  }
1180
 
 
 
 
 
 
 
 
 
 
1181
  /* Headings for a consistent, professional look */
1182
  h1 {
1183
  color: #1A237E; /* Deep Indigo */
1184
  text-align: center;
1185
- margin-bottom: 25px;
1186
  font-size: 2.5em; /* Larger main title */
1187
  font-weight: 600; /* Slightly bolder */
1188
  }
1189
  h2 {
1190
  color: #283593; /* Indigo */
1191
- margin-top: 30px;
1192
- padding-bottom: 8px;
1193
  border-bottom: 1px solid #E0E0E0; /* Subtle separator */
1194
  font-size: 1.8em;
1195
  font-weight: 500;
1196
  }
1197
  h3 {
1198
  color: #1A5276; /* Darker Blue-Green */
1199
- margin-top: 15px; /* Reduced space */
1200
- margin-bottom: 8px; /* Reduced space */
1201
  font-size: 1.3em;
1202
  font-weight: 600;
1203
  }
1204
  h4 { /* Added h4 styling */
1205
  color: #3F51B5; /* Medium Indigo */
1206
- margin-top: 12px; /* Reduced space */
1207
- margin-bottom: 6px; /* Reduced space */
1208
  font-size: 1.1em;
1209
  font-weight: 600;
1210
  }
@@ -1212,18 +1290,18 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
1212
 
1213
  /* Paragraphs and list items */
1214
  p {
1215
- line-height: 1.6;
1216
- margin-bottom: 10px; /* Consistent paragraph spacing */
1217
  }
1218
  ul {
1219
  list-style-type: disc; /* Standard disc bullets */
1220
  margin-left: 25px; /* Indent lists properly */
1221
- margin-top: 8px;
1222
- margin-bottom: 8px;
1223
- line-height: 1.5;
1224
  }
1225
  li {
1226
- margin-bottom: 4px; /* Spacing between list items */
1227
  padding-left: 2px; /* Small padding for visual comfort */
1228
  }
1229
  strong {
@@ -1234,9 +1312,9 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
1234
  /* Specific section styling for minimalist appeal */
1235
  .overview-section {
1236
  background-color: #E8EAF6; /* Lightest Indigo background */
1237
- padding: 20px;
1238
  border-radius: 6px;
1239
- margin-bottom: 25px;
1240
  border: 1px solid #C5CAE9; /* Subtle border */
1241
  }
1242
  .category-block {
@@ -1251,7 +1329,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
1251
  background-color: #f5f5f5; /* Light gray for disclaimers */
1252
  padding: 15px;
1253
  border-radius: 6px;
1254
- margin-top: 30px;
1255
  border: 1px solid #e0e0e0;
1256
  font-size: 0.85em;
1257
  color: #555;
@@ -1318,30 +1396,37 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
1318
  }
1319
  /* Specific hr styling for Markdown content separators */
1320
  .gradio-container hr { /* Target hr within the gradio container, particularly for Markdown */
1321
- margin-top: 15px; /* Reduced space */
1322
- margin-bottom: 15px; /* Reduced space */
1323
  border: 0;
1324
  height: 1px;
1325
  background-color: #E0E0E0;
1326
  }
 
 
 
 
1327
  """) as demo:
1328
  gr.HTML("""
1329
  <h1 align="center">Advanced AI Customer Ticket Analyst</h1>
1330
- <p align="center" style="font-size: 1.1em; color: #555;">Derive actionable insights from your customer support data.</p>
1331
  """)
1332
 
1333
  gr.Markdown(
1334
  """
1335
  ### System Overview
1336
  This tool provides structured insights from customer support tickets. Upload your data to receive a comprehensive analysis including categorization, root causes, strategic recommendations, and future outlooks.
 
 
1337
 
1338
- ---
1339
-
1340
  #### Important Note on AI Inferences:
1341
  AI-generated insights, including inferred root causes, future predictions, and proactive measures, are based on the provided ticket data. These analyses are intended for guidance and require human expert validation for critical business decisions.
1342
-
1343
- ---
1344
-
 
1345
  #### Input File Requirements:
1346
  * **Supported Formats:** `.xlsx` (Excel), `.xls` (Legacy Excel), and `.csv` (Comma Separated Values).
1347
  * **Data Structure:** Files must contain tabular data with clear headers in the first row. Each row represents an individual customer ticket.
@@ -1352,18 +1437,20 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
1352
  TKT001,User cannot log in; password reset not working.
1353
  TKT002,Billing error: duplicate charge for last month's service.
1354
  ```
1355
- * **Recommended Columns:** For richer analysis, include columns such as `Status`, `Priority`, `CustomerName`, and `Timestamp`. More relevant details enhance AI inferences.
1356
  * **Data Volume:** For large files (e.g., over 60,000 tickets), processing occurs in batches to optimize memory and API usage. Processing times may vary.
1357
  * **Data Privacy:** Please do not upload highly sensitive or confidential customer information. This is a public demonstration environment.
 
 
1358
 
1359
- ---
1360
-
1361
  #### Output Structure:
1362
  The AI will generate a clear, data-driven report based solely on your uploaded file. The visual output will feature:
1363
  * **Category Name (Total Count for Category)**
1364
  * **Summary:** Key issue descriptions and common themes.
1365
  * **Sub-Categories:** More granular sub-categories and their counts.
1366
- * **Root Cause:** AI-inferred underlying reasons for issues.
1367
  * **Resolution Steps:** Strategic actions to resolve issues.
1368
  * **Insights & Recommendations:**
1369
  * **Future Prediction:** Predictions if underlying issues persist.
@@ -1401,7 +1488,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
1401
  interactive=False,
1402
  lines=5,
1403
  autoscroll=True,
1404
- elem_id="progress-log-box"
 
1405
  )
1406
 
1407
  output_html_display = gr.HTML(
@@ -1416,7 +1504,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css="""
1416
 
1417
 
1418
  gr.HTML("""
1419
- <hr style="margin-top: 30px; margin-bottom: 20px; border-color: #E0E0E0;">
1420
  <p align="center" style="font-size: 0.8em; color: #777;">
1421
  *All AI-generated insights are for informational purposes and should be verified by human experts.*
1422
  </p>
 
83
  global_raw_markdown_for_export = ""
84
  global_html_report_for_export = "" # New global variable to store the full HTML report for PDF conversion
85
 
86
+ # Global variables for filename versioning
87
+ _last_uploaded_filename_base = None
88
+ _file_versions = {}
89
+ _original_total_tickets_count = 0 # New global variable to store the total ticket count from the original file
90
+
91
  # --- Custom Document Structure ---
92
  # Documents are dictionaries: {"page_content": "...", "metadata": {...}}
93
 
 
288
 
289
  summary_html = ""
290
  # Ensure all required columns exist, fill with N/A if missing
291
+ required_cols = ['Issue Description (Summary)', 'Sub-Category', 'Potential Root Cause (Inferred)', # Changed to Potential Root Cause
292
  'Potential Resolution Steps', 'Future Prediction', 'Proactive Measures', 'Count']
293
  for col in required_cols:
294
  if col not in df.columns:
 
323
  sub_category_summary += "</ul>" if 'Sub-Category' in category_df.columns and not sub_cat_counts.empty else "<p>No sub-categories identified.</p>"
324
 
325
 
326
+ # --- Potential Root Cause (Inferred) --- Changed to Potential Root Cause
327
  root_causes_list = []
328
+ for rc_text in category_df['Potential Root Cause (Inferred)'].astype(str).drop_duplicates().tolist(): # Changed to Potential Root Cause
329
  lines = re.split(r'\s*[\*\-]\s*|\s*\n\s*', rc_text)
330
  for line in lines:
331
  line = line.strip()
 
385
  <p style="font-weight: bold; color: #333; margin-bottom: 5px;">Sub-Categories:</p>
386
  {sub_category_summary}
387
 
388
+ <p style="font-weight: bold; color: #333; margin-bottom: 5px;">Potential Root Cause:</p> {root_causes}
 
389
 
390
  <p style="font-weight: bold; color: #333; margin-bottom: 5px;">Resolution Steps:</p>
391
  {resolution_steps}
 
493
  """
494
  global global_analysis_df_for_export
495
  global global_raw_markdown_for_export
496
+ global global_html_report_for_export
497
+ global _last_uploaded_filename_base
498
+ global _file_versions
499
+ global _original_total_tickets_count # Use the new global variable
500
 
501
  start_time = time.time()
502
  error_message_markdown = ""
 
507
  display_time = format_time_for_display(now_utc, selected_timezone)
508
 
509
  def append_log_message(message):
510
+ timestamp = datetime.now().strftime("%H:%M:%S")
511
  log_messages.append(f"[{timestamp}] {message}")
512
+ # When logging, we don't want to change the HTML output yet, so we yield an empty update.
513
+ # This ensures both outputs are always returned.
514
+ return gr.update(value="", visible=False), gr.update(value="\n".join(log_messages)) # Return HTML and Textbox updates
515
+
516
+ # Initial yield to clear previous outputs, make log visible, and update progress bar to 0
517
+ # Corrected: yield directly instead of yield from for the first log
518
+ yield gr.update(value="", visible=False), gr.update(value="\n".join(log_messages), visible=True) # Hide HTML output, show log
519
+ yield append_log_message("Starting analysis...") # Corrected: yield directly
520
  progress(0, desc="Starting analysis...")
521
+ time.sleep(0.01) # Small sleep for progress bar to render
522
+
523
 
524
  if not rag_initialized:
525
  error_message_markdown = "RAG system not initialized. Please ensure your GOOGLE_API_KEY is configured correctly and the specified AI model is accessible."
526
+ yield gr.update(value=f"<p style='color:#CC3300;'>{error_message_markdown}</p>", visible=True), gr.update(value="\n".join(log_messages))
527
+ return
528
+
 
529
  if gemini_llm_model is None:
530
  error_message_markdown = "Google Generative AI LLM was not initialized. Please check startup logs for API key or model access issues."
531
+ yield gr.update(value=f"<p style='color:#CC3300;'>{error_message_markdown}</p>", visible=True), gr.update(value="\n".join(log_messages))
532
+ return
 
533
 
534
  if file is None:
535
+ error_message_markdown = "No file has been uploaded. Please upload a customer ticket file to proceed."
536
+ yield gr.update(value=f"<p style='color:#CC3300;'>{error_message_markdown}</p>", visible=True), gr.update(value="\n".join(log_messages))
537
+ return
538
+
 
539
  file_path = file.name
540
  filename = os.path.basename(file_path)
541
  file_extension = os.path.splitext(filename)[1].lower()
542
+ base_filename = os.path.splitext(filename)[0]
543
+
544
+ # Update versioning
545
+ if _last_uploaded_filename_base != base_filename:
546
+ _last_uploaded_filename_base = base_filename
547
+ _file_versions[_last_uploaded_filename_base] = 1
548
+ else:
549
+ _file_versions[_last_uploaded_filename_base] = _file_versions.get(_last_uploaded_filename_base, 0) + 1
550
 
551
+
552
+ disclaimer_text_for_raw = "\n\n**Note:** 'Potential Root Cause', 'Future Prediction', and 'Proactive Measures' are AI-inferred based on descriptions provided. For critical decisions, human expert review is essential."
553
 
554
  try:
555
  # --- PHASE 1: Initial Categorization of ALL Tickets ---
556
+ yield append_log_message("Phase 1/2: Initializing categorization...") # Corrected
557
  progress(0.1, desc="Phase 1/2: Initializing categorization...")
558
+ time.sleep(0.01) # Small sleep for progress bar to render
559
 
560
  # Using a generator for `get_ticket_documents_iterator` for memory efficiency
561
  ticket_iterator = get_ticket_documents_iterator(file_path, file_extension, filename, chunksize=5000)
562
 
563
  all_categorized_tickets_data = [] # To accumulate all categorized ticket dictionaries
564
+ # total_tickets_count = 0 # This will be updated by the iterator's last yield
565
+
566
+ # Get the full DataFrame once for total rows, then iterate chunks
567
+ df_tickets_full = None
568
+ if file_extension in ['.xlsx', '.xls']:
569
+ df_tickets_full = pd.read_excel(file_path, engine='openpyxl')
570
+ elif file_extension == '.csv':
571
+ df_tickets_full = pd.read_csv(file_path)
572
+
573
+ if df_tickets_full is None or df_tickets_full.empty:
574
+ raise ValueError("No data found in the uploaded file after initial read.")
575
+
576
+ _original_total_tickets_count = len(df_tickets_full) # Store global total
577
+ yield append_log_message(f"Detected {_original_total_tickets_count} total tickets in the file.") # Corrected
578
 
579
+ chunk_size = 5000 # Must match the chunksize in get_ticket_documents_iterator
580
+
581
+ processed_chunks_count = 0
582
+ total_tickets_processed_in_phase1 = 0
583
+
584
+ # Iterate manually through the DataFrame in chunks for consistent processing and progress
585
+ for i in range(0, _original_total_tickets_count, chunk_size):
586
+ df_chunk = df_tickets_full.iloc[i : i + chunk_size]
587
 
588
+ chunk_documents = []
589
+ current_chunk_tickets_count = 0
590
+ for index, row in df_chunk.iterrows():
591
+ ticket_content_parts = []
592
+ ticket_id = f"Row {index + 1}" # Default ticket ID based on original DataFrame index
593
+
594
+ for col_name, value in row.items():
595
+ if pd.notna(value) and str(value).strip() != '':
596
+ formatted_col_name = col_name.replace('_', ' ').title()
597
+ ticket_content_parts.append(f"{formatted_col_name}: {value}")
598
+
599
+ if col_name.lower() in ['ticketid', 'ticket_id', 'id']:
600
+ ticket_id = str(value).strip()
601
+
602
+ if not ticket_content_parts:
603
+ ticket_content = f"Ticket {ticket_id}: No detailed content provided."
604
+ else:
605
+ ticket_content = "\n".join(ticket_content_parts)
606
+
607
+ metadata = {"source": filename, "ticket_row_number": index + 1, "ticket_id": ticket_id}
608
+ metadata.update(row.to_dict()) # Include all original row data in metadata
609
+
610
+ chunk_documents.append({"page_content": ticket_content, "metadata": metadata})
611
+ current_chunk_tickets_count += 1
612
+
613
+ processed_chunks_count += 1
614
+ total_tickets_processed_in_phase1 += current_chunk_tickets_count
615
+
616
+ # Phase 1 progress (0.1 to 0.5)
617
+ progress_val = 0.1 + (0.4 * (total_tickets_processed_in_phase1 / _original_total_tickets_count))
618
+ progress(progress_val, desc=f"Phase 1/2: Categorizing tickets... {total_tickets_processed_in_phase1}/{_original_total_tickets_count} processed.")
619
+ time.sleep(0.01) # Small sleep for progress bar to render
620
 
621
  if not chunk_documents:
622
  continue
623
 
624
  categorization_context_parts = []
625
+ ticket_id_map = {}
626
  for doc in chunk_documents:
627
  ticket_id_for_prompt = str(doc["metadata"].get("ticket_id", f"Row {doc['metadata']['ticket_row_number']}"))
628
  categorization_context_parts.append(f"--- Ticket ID: {ticket_id_for_prompt} ---\n{doc['page_content']}\n")
 
649
  """
650
 
651
  try:
652
+ yield append_log_message(f"Calling LLM for categorization of {len(chunk_documents)} tickets...") # Corrected
653
  categorization_response = gemini_llm_model.generate_content(categorization_prompt)
654
  categorization_raw_output = categorization_response.text
655
+ yield append_log_message("LLM categorization response received. Parsing...") # Corrected
656
 
657
  for line in categorization_raw_output.split('\n'):
658
  line = line.strip()
 
670
  })
671
  elif line:
672
  # Fallback for malformed lines: try to extract best effort
673
+ print(f"Warning: Line did not fully match categorization format in chunk {processed_chunks_count}: {line}", file=sys.stderr)
674
+ yield append_log_message(f"Warning: Malformed categorization line in chunk {processed_chunks_count}, attempting partial parse: {line}") # Corrected
675
  parts = line.split("Category:")
676
  ticket_id_part = parts[0].replace("Ticket ID:", "").strip() if len(parts) > 0 else f"Unparsed_Line_{len(all_categorized_tickets_data)+1}"
677
  category_part = parts[1].split("Issue Summary:")[0].strip() if len(parts) > 1 and "Issue Summary:" in parts[1] else "Uncategorized/Parsing Error"
 
685
  })
686
 
687
  except Exception as e:
688
+ yield append_log_message(f"Error during categorization of chunk {processed_chunks_count}: {e}") # Corrected
689
+ print(f"Error during categorization of chunk {processed_chunks_count}: {e}", file=sys.stderr)
690
  # If an error occurs during API call for a chunk, categorize its tickets as error
691
  for doc in chunk_documents:
692
  ticket_id = str(doc["metadata"].get("ticket_id", f"Row {doc['metadata']['ticket_row_number']}"))
 
700
  final_categorized_df = pd.DataFrame(all_categorized_tickets_data)
701
  if final_categorized_df.empty:
702
  error_message_markdown = "AI failed to categorize any tickets. Please ensure the ticket data is clear or increase AI model's tolerance."
703
+ yield gr.update(value=f"<p style='color:#CC3300;'>{error_message_markdown}</p>", visible=True), gr.update(value="\n".join(log_messages))
 
704
  return
705
 
706
  # --- PHASE 2: Detailed Analysis per Category ---
707
+ yield append_log_message("Phase 2/2: Detailing analysis for categories...") # Corrected
708
  progress(0.5, desc=f"Phase 2/2: Detailing analysis for categories...")
709
+ time.sleep(0.01) # Small sleep for progress bar to render
710
 
711
  final_analysis_rows = []
712
  unique_categories = final_categorized_df['category'].unique()
713
+ yield append_log_message(f"Identified {len(unique_categories)} unique categories for detailed analysis.") # Corrected
714
 
715
  category_batch_size = 5 # Process categories in small batches if many unique categories exist
716
 
717
+ # Calculate total number of category batches for progress bar
718
+ total_category_batches = max(1, (len(unique_categories) + category_batch_size - 1) // category_batch_size) # Ceiling division
719
+
720
+ for i, category_chunk in enumerate(np.array_split(unique_categories, total_category_batches)):
721
+ # Progress from 0.5 to 0.95
722
+ progress_val = 0.5 + (0.45 * (i+1) / total_category_batches)
723
+ progress(progress_val, desc=f"Phase 2/2: Detailing analysis for category batch {i+1} of {total_category_batches}...")
724
+ time.sleep(0.01) # Small sleep for progress bar to render
725
 
726
  for category in category_chunk:
727
  category_specific_tickets_df = final_categorized_df[final_categorized_df['category'] == category]
 
729
  category_tickets_content = "\n\n".join(category_specific_tickets_df['original_content'].tolist())
730
 
731
  category_count = len(category_specific_tickets_df)
732
+ yield append_log_message(f"Analyzing category: '{category}' with {category_count} tickets.") # Corrected
733
 
734
  # --- START: OPTIMIZED PROMPT FOR DETAILED ANALYSIS ---
735
  detail_prompt = f"""
 
739
 
740
  **Crucial Instructions:**
741
  1. **Strict Output Format:** Your output MUST be a **single Markdown table row**. Do NOT include the header row, only the data row.
742
+ **Table Columns (MUST be in this exact order):** `Category`, `Sub-Category`, `Count`, `Issue Description (Summary)`, `Potential Root Cause (Inferred)`, `Potential Resolution Steps`, `Future Prediction`, `Proactive Measures`.
743
  2. **Category:** This MUST be '{category}'.
744
  3. **Count:** This MUST be the **exact number of tickets provided for this category ({category_count})**.
745
  4. **Issue Description (Summary):**
 
751
  * Based on internal patterns, identify one or two **most prominent, granular sub-categories**.
752
  * If no clear sub-category emerges, state 'General {category}' or 'N/A'.
753
  * Examples: For 'Technical Issue' -> 'Login Failure', 'Software Bug'; for 'Billing Inquiry' -> 'Incorrect Charge', 'Subscription Error'.
754
+ 6. **Potential Root Cause (Inferred):** * Perform an **in-depth inference** of the **underlying systemic or causal factors** leading to these issues. Do not just restate the symptom.
 
755
  * Present each distinct root cause as a new bullet point (e.g., "* Root cause 1\\n* Root cause 2").
756
  * Think about why the problem exists. (e.g., "* System misconfiguration", "* User error due to lack of documentation", "* Third-party integration failure", "* Defect in recent software update").
757
  * If truly undetermined from the data, state 'Undetermined from available data'.
 
774
  # --- END: OPTIMIZED PROMPT FOR DETAILED ANALYSIS ---
775
 
776
  try:
777
+ yield append_log_message(f"Calling LLM for detailed analysis of category '{category}'...") # Corrected
778
  detail_response = gemini_llm_model.generate_content(detail_prompt)
779
  detail_raw_output = detail_response.text
780
+ yield append_log_message(f"LLM detailed response for '{category}' received. Parsing...") # Corrected
781
 
782
  detailed_df, parsing_err = parse_markdown_table_to_df(detail_raw_output)
783
  if parsing_err or detailed_df.empty:
784
+ yield append_log_message(f"Warning: Parsing error or empty DataFrame for category '{category}': {parsing_err or 'Empty DataFrame'}") # Corrected
785
  print(f"Warning: Parsing error or empty DataFrame for category '{category}': {parsing_err or 'Empty DataFrame'}", file=sys.stderr)
786
  print(f"Raw AI output for detail: {detail_raw_output}", file=sys.stderr)
787
  # Fallback row if parsing fails
 
790
  "Sub-Category": "N/A",
791
  "Count": category_count,
792
  "Issue Description (Summary)": category_specific_tickets_df['issue_summary_short'].iloc[0] if not category_specific_tickets_df.empty else "Error in summary generation/parsing for this category.",
793
+ "Potential Root Cause (Inferred)": "Undetermined due to parsing error or insufficient data.", # Changed
794
  "Potential Resolution Steps": "N/A",
795
  "Future Prediction": "N/A",
796
  "Proactive Measures": "N/A"
797
  }
798
  # Add a check to ensure all expected columns are present, add if missing
799
+ for col in ['Sub-Category', 'Potential Root Cause (Inferred)', 'Potential Resolution Steps', 'Future Prediction', 'Proactive Measures']: # Changed
800
  if col not in default_row:
801
  default_row[col] = 'N/A'
802
  final_analysis_rows.append(default_row)
 
810
  try:
811
  ai_reported_count = pd.to_numeric(detailed_row['Count'], errors='coerce')
812
  if pd.isna(ai_reported_count) or int(ai_reported_count) != category_count:
813
+ yield append_log_message(f"Warning: AI-reported count {ai_reported_count} for '{category}' does not match actual count {category_count}. Correcting.") # Corrected
814
  print(f"Warning: AI-reported count {ai_reported_count} for '{category}' does not match actual count {category_count}. Correcting.", file=sys.stderr)
815
  detailed_row['Count'] = category_count
816
  except: # Catch any other conversion errors
 
819
  detailed_row['Count'] = category_count
820
 
821
  # Ensure all required columns are present in the parsed row
822
+ for col in ['Sub-Category', 'Potential Root Cause (Inferred)', 'Potential Resolution Steps', 'Future Prediction', 'Proactive Measures']: # Changed
823
  if col not in detailed_row:
824
  detailed_row[col] = 'N/A' # Default to N/A if LLM missed a column
825
  final_analysis_rows.append(detailed_row)
826
 
827
  except Exception as e:
828
+ yield append_log_message(f"Error detailing analysis for category '{category}': {e}") # Corrected
829
  print(f"Error detailing analysis for category '{category}': {e}", file=sys.stderr)
830
  # Fallback row if API call fails for a category
831
  default_row = {
 
833
  "Sub-Category": "N/A",
834
  "Count": category_count,
835
  "Issue Description (Summary)": f"Error during detailed analysis for this category: {e}",
836
+ "Potential Root Cause (Inferred)": "Undetermined due to error.", # Changed
837
  "Potential Resolution Steps": "N/A",
838
  "Future Prediction": "N/A",
839
  "Proactive Measures": "N/A"
 
846
  analysis_df['Count'] = pd.to_numeric(analysis_df['Count'], errors='coerce').fillna(0).astype(int)
847
 
848
  summarized_count = analysis_df['Count'].sum()
849
+ if summarized_count != _original_total_tickets_count: # Use total_rows_in_file from the full dataframe read
850
+ yield append_log_message(f"CRITICAL WARNING: Total count in summarized DF ({summarized_count}) does not match original tickets ({_original_total_tickets_count}). This might indicate a parsing or AI categorization issue.") # Corrected
851
+ print(f"CRITICAL WARNING: Total count in summarized DF ({summarized_count}) does not match original tickets ({_original_total_tickets_count}). This might indicate a parsing or AI categorization issue.", file=sys.stderr)
852
 
853
  global_analysis_df_for_export = analysis_df.copy()
854
  global_raw_markdown_for_export = analysis_df.to_markdown(index=False) + disclaimer_text_for_raw
855
 
856
  except ValueError as e:
857
  error_message_markdown = f"Ticket Analysis Error: {e}"
858
+ yield gr.update(value=f"<p style='color:#CC3300;'>{error_message_markdown}</p>", visible=True), gr.update(value="\n".join(log_messages))
859
  global_raw_markdown_for_export = error_message_markdown + disclaimer_text_for_raw
860
  global_analysis_df_for_export = pd.DataFrame() # Clear dataframe on error
 
861
  return
862
  except Exception as e:
863
  error_message_markdown = f"An unexpected error occurred during analysis: {e}. Check server logs for details."
864
+ yield gr.update(value=f"<p style='color:#CC3300;'>{error_message_markdown}</p>", visible=True), gr.update(value="\n".join(log_messages))
865
  print(f"Error during RAG analysis: {e}", file=sys.stderr)
866
+ # Typo fixed here: disclaimer_text_for_export changed to disclaimer_text_for_raw
867
+ global_raw_markdown_for_export = error_message_markdown + disclaimer_text_for_raw
868
  global_analysis_df_for_export = pd.DataFrame() # Clear dataframe on error
 
869
  return
870
 
871
+ yield append_log_message("Generating summary report for display...") # Corrected
872
  progress(0.98, desc="Generating summary report for display...")
873
+ time.sleep(0.01) # Small sleep for progress bar to render
874
 
875
  summarized_analysis_html = ""
876
  try:
877
  if not analysis_df.empty:
878
  summarized_analysis_html = generate_summary_html_by_category(analysis_df)
879
+ yield append_log_message("HTML summary generated successfully.") # Corrected
880
  else:
881
  summarized_analysis_html = "<p style='color:#CC3300;'>AI output parsed into an empty DataFrame or no categories were generated. Please check the uploaded file and try again.</p>"
882
+ yield append_log_message("Warning: No categories generated, HTML summary might be empty.") # Corrected
883
 
884
  except Exception as e:
885
+ yield append_log_message(f"Failed to generate HTML summary from DataFrame: {e}") # Corrected
886
  print(f"Failed to generate HTML summary from DataFrame: {e}", file=sys.stderr)
887
  summarized_analysis_html = f"""
888
  <div style="margin-bottom: 20px;">
889
  <p style="color: #CC3300;'>An unexpected error occurred while attempting to transform AI output into a visual summary: {e}</p>
 
890
  <pre style="background-color: #f0f0f0; padding: 15px; border: 1px solid #ddd; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;">{global_raw_markdown_for_export}</pre>
891
  </div>
892
  """
 
1004
  This report presents a comprehensive analysis of recent customer support tickets, leveraging advanced AI to identify key trends, underlying root causes, and actionable recommendations.
1005
  </p>
1006
  <p>
1007
+ <strong style="color: #283593;">Total Tickets Processed:</strong> {_original_total_tickets_count}<br>
1008
  <strong style="color: #283593;">Total Categories Identified:</strong> {len(unique_categories) if 'unique_categories' in locals() else 0}
1009
+ {'<p class="warning-text">WARNING: Summarized ticket count ({summarized_count}) does not match original total ({_original_total_tickets_count}). Data parsing or AI categorization may have occurred.</p>' if summarized_count != _original_total_tickets_count else ''}
1010
  </p>
1011
  </div>
1012
 
 
1031
 
1032
  global_html_report_for_export = full_html_report # Store the full HTML content
1033
 
1034
+ yield append_log_message("Analysis complete. Report generated.") # Corrected
1035
  progress(1, desc="Analysis complete. Report generated.")
1036
+ time.sleep(0.01) # Final small sleep for progress bar to render
1037
 
1038
+ yield gr.update(value=full_html_report, visible=True), gr.update(value="\n".join(log_messages))
1039
 
1040
 
1041
  # --- Output Download Functions (using global_html_report_for_export) ---
 
1045
  # Use the pre-generated HTML report
1046
  html_content = global_html_report_for_export
1047
 
1048
+ if not html_content or _last_uploaded_filename_base is None:
1049
  return gr.update(value=None, visible=False)
1050
 
1051
  try:
1052
  html_obj = HTML(string=html_content)
 
 
 
1053
 
1054
+ version_num = _file_versions.get(_last_uploaded_filename_base, 1)
1055
+ output_filename = f"{_last_uploaded_filename_base}_Ticket_Analysis_Report_v{version_num}.pdf"
1056
+ temp_file_path = os.path.join(tempfile.gettempdir(), output_filename)
1057
+
1058
+ html_obj.write_pdf(temp_file_path)
1059
+
1060
+ return gr.update(value=temp_file_path, visible=True, label=f"Download {output_filename}")
1061
 
1062
  except Exception as e:
1063
  print(f"Error generating PDF with WeasyPrint: {e}", file=sys.stderr)
 
1066
 
1067
  def generate_docx_report(selected_timezone):
1068
  df = global_analysis_df_for_export
1069
+ if df.empty or _last_uploaded_filename_base is None:
1070
  return gr.update(value=None, visible=False)
1071
 
1072
  doc = DocxDocument()
 
1094
  total_tickets_processed = df['Count'].sum() if 'Count' in df.columns else 0
1095
  total_categories_identified = len(df['Category'].unique()) if 'Category' in df.columns else 0
1096
 
1097
+ doc.add_paragraph(f"Total Tickets Processed: {_original_total_tickets_count}")
1098
  doc.add_paragraph(f"Total Categories Identified: {total_categories_identified}")
1099
 
1100
  # Add warning if counts don't match
1101
+ if '_original_total_tickets_count' in globals() and _original_total_tickets_count != total_tickets_processed:
 
1102
  warning_p = doc.add_paragraph("WARNING: Summarized ticket count does not match original total. Data parsing or AI categorization may have occurred.")
1103
  warning_p.runs[0].font.color.rgb = RGBColor(0xFF, 0x00, 0x00) # Red color for warning
1104
  warning_p.runs[0].bold = True
 
1128
  issue_descriptions_list.append(line)
1129
  if issue_descriptions_list:
1130
  for item in issue_descriptions_list:
1131
+ doc.add_paragraph(item, style='List Bullet')
1132
  else:
1133
+ doc.add_paragraph("No specific summary available.", style='List Bullet')
1134
  doc.add_paragraph("")
1135
 
1136
  # Sub-Categories
 
1140
  sub_cat_counts = category_df.groupby('Sub-Category')['Count'].sum().sort_values(ascending=False)
1141
  for sub_cat, count in sub_cat_counts.items():
1142
  if str(sub_cat).strip() and str(sub_cat).strip().lower() != 'n/a':
1143
+ doc.add_paragraph(f"{sub_cat} (Count: {count})", style='List Bullet')
1144
  sub_cat_found = True
1145
  if not sub_cat_found:
1146
+ doc.add_paragraph("No sub-categories identified.", style='List Bullet')
1147
  doc.add_paragraph("")
1148
 
1149
+ # Potential Root Cause
1150
+ doc.add_paragraph("Potential Root Cause:").runs[0].bold = True # Changed to Potential Root Cause
1151
  root_causes_list = []
1152
+ for rc_text in category_df['Potential Root Cause (Inferred)'].astype(str).drop_duplicates().tolist(): # Changed to Potential Root Cause
1153
  lines = re.split(r'\s*[\*\-]\s*|\s*\n\s*', rc_text)
1154
  for line in lines:
1155
  line = line.strip()
 
1157
  root_causes_list.append(line)
1158
  if root_causes_list:
1159
  for item in root_causes_list:
1160
+ doc.add_paragraph(item, style='List Bullet')
1161
  else:
1162
+ doc.add_paragraph("Undetermined.", style='List Bullet')
1163
  doc.add_paragraph("")
1164
 
1165
  # Resolution Steps
 
1173
  resolution_steps_list.append(line)
1174
  if resolution_steps_list:
1175
  for item in resolution_steps_list:
1176
+ doc.add_paragraph(item, style='List Bullet')
1177
  else:
1178
+ doc.add_paragraph("No specific steps identified.", style='List Bullet')
1179
  doc.add_paragraph("")
1180
 
1181
  # Insights & Recommendations section
 
1192
  future_prediction_items.append(line)
1193
  if future_prediction_items:
1194
  for item in future_prediction_items:
1195
+ doc.add_paragraph(item, style='List Bullet')
1196
  else:
1197
+ doc.add_paragraph("No future predictions identified.", style='List Bullet')
1198
  doc.add_paragraph("")
1199
 
1200
  # Proactive Measures
 
1208
  proactive_measures_items.append(line)
1209
  if proactive_measures_items:
1210
  for item in proactive_measures_items:
1211
+ doc.add_paragraph(item, style='List Bullet')
1212
  else:
1213
+ doc.add_paragraph("No proactive measures identified.", style='List Bullet')
1214
  doc.add_paragraph("")
1215
 
1216
 
 
1220
  )
1221
  doc.add_paragraph(f"Report Generation Timestamp: {current_gen_time}")
1222
 
1223
+ version_num = _file_versions.get(_last_uploaded_filename_base, 1)
1224
+ output_filename = f"{_last_uploaded_filename_base}_Ticket_Analysis_Report_v{version_num}.docx"
1225
+ temp_file_path = os.path.join(tempfile.gettempdir(), output_filename)
1226
+
1227
+ doc.save(temp_file_path)
1228
 
1229
+ return gr.update(value=temp_file_path, visible=True, label=f"Download {output_filename}")
1230
 
1231
 
1232
  # --- Gradio Interface Setup ---
 
1247
  color: #333;
1248
  }
1249
 
1250
+ /* Target Gradio's markdown output blocks directly to reduce spacing between them */
1251
+ .gradio-container .output_markdown,
1252
+ .gradio-container .gr-html { /* .gr-html is also used for gr.HTML */
1253
+ margin-top: 5px !important;
1254
+ margin-bottom: 5px !important;
1255
+ padding-top: 0 !important;
1256
+ padding-bottom: 0 !important;
1257
+ }
1258
+
1259
  /* Headings for a consistent, professional look */
1260
  h1 {
1261
  color: #1A237E; /* Deep Indigo */
1262
  text-align: center;
1263
+ margin-bottom: 20px; /* Reduced from 25px */
1264
  font-size: 2.5em; /* Larger main title */
1265
  font-weight: 600; /* Slightly bolder */
1266
  }
1267
  h2 {
1268
  color: #283593; /* Indigo */
1269
+ margin-top: 25px; /* Reduced from 30px */
1270
+ padding-bottom: 6px; /* Reduced from 8px */
1271
  border-bottom: 1px solid #E0E0E0; /* Subtle separator */
1272
  font-size: 1.8em;
1273
  font-weight: 500;
1274
  }
1275
  h3 {
1276
  color: #1A5276; /* Darker Blue-Green */
1277
+ margin-top: 10px; /* Reduced from 15px */
1278
+ margin-bottom: 5px; /* Reduced from 8px */
1279
  font-size: 1.3em;
1280
  font-weight: 600;
1281
  }
1282
  h4 { /* Added h4 styling */
1283
  color: #3F51B5; /* Medium Indigo */
1284
+ margin-top: 8px; /* Reduced from 12px */
1285
+ margin-bottom: 4px; /* Reduced from 6px */
1286
  font-size: 1.1em;
1287
  font-weight: 600;
1288
  }
 
1290
 
1291
  /* Paragraphs and list items */
1292
  p {
1293
+ line-height: 1.5; /* Slightly tighter line height */
1294
+ margin-bottom: 6px; /* Reduced from 10px */
1295
  }
1296
  ul {
1297
  list-style-type: disc; /* Standard disc bullets */
1298
  margin-left: 25px; /* Indent lists properly */
1299
+ margin-top: 4px; /* Reduced from 8px */
1300
+ margin-bottom: 4px; /* Reduced from 8px */
1301
+ line-height: 1.4; /* Slightly tighter line height */
1302
  }
1303
  li {
1304
+ margin-bottom: 2px; /* Reduced from 4px */
1305
  padding-left: 2px; /* Small padding for visual comfort */
1306
  }
1307
  strong {
 
1312
  /* Specific section styling for minimalist appeal */
1313
  .overview-section {
1314
  background-color: #E8EAF6; /* Lightest Indigo background */
1315
+ padding: 18px; /* Reduced from 20px */
1316
  border-radius: 6px;
1317
+ margin-bottom: 18px; /* Reduced from 25px */
1318
  border: 1px solid #C5CAE9; /* Subtle border */
1319
  }
1320
  .category-block {
 
1329
  background-color: #f5f5f5; /* Light gray for disclaimers */
1330
  padding: 15px;
1331
  border-radius: 6px;
1332
+ margin-top: 25px; /* Reduced from 30px */
1333
  border: 1px solid #e0e0e0;
1334
  font-size: 0.85em;
1335
  color: #555;
 
1396
  }
1397
  /* Specific hr styling for Markdown content separators */
1398
  .gradio-container hr { /* Target hr within the gradio container, particularly for Markdown */
1399
+ margin-top: 12px; /* Reduced from 15px */
1400
+ margin-bottom: 12px; /* Reduced from 15px */
1401
  border: 0;
1402
  height: 1px;
1403
  background-color: #E0E0E0;
1404
  }
1405
+ /* CSS to hide the Hugging Face Space footer */
1406
+ footer {
1407
+ display: none !important;
1408
+ }
1409
  """) as demo:
1410
  gr.HTML("""
1411
  <h1 align="center">Advanced AI Customer Ticket Analyst</h1>
1412
+ <p align="center" style="font-size: 1.0em; color: #555; margin-bottom: 15px;">Derive actionable insights from your customer support data.</p>
1413
  """)
1414
 
1415
  gr.Markdown(
1416
  """
1417
  ### System Overview
1418
  This tool provides structured insights from customer support tickets. Upload your data to receive a comprehensive analysis including categorization, root causes, strategic recommendations, and future outlooks.
1419
+ """
1420
+ )
1421
 
1422
+ gr.Markdown(
1423
+ """
1424
  #### Important Note on AI Inferences:
1425
  AI-generated insights, including inferred root causes, future predictions, and proactive measures, are based on the provided ticket data. These analyses are intended for guidance and require human expert validation for critical business decisions.
1426
+ """
1427
+ )
1428
+ gr.Markdown(
1429
+ """
1430
  #### Input File Requirements:
1431
  * **Supported Formats:** `.xlsx` (Excel), `.xls` (Legacy Excel), and `.csv` (Comma Separated Values).
1432
  * **Data Structure:** Files must contain tabular data with clear headers in the first row. Each row represents an individual customer ticket.
 
1437
  TKT001,User cannot log in; password reset not working.
1438
  TKT002,Billing error: duplicate charge for last month's service.
1439
  ```
1440
+ * **Recommended Columns:** For richer analysis, include columns suchs as `Status`, `Priority`, `CustomerName`, and `Timestamp`. More relevant details enhance AI inferences.
1441
  * **Data Volume:** For large files (e.g., over 60,000 tickets), processing occurs in batches to optimize memory and API usage. Processing times may vary.
1442
  * **Data Privacy:** Please do not upload highly sensitive or confidential customer information. This is a public demonstration environment.
1443
+ """
1444
+ )
1445
 
1446
+ gr.Markdown(
1447
+ """
1448
  #### Output Structure:
1449
  The AI will generate a clear, data-driven report based solely on your uploaded file. The visual output will feature:
1450
  * **Category Name (Total Count for Category)**
1451
  * **Summary:** Key issue descriptions and common themes.
1452
  * **Sub-Categories:** More granular sub-categories and their counts.
1453
+ * **Potential Root Cause:** AI-inferred underlying reasons for issues.
1454
  * **Resolution Steps:** Strategic actions to resolve issues.
1455
  * **Insights & Recommendations:**
1456
  * **Future Prediction:** Predictions if underlying issues persist.
 
1488
  interactive=False,
1489
  lines=5,
1490
  autoscroll=True,
1491
+ elem_id="progress-log-box",
1492
+ visible=False # Hidden on load
1493
  )
1494
 
1495
  output_html_display = gr.HTML(
 
1504
 
1505
 
1506
  gr.HTML("""
1507
+ <hr style="margin-top: 20px; margin-bottom: 10px; border-color: #E0E0E0;">
1508
  <p align="center" style="font-size: 0.8em; color: #777;">
1509
  *All AI-generated insights are for informational purposes and should be verified by human experts.*
1510
  </p>