Spaces:
Sleeping
Sleeping
Update app.py (#15)
Browse files- Update app.py (6259a26abe6c29bd30ed6254dc9bb03101dd584c)
298 unmodified lines29930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540611 unmodified lines418419420421422423424425426427428429430431432433434435436437438439440441442443298 unmodified linesreturn m.group(1).strip()return Nonedef _parse_op_table_from_text(self, text: str):"""Parse a Markdown table like:|*|a|b|c||---|---|---|---||a|a|b|c||b|b|c|a||c|c|a|b|Returns (S, T) where S is a list of symbols, andT is a dict-of-dicts T[row][col] = result."""if not text:return Nonelines = [ln.strip() for ln in text.splitlines() if "|" in ln]if not lines:return None# find header line (the one starting with |*|)header = Nonefor ln in lines:cells = [c.strip() for c in ln.strip("|").split("|")]if cells and cells[0] in {"*", "∗"}:header = cellsbreakif not header:return Nonesymbols = header[1:]if not symbols:return None# build table from subsequent lines that look like rowsT = {r: {} for r in symbols}for ln in lines:cells = [c.strip() for c in ln.strip("|").split("|")]if not cells or cells[0] not in symbols:continuerow = cells[0]vals = cells[1:]if len(vals) != len(symbols):continuefor col, val in zip(symbols, vals):T[row][col] = val# sanity: ensure all rows presentif any(r not in T or len(T[r]) != len(symbols) for r in symbols):return Nonereturn symbols, Tdef _find_identity(self, S, T):# two-sided identity e satisfies e*x = x and x*e = x for all xfor e in S:if all(T[e][x] == x for x in S) and all(T[x][e] == x for x in S):return ereturn Nonedef _is_commutative(self, S, T):for x in S:for y in S:if T[x][y] != T[y][x]:return Falsereturn Truedef _is_associative(self, S, T):for x in S:for y in S:for z in S:if T[T[x][y]][z] != T[x][T[y][z]]:return Falsereturn Truedef _inverse_of(self, x, S, T, e):if e is None:return Nonefor y in S:if T[x][y] == e and T[y][x] == e:return yreturn Nonedef _idempotents(self, S, T):return [x for x in S if T[x][x] == x]def _answer_from_op_table(self, question: str, table_text: str):"""Detect common prompts and answer exactly from the parsed table."""parsed = self._parse_op_table_from_text(table_text)if not parsed:return NoneS, T = parsedql = question.lower()# 1) direct product like "b*e" or "what is a * d?"m = re.search(r"\b([A-Za-z])\s*\*\s*([A-Za-z])\b", question)if m and m.group(1) in S and m.group(2) in S:return T[m.group(1)][m.group(2)]# 2) identity element?if "identity" in ql or "neutral element" in ql or "unit element" in ql:e = _e = self._find_identity(S, T)return _e if _e is not None else "None"# 3) associativity?if "associative" in ql or "associativity" in ql:11 unmodified linese = self._find_identity(S, T)inv = self._inverse_of(x, S, T, e)return inv if inv is not None else "None"# 6) idempotent elements?if "idempotent" in ql or "idempotents" in ql:ids = self._idempotents(S, T)return " ".join(ids) if ids else "None"# If we parsed the table but didn't recognize the ask, return None and let other handlers/LLM try.return None# change the template call to pass task_id as second argdef __call__(self, question: str, task_id: str | None = None) -> str:ql = question.lower()table_text = None# NEW: matrix questif task_id:table_text = self._fetch_file_text(task_id)if table_text is None:table_text = question # sometimes the table is embedded in the promptop_ans = self._answer_from_op_table(question, table_text)if op_ans is not None:return op_ans298 unmodified lines29930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633711 unmodified lines349350351352353354355356357358359360361362363364365366367368369370371298 unmodified linesreturn m.group(1).strip()return Nonedef _answer_from_op_table(self, question: str, table_text: str):parsed = self._parse_op_table_from_text(table_text)if not parsed:return NoneS, T = parsedql = question.lower()# --- NEW: subset of S that appears in any non-commutative counterexample ---# e.g., "provide the subset of S involved in any possible counter-examples# that prove * is not commutative. Provide ... comma separated ... alphabetical order."if (("counter-example" in ql or "counter examples" in ql or "counter-examples" in ql or "counterexample" in ql)and "commutative" in qland "subset" in ql):elems = set()for x in S:for y in S:if T[x][y] != T[y][x]:elems.add(x); elems.add(y)# EXACT MATCH expects alphabetical, comma+space separated, bare stringreturn ", ".join(sorted(elems)) if elems else ""# 1) direct product like "b*e" or "what is a * d?"m = re.search(r"\b([A-Za-z])\s*\*\s*([A-Za-z])\b", question)if m and m.group(1) in S and m.group(2) in S:return T[m.group(1)][m.group(2)]# 2) identity element?if "identity" in ql or "neutral element" in ql or "unit element" in ql:e = self._find_identity(S, T)return e if e is not None else "None"# 3) associativity?if "associative" in ql or "associativity" in ql:11 unmodified linese = self._find_identity(S, T)inv = self._inverse_of(x, S, T, e)return inv if inv is not None else "None"# 6) idempotent elements?if "idempotent" in ql or "idempotents" in ql:ids = self._idempotents(S, T)return " ".join(ids) if ids else "None"return None# change the template call to pass task_id as second argdef __call__(self, question: str, task_id: str | None = None) -> str:ql = question.lower()table_text = self._fetch_file_text(task_id) if task_id else None# NEW: matrix questif table_text is None:table_text = question # sometimes the table is embedded in the promptop_ans = self._answer_from_op_table(question, table_text)if op_ans is not None:return op_ans
app.py+25-97