Update app.py
Browse files
app.py
CHANGED
|
@@ -95,18 +95,40 @@ class BasicAgent:
|
|
| 95 |
)
|
| 96 |
print("BasicAgent initialized.")
|
| 97 |
# print("Available tools:", [tool.name for tool in self.agent.tools])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
def __call__(self, question):
|
| 99 |
if isinstance(question, dict):
|
| 100 |
text = question.get("question", "")
|
| 101 |
image = question.get("image", None)
|
| 102 |
-
if image:
|
| 103 |
-
question["image"] = {"type": "pil", "data": image}
|
| 104 |
else:
|
| 105 |
text = question
|
|
|
|
| 106 |
|
| 107 |
print(f"Agent received question (first 50 chars): {text[:50]}...")
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
|
| 112 |
|
|
@@ -180,7 +202,7 @@ def ask_agent(question):
|
|
| 180 |
image = download_image(image_url)
|
| 181 |
if image:
|
| 182 |
# Use the ImageCaptioningTool to get a caption
|
| 183 |
-
image_captioner = [tool for tool in tools if tool.name == "
|
| 184 |
image_caption = image_captioner(image=image, question=question)
|
| 185 |
#Append the caption to the user's original question
|
| 186 |
prompt +=f"\n\nThe image contains: {image_caption}"
|
|
@@ -269,7 +291,8 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 269 |
except Exception as e:
|
| 270 |
print(f"Failed to decode image for task {task_id}: {e}")
|
| 271 |
|
| 272 |
-
submitted_answer = agent(question_input)
|
|
|
|
| 273 |
|
| 274 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 275 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
@@ -385,6 +408,10 @@ if __name__ == "__main__":
|
|
| 385 |
|
| 386 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 387 |
demo.launch(debug=True, share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 388 |
# import os
|
| 389 |
# import pandas as pd
|
| 390 |
# import requests
|
|
|
|
| 95 |
)
|
| 96 |
print("BasicAgent initialized.")
|
| 97 |
# print("Available tools:", [tool.name for tool in self.agent.tools])
|
| 98 |
+
# def __call__(self, question):
|
| 99 |
+
# if isinstance(question, dict):
|
| 100 |
+
# text = question.get("question", "")
|
| 101 |
+
# image = question.get("image", None)
|
| 102 |
+
# if image:
|
| 103 |
+
# question["image"] = {"type": "pil", "data": image}
|
| 104 |
+
# else:
|
| 105 |
+
# text = question
|
| 106 |
+
|
| 107 |
+
# print(f"Agent received question (first 50 chars): {text[:50]}...")
|
| 108 |
+
# answer = self.agent.run(text)
|
| 109 |
+
# return answer.strip()
|
| 110 |
def __call__(self, question):
|
| 111 |
if isinstance(question, dict):
|
| 112 |
text = question.get("question", "")
|
| 113 |
image = question.get("image", None)
|
|
|
|
|
|
|
| 114 |
else:
|
| 115 |
text = question
|
| 116 |
+
image = None
|
| 117 |
|
| 118 |
print(f"Agent received question (first 50 chars): {text[:50]}...")
|
| 119 |
+
|
| 120 |
+
prompt = system_prompt + "\n\nUser: " + text.strip()
|
| 121 |
+
inputs = {}
|
| 122 |
+
|
| 123 |
+
if image:
|
| 124 |
+
try:
|
| 125 |
+
image_caption = image_captioner(image=image, question=text)
|
| 126 |
+
prompt += f"\n\nThe image contains: {image_caption}"
|
| 127 |
+
inputs["image"] = image
|
| 128 |
+
except Exception as e:
|
| 129 |
+
print(f"Image captioning failed: {e}")
|
| 130 |
+
|
| 131 |
+
return self.agent.run(prompt, inputs=inputs).strip()
|
| 132 |
|
| 133 |
|
| 134 |
|
|
|
|
| 202 |
image = download_image(image_url)
|
| 203 |
if image:
|
| 204 |
# Use the ImageCaptioningTool to get a caption
|
| 205 |
+
image_captioner = [tool for tool in tools if tool.name == "image_captioner "][0]
|
| 206 |
image_caption = image_captioner(image=image, question=question)
|
| 207 |
#Append the caption to the user's original question
|
| 208 |
prompt +=f"\n\nThe image contains: {image_caption}"
|
|
|
|
| 291 |
except Exception as e:
|
| 292 |
print(f"Failed to decode image for task {task_id}: {e}")
|
| 293 |
|
| 294 |
+
# submitted_answer = agent(question_input)
|
| 295 |
+
submitted_answer = ask_agent(question_text)
|
| 296 |
|
| 297 |
answers_payload.append({"task_id": task_id, "submitted_answer": submitted_answer})
|
| 298 |
results_log.append({"Task ID": task_id, "Question": question_text, "Submitted Answer": submitted_answer})
|
|
|
|
| 408 |
|
| 409 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 410 |
demo.launch(debug=True, share=False)
|
| 411 |
+
|
| 412 |
+
# DEBUG TEST
|
| 413 |
+
agent = BasicAgent(model, tools)
|
| 414 |
+
print(agent("What is the capital of France?"))
|
| 415 |
# import os
|
| 416 |
# import pandas as pd
|
| 417 |
# import requests
|