tatianija commited on
Commit
9d9d6be
·
verified ·
1 Parent(s): 873879c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -4
app.py CHANGED
@@ -85,7 +85,34 @@ class SimpleSearchTool:
85
 
86
  except Exception as e:
87
  return f"Search failed: {str(e)}"
 
 
 
 
 
 
 
 
 
 
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  # --- Web Content Fetcher ---
90
  class WebContentFetcher:
91
  def __init__(self, debug: bool = True):
@@ -452,6 +479,7 @@ class IntelligentAgent:
452
  self.image_tool = ImageAnalysisTool()
453
  self.audio_tool = AudioTranscriptionTool()
454
  self.code_tool = CodeAnalysisTool(model_name)
 
455
  self.web_fetcher = WebContentFetcher(debug)
456
  self.debug = debug
457
  if self.debug:
@@ -531,6 +559,7 @@ class IntelligentAgent:
531
  image_files = []
532
  audio_files = []
533
  code_files = []
 
534
 
535
  if not file_name:
536
  return image_files, audio_files, code_files
@@ -558,6 +587,10 @@ class IntelligentAgent:
558
  is_code = (
559
  file_ext in ['.py', '.txt', '.js', '.html', '.css', '.json', '.xml', '.md', '.c', '.cpp', '.java']
560
  )
 
 
 
 
561
 
562
  # Categorize the file
563
  if is_image:
@@ -566,6 +599,8 @@ class IntelligentAgent:
566
  audio_files.append(local_path)
567
  elif is_code:
568
  code_files.append(local_path)
 
 
569
  else:
570
  # Default to code/text for unknown types
571
  code_files.append(local_path)
@@ -578,9 +613,9 @@ class IntelligentAgent:
578
  print(f"Error processing attachment {file_name}: {e}")
579
 
580
  if self.debug:
581
- print(f"Processed attachment: {len(image_files)} images, {len(audio_files)} audio, {len(code_files)} code files")
582
 
583
- return image_files, audio_files, code_files
584
 
585
  def _process_attachments(self, image_files: List[str], audio_files: List[str], code_files: List[str]) -> str:
586
  """
@@ -631,7 +666,18 @@ class IntelligentAgent:
631
  if self.debug:
632
  print(f"Error processing code {code_file}: {e}")
633
  attachment_context += f"\n\nCODE PROCESSING ERROR ({code_file}): {e}\n"
634
-
 
 
 
 
 
 
 
 
 
 
 
635
  return attachment_context
636
 
637
  def _should_search(self, question: str, attachment_context: str, url_context: str) -> bool:
@@ -779,7 +825,7 @@ Answer with just "YES" only if your general knowledge is insufficient to answer
779
  with open(local_file_path, 'wb') as f:
780
  f.write(response.content)
781
 
782
- image_files, audio_files, code_files = self._detect_and_process_direct_attachments(attachment_name, local_file_path)
783
 
784
  # Process attachments to get context
785
  attachment_context = self._process_attachments(image_files, audio_files, code_files)
 
85
 
86
  except Exception as e:
87
  return f"Search failed: {str(e)}"
88
+ # --- Excel Processing Tool ---
89
+ class ExcelAnalysisTool:
90
+ def __init__(self):
91
+ pass
92
+
93
+ def analyze_excel(self, file_path: str) -> str:
94
+ """Extract and format Excel content for LLM context."""
95
+ try:
96
+ # Read all sheets
97
+ excel_data = pd.read_excel(file_path, sheet_name=None, nrows=100)
98
 
99
+ result = []
100
+ result.append(f"EXCEL FILE ANALYSIS: {file_path}")
101
+
102
+ for sheet_name, df in excel_data.items():
103
+ result.append(f"\nSHEET: {sheet_name}")
104
+ result.append(f"Size: {df.shape[0]} rows × {df.shape[1]} columns")
105
+ result.append(f"Columns: {list(df.columns)}")
106
+
107
+ # Show first few rows
108
+ if not df.empty:
109
+ result.append("Sample data:")
110
+ result.append(df.head(3).to_string(index=False))
111
+
112
+ return "\n".join(result)
113
+
114
+ except Exception as e:
115
+ return f"Excel analysis failed: {e}"
116
  # --- Web Content Fetcher ---
117
  class WebContentFetcher:
118
  def __init__(self, debug: bool = True):
 
479
  self.image_tool = ImageAnalysisTool()
480
  self.audio_tool = AudioTranscriptionTool()
481
  self.code_tool = CodeAnalysisTool(model_name)
482
+ self.excel_tool = ExcelAnalysisTool()
483
  self.web_fetcher = WebContentFetcher(debug)
484
  self.debug = debug
485
  if self.debug:
 
559
  image_files = []
560
  audio_files = []
561
  code_files = []
562
+ excel_files = []
563
 
564
  if not file_name:
565
  return image_files, audio_files, code_files
 
587
  is_code = (
588
  file_ext in ['.py', '.txt', '.js', '.html', '.css', '.json', '.xml', '.md', '.c', '.cpp', '.java']
589
  )
590
+ is_excel = (
591
+ file_ext in ['.xlsx', '.xls']
592
+ )
593
+
594
 
595
  # Categorize the file
596
  if is_image:
 
599
  audio_files.append(local_path)
600
  elif is_code:
601
  code_files.append(local_path)
602
+ elif is_excel:
603
+ excel_files.append(local_path)
604
  else:
605
  # Default to code/text for unknown types
606
  code_files.append(local_path)
 
613
  print(f"Error processing attachment {file_name}: {e}")
614
 
615
  if self.debug:
616
+ print(f"Processed attachment: {len(image_files)} images, {len(audio_files)} audio, {len(code_files)} code files, {len(excel_files)} excel files")
617
 
618
+ return image_files, audio_files, code_files, excel_files
619
 
620
  def _process_attachments(self, image_files: List[str], audio_files: List[str], code_files: List[str]) -> str:
621
  """
 
666
  if self.debug:
667
  print(f"Error processing code {code_file}: {e}")
668
  attachment_context += f"\n\nCODE PROCESSING ERROR ({code_file}): {e}\n"
669
+ # Process excel files
670
+ for excel_file in excel_files:
671
+ if self.debug:
672
+ print(f"Processing excel: {code_file}")
673
+ try:
674
+ excel_analysis = self.excel_tool.analyze_excel(excel_file)
675
+ attachment_context += f"\n\nEXCEL ANALYSIS ({excel_file}):\n{excel_analysis}\n"
676
+
677
+ except Exception as e:
678
+ if self.debug:
679
+ print(f"Error processing code {code_file}: {e}")
680
+ attachment_context += f"\n\nCODE PROCESSING ERROR ({code_file}): {e}\n"
681
  return attachment_context
682
 
683
  def _should_search(self, question: str, attachment_context: str, url_context: str) -> bool:
 
825
  with open(local_file_path, 'wb') as f:
826
  f.write(response.content)
827
 
828
+ image_files, audio_files, code_files, excel_files = self._detect_and_process_direct_attachments(attachment_name, local_file_path)
829
 
830
  # Process attachments to get context
831
  attachment_context = self._process_attachments(image_files, audio_files, code_files)