dawid-lorek commited on
Commit
e802b30
·
verified ·
1 Parent(s): 3686433

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +30 -33
agent.py CHANGED
@@ -1,4 +1,4 @@
1
- # agent_v43.py — Najlepsze cechy z V18–V34: precyzja, retry fallback, stabilność
2
  import os
3
  import re
4
  import io
@@ -30,7 +30,7 @@ class GaiaAgent:
30
  model="gpt-4-turbo",
31
  messages=[{"role": "user", "content": prompt}],
32
  temperature=0,
33
- timeout=30
34
  )
35
  return r.choices[0].message.content.strip()
36
  except Exception:
@@ -66,43 +66,38 @@ class GaiaAgent:
66
  if "excel" in ctype:
67
  df = pd.read_excel(io.BytesIO(content), engine="openpyxl")
68
  df.columns = [c.lower().strip() for c in df.columns]
69
- if 'category' in df.columns and 'sales' in df.columns:
70
- df = df.dropna(subset=['category', 'sales'])
71
- df = df[df['category'].str.lower() == 'food']
72
- df['sales'] = pd.to_numeric(df['sales'], errors='coerce')
73
- return f"${df['sales'].sum():.2f}"
74
- return "[NO FOOD SALES DATA]"
75
  return content.decode("utf-8", errors="ignore")[:3000]
76
  except Exception as e:
77
  return f"[FILE ERROR: {e}]"
78
 
79
  def extract_commutativity_set(self, table_txt):
80
  try:
81
- lines = table_txt.splitlines()
82
- S, table = [], {}
83
- for l in lines:
84
- if l.startswith("|*"):
85
- S = l.strip().split("|")[2:]
86
- elif l.startswith("|"):
87
- parts = l.strip().split("|")[1:-1]
88
- table[parts[0]] = parts[1:]
89
- fail = set()
90
- for x in S:
91
- for y in S:
92
- if table[x][S.index(y)] != table[y][S.index(x)]:
93
- fail |= {x, y}
94
- return ", ".join(sorted(fail))
95
  except Exception:
96
  return "[COMMUTATIVE ERROR]"
97
 
98
  def extract_ingredients(self, text):
99
  try:
100
- candidates = re.findall(r"[a-zA-Z]+(?:\s[a-zA-Z]+)?", text)
101
- blocked = {"add", "combine", "cook", "stir", "remove", "cool", "mixture", "saucepan", "until", "heat", "dash"}
102
- clean = [c.lower() for c in candidates if c.lower() not in blocked and len(c.split()) <= 3]
103
- return ", ".join(sorted(set(clean)))
104
  except Exception:
105
- return text[:100]
106
 
107
  def format_answer(self, answer, question):
108
  q = question.lower()
@@ -120,21 +115,23 @@ class GaiaAgent:
120
  if "award number" in q:
121
  m = re.search(r"80NSSC[0-9A-Z]+", raw)
122
  return m.group(0) if m else raw
 
 
 
123
  if "first name" in q:
124
  return raw.split()[0]
125
  try:
126
  return str(w2n.word_to_num(raw))
127
- except Exception:
128
  m = re.search(r"\d+", raw)
129
  return m.group(0) if m else raw
130
 
131
  def retry_fallback(self, question):
132
- try:
133
- prompt = f"""Answer concisely and factually:
 
134
  Question: {question}"""
135
- return self.ask(prompt)
136
- except Exception:
137
- return "[RETRY FAILED]"
138
 
139
  def __call__(self, question, task_id=None):
140
  try:
 
1
+ # agent_v44.py — Poprawiona obsługa: YouTube, commutativity, web fallback, Excel
2
  import os
3
  import re
4
  import io
 
30
  model="gpt-4-turbo",
31
  messages=[{"role": "user", "content": prompt}],
32
  temperature=0,
33
+ timeout=40
34
  )
35
  return r.choices[0].message.content.strip()
36
  except Exception:
 
66
  if "excel" in ctype:
67
  df = pd.read_excel(io.BytesIO(content), engine="openpyxl")
68
  df.columns = [c.lower().strip() for c in df.columns]
69
+ food = df[df['category'].str.lower() == 'food'] if 'category' in df.columns else df
70
+ food['sales'] = pd.to_numeric(food.get('sales'), errors='coerce')
71
+ return f"${food['sales'].sum():.2f}"
 
 
 
72
  return content.decode("utf-8", errors="ignore")[:3000]
73
  except Exception as e:
74
  return f"[FILE ERROR: {e}]"
75
 
76
  def extract_commutativity_set(self, table_txt):
77
  try:
78
+ rows = [r for r in table_txt.splitlines() if r.strip().startswith("|")]
79
+ head = rows[0].split("|")[2:-1]
80
+ table = {}
81
+ for row in rows[1:]:
82
+ parts = row.split("|")[1:-1]
83
+ table[parts[0]] = parts[1:]
84
+ s = set()
85
+ for a in head:
86
+ for b in head:
87
+ if table[a][head.index(b)] != table[b][head.index(a)]:
88
+ s |= {a, b}
89
+ return ", ".join(sorted(s))
 
 
90
  except Exception:
91
  return "[COMMUTATIVE ERROR]"
92
 
93
  def extract_ingredients(self, text):
94
  try:
95
+ tokens = re.findall(r"[a-zA-Z]+(?:\s[a-zA-Z]+)?", text)
96
+ noise = {"add", "combine", "cook", "mixture", "until", "dash", "medium", "heat", "remove", "stir"}
97
+ filtered = [t.lower() for t in tokens if t.lower() not in noise and len(t.split()) <= 3]
98
+ return ", ".join(sorted(set(filtered)))
99
  except Exception:
100
+ return text[:80]
101
 
102
  def format_answer(self, answer, question):
103
  q = question.lower()
 
115
  if "award number" in q:
116
  m = re.search(r"80NSSC[0-9A-Z]+", raw)
117
  return m.group(0) if m else raw
118
+ if "ioc" in q:
119
+ m = re.search(r"\b[A-Z]{3}\b", raw)
120
+ return m.group(0) if m else raw
121
  if "first name" in q:
122
  return raw.split()[0]
123
  try:
124
  return str(w2n.word_to_num(raw))
125
+ except:
126
  m = re.search(r"\d+", raw)
127
  return m.group(0) if m else raw
128
 
129
  def retry_fallback(self, question):
130
+ context = self.search_context(question)
131
+ prompt = f"""Answer concisely and factually:
132
+ Context: {context}
133
  Question: {question}"""
134
+ return self.ask(prompt)
 
 
135
 
136
  def __call__(self, question, task_id=None):
137
  try: