vladd19 commited on
Commit
4bb8d2a
·
verified ·
1 Parent(s): 952cfd7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -8
app.py CHANGED
@@ -440,6 +440,52 @@ def solve_directly_with_python(question: str) -> str | None:
440
  return None
441
 
442
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
443
  def direct_question(question: str) -> bool:
444
  q = question.lower()
445
  rev = q[::-1]
@@ -620,20 +666,29 @@ class BasicAgent:
620
  )
621
  return {"context": context, "raw_answer": raw_answer}
622
 
623
- def solve_spreadsheet(self, state: AgentState) -> dict[str, Any]:
624
- question = state.get("question", "")
625
- local_path = state.get("local_path")
626
 
627
- if not local_path:
628
- return {"raw_answer": "ERROR: spreadsheet route selected but no local file path found"}
 
 
 
 
 
 
 
 
 
629
 
630
- context = read_spreadsheet_context(Path(local_path))
631
  raw_answer = self.answer_from_context(
632
  question=question,
633
- context=context,
634
- context_label="Spreadsheet contents",
635
  llm=self.answer_llm,
636
  )
 
637
  return {"context": context, "raw_answer": raw_answer}
638
 
639
  def solve_code(self, state: AgentState) -> dict[str, Any]:
@@ -671,6 +726,10 @@ class BasicAgent:
671
  shortcut = solve_directly_with_python(question)
672
  if shortcut is not None:
673
  return {"context": "Solved by deterministic Python shortcut.", "raw_answer": shortcut}
 
 
 
 
674
 
675
  context = ""
676
  if local_path and file_kind in {"pdf", "text", "binary"}:
 
440
  return None
441
 
442
 
443
+ def solve_commutativity_table(question: str) -> str | None:
444
+ q = question.lower()
445
+
446
+ if "|---" not in question:
447
+ return None
448
+
449
+ if "commutative" not in q and "commutativity" not in q:
450
+ return None
451
+
452
+ lines = [
453
+ line.strip()
454
+ for line in question.splitlines()
455
+ if line.strip().startswith("|")
456
+ ]
457
+
458
+ if len(lines) < 3:
459
+ return None
460
+
461
+ header = [x.strip() for x in lines[0].strip("|").split("|")]
462
+ cols = header[1:]
463
+
464
+ table = {}
465
+
466
+ for line in lines[2:]:
467
+ cells = [x.strip() for x in line.strip("|").split("|")]
468
+ if len(cells) != len(cols) + 1:
469
+ continue
470
+
471
+ row = cells[0]
472
+ values = cells[1:]
473
+ table[row] = dict(zip(cols, values))
474
+
475
+ for a in cols:
476
+ for b in cols:
477
+ if a == b:
478
+ continue
479
+
480
+ ab = table.get(a, {}).get(b)
481
+ ba = table.get(b, {}).get(a)
482
+
483
+ if ab is not None and ba is not None and ab != ba:
484
+ return ", ".join(sorted([a, b]))
485
+
486
+ return "commutative"
487
+
488
+
489
  def direct_question(question: str) -> bool:
490
  q = question.lower()
491
  rev = q[::-1]
 
666
  )
667
  return {"context": context, "raw_answer": raw_answer}
668
 
669
+ def solve_spreadsheet(self, state: AgentState) -> dict:
670
+ path = state["local_path"]
671
+ question = state["question"]
672
 
673
+ xls = pd.ExcelFile(path)
674
+ parts = []
675
+
676
+ for sheet in xls.sheet_names:
677
+ df = pd.read_excel(path, sheet_name=sheet)
678
+ parts.append(f"Sheet: {sheet}")
679
+ parts.append(f"Columns: {list(df.columns)}")
680
+ parts.append(f"Shape: {df.shape}")
681
+ parts.append(df.head(20).to_csv(index=False))
682
+
683
+ context = "\n\n".join(parts)
684
 
 
685
  raw_answer = self.answer_from_context(
686
  question=question,
687
+ context=context[:12000],
688
+ context_label="spreadsheet preview",
689
  llm=self.answer_llm,
690
  )
691
+
692
  return {"context": context, "raw_answer": raw_answer}
693
 
694
  def solve_code(self, state: AgentState) -> dict[str, Any]:
 
726
  shortcut = solve_directly_with_python(question)
727
  if shortcut is not None:
728
  return {"context": "Solved by deterministic Python shortcut.", "raw_answer": shortcut}
729
+
730
+ direct = solve_commutativity_table(question)
731
+ if direct is not None:
732
+ return {"raw_answer": direct}
733
 
734
  context = ""
735
  if local_path and file_kind in {"pdf", "text", "binary"}: