update app.py
Browse files
app.py
CHANGED
|
@@ -17,14 +17,20 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 17 |
from openai import OpenAI
|
| 18 |
|
| 19 |
class BasicAgent:
|
|
|
|
| 20 |
def __init__(self):
|
| 21 |
-
print("
|
| 22 |
self.client = OpenAI()
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
-
|
|
|
|
|
|
|
| 28 |
except:
|
| 29 |
return None
|
| 30 |
|
|
@@ -35,21 +41,32 @@ class BasicAgent:
|
|
| 35 |
text = re.sub(r"(?i)final answer[:\- ]*", "", text)
|
| 36 |
text = re.sub(r"(?i)answer[:\- ]*", "", text)
|
| 37 |
|
| 38 |
-
# take last line
|
| 39 |
text = text.split("\n")[-1]
|
| 40 |
|
| 41 |
return text.strip()
|
| 42 |
|
| 43 |
-
def ask_llm(self, question):
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
Question:
|
| 55 |
{question}
|
|
@@ -66,37 +83,20 @@ Question:
|
|
| 66 |
|
| 67 |
return response.choices[0].message.content.strip()
|
| 68 |
|
| 69 |
-
def
|
| 70 |
-
|
| 71 |
-
# detect simple math expressions
|
| 72 |
-
matches = re.findall(r"[\d\.\+\-\*\/\(\) ]+", question)
|
| 73 |
-
|
| 74 |
-
for m in matches:
|
| 75 |
-
m = m.strip()
|
| 76 |
-
if len(m) > 3:
|
| 77 |
-
result = self.calculate(m)
|
| 78 |
-
if result:
|
| 79 |
-
return result
|
| 80 |
-
|
| 81 |
-
return None
|
| 82 |
-
|
| 83 |
-
def __call__(self, question: str) -> str:
|
| 84 |
|
| 85 |
print("Question:", question)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
math_result = self.try_math(question)
|
| 89 |
|
| 90 |
-
if
|
| 91 |
-
print("
|
| 92 |
-
return math_result
|
| 93 |
|
| 94 |
-
|
| 95 |
-
raw_answer = self.ask_llm(question)
|
| 96 |
|
| 97 |
final_answer = self.clean_answer(raw_answer)
|
| 98 |
|
| 99 |
-
print("Final
|
| 100 |
|
| 101 |
return final_answer
|
| 102 |
|
|
@@ -161,7 +161,9 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 161 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 162 |
continue
|
| 163 |
try:
|
| 164 |
-
|
|
|
|
|
|
|
| 165 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 166 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 167 |
except Exception as e:
|
|
|
|
| 17 |
from openai import OpenAI
|
| 18 |
|
| 19 |
class BasicAgent:
|
| 20 |
+
|
| 21 |
def __init__(self):
|
| 22 |
+
print("File-aware GAIA agent initialized")
|
| 23 |
self.client = OpenAI()
|
| 24 |
|
| 25 |
+
def download_file(self, url):
|
| 26 |
+
|
| 27 |
+
if not url:
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
try:
|
| 31 |
+
r = requests.get(url, timeout=20)
|
| 32 |
+
r.raise_for_status()
|
| 33 |
+
return r.text[:4000]
|
| 34 |
except:
|
| 35 |
return None
|
| 36 |
|
|
|
|
| 41 |
text = re.sub(r"(?i)final answer[:\- ]*", "", text)
|
| 42 |
text = re.sub(r"(?i)answer[:\- ]*", "", text)
|
| 43 |
|
|
|
|
| 44 |
text = text.split("\n")[-1]
|
| 45 |
|
| 46 |
return text.strip()
|
| 47 |
|
| 48 |
+
def ask_llm(self, question, file_content=None):
|
| 49 |
|
| 50 |
+
if file_content:
|
| 51 |
+
prompt = f"""
|
| 52 |
+
You are solving a GAIA benchmark question.
|
| 53 |
|
| 54 |
+
A file is provided.
|
| 55 |
+
|
| 56 |
+
File content:
|
| 57 |
+
{file_content}
|
| 58 |
+
|
| 59 |
+
Question:
|
| 60 |
+
{question}
|
| 61 |
+
|
| 62 |
+
Return ONLY the final answer.
|
| 63 |
+
No explanation.
|
| 64 |
+
"""
|
| 65 |
+
else:
|
| 66 |
+
prompt = f"""
|
| 67 |
+
Solve the following GAIA question.
|
| 68 |
+
|
| 69 |
+
Return ONLY the final answer.
|
| 70 |
|
| 71 |
Question:
|
| 72 |
{question}
|
|
|
|
| 83 |
|
| 84 |
return response.choices[0].message.content.strip()
|
| 85 |
|
| 86 |
+
def __call__(self, question, file_url=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
print("Question:", question)
|
| 89 |
|
| 90 |
+
file_content = self.download_file(file_url)
|
|
|
|
| 91 |
|
| 92 |
+
if file_content:
|
| 93 |
+
print("File detected and loaded")
|
|
|
|
| 94 |
|
| 95 |
+
raw_answer = self.ask_llm(question, file_content)
|
|
|
|
| 96 |
|
| 97 |
final_answer = self.clean_answer(raw_answer)
|
| 98 |
|
| 99 |
+
print("Final Answer:", final_answer)
|
| 100 |
|
| 101 |
return final_answer
|
| 102 |
|
|
|
|
| 161 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 162 |
continue
|
| 163 |
try:
|
| 164 |
+
# submitted_answer = agent(question_text) //vineet
|
| 165 |
+
file_url = item.get("file", None)
|
| 166 |
+
submitted_answer = agent(question_text, file_url)
|
| 167 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 168 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 169 |
except Exception as e:
|