Update app.py
Browse files
app.py
CHANGED
|
@@ -32,18 +32,18 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 32 |
# def forward(self, image: Image.Image) -> str:
|
| 33 |
# return "This is a placeholder caption for the uploaded image."
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
|
| 48 |
class BasicAgent:
|
| 49 |
def __init__(self):
|
|
@@ -54,7 +54,7 @@ class BasicAgent:
|
|
| 54 |
WikipediaSearchTool(user_agent="my-agent", language="en"),
|
| 55 |
PythonInterpreterTool(),
|
| 56 |
UserInputTool(),
|
| 57 |
-
|
| 58 |
],
|
| 59 |
model=model,
|
| 60 |
system_prompt="""
|
|
@@ -148,20 +148,37 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 148 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 149 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
# 3. Run your Agent
|
| 152 |
results_log = []
|
| 153 |
answers_payload = []
|
| 154 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 155 |
for item in questions_data:
|
| 156 |
task_id = item.get("task_id")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
question_text = item.get("question")
|
|
|
|
| 158 |
if not task_id or question_text is None:
|
| 159 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 160 |
continue
|
| 161 |
try:
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
| 165 |
except Exception as e:
|
| 166 |
print(f"Error running agent on task {task_id}: {e}")
|
| 167 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|
|
|
|
| 32 |
# def forward(self, image: Image.Image) -> str:
|
| 33 |
# return "This is a placeholder caption for the uploaded image."
|
| 34 |
|
| 35 |
+
class ImageCaptioningTool(Tool):
|
| 36 |
+
name = "image_captioner"
|
| 37 |
+
description = "Generate a caption for an image."
|
| 38 |
+
inputs = {"image": "image"}
|
| 39 |
+
output_type = "text"
|
| 40 |
|
| 41 |
+
def run(self, inputs: dict) -> str:
|
| 42 |
+
image = inputs.get("image")
|
| 43 |
+
if not image:
|
| 44 |
+
return "No image provided."
|
| 45 |
+
# You could run your model here instead
|
| 46 |
+
return "This is a placeholder caption for the uploaded image."
|
| 47 |
|
| 48 |
class BasicAgent:
|
| 49 |
def __init__(self):
|
|
|
|
| 54 |
WikipediaSearchTool(user_agent="my-agent", language="en"),
|
| 55 |
PythonInterpreterTool(),
|
| 56 |
UserInputTool(),
|
| 57 |
+
ImageCaptioningTool(),
|
| 58 |
],
|
| 59 |
model=model,
|
| 60 |
system_prompt="""
|
|
|
|
| 148 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 149 |
return f"An unexpected error occurred fetching questions: {e}", None
|
| 150 |
|
| 151 |
+
|
| 152 |
+
# question_text = item.get("question")
|
| 153 |
+
# question_input = {"question": question_text}
|
| 154 |
+
# if "image" in item:
|
| 155 |
+
# question_input["image"] = item["image"]
|
| 156 |
+
# submitted_answer = agent(question_input)
|
| 157 |
# 3. Run your Agent
|
| 158 |
results_log = []
|
| 159 |
answers_payload = []
|
| 160 |
print(f"Running agent on {len(questions_data)} questions...")
|
| 161 |
for item in questions_data:
|
| 162 |
task_id = item.get("task_id")
|
| 163 |
+
# question_text = item.get("question")
|
| 164 |
+
# if not task_id or question_text is None:
|
| 165 |
+
# print(f"Skipping item with missing task_id or question: {item}")
|
| 166 |
+
# continue
|
| 167 |
+
# try:
|
| 168 |
+
# submitted_answer = agent(question_text)
|
| 169 |
+
# answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 170 |
+
# results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
| 171 |
question_text = item.get("question")
|
| 172 |
+
image = item.get("image", None)
|
| 173 |
if not task_id or question_text is None:
|
| 174 |
print(f"Skipping item with missing task_id or question: {item}")
|
| 175 |
continue
|
| 176 |
try:
|
| 177 |
+
question_input = {"question": question_text}
|
| 178 |
+
if image:
|
| 179 |
+
question_input["image"] = image
|
| 180 |
+
submitted_answer = agent(question_input)
|
| 181 |
+
|
| 182 |
except Exception as e:
|
| 183 |
print(f"Error running agent on task {task_id}: {e}")
|
| 184 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": f"AGENT ERROR: {e}"})
|