Update app.py
Browse files
app.py
CHANGED
|
@@ -18,37 +18,37 @@ from openai import OpenAI
|
|
| 18 |
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
| 21 |
-
print("
|
| 22 |
self.client = OpenAI()
|
| 23 |
|
| 24 |
-
# Simple safe calculator
|
| 25 |
-
def calculate(self, expression):
|
| 26 |
-
try:
|
| 27 |
-
return eval(expression)
|
| 28 |
-
except:
|
| 29 |
-
return None
|
| 30 |
-
|
| 31 |
def clean_answer(self, text):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
text = text.strip()
|
| 34 |
|
|
|
|
| 35 |
text = re.sub(r"(?i)final answer[:\- ]*", "", text)
|
| 36 |
text = re.sub(r"(?i)answer[:\- ]*", "", text)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
| 38 |
return text.strip()
|
| 39 |
|
| 40 |
def ask_llm(self, question):
|
| 41 |
|
| 42 |
prompt = f"""
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
You can:
|
| 46 |
-
- reason step by step
|
| 47 |
-
- perform calculations if needed
|
| 48 |
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
Question:
|
| 54 |
{question}
|
|
@@ -56,45 +56,26 @@ Question:
|
|
| 56 |
|
| 57 |
response = self.client.chat.completions.create(
|
| 58 |
model="gpt-4o-mini",
|
| 59 |
-
messages=[
|
|
|
|
|
|
|
|
|
|
| 60 |
temperature=0
|
| 61 |
)
|
| 62 |
|
| 63 |
return response.choices[0].message.content.strip()
|
| 64 |
|
| 65 |
-
def try_calculation(self, question):
|
| 66 |
-
|
| 67 |
-
# find simple math expressions
|
| 68 |
-
match = re.findall(r"[\d\.\+\-\*\/\(\) ]+", question)
|
| 69 |
-
|
| 70 |
-
for m in match:
|
| 71 |
-
m = m.strip()
|
| 72 |
-
if len(m) > 2:
|
| 73 |
-
result = self.calculate(m)
|
| 74 |
-
if result is not None:
|
| 75 |
-
return result
|
| 76 |
-
|
| 77 |
-
return None
|
| 78 |
-
|
| 79 |
def __call__(self, question: str) -> str:
|
| 80 |
|
| 81 |
print("Question:", question)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
calc_result = self.try_calculation(question)
|
| 85 |
-
|
| 86 |
-
if calc_result is not None:
|
| 87 |
-
print("Used calculator:", calc_result)
|
| 88 |
-
return str(calc_result)
|
| 89 |
-
|
| 90 |
-
# otherwise ask LLM
|
| 91 |
-
answer = self.ask_llm(question)
|
| 92 |
|
| 93 |
-
|
| 94 |
|
| 95 |
-
print("Final
|
| 96 |
|
| 97 |
-
return
|
| 98 |
|
| 99 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 100 |
"""
|
|
|
|
| 18 |
|
| 19 |
class BasicAgent:
|
| 20 |
def __init__(self):
|
| 21 |
+
print("GAIA Smart Agent initialized")
|
| 22 |
self.client = OpenAI()
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def clean_answer(self, text):
|
| 25 |
+
"""
|
| 26 |
+
GAIA scoring requires exact answers.
|
| 27 |
+
This removes explanations and keeps only the final value.
|
| 28 |
+
"""
|
| 29 |
|
| 30 |
text = text.strip()
|
| 31 |
|
| 32 |
+
# remove phrases
|
| 33 |
text = re.sub(r"(?i)final answer[:\- ]*", "", text)
|
| 34 |
text = re.sub(r"(?i)answer[:\- ]*", "", text)
|
| 35 |
|
| 36 |
+
# keep only last line
|
| 37 |
+
text = text.split("\n")[-1]
|
| 38 |
+
|
| 39 |
return text.strip()
|
| 40 |
|
| 41 |
def ask_llm(self, question):
|
| 42 |
|
| 43 |
prompt = f"""
|
| 44 |
+
Solve the question carefully.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
Think step by step internally but output ONLY the final answer.
|
| 47 |
|
| 48 |
+
Rules:
|
| 49 |
+
- No explanation
|
| 50 |
+
- No sentences
|
| 51 |
+
- Only the final value
|
| 52 |
|
| 53 |
Question:
|
| 54 |
{question}
|
|
|
|
| 56 |
|
| 57 |
response = self.client.chat.completions.create(
|
| 58 |
model="gpt-4o-mini",
|
| 59 |
+
messages=[
|
| 60 |
+
{"role": "system", "content": "You are a precise problem solver."},
|
| 61 |
+
{"role": "user", "content": prompt}
|
| 62 |
+
],
|
| 63 |
temperature=0
|
| 64 |
)
|
| 65 |
|
| 66 |
return response.choices[0].message.content.strip()
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
def __call__(self, question: str) -> str:
|
| 69 |
|
| 70 |
print("Question:", question)
|
| 71 |
|
| 72 |
+
raw_answer = self.ask_llm(question)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
final_answer = self.clean_answer(raw_answer)
|
| 75 |
|
| 76 |
+
print("Final Answer:", final_answer)
|
| 77 |
|
| 78 |
+
return final_answer
|
| 79 |
|
| 80 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
| 81 |
"""
|