Neom99 commited on
Commit
71a1536
·
verified ·
1 Parent(s): 0e8c67a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -26
app.py CHANGED
@@ -162,16 +162,20 @@ def get_ticket_documents_iterator(file_path, file_extension, filename, chunksize
162
 
163
  if file_extension in ['.xlsx', '.xls']:
164
  try:
165
- df_reader = pd.read_excel(file_path, engine='openpyxl', chunksize=chunksize)
 
 
 
 
166
  total_rows_processed = 0
167
- for i, df_chunk in enumerate(df_reader):
168
- if df_chunk.empty:
169
- continue
170
 
171
  chunk_documents = []
172
  for index, row in df_chunk.iterrows():
173
  ticket_content_parts = []
174
- ticket_id = f"Row {total_rows_processed + 1}" # Default ticket ID
175
 
176
  for col_name, value in row.items():
177
  if pd.notna(value) and str(value).strip() != '':
@@ -186,11 +190,11 @@ def get_ticket_documents_iterator(file_path, file_extension, filename, chunksize
186
  else:
187
  ticket_content = "\n".join(ticket_content_parts)
188
 
189
- metadata = {"source": filename, "ticket_row_number": total_rows_processed + 1, "ticket_id": ticket_id}
190
  metadata.update(row.to_dict()) # Include all original row data in metadata
191
 
192
  chunk_documents.append({"page_content": ticket_content, "metadata": metadata})
193
- total_rows_processed += 1
194
 
195
  yield chunk_documents, total_rows_processed # Yield chunk and running total
196
 
@@ -359,8 +363,24 @@ def parse_markdown_table_to_df(markdown_string):
359
  break
360
 
361
  if header_line is None or separator_line_index == -1:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
  return pd.DataFrame(), "No valid Markdown table header or separator found."
363
 
 
364
  data_lines = []
365
  for line in lines[separator_line_index + 1:]:
366
  if line.strip().startswith('|') and '|' in line.strip():
@@ -596,25 +616,43 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
596
  category_count = len(category_specific_tickets_df)
597
  yield from append_log_message(f"Analyzing category: '{category}' with {category_count} tickets.")
598
 
 
599
  detail_prompt = f"""
600
- You are an expert customer support ticket analyst.
601
- Analyze the following customer tickets, which all belong to the '{category}' category.
602
- Your output MUST be a **single Markdown table row** that summarizes the common themes, root causes, resolution steps, future predictions, and proactive measures *for this specific category of tickets*.
603
-
604
- **Ensure the 'Count' in your table row EXACTLY matches the number of tickets provided for this category ({category_count}).**
605
-
606
- Keep 'Issue Description (Summary)' concise (max 3-4 sentences), summarizing common themes *across these tickets*.
607
- If a specific detail (like root cause) cannot be inferred from the available ticket data, state 'Undetermined' or 'N/A'.
608
- Do not make up information.
609
-
610
- **Desired Output Format (Markdown Table - single row):**
611
- | Category | Sub-Category | Count | Issue Description (Summary) | Root Cause (Inferred) | Potential Resolution Steps | Future Prediction | Proactive Measures |
612
- |---|---|---|---|---|---|---|---|
613
- | ... | ... | ... | ... | ... | ... | ... | ... |
614
-
615
- Context (Tickets for Category '{category}'):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
616
  {category_tickets_content}
617
  """
 
618
 
619
  try:
620
  yield from append_log_message(f"Calling LLM for detailed analysis of category '{category}'...")
@@ -633,12 +671,17 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
633
  "Sub-Category": "N/A",
634
  "Count": category_count,
635
  "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.",
636
- "Root Cause (Inferred)": "Undetermined due to parsing error.",
637
  "Potential Resolution Steps": "N/A",
638
  "Future Prediction": "N/A",
639
  "Proactive Measures": "N/A"
640
  }
 
 
 
 
641
  final_analysis_rows.append(default_row)
 
642
  else:
643
  detailed_row = detailed_df.iloc[0].to_dict()
644
  detailed_row['Category'] = category # Ensure category is correctly assigned
@@ -656,6 +699,10 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
656
  else: # Add 'Count' if missing from AI output
657
  detailed_row['Count'] = category_count
658
 
 
 
 
 
659
  final_analysis_rows.append(detailed_row)
660
 
661
  except Exception as e:
@@ -729,7 +776,7 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
729
 
730
 
731
  html_output = f"""
732
- <div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.1);">
733
  <h1 align="center">🕵️‍♀️ Executive Summary: Customer Ticket Analysis 🕵️‍♀️</h1>
734
 
735
  <div style="background-color: #E8EAF6; padding: 15px; border-radius: 8px; margin-bottom: 20px;">
@@ -742,7 +789,7 @@ def perform_ticket_analysis(file, user_query, selected_timezone, progress=gr.Pro
742
  Total Original Tickets in File: <strong>{total_tickets_count}</strong>.<br>
743
  Total Tickets Summarized in Report: <strong>{summarized_count}</strong>.<br>
744
  Total Categories Identified: <strong>{len(unique_categories) if 'unique_categories' in locals() else 0}</strong>.
745
- {"<p style='color:red; font-weight:bold;'>WARNING: The number of tickets summarized in the report does not match the total original tickets. This may indicate a parsing or AI processing issue. Please review the raw output if available.</p>" if summarized_count != total_tickets_count else ""}
746
  </p>
747
  </div>
748
  """
 
162
 
163
  if file_extension in ['.xlsx', '.xls']:
164
  try:
165
+ # Read the entire Excel file into a DataFrame first
166
+ df_full = pd.read_excel(file_path, engine='openpyxl')
167
+ if df_full.empty:
168
+ raise ValueError("The uploaded Excel file contains no data.")
169
+
170
  total_rows_processed = 0
171
+ # Manually chunk the DataFrame
172
+ for i in range(0, len(df_full), chunksize):
173
+ df_chunk = df_full.iloc[i : i + chunksize]
174
 
175
  chunk_documents = []
176
  for index, row in df_chunk.iterrows():
177
  ticket_content_parts = []
178
+ ticket_id = f"Row {index + 1}" # Default ticket ID based on original DataFrame index
179
 
180
  for col_name, value in row.items():
181
  if pd.notna(value) and str(value).strip() != '':
 
190
  else:
191
  ticket_content = "\n".join(ticket_content_parts)
192
 
193
+ metadata = {"source": filename, "ticket_row_number": index + 1, "ticket_id": ticket_id}
194
  metadata.update(row.to_dict()) # Include all original row data in metadata
195
 
196
  chunk_documents.append({"page_content": ticket_content, "metadata": metadata})
197
+ total_rows_processed += 1 # Update count based on actual processed rows in this chunk
198
 
199
  yield chunk_documents, total_rows_processed # Yield chunk and running total
200
 
 
363
  break
364
 
365
  if header_line is None or separator_line_index == -1:
366
+ # If no header/separator found, assume the first non-empty line is a data row if it looks like one
367
+ if any(line.strip().startswith('|') and '|' in line.strip() for line in lines):
368
+ # Attempt to parse as if it's just a data row without a header.
369
+ # This is a fallback and might not be perfect.
370
+ potential_data_line = next((line for line in lines if line.strip().startswith('|') and '|' in line.strip()), None)
371
+ if potential_data_line:
372
+ # Create dummy headers if no header was provided
373
+ num_cols = len([v for v in potential_data_line.strip('|').split('|') if v])
374
+ headers = [f"Col{i+1}" for i in range(num_cols)]
375
+ data = [[v.strip() for v in potential_data_line.strip('|').split('|') if v]]
376
+ if len(data[0]) == len(headers):
377
+ df = pd.DataFrame(data, columns=headers)
378
+ df = df.dropna(axis=1, how='all')
379
+ df.columns = df.columns.str.strip()
380
+ return df, "No explicit Markdown table header or separator found, parsed as a single data row."
381
  return pd.DataFrame(), "No valid Markdown table header or separator found."
382
 
383
+
384
  data_lines = []
385
  for line in lines[separator_line_index + 1:]:
386
  if line.strip().startswith('|') and '|' in line.strip():
 
616
  category_count = len(category_specific_tickets_df)
617
  yield from append_log_message(f"Analyzing category: '{category}' with {category_count} tickets.")
618
 
619
+ # --- START: OPTIMIZED PROMPT FOR DETAILED ANALYSIS ---
620
  detail_prompt = f"""
621
+ You are an expert customer support ticket analyst, specializing in NLP and data interpretation.
622
+ Deeply analyze the following raw customer support tickets, which all belong to the **'{category}'** category.
623
+ Your goal is to synthesize the information into a single, comprehensive markdown table row, providing highly contextualized and actionable insights for this specific category.
624
+
625
+ **Crucial Instructions:**
626
+ 1. **Strict Output Format:** Your output MUST be a **single Markdown table row**. Do NOT include the header row, only the data row.
627
+ **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`.
628
+ 2. **Category:** This MUST be '{category}'.
629
+ 3. **Count:** This MUST be the **exact number of tickets provided for this category ({category_count})**.
630
+ 4. **Issue Description (Summary):**
631
+ * Synthesize the **common themes and recurring patterns** observed across ALL tickets in this category.
632
+ * Identify and mention **key affected systems, user actions, or specific features** causing issues.
633
+ * Keep it concise (3-5 sentences), focusing on the most prevalent aspects.
634
+ 5. **Sub-Category:**
635
+ * Based on internal patterns, identify one or two **most prominent, granular sub-categories**.
636
+ * If no clear sub-category emerges, state 'General {category}' or 'N/A'.
637
+ * Examples: For 'Technical Issue' -> 'Login Failure', 'Software Bug'; for 'Billing Inquiry' -> 'Incorrect Charge', 'Subscription Error'.
638
+ 6. **Root Cause (Inferred):**
639
+ * Perform an **in-depth inference** of the **underlying systemic or causal factors** leading to these issues. Do not just restate the symptom.
640
+ * 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").
641
+ * If truly undetermined from the data, state 'Undetermined from available data'.
642
+ 7. **Potential Resolution Steps:**
643
+ * Suggest **actionable, generic steps** that would effectively resolve or mitigate the issues in this category.
644
+ * These should be **strategic resolutions**, not just individual ticket fixes. (e.g., "Develop new API endpoint", "Update user guides", "Patch database system", "Conduct user training").
645
+ 8. **Future Prediction:**
646
+ * Based on the trends and nature of these tickets, predict **what might happen if the underlying issues are not addressed**.
647
+ * Consider potential increase in ticket volume, customer churn, or emergence of new, related problems. (e.g., "Increased call volume for billing disputes", "Decline in user retention for feature X", "Escalation to higher severity issues").
648
+ 9. **Proactive Measures:**
649
+ * Suggest **specific, actionable steps to prevent recurrence or significantly reduce the volume** of these types of tickets in the future.
650
+ * Focus on **system improvements, process changes, documentation, or user education**. (e.g., "Implement automated system health checks", "Revise onboarding flow documentation", "Roll out a mandatory security update", "Train support staff on new refund process").
651
+
652
+ Context (Raw Customer Tickets for Category '{category}' - Total {category_count} tickets):
653
  {category_tickets_content}
654
  """
655
+ # --- END: OPTIMIZED PROMPT FOR DETAILED ANALYSIS ---
656
 
657
  try:
658
  yield from append_log_message(f"Calling LLM for detailed analysis of category '{category}'...")
 
671
  "Sub-Category": "N/A",
672
  "Count": category_count,
673
  "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.",
674
+ "Root Cause (Inferred)": "Undetermined due to parsing error or insufficient data.",
675
  "Potential Resolution Steps": "N/A",
676
  "Future Prediction": "N/A",
677
  "Proactive Measures": "N/A"
678
  }
679
+ # Add a check to ensure all expected columns are present, add if missing
680
+ for col in ['Sub-Category', 'Root Cause (Inferred)', 'Potential Resolution Steps', 'Future Prediction', 'Proactive Measures']:
681
+ if col not in default_row:
682
+ default_row[col] = 'N/A'
683
  final_analysis_rows.append(default_row)
684
+
685
  else:
686
  detailed_row = detailed_df.iloc[0].to_dict()
687
  detailed_row['Category'] = category # Ensure category is correctly assigned
 
699
  else: # Add 'Count' if missing from AI output
700
  detailed_row['Count'] = category_count
701
 
702
+ # Ensure all required columns are present in the parsed row
703
+ for col in ['Sub-Category', 'Root Cause (Inferred)', 'Potential Resolution Steps', 'Future Prediction', 'Proactive Measures']:
704
+ if col not in detailed_row:
705
+ detailed_row[col] = 'N/A' # Default to N/A if LLM missed a column
706
  final_analysis_rows.append(detailed_row)
707
 
708
  except Exception as e:
 
776
 
777
 
778
  html_output = f"""
779
+ <div style="font-family: Arial, sans-serif; padding: 20px; background-color: #f9f9f9; border-radius: 10px; box-shadow: 0 4px 8px rgba(0,0,0,0.1);">
780
  <h1 align="center">🕵️‍♀️ Executive Summary: Customer Ticket Analysis 🕵️‍♀️</h1>
781
 
782
  <div style="background-color: #E8EAF6; padding: 15px; border-radius: 8px; margin-bottom: 20px;">
 
789
  Total Original Tickets in File: <strong>{total_tickets_count}</strong>.<br>
790
  Total Tickets Summarized in Report: <strong>{summarized_count}</strong>.<br>
791
  Total Categories Identified: <strong>{len(unique_categories) if 'unique_categories' in locals() else 0}</strong>.
792
+ {"<p style='color:red; font-weight:bold;'>WARNING: The number of tickets summarized in the report does not match the total original tickets. This might indicate a parsing or AI categorization issue.</p>" if summarized_count != total_tickets_count else ""}
793
  </p>
794
  </div>
795
  """