Nancy1906 commited on
Commit
236c7ed
·
verified ·
1 Parent(s): ba6fb69
Files changed (1) hide show
  1. my_tools.py +51 -18
my_tools.py CHANGED
@@ -224,28 +224,44 @@ def _pd_safe_import():
224
 
225
 
226
  def analyze_markdown_table(table_md: str, question: str) -> str:
227
- """Check commutativity or return CSV. Requires pandas lazily."""
228
  try:
229
  pd = _pd_safe_import()
230
- lines = [l for l in table_md.strip().splitlines() if l.strip() and not l.lstrip().startswith("|---")]
231
- rows = [[c.strip() for c in l.strip().strip("|").split("|")] for l in lines]
 
 
 
 
 
232
  if len(rows) < 2:
233
  return "Error analyze_table: empty or malformed markdown table"
 
234
  df = pd.DataFrame(rows[1:], columns=rows[0])
 
 
235
  if "conmut" in question.lower():
236
- cols = df.columns.tolist()[1:]
237
- offenders = {
238
- col
239
- for x in cols
240
- for y in cols
241
- if df.loc[df[rows[0][0]] == x, y].iat[0] != df.loc[df[rows[0][0]] == y, x].iat[0]
242
- }
 
 
 
 
 
 
243
  return ", ".join(sorted(offenders)) or "Conmutativa"
 
 
244
  return df.to_csv(index=False)
245
  except Exception as exc:
246
  return f"Error analyze_markdown_table: {exc}"
247
 
248
 
 
249
  def execute_code(code: str) -> str:
250
  """Runs arbitrary **short** python code in a sandboxed subprocess."""
251
  try:
@@ -363,18 +379,23 @@ def scrape_wiki_table(page_title: str, section: str | None = None, table_index:
363
  # --- generic URL text scraper ---
364
 
365
  def scrape_url_text(url: str) -> str:
366
- """Downloads a webpage and returns cleaned visible text (trimmed to 8k chars)."""
367
  try:
368
- html = requests.get(url, headers=HEADERS, timeout=20).text
369
- soup = BeautifulSoup(html, "html.parser")
 
 
 
 
370
  for tag in soup(["script", "style", "noscript"]):
371
  tag.decompose()
372
- raw_text = "\n".join(t.strip() for t in soup.get_text("\n").splitlines() if t.strip())
373
- return raw_text[:8000]
374
  except Exception as exc:
375
  return f"Error scrape_url_text: {exc}"
376
 
377
 
 
378
  # ---------- TOOL WRAPPERS ----------
379
 
380
  tool_defs = [
@@ -432,9 +453,21 @@ def _extract_observation(raw: str) -> str:
432
 
433
  def basic_agent_response(question: str) -> str:
434
  try:
435
- print(f"[DEBUG] Question: {question}")
436
- raw_resp = agent.query(question)
437
- cleaned = _extract_observation(str(raw_resp.response if hasattr(raw_resp, "response") else raw_resp))
 
 
 
 
 
 
 
 
 
 
 
 
438
  return cleaned or "I cannot answer with the available tools."
439
  except Exception as exc:
440
  print(f"[ERROR] {exc}")
 
224
 
225
 
226
  def analyze_markdown_table(table_md: str, question: str) -> str:
 
227
  try:
228
  pd = _pd_safe_import()
229
+
230
+ # 1️⃣ Parseo seguro del markdown
231
+ clean = [
232
+ ln for ln in table_md.strip().splitlines()
233
+ if ln.strip() and not ln.lstrip().startswith("|---")
234
+ ]
235
+ rows = [ [c.strip() for c in ln.strip("|").split("|")] for ln in clean ]
236
  if len(rows) < 2:
237
  return "Error analyze_table: empty or malformed markdown table"
238
+
239
  df = pd.DataFrame(rows[1:], columns=rows[0])
240
+
241
+ # — 2️⃣ ¿Nos piden conmutatividad? —
242
  if "conmut" in question.lower():
243
+ offenders: set[str] = set()
244
+ header = df.columns[0] # nombre de la columna/índice
245
+
246
+ cols = df.columns[1:] # solo las etiquetas
247
+ for x in cols:
248
+ for y in cols:
249
+ try:
250
+ val_xy = df.loc[df[header] == x, y].iat[0]
251
+ val_yx = df.loc[df[header] == y, x].iat[0]
252
+ if val_xy != val_yx:
253
+ offenders.update([x, y])
254
+ except (IndexError, KeyError):
255
+ continue
256
  return ", ".join(sorted(offenders)) or "Conmutativa"
257
+
258
+ # — 3️⃣ Si no, devolvemos CSV —
259
  return df.to_csv(index=False)
260
  except Exception as exc:
261
  return f"Error analyze_markdown_table: {exc}"
262
 
263
 
264
+
265
  def execute_code(code: str) -> str:
266
  """Runs arbitrary **short** python code in a sandboxed subprocess."""
267
  try:
 
379
  # --- generic URL text scraper ---
380
 
381
  def scrape_url_text(url: str) -> str:
382
+ """Descarga página y devuelve texto visible (trim-8k)."""
383
  try:
384
+ resp = requests.get(url, headers=HEADERS, timeout=20)
385
+ if "Just a moment" in resp.text and "cloudflare" in resp.text.lower():
386
+ return "Error scrape_url_text: Cloudflare protection detected"
387
+ resp.raise_for_status()
388
+
389
+ soup = BeautifulSoup(resp.text, "html.parser")
390
  for tag in soup(["script", "style", "noscript"]):
391
  tag.decompose()
392
+ text = "\n".join(t.strip() for t in soup.get_text("\n").splitlines() if t.strip())
393
+ return text[:8000]
394
  except Exception as exc:
395
  return f"Error scrape_url_text: {exc}"
396
 
397
 
398
+
399
  # ---------- TOOL WRAPPERS ----------
400
 
401
  tool_defs = [
 
453
 
454
  def basic_agent_response(question: str) -> str:
455
  try:
456
+ # ⚠️ agente nuevo por pregunta
457
+ fresh_agent = ReActAgent.from_tools(
458
+ tools=TOOLS,
459
+ llm=GeminiLLM(),
460
+ system_prompt=SYSTEM_PROMPT,
461
+ verbose=False,
462
+ max_iterations=25,
463
+ callback_manager=None, # sin historial previo
464
+ handle_parsing_errors=True,
465
+ )
466
+
467
+ raw = fresh_agent.query(question)
468
+ cleaned = _extract_observation(
469
+ str(raw.response if hasattr(raw, "response") else raw)
470
+ )
471
  return cleaned or "I cannot answer with the available tools."
472
  except Exception as exc:
473
  print(f"[ERROR] {exc}")