AnhLee0 commited on
Commit
ed09bbe
·
verified ·
1 Parent(s): 3611b72

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -17
app.py CHANGED
@@ -44,22 +44,54 @@ class AssistantAgent:
44
  return f"Error calling xAI API: {e}"
45
 
46
  def check_commutative(self, table: str) -> str:
47
- rows = table.strip().split('\n')[2:]
48
- elements = ['a', 'b', 'c', 'd', 'e']
49
- operation = {}
50
- for i, row in enumerate(rows):
51
- cols = row.split('|')[1:-1]
52
- for j, val in enumerate(cols[1:]):
53
- operation[(elements[i], elements[j])] = val
54
-
55
- non_commutative = set()
56
- for a in elements:
57
- for b in elements:
58
- if operation.get((a, b)) != operation.get((b, a)):
59
- non_commutative.add(a)
60
- non_commutative.add(b)
61
-
62
- return ", ".join(sorted(non_commutative)) if non_commutative else "No counter-examples found"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  def classify_vegetables(self, items: str) -> str:
65
  all_items = [item.strip() for item in items.split(",")]
@@ -103,7 +135,7 @@ class AssistantAgent:
103
  answers.append(answer)
104
  if i + batch_size < len(questions):
105
  print("Waiting 1 second before next batch to avoid rate limit...")
106
- time.sleep(1) # Độ trễ nhỏ để tránh gọi API quá nhanh
107
  return answers
108
 
109
  def process_file(self, question: str, file_path: str) -> str:
 
44
  return f"Error calling xAI API: {e}"
45
 
46
  def check_commutative(self, table: str) -> str:
47
+ try:
48
+ # Tách các dòng của bảng
49
+ rows = table.strip().split('\n')
50
+ if len(rows) < 3: # Cần ít nhất 3 dòng (header, phân cách, dữ liệu)
51
+ return "Error: Table format is invalid (not enough rows)."
52
+
53
+ # Lấy danh sách phần tử từ dòng đầu tiên (header)
54
+ header = rows[0].split('|')
55
+ if len(header) < 3: # Cần ít nhất 3 cột (|, phần tử, |)
56
+ return "Error: Table format is invalid (header too short)."
57
+
58
+ elements = header[1:-1][1:] # Bỏ cột đầu tiên (*) cột cuối, bỏ ký hiệu |
59
+ num_elements = len(elements)
60
+ if num_elements < 1:
61
+ return "Error: No elements found in table header."
62
+
63
+ # Kiểm tra số dòng dữ liệu (phải khớp với số phần tử + 2 dòng header)
64
+ if len(rows) != num_elements + 2:
65
+ return f"Error: Table row count ({len(rows)}) does not match expected ({num_elements + 2})."
66
+
67
+ # Kiểm tra định dạng dòng phân cách
68
+ if not rows[1].startswith('|---'):
69
+ return "Error: Table format is invalid (missing separator row)."
70
+
71
+ # Xây dựng bảng phép toán
72
+ operation = {}
73
+ for i in range(2, len(rows)):
74
+ cols = rows[i].split('|')
75
+ if len(cols) != num_elements + 2: # Số cột phải khớp với số phần tử + 2
76
+ return f"Error: Row {i} has incorrect number of columns (expected {num_elements + 2}, got {len(cols)})."
77
+
78
+ for j in range(1, len(cols) - 1):
79
+ val = cols[j + 1].strip()
80
+ if val not in elements:
81
+ return f"Error: Invalid value '{val}' in table (not in {elements})."
82
+ operation[(elements[i - 2], elements[j - 1])] = val
83
+
84
+ # Tìm các cặp không giao hoán: a*b != b*a
85
+ non_commutative = set()
86
+ for a in elements:
87
+ for b in elements:
88
+ if operation.get((a, b)) != operation.get((b, a)):
89
+ non_commutative.add(a)
90
+ non_commutative.add(b)
91
+
92
+ return ", ".join(sorted(non_commutative)) if non_commutative else "No counter-examples found"
93
+ except Exception as e:
94
+ return f"Error processing table: {e}"
95
 
96
  def classify_vegetables(self, items: str) -> str:
97
  all_items = [item.strip() for item in items.split(",")]
 
135
  answers.append(answer)
136
  if i + batch_size < len(questions):
137
  print("Waiting 1 second before next batch to avoid rate limit...")
138
+ time.sleep(1)
139
  return answers
140
 
141
  def process_file(self, question: str, file_path: str) -> str: