Update app.py
Browse files
app.py
CHANGED
@@ -35,6 +35,8 @@ from smolagents import CodeAgent,DuckDuckGoSearchTool,VisitWebpageTool, Inferenc
|
|
35 |
from tools.final_answer import FinalAnswerTool
|
36 |
import yaml
|
37 |
import time
|
|
|
|
|
38 |
|
39 |
# (Keep Constants as is)
|
40 |
# --- Constants ---
|
@@ -47,7 +49,7 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
47 |
set_inference_on = True
|
48 |
|
49 |
# Utility functions
|
50 |
-
def
|
51 |
time.sleep(3)
|
52 |
|
53 |
# Agent stuff starts here
|
@@ -66,7 +68,7 @@ class BasicAgent:
|
|
66 |
additional_authorized_imports=['pandas', 'requests', 'markdownify'],
|
67 |
max_steps=10,
|
68 |
verbosity_level=1
|
69 |
-
step_callbacks=[
|
70 |
)
|
71 |
|
72 |
|
@@ -90,6 +92,19 @@ class BasicAgent:
|
|
90 |
print(f"Agent returning answer: {answer}")
|
91 |
return answer
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
94 |
"""
|
95 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
@@ -147,11 +162,15 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
147 |
for item in questions_data:
|
148 |
task_id = item.get("task_id")
|
149 |
question_text = item.get("question")
|
|
|
|
|
|
|
|
|
150 |
if not task_id or question_text is None:
|
151 |
print(f"Skipping item with missing task_id or question: {item}")
|
152 |
continue
|
153 |
try:
|
154 |
-
submitted_answer = agent(
|
155 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
156 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
157 |
except Exception as e:
|
|
|
35 |
from tools.final_answer import FinalAnswerTool
|
36 |
import yaml
|
37 |
import time
|
38 |
+
import jinja2
|
39 |
+
import json
|
40 |
|
41 |
# (Keep Constants as is)
|
42 |
# --- Constants ---
|
|
|
49 |
set_inference_on = True
|
50 |
|
51 |
# Utility functions
|
52 |
+
def delay_execution():
|
53 |
time.sleep(3)
|
54 |
|
55 |
# Agent stuff starts here
|
|
|
68 |
additional_authorized_imports=['pandas', 'requests', 'markdownify'],
|
69 |
max_steps=10,
|
70 |
verbosity_level=1
|
71 |
+
step_callbacks=[delay_execution()]
|
72 |
)
|
73 |
|
74 |
|
|
|
92 |
print(f"Agent returning answer: {answer}")
|
93 |
return answer
|
94 |
|
95 |
+
def compose_question_for_agent(task_id -> str, question_text -> str, question_file_name -> str) -> str:
|
96 |
+
# Define the updated Jinja2 template with a conditional block to add information where file attachments can be found.
|
97 |
+
template_string = "{{ question_text }}{% if question_file_name %}\nThis question refers to a file. You can find the URL of the file by performing a REST API GET request at https://agents-course-unit4-scoring.hf.space/files/{{ task_id }}{% endif %}"
|
98 |
+
|
99 |
+
# Create a Jinja2 template object
|
100 |
+
template = jinja2.Template(template_string)
|
101 |
+
|
102 |
+
# --- Process the first JSON object (with a file) ---
|
103 |
+
data_with_file = json.loads(json_with_file)
|
104 |
+
result_with_file = template.render(data_with_file)
|
105 |
+
composed_question = template.render(task_id=task_id, question_text=question_text, question_file_name=question_file_name)
|
106 |
+
return composed_question
|
107 |
+
|
108 |
def run_and_submit_all( profile: gr.OAuthProfile | None):
|
109 |
"""
|
110 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
|
|
162 |
for item in questions_data:
|
163 |
task_id = item.get("task_id")
|
164 |
question_text = item.get("question")
|
165 |
+
question_file_name = item.get("file_name")
|
166 |
+
|
167 |
+
composed_question_text = compose_question_for_agent(task_id, question_text, question_file_name)
|
168 |
+
|
169 |
if not task_id or question_text is None:
|
170 |
print(f"Skipping item with missing task_id or question: {item}")
|
171 |
continue
|
172 |
try:
|
173 |
+
submitted_answer = agent(composed_question_text)
|
174 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
175 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
176 |
except Exception as e:
|